#!perl

# /* LICENSE:
#   =========================================================================
#     CMPack'03 Source Code Release for OPEN-R SDK v1.0
#     Copyright (C) 2003 Multirobot Lab [Project Head: Manuela Veloso]
#     School of Computer Science, Carnegie Mellon University
#     All rights reserved.
#   ========================================================================= */

use Getopt::Long;

$usage = "usage: compile.pl -u <user name>
required options:
-u|--user <user>                  user to compile for
options:
-h|--help                         get help
-s|--stages <stages to perform>   select stages to perform (clean, fetch, compile, send)
";

my $CYGPATH="C:\\CYGWIN\\BIN\\";

my $help=0;
my $user="default";
my @stages;

my %opts=
  (
   'user|u=s'   => \$user,
   'help|h'     => \$help,
   'stages|s:s' => \@stages
   );

$Getopt::Long::bundling = 1;

GetOptions(%opts) || die $usage;
if($user eq "default") {die $usage;}

if($help) {
  print $usage;
  exit(0);
}

if(scalar(@stages)==0) {@stages=("clean", "fetch", "compile", "send");}

@stages = split(/,/,join(',',@stages));

my $clean=0;
my $fetch=0;
my $compile=0;
my $send=0;

for(my $i=0; $i<=$#stages; $i++) {
  if   ($stages[$i] =~ /^cl/) {
    $clean=1;
  }
  elsif($stages[$i] =~ /^f/) {
    $fetch=1;
  }
  elsif($stages[$i] =~ /^co/) {
    $compile=1;
  }
  elsif($stages[$i] =~ /^s/) {
    $send=1;
  }
  else {
    die "unrecognized stage/abbreviation '$stages[$i]'\n";
  }
}

system("${CYGPATH}mkdir -p /c/sony_pet/compile_zone/$user");
chdir "C:\\sony_pet\\compile_zone\\$user";
if($fetch) {
  system("COPY C:\\transfer_tmp\\$user\\agent.tgz .");
  system("rm -r -f agent");
  system("${CYGPATH}bash -c 'gunzip -c agent.tgz | tar xvf -'");
}
chdir "agent";

if($clean) {
  system("${CYGPATH}rm -r -f compile");
}

if($compile) {
  @object_dirs=();
  open(OBJECT_CFG,"<object_dirs.cfg") || 
      die "couldn't open object_dirs.cfg\n";
  while($object_dir=<OBJECT_CFG>) {
    chomp($object_dir);
    $object_dir=~s|//(.*)$||g;
    if($object_dir=~m/\s*(\S+)\s*/) {
      $object_dir=$1;
      push @object_dirs,$object_dir;
      print "found object dir $object_dir\n";
    }
  }
  close(OBJECT_CFG);

  foreach $object_dir ( @object_dirs ) {
    chdir $object_dir;

    printf("dir %s\n",$object_dir);
    if(-e "timer.cfg") {
      print("C:\\Perl\\bin\\Perl.exe C:\\sony_pet\\compile_zone\\stubgentime.pl stub.cfg timer.cfg\n");
      system("C:\\Perl\\bin\\Perl.exe C:\\sony_pet\\compile_zone\\stubgentime.pl stub.cfg timer.cfg");
    }
    else {
      print ("C:\\Perl\\bin\\Perl.exe C:\\OPEN_R_SYS\\scripts\\stubgen stub.cfg\n");
      system("C:\\Perl\\bin\\Perl.exe C:\\OPEN_R_SYS\\scripts\\stubgen stub.cfg");
    }

    chdir ".."; # cur dir is now compile_zone/<user>/agent/
  }

  system("${CYGPATH}mkdir -p compile");
  system("${CYGPATH}bash -c 'cp -r build/* compile'");
  chdir "compile";
  system("${CYGPATH}bash -c 'source ~/.bashrc; date >log_build; source code01.bash >>log_build 2>&1'");

  chdir ".."; # cur dir is now compile_zone/<user>/agent
}

if($send) {
  system("${CYGPATH}mkdir -p /c/transfer_tmp/$user/out/tmp/objs");
  system("${CYGPATH}mkdir -p /c/transfer_tmp/$user/out/tmp/conf");

  system("COPY C:compile\\build\\*.bin c:\\transfer_tmp\\$user\\out\\tmp\\objs");
  system("COPY C:compile\\log_build    c:\\transfer_tmp\\$user\\out\\tmp");
  system("COPY C:connect.cfg c:\\transfer_tmp\\$user\\out\\tmp\\conf");
  system("COPY C:object.cfg  c:\\transfer_tmp\\$user\\out\\tmp\\conf");

  system("${CYGPATH}cp -r config/* /c/transfer_tmp/$user/out/tmp/conf/");

  chdir "c:\\transfer_tmp\\$user\\out\\tmp";
  system("tar zcvf ../result.tgz .");
  chdir "..";
  system("rm -rf tmp");
}

