#!/usr/bin/perl

# /* LICENSE:
#   =========================================================================
#     CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
#     Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
#     School of Computer Science, Carnegie Mellon University
#     All rights reserved.
#   ========================================================================= */

use Getopt::Long;

$usage = "usage: sendit
required options:
-a|--all                    send all stuff to ftp machine
-b|--binary                 send binary to ftp machine
-c|--config [<path>]        send configuration [at <path>] to ftp machine
options:
-d|--dogroot <root>         set root directory to use
-h|--help                   get help
-H|--host <compile host>    select compile host
-u|--user <user>            select user name to use
-v|--verbose                print out verbose status
";

my $help=0;
my $host="default";
my $user="default";
my $dogroot="default";
my $verbose=0;

my @stuff_to_send=();
my $send_all=0;
my $send_binary=0;
my $send_config=undef;

my %opts=
  (
   'help|h' => \$help,
   'host|H:s' => \$host,
   'user|u:s' => \$user,
   'verbose|v' => \$verbose,
   'all|a' => \$send_all,
   'binary|b' => \$send_binary,
   'config|c:s' => \$send_config
  );

Getopt::Long::Configure("bundling");

GetOptions(%opts) || die $usage;

if($help) {
  print $usage;
  exit(0);
}

my $login_user;
chomp($login_user=`whoami`);

if($host    eq "default") {$host   =$ENV{"DOGHOST"}}
if($user    eq "default") {chomp($user   =`whoami`)}
if($dogroot eq "default") {$dogroot=$ENV{"DOGROOT"}}

if(!defined($host)) {
  $host="guster.coral.cs.cmu.edu";
}

if($send_all) {
  @stuff_to_send = ("binary", "config");
  $send_binary = 1;
  $send_config = "";
} else {
  if($send_binary) {
    push(@stuff_to_send,"binary")
  }
  if(defined($send_config)) {
    push(@stuff_to_send,"config $send_config")
  }
}

if(scalar(@stuff_to_send) < 1) {
  print "You didn't specify any of the required options, you must specify at least one.\n";
  print $usage;
  exit(2);
}

if(defined($send_config) && $send_config eq "") {
  $send_config = "$dogroot/agent/config/";
}

if(defined($send_config)) {
  if($send_config !~ m%^/%) {
    $send_config = "./" . $send_config;
  }

  if($send_config =~ m%^\.%) {
    $pwd = `pwd`;
    chomp($pwd);
    $send_config = $pwd . "/" . $send_config;
  }
  #$send_config =~ m%^((?:.*/)?)([^/]+)/?$%;
  #$send_config_dir  = $1;
  #if($send_config_dir eq "") {
  #  $send_config_dir = "./";
  #}
  #$send_config_file = $2;
}

#print "[",join(':',@stuff_to_send),"]\n";
#print "send_config=$send_config\n";

print "host=$host user=$user dogroot=$dogroot\n";

chdir "$dogroot/agent";
$verbose_flag="";
if($verbose) {$verbose_flag="-v ";}
$sysname=`uname`;
chomp($sysname);
$exclude_flag="";
if($sysname ne "Darwin") {$exclude_flag="--exclude '*.o'";}
system("mkdir -p /tmp/$user");
if($verbose) {
  printf("tar zcf /tmp/$user/objs.tgz $verbose_flag $exclude_flag MS\n");
}
if($send_binary) {
  system("tar zcf /tmp/$user/objs.tgz $verbose_flag $exclude_flag MS");
}
chdir "..";
if(defined($send_config)) {
  chdir "$send_config";
  system("tar zcf /tmp/$user/conf.tgz $verbose_flag --exclude CVS --exclude config.opts ./");
  chdir "$dogroot";
}

if($send_binary || defined($send_config)) {
  $ssh_cmd="ssh $login_user\@$host mkdir -p /data/dogs/shared_storage/$user";
  system($ssh_cmd);
}

if($send_binary) {
  $scp_cmd="scp -q /tmp/$user/objs.tgz $login_user\@$host:/data/dogs/shared_storage/$user/objs.tgz";
  #print("cmd: $scp_cmd\n");
  system($scp_cmd);
}
if(defined($send_config)) {
  $scp_cmd="scp -q /tmp/$user/conf.tgz $login_user\@$host:/data/dogs/shared_storage/$user/conf.tgz";
  #print("cmd: $scp_cmd\n");
  system($scp_cmd);
}

unlink("/tmp/$user/objs.tgz") if($send_binary);
unlink("/tmp/$user/conf.tgz") if(defined($send_config));

