#!/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: stickit
required options:
-a|--all                    store all stuff to memory stick
-b|--binary                 store binary to memory stick
-c|--config [<path>]        store configuration [store at <path>] to memory stick
options:
-d|--dogroot <root>         set root directory to use
-h|--help                   get help
-u|--user <user>            select user name to use
-n|--nostop                 don't stop the stick when finished
-H|--host <hostname>        write to another machine's memory stick via ssh
";

my $help=0;
my $user="default";
my $dogroot="default";
my $binary_only=0;
my $nostop=0;

my $host="localhost";
my $stickdir="/memstick";

my @stuff_to_store=();
my $store_all=0;
my $store_binary=0;
my $store_config=undef;
my $pc_card=0;
my $user_is_me=0;

my $configignore = <<'CONFIGIGNORE';
behave.cfg
run.cfg
spout.cfg
config.opts
wavelan.cfg
wlanconf.txt
teamplay.cfg
CONFIGIGNORE

my $cvsignore = <<'CVSIGNORE';
RCS 
SCCS 
CVS 
CVS.adm 
RCSLOG 
cvslog.* 
tags 
TAGS 
.make.state     
.nse_depinfo
*~      
#*      
.#*     
,*      
_$*     
*$
*.old   
*.bak   
*.BAK   
*.orig  
*.rej   
.del-*
*.a     
*.olb   
*.o     
*.obj   
*.so    
*.exe
*.Z     
*.elc   
*.ln  
core
CVSIGNORE

$cvsignore =~ s/^/--exclude=/mg;
$cvsignore =~ s/\n/ /g;
$cvsignore =~ s/\s+/ /g;

$configignore =~ s/^/--exclude=/mg;
$configignore =~ s/\n/ /g;
$configignore =~ s/\s+/ /g;

my %opts=
  (
   'help|h' => \$help,
   'user|u:s' => \$user,
   'dogroot|d:s' => \$dogroot,
   'binary|b' => \$binary_only,
   'nostop|n' => \$nostop,
   'all|a' => \$store_all,
   'binary|b' => \$store_binary,
   'config|c:s' => \$store_config,
   'host|H:s' => \$host
  );

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

GetOptions(%opts) || die $usage;

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

if($user    eq "default") {chomp($user   =`whoami`)}
if($dogroot eq "default") {$dogroot=$ENV{"DOGROOT"}}
if(!($host eq "localhost")) {$stickdir="$host:/memstick"}

print "host is $host\n";

if($store_all) {
  @stuff_to_store = ("binary", "config");
  $store_binary = 1;
  $store_config = "";
} else {
  if($store_binary) {
    push(@stuff_to_store,"binary")
  }
  if(defined($store_config)) {
    push(@stuff_to_store,"config $store_config")
  }
}

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

if(-e "/sbin/cardctl") {
  $pc_card = 1;
} else {
  $pc_card = 0;
}

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

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

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

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

#print "[",join(':',@stuff_to_store),"]\n";
print "store=$store_config\n";

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

chdir "$dogroot";

#exit(1);

if($pc_card) {
  open(CARDCTL_OUT,"/sbin/cardctl status |");
  while(<CARDCTL_OUT>) {
    /suspended/ && do {
      printf("card suspended, please reinsert\n");
      exit(8);
    };
  }
}

printf("mounting stick\n");

if($host eq "localhost") {
  # check if local stick is mounted
  $mounted=0;
  open(MOUNT_OUT,"/bin/mount |");
  while(<MOUNT_OUT>) {
    m&/memstick& && do {
      $mounted=1;
    };
  }

  # mount it if not
  if(!$mounted) {
    system("mount /memstick");
  }
} else {
  # This currently attempts to mount a remote memory stick even when it
  # is already mounted.
  system("ssh $host mount /memstick");
}

printf("copying files to $stickdir\n");
if($store_binary) {
  if($user_is_me) {
    system("rsync -e ssh -rt $cvsignore $dogroot/agent/MS/OPEN-R/MW/OBJS/*.BIN $stickdir/open-r/mw/objs/")!=-1 ||
      die "couldn't find rsync in your path\n";
  } else {
    system("rsync -e ssh -rt $cvsignore $dogroot/agent/bin/$user/objs/*.BIN $stickdir/open-r/mw/objs/")!=-1 ||
      die "couldn't find rsync in your path\n";
  }
}
if($store_config) {
    system("rsync -e ssh -rt $configignore $cvsignore $store_config/ $stickdir/")!=-1 ||
    die "couldn't find rsync in your path\n";
}

if(!$nostop) {
  printf("stopping stick\n");
  if($host eq "localhost") {
    system("stopit")!=-1 || die "couldn't find stopit in your path\n";
  } else {
    system("ssh $host stopit")!=-1 || die "couldn't find stopit in your path\n";
  }
}
