#!/usr/bin/perl -w

local %OBJS = ();
local %ROBJS = ();
local %RSRC = ();

sub SubDir {
  my $dir = shift;

  if (-d $dir)
  {
    opendir(DIR, $dir) || die("opendir $dir failed");
    my @dirent = readdir(DIR);
    closedir(DIR);
    foreach my $ent (sort @dirent)
    {
            if ($ent ne "." && $ent ne "..")
      {
        if (-d $dir."/".$ent)
        {
                  SubDir($dir."/".$ent);
              }
              elsif(-f $dir."/".$ent)
        {
          if ($ent =~ s#\.class##s)
          {
            next if ($res);
            next if ($ent =~ m#\$#s);
            my @java = split(/[\r\n]+/, `find $ARGV[0] -type f -name $ent.java`);
            if (0 < $#java)
            {
              my $j = -1;
              for(my $i = 0; $i <= $#java; $i++)
              {
                open(INF, $java[$i]) || die("Cannot read $java[$i]");
                $_ = join('', <INF>);
                close(INF);
                if (m#^\s*package\s+([a-zA-Z0-9\.]+);#s)
                {
                  my $pat = $1;
                  $pat =~ s#\.#/#gs;
                  $pat = "tmp.$$/$pat";
                  $j = $i if ($pat eq $dir);
                }
              }
              if (0 <= $j)
              {
                ($java[$j] =~ s#\.java$##s ) || die("Oops: $java[$j]");
                if (!defined($OBJS{$java[$j]}))
                {
                  $OBJS{$java[$j]} = 1;
                  print "OBJS += $java[$j].o\n";
                }
              }
              else
              {
                print "No source for $ent.class!\n";
              }
            }
            elsif (0 == $#java)
            {
              ($java[0] =~ s#\.java$##s ) || die("Oops: $java[0]");
              if (!defined($OBJS{$java[0]}))
              {
                $OBJS{$java[0]} = 1;
                print "OBJS += $java[0].o\n";
              }
            }
            else
            {
              print "No source for $ent.class!\n";
            }
          }
          else
          {
            next if (!$res);
            my $pat = $ARGV[0];
            $pat =~ s#[\\\/\:]+$##;
            $pat =~ s#[^a-zA-Z0-9_]#\\$&#gs;
            $pat = '^'.$pat.'[\\\/\:]+';
            my @res = split(/[\r\n]+/, `find $ARGV[0] -type f -name $ent`);
            my $url = "$dir/$ent";
            $url =~ s#[^\\\/\:]+[\\\/\:]+##s;
            my $max = -1;
            my $fnd = -1;
            for(my $i = 0; $i <= $#res; $i++)
            {
              $res[$i] =~ s#$pat##s;
              my @dst = split(/[\\\/\:]+/, $url);
              my @src = split(/[\\\/\:]+/, $res[$i]);
              #print "Resource[$i]: _=$_, res=$res[$i]!\n";
              my $j = 0;
              while(0 <= $#dst && 0 <= $#src && $dst[$#dst] eq $src[$#src])
              {
                $j++;
                pop(@dst);
                pop(@src);
              }
              if ($max < $j)
              {
                $max = $j;
                $fnd = $i;
              }
            }
            if (0 <= $fnd)
            {
              if ($res[$fnd] =~ s#\.[^\.]+$##s)
              {
                $RSRC{$res[$fnd]} = $&;
                if (!defined($ROBJS{$res[$fnd]}))
                {
                  $ROBJS{$res[$fnd]} = $url;
                  print "ROBJS += $res[$fnd].o\n";
                }
              }
              else
              {
                print "No resource type for $dir/$ent!\n";
              }
            }
            else
            {
              print "No resource for $dir/$ent!\n";
            }
          }
        }
        else
        {
          die("Oops: $dir/$ent");
        }
      }
    }
  }
}

local $res = '';
if (0 <= $#ARGV && "-res" eq $ARGV[0])
{
	$res = 1;
	shift(@ARGV);
}

if (1 <= $#ARGV && -d $ARGV[0] && -f $ARGV[1])
{
  system("mkdir tmp.$$");
  system("cd tmp.$$ && jar -xf ../$ARGV[1]");
  SubDir("tmp.$$");
  system("rm -rf tmp.$$");
  if ($res)
  {
    foreach(sort keys %ROBJS)
    {
      print "$_.o: $_$RSRC{$_}\n";
      print "\t\$(GCJ) -c \$(GCJFLAGS) --resource $ROBJS{$_} \$< -o \$@\n";
      print "\n";
    }
  }
}
else
{
  die("Usage: $0 ../sourcedir jarfile.jar");
}
