#!/usr/bin/perl -w

# vim: syntax=perl
use strict;
use warnings;

my $PARROT = '../../parrot';
my $CARDINAL = 'cardinal.pbc';
my $CURRENT_TEST = slurp_current_test();
my $TRACE_ARGS = "";
my $DEBUG_FLAGS = "";
my $POST_PROC = "";

sub slurp_current_test
{
  local $\;
  open my $FH, "<.current_test";
  <$FH>;
}

if ( @ARGV > 1 )
{
  $CURRENT_TEST = $ARGV[1];
}

if ( @ARGV > 0 )
{
  if ( $ARGV[0] eq "--help" )
  {
    help();
    exit;
  }
  eval($ARGV[0]);
}
else
{
  debug2();
}

sub help
{
  print <<"EOF"
./rit command program.rb
Commands
  trace :-> parrot -t
  execute :-> executes the 
  debug executes cardinal.pbc w/ -d
  gdb executes cardinal under gdb
EOF
}

sub trace { $TRACE_ARGS = "-t 4"; execute(); }
sub debug { $DEBUG_FLAGS = "-d"; execute(); }
sub debug2 { $DEBUG_FLAGS = "-a -o -i -s -e"; execute(); }

sub execute
{
  execute_cmd( "make ; $PARROT $TRACE_ARGS $CARDINAL $DEBUG_FLAGS $CURRENT_TEST $POST_PROC" );
}

sub gdb
{
  execute_cmd( "echo 'set args $CARDINAL -d $CURRENT_TEST\n run\n set print pretty \n' > .rit_gdb_cmds");
  exec( "gdb -x .rit_gdb_cmds ../../parrot" );
}

sub execute_cmd
{
  my ($cmd) = @_;
  print $cmd . "\n";
  print `$cmd`;
  $$;
}

