#!/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: set_test_motions <test_mot1> <test_mot2> ...
options:
-h|--help                   get help
-n|--nostop                 don't stop memory stick after writing files
";

my $help=0;
my $nostop=0;

my $pc_card=0;

my %opts=
  (
   'help|h' => \$help,
   'nostop|n' => \$nostop
  );

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

GetOptions(%opts) || die $usage;

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

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

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");
$mounted=0;
open(MOUNT_OUT,"/bin/mount |");
while(<MOUNT_OUT>) {
  m&/memstick& && do {
    $mounted=1;
  };
}

if(!$mounted) {
  system("mount /memstick");
}

@files = @ARGV;
@out_files = (
              "dance1.mot",
              "k_dive.mot",
              "k_bump.mot",
              "k_fwd.mot",
              "k_head.mot",
              "k_heads.mot",
              "k_hold.mot",
              "gu_back.mot",
              "gu_front.mot",
              "gu_side.mot");

printf("copying files\n");
for($i=0; $i<=$#files && $i<=$#out_files; $i++) {
  printf("%2d = '%s'\n",$i,$files[$i]);
  system("cp $files[$i] /memstick/motion/ers7/$out_files[$i]");
  system("cp $files[$i] /memstick/motion/ers7/$out_files[$i]");
}
if($#files > $#out_files) {
  printf("too many files: extra %d files ignored starting with '%s'\n",$#files-$#out_files,$files[$#out_files+1]);
} else {
  printf("used %d out of %d possible motion file slots\n",$#files+1,$#out_files+1);
}

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