#!/usr/bin/env slsh

require ("pvm_ms");

variable Slave_Pgm= path_concat (getcwd(), "slave");
if (__argc != 3)
{
   () = fprintf (stderr, "%s <remote-cmd> <N>\n", __argv[0]);
   exit (1);
}

static variable Cmd = __argv[1];
static variable N = integer (__argv[2]);

static define slave_spawned_callback (tid, host, argv)
{
   vmessage ("Slave running %s spawned on %s with task-id %d\n",
	     argv[0], host, tid);
}
pvm_ms_set_slave_spawned_callback (&slave_spawned_callback);

static define main ()
{
   variable pgm_argvs = Array_Type[N];
   variable pgm_argv = [Slave_Pgm, Cmd];

   pgm_argvs[*] = pgm_argv;

   % Make sure these variables are set up on the remote end
   %pvm_export ("SLSH_CONF_DIR");
   %pvm_export ("SLSH_PATH");
   
   pvm_ms_set_debug (0);
   %pvm_ms_set_hosts ("vex", "verus", "aluche");
   pvm_ms_set_num_processes_per_host (2);

   variable exit_status = pvm_ms_run_master (pgm_argvs);

   foreach (exit_status)
     {
	variable s = ();
	() = fprintf (stdout, "%s [exit %d]==>\n%s\n\n", 
		      Cmd, s.exit_status, s.stdout);
     }
   
   exit (0);
}
main ();
