#!/devtool/local/bin/perl
#
# remim.pl Script for remim[fqs].pl
#
use Env;

sub usage0()
{
	print "usage: remim.pl tool type [<platform>] {<${imtool}_parameter>}\n";
    print "Used to run commands on several platforms\n";
	print "Normally used indirectly by other scripts like remimf,remimq,remims,remfree etc.\n";
	print "<platform>      32-bit: rs6000 aix aix32 sun32 hp hp_32 sni rm600 linux\n";
	print "                64-bit: alphaosf dec aix64 sun sun64 hp64\n";
	print "                all\n";
	print "<${imtool}_parameter> are passed to '${imtool}'\n\n";
	print "Example: remim.pl imf fast aix kernel\n";

	exit 1;
}

if ( @ARGV < 2 )
{
	usage0();
	exit 1;
}

$imtool   = $ARGV[0];
shift;
$imtype   = $ARGV[0];
shift;

sub usage()
{
	print "usage: rem${imtool} [<platform>] {<${imtool}_parameter>}\n";
	print "<platform>      32-bit: rs6000 aix aix32 sun32 hp hp_32 sni rm600 linux\n";
	print "                64-bit: alphaosf aix64 sun sun64\n";
	print "                all\n";
	print "<${imtool}_parameter> passed to '${imtool}'\n\n";

	print "This is the remote version of ${imtool}.\n";

	print "Example: rem${imtool} aix kernel    recompiles kernel ${imtype} on aix platform\n\n";

	print "Environment: REMUSER_PLATFORM default for <platform>";
	print "             Used only if first parameter given is no valid platform!\n";

	exit 1;
}

if ( @ARGV < 1 )
{
	usage();
	exit 1;
}

$platform = $ARGV[0];
shift;

do "$ENV{'TOOL'}\\bin\\remuser_mach$ENV{'TOOLEXT'}";

if ( $platform =~ /all/ )
{
	@platforms=@REMUSER_ALLHOSTS;
}
else
{
	@platforms= ( "$platform" );
}

@tmpargv=@ARGV;

@commonargv=( "-$imtype" );
push @commonargv, "-uselocaldev" if ( ! defined $IGNOREOWN );

foreach (@platforms) {

	print "Using platform $_\n";

	$host = $REMUSER_HOST{$_};
	if ( ! defined $host )
	{
		$host=$REMUSER_HOST{$ENV{'REMUSER_PLATFORM'}};
		if ( ! defined $host )
		{
			print "no host for platform ${platform} given\n";
			usage();
		}
	}

	@ARGV=@commonargv;

	if ( defined $REMUSER_BIT64MODE{$_} )
	{
		push @ARGV, "-bit64";
	}

	push @ARGV, "-host", $host, "-e", @tmpargv;

	if ( defined $DEBUGREMIM )
	{
		print "$ENV{'TOOL'}\\bin\\rem_vmake$ENV{'TOOLEXT'}";
	    foreach ( @ARGV ) { print " $_"; }
		print "\n";
	}
	else
	{
		do "$ENV{'TOOL'}\\bin\\rem_vmake$ENV{'TOOLEXT'}";
	}
}
