#!/usr/local/bin/perl -w

#This script takes the arguments to ssh from the ircam rack pc
#and runs them as commands.   Hopefully, this alleviates any
#strange behavior of the ssh not running certain commands.  Normally,
#this stuff would be unnecessary, but may be valuable with future
#additions, liking making it a socket based daemon.  DJM 03/09.

#use strict;

#Read in command line arguments, eventually call the equivalent program
#except for the "follow" command, which should prepend the loading
#of a catalog name.
$nargs=$#ARGV + 1;
#print "Number of arguments: ",$nargs,"\n";
#print $ARGV[0]," ", $ARGV[1], " ", $ARGV[2],"\n";

if ($nargs == 0) {
 @theargs = ("");
} elsif ($nargs == 1) {
  @theargs=split(/ /,$ARGV[0]);
} else {
  @theargs = @ARGV;
}
$nargs=$#theargs+1;

#chdir("/home/observer/wiro/track/");
chdir("/home/observer/wiro/track/");

#Check number of command line arguments, to see if it was passed a
#quote delimited string or just the individual elements. If it is
#a single string, and asks for the follow command, split it first.
#Also, check for the correct command names.

#$combine = join(' ',@theargs);
#$basedir = "/home/observer/wiro/track/";
$basedir="";


if ($theargs[0] =~ /nn|ss|ee|ww/) { 
    $thecommand = $basedir . $theargs[0] . " " . $theargs[1];
    print $thecommand,"\n";
} elsif ($theargs[0] =~ /follow/) {
    $thecommand = $basedir . $theargs[0] . " " . $theargs[1];
    $catalog = $theargs[2];
    $thecommand = $basedir . "catalog "  . $catalog . "; " . $thecommand;
    #print $thecommand,"\n";
} else {
    $thecommand = $theargs[0];
}

#print "Number of arguments: ",$nargs,"\n";
#print, $thecommand, "\n";

system($thecommand);
