#! /bin/sh
#############################################################################
# Client Mass Install Aid for Job Partitioner        Udo Sprute, 1995.10.27 #
#############################################################################

CLIENT="./partnerd -t -n5"

if test "@$1" = "@-b"
then
   BINARY=true
   shift
fi

if test $# -ne 1 || test "@$1" = "@-?"
then
   echo "Usage:  $0 [-b] <server's IP number>"
   echo ""
   echo "This tool reads a list of hostnames and directories from stdin,"
   echo "one pair per line.  It tries to create uniq subdirectories"
   echo "for client daemons there within and to start them there."
   echo ""
   echo "Directories are relative to your home directory on each machine."
   echo "If you ommit them, your current working directory will be taken."
   echo ""
   echo "Calls to $0 may be accumulated."
   echo "Option -b saves compilation time if machines are binary compatible."
   echo "$0 -s tries to restart regularly stopped remote clients."
   echo "$0 -r tries to remove all clients again."
   echo ""
   echo "This may only work if you can rcp to those machines and use rsh."
   echo "Good luck."
   exit 1
fi

RCP="rcp -p"
case `uname` in
   HP-UX)
      RSH="remsh"
      ;;
   *)
      RSH="rsh"
      ;;
esac

if test "@$1" = "@-r"
then

   cat partnerd.lst |
   while read HOST DIR
   do
      $RSH $HOST -n "cd $DIR/$HOST && {
         test -f partnerd.pid && { kill \`cat partnerd.pid\`; sleep 1; }
         rm * && cd && rmdir $DIR/$HOST && { rmdir $DIR 2>&-; true; } &&
         echo Client stopped on $HOST, directory $DIR/$HOST removed. &&
         exit 0
         echo Files remaining on host $HOST.
      }" &&
      continue
      echo "Remote procedure call to host $HOST failed."
   done
   rm -f partnerd.lst

elif test "@$1" = "@-s"
then

   cat partnerd.lst |
   while read HOST DIR
   do
      $RSH $HOST -n "
         PATH=\$PATH:$PATH
         cd $DIR/$HOST &&
         if test -f partnerd.pid
         then
            echo Client pretends to be running on $HOST in $DIR/$HOST.
         else
            $CLIENT &&
            echo Client restarted on $HOST in directory $DIR/$HOST. &&
            exit 0
            echo Client execution problem on $HOST.
         fi &&
         exit 0
         echo Directory $DIR/$HOST missing on $HOST.
      " &&
      continue
      echo "Remote procedure call to host $HOST failed."
   done

else

   if test "`echo $1 | sed s/[0-9.]//g`" != ""
   then
      echo "You're supposed to provide your server's IP number."
      echo "A failure in this respect would make all clients worthless."
      exit 2
   fi

   if test "$BINARY" != ""
   then
      rm -f partnerd
      make SERVER="$1" partnerd || exit 3
   fi

   while read HOST DIR
   do
      if test "$HOST" = ""
      then
         echo "End with ^D."
         continue
      fi
      if test "$DIR" = ""
      then
         DIR=`echo $PWD | sed "s%^$HOME/*%%"`
         if test "$DIR" = ""
         then
            DIR=.
         fi
      else
         DIR=`echo $DIR | sed "s%/$%%"`
      fi
      if test "$BINARY" = ""
      then
         $RSH $HOST -n "mkdir -p $DIR/$HOST" &&
         $RCP Makefile $HOST:$DIR/$HOST &&
         $RCP client.c $HOST:$DIR/$HOST &&
         $RSH $HOST -n "
            PATH=\$PATH:$PATH
            cd $DIR/$HOST &&
            make SERVER=$1 partnerd &&
            $CLIENT &&
            echo Client running on $HOST in directory $DIR/$HOST. &&
            exit 0
            echo Client compilation problem on $HOST.
         "
      else
         $RSH $HOST -n "mkdir -p $DIR/$HOST" &&
         $RCP partnerd $HOST:$DIR/$HOST &&
         $RSH $HOST -n "
            PATH=\$PATH:$PATH
            cd $DIR/$HOST &&
            $CLIENT &&
            echo Client running on $HOST in directory $DIR/$HOST. &&
            exit 0
            echo Client execution problem on $HOST.
         "
      fi &&
      echo "$HOST" "$DIR" >> partnerd.lst &&
      continue
      echo "Remote procedure call to host $HOST failed."
   done

fi

