#! /bin/sh
#
# Start the p4_server for the machines in the given file, or the current 
# machines table
defport=1234
MPIR_HOME=#MPIR_HOME#
LARCH=#DEFAULT_ARCH#
do_script=/bin/sh
for arg in "$@" ; do
    case $arg in 
	-help)
	echo "$0 [ -port=n ] [ -hosts=filename ] [ -arch=name ] [-hunt] [-kill]"
	exit 1
	;;
	
	-port=*)
	defport=`echo $arg | sed 's/-port=//'`
	;;
	
	-hosts=*)
	filename=`echo $arg | sed 's/-hosts=//'`
	;;

	-arch=*)
	LARCH=`echo $arg | sed 's/-arch=//'`
	;;

	-hunt)
	do_hunt=1
	;;

	-kill)
	do_kill=1
	echo "Kill not implemented..."
	exit 1
	;;
		
	-echo)
	set -x
	;;

	-trial)
	do_script=cat
	;;

	*)
	echo "Unrecognized argument $arg"
	exit 1
	;;
    esac
done
if [ -z "$filename" ] ; then
    if [ -z "$LARCH" ] ; then 
        LARCH=`$MPIR_HOME/bin/tarch`
    fi
    filename=$MPIR_HOME/util/machines/machines.$LARCH
fi
if [ ! -s $filename ] ; then 
    echo "Can not find file $filename containing machines"
    exit 1
fi
serverloc=$MPIR_HOME/lib/$LARCH/ch_p4/serv_p4
if test "$do_kill" = 1 ; then	
    /bin/rm -f .tmp
    awk '{ if (substr($1,1,1) != "#") {
    printf( "#RSH_COMMAND# %s %c(ps auxww | grep serv_p4 | grep -v grep | awk %c{printf(%ckill -2 %%s\\n%c, \\$2);}%c -)%c\n", $1, 34, 39, 34, 34, 39, 34 );
    }}' $filename > .tmp
    $do_script .tmp
#    /bin/rm -f .tmp
elif test "$do_hunt" = 1 ; then
    # We'd like to send the rsh's to sh, but doing so failed (cat foo | sh
    # would read part of the file and exit).
    /bin/rm -f .tmp
    awk '{ if (substr($1,1,1) != "#") {
    printf( "echo %s\n", $1 );
printf( "#RSH_COMMAND# %s %c(ps auxww | grep serv_p4 | grep -v grep | grep -v #RSH_COMMAND# )%c\n", $1, 34, 34 );
	}}' $filename > .tmp
     $do_script .tmp
     /bin/rm -f .tmp
else
    # Using machine[$1] == "" allows use to capture duplicates in the
    # list of machines before starting servers on them
    echo "$defport $serverloc" | awk '{ if (NR==1) { PORT=$1;SERV=$2;} else {
	if (substr($1,1,1) != "#" && machine[$1]=="") {
	    machine[$1]=PORT;
	    printf( "#RSH_COMMAND# %s %s -o -p %s &\n", $1, SERV, PORT );
	    printf( "echo starting %s on %s with %s\n", SERV, $1, PORT );
	}}}' - $filename | $do_script
fi
