#!/usr/bin/perl -w

#### reminder: this file (which resides in $JR_HOME/bin or $JR_HOME\bin)
#### is generated from files in $JR_HOME/cmds,
#### so you probably don't want to edit this file directly.

use strict;

sub u_findmain($$);
sub u_init();
sub u_jfindmain();
sub u_jgo($);
sub u_jgot(\@);
sub u_jgox($);
sub u_jr(\@);
sub u_jrc(\@);
sub u_jrfindmain();
sub u_jrgo($);
sub u_jrgot(\@);
sub u_jrgox($);
sub u_jrrun(\@);
sub u_jrt(\@);
sub u_mysystem($);
sub u_quote_args(\@);

# finds java file containing main
# and then runs java using that file's name as name_of_main_class.
# (not symmetric with jrgo; more a combination of jr and jrgo)

# u_jfindmain does all the work and exits on failure.
my $basen;
$basen = u_jfindmain();

my $exitstatus;
$exitstatus = u_jgox($basen);
exit $exitstatus;

#
# finds jr or java file containing main
# and then echoes that class name
#
# invoke as jrfindmain for jr files
# invoke as jfindmain for java files
#
# not perfect:
#   assumes main declaration
#     appears entirely on 1 line
#     does not appear in comments.
#     contains no comments within (e.g., "... void /*haha*/ main ...")
#   assumes the program does not have multiple main methods
#        (allowed but probably not common).

sub u_findmain($$) {
  my $suffix = shift(@_);
  my $msg = shift(@_);

  # make sure have at least one file with $suffix
  my $none = 1;
  my @suffixfiles = ("*$suffix");
  foreach my $f ( <@suffixfiles> ) {
    $none = 0;
    last;
  }
  if ($none) {
    $! = 1;
    die "$0 sees no $suffix files\n";
  }

  # build up expression for which to search

  # "void main" part
  my $v = "void\\s+main";

  # parameter parts
  # q is (String [] args)
  # r is (String args [])
  # remember to escape \, (, ), [, ]
  my $idpat = "[a-zA-Z_][a-zA-Z_0-9]*";
  my $q = "\\(\(\\s*final\\s\)?\\s*String\\s*\\[\\s*\\]\\s*$idpat\\s*\\)";
  my $r = "\\(\(\\s*final\\s\)?\\s*String\\s+$idpat\\s*\\[\\s*\\]\\s*\\)";

  my $p = "($q|$r)";

  # specifier parts
  my $s = "(public\\s+static|static\\s+public)";

  my $a = "$s\\s+$v\\s*$p";
  #######print $a;

  my $count = 0;
  my $file = ""; # set only if count>0
  foreach my $f ( <@suffixfiles> ) {
  ####  print "xxxxxxxx $f\n";
    open(F,$f);
    while ( <F> ) {
  ####    print "$_\n";
      if (/$a/) {
  #######print "found";
        $count++;
        $file = $f;
      }
    }
    close(F);
  }
  ##print $count, "\n";
  ##print $file, "\n";
  ##print $0, "\n";

  if ($count == 0) {
    $! = 1;
    die "$0 can't find main_class\n";
  }
  if ($count != 1) {
    $! = 1;
    die "$0 found multiple ($count) mains; use $msg\n";
  }

  my $basen = $file;
  # strip off any leading path
  $basen =~ s#.*/##;
  # strip off suffix
  $basen =~ s/$suffix$//;

  return $basen;
}

sub u_jfindmain() {
  return u_findmain(".java",
         "`javac filename_of_main_class; java name_of_main_class'");
}

sub u_jgox($) {
  my $basen = shift(@_);
  my $args = u_quote_args(@ARGV);
  
  $exitstatus = u_mysystem("java $basen $args");

  return $exitstatus;
}

sub u_mysystem($) {
  my $s = shift(@_);
  system($s);
  return $?>>8;
}

# preserve any quoting on command line by putting each A in quotes.
sub u_quote_args(\@) {
  my $A = shift(@_);
  my $args = "";
  foreach my $a ( @$A ) {
    $args .= "\"$a\" ";
    # print ":$a \n"
  }
  return $args;
}

