#!/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: getit
required options:
-a|--all                    get all stuff from ftp machine
-b|--binary                 get binary from ftp machine
-c|--config [<path>]        get configuration [store at <path>] from 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
";

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

my @stuff_to_get=();
my $get_all=0;
my $get_binary=0;
my $get_config=undef;
my $login_user;
my $user_is_me=0;

my %opts=
  (
   'help|h' => \$help,
   'host|H=s' => \$host,
   'user|u=s' => \$user,
   'dogroot|d=s' => \$dogroot,
   'all|a' => \$get_all,
   'binary|b' => \$get_binary,
   'config|c:s' => \$get_config
  );

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

GetOptions(%opts) || die $usage;

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

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

chomp($login_user=`whoami`);
if($user eq $login_user) {
  $user_is_me = 1;
} else {
  $user_is_me = 0;
}

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

if($get_all) {
  @stuff_to_get = ("binary", "config");
  $get_binary = 1;
  $get_config = "";
} else {
  if($get_binary) {
    push(@stuff_to_get,"binary")
  }
  if(defined($get_config)) {
    push(@stuff_to_get,"config $get_config")
  }
}

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

if(defined($get_config) && $get_config eq "") {
  if($user_is_me) {
    $get_config = "$dogroot/agent/config/";
  } else {
    $get_config = "$dogroot/agent/bin/$user/conf/";
  }
}

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

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

#print "[",join(':',@stuff_to_get),"]\n";

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

mkdir "/tmp/transfer_out",0777;
chdir "/tmp/transfer_out";

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

if($get_binary) {
  system("tar zxf objs.tgz");
}
system("mkdir config");
if(defined($get_config)) {
  chdir "config";
  system("tar zxf ../conf.tgz");
  chdir "..";
}

if(!$user_is_me) {
  system("mkdir -p $dogroot/agent/bin/$user/objs");
  system("mkdir -p $dogroot/agent/bin/$user/conf");
}

if($get_binary) {
  my $target_dir="$dogroot/agent/bin/$user/objs/";
  if($user_is_me) {
    $target_dir="$dogroot/agent/MS/OPEN-R/MW/OBJS/";
  } else {
    $target_dir="$dogroot/agent/bin/$user/objs/";
  }
  #system("pwd");
  #print("cp MS/OPEN-R/MW/OBJS/*.BIN $target_dir\n");
  system("cp MS/OPEN-R/MW/OBJS/*.BIN $target_dir");
  #print("chmod u+x $target_dir/*.BIN\n");
  system("chmod u+x $target_dir/*.BIN");
}

if(defined($get_config)) {
  #system("rm -rf $get_config/config $get_config/config $get_config/open-r");
  system("cp -r config/* $get_config/");
}

chdir "/";
system("rm -rf /tmp/transfer_out");

exit 0;
