#!/devtool/local/bin/perl
#
# rem_vmake.pl [-fast | -slow | -quick]
#              [ -uselocaldev ] [ -bit64 ]
#              [ -os name ] [ -host name ]
#              [ -e cmd ]
#

use Env;
use Getopt::Long;

$fast = $slow = $quick = $free = $uselocaldev =$os = $host = $bit64 = undef;

@tmp=@ARGV;
@ARGV=();
$cmd_opts=undef;
foreach (@tmp) {
        if ( ! defined $cmd_opts && $_ ne "-e") {
                push @ARGV, $_;
        } else {
				if ( !defined $cmd_opts )
				{
					$cmd_opts = "";
				}
				else
				{
	                $cmd_opts=$cmd_opts." ".$_;
				}
        }
}

GetOptions ('fast!', 'slow!', 'quick!', 'free!', 'uselocaldev!', 'bit64!', 'os:s', 'host:s');

$os          = $opt_os          if ( defined $opt_os );
$host        = $opt_host        if ( defined $opt_host );
$uselocaldev = $opt_uselocaldev if ( defined $opt_uselocaldev );
$bit64       = $opt_bit64       if ( defined $opt_bit64 );

if ( !defined $host )
{
   do "$ENV{'TOOL'}\\bin\\remuser_mach$ENV{'TOOLEXT'}";
   $host = $REMUSER_HOST{$os};
}

die "missing remote host specification\n" if ( ! defined $host );

#
# build compilation mode ( only one out of fast, slow, quick survives... )
# if none is given command is handled directly...
#
if ( defined $opt_fast )
{
	$opt_slow = undef;
	$opt_quick = undef;
}
if ( defined $opt_slow )
{
	$opt_fast = undef;
	$opt_quick = undef;
}
if ( defined $opt_quick )
{
	$opt_fast = undef;
	$opt_slow = undef;
}
if ( defined $opt_free )
{
	$opt_fast = undef;
	$opt_quick = undef;
	$opt_slow = undef;
}

$remote_cmd="rsh $host -l remuser ( ./serial.sh $USERNAME ";
chomp ( $local_host = `hostname` );
$remote_cmd = $remote_cmd."$local_host ";

if ( defined $uselocaldev )
{
	$local_own  = $OWN;
	$local_own  =~ s/\\/\//g;
	$remote_cmd = $remote_cmd."$local_own// ";
}
else
{
	$remote_cmd=$remote_cmd."X ";
}

if ( defined $bit64 )
{
	$remote_cmd=$remote_cmd."BIT64 ";
}

$remote_cmd=$remote_cmd."imf" if ( defined $opt_fast );
$remote_cmd=$remote_cmd."ims" if ( defined $opt_slow );
$remote_cmd=$remote_cmd."imq" if ( defined $opt_quick );
$remote_cmd=$remote_cmd.$cmd_opts." )";

if ( defined $DEBUGREM_VMAKE )
{
    print $remote_cmd;
}
else
{
	system($remote_cmd);
}
