#!/bin/sh

# $Id: makexite_remote,v 1.9 1997/03/06 13:16:01 svein Exp $

help=0; dont=0; verbose=0; targets=""; tars=""; build_dir=""; hosts="";
h1=""; src_dir=""; loc_dir=0

while test $# -gt 0
do
  case $1 in
    -all)     targets="-c -d -e -i -l -v";
              tars="-c -e -l -v"; verbose=1;           shift;;
    -h|-help) help=1;                                  break;;
    -n)       dont=1;                                  shift;;
    -d|-i)    targets="$targets $1";                   shift;;
    -p)       loc_dir=1;                               shift;;
    -s)       shift
              if test $# -le 1
              then
                echo "Argument missing for option $1."
                help=1;                                break;
              fi
              src_dir="$src_dir $1";                   shift;;
    -v)       targets="$targets $1"; tars="$tars $1";
              verbose=1;                               shift;;
    -xite)    shift
              if test $# -le 1
              then
                echo "Argument missing for option $1."
                help=1;                                break;
              fi
              build_dir=$1;                            shift;;
    -*)       targets="$targets $1"; tars="$tars $1";  shift;;
    *)        hosts="$hosts $1";
              if test -z "$h1"
	      then
	        h1=$1
	      fi;                                      shift;;
  esac
done

if test "$loc_dir" -eq 1; then
  tars="$tars -p"
  targets="$tars"
fi

if test "$help" = 1
then
  echo "usage: $0 [<option>...] <host> [<host>...]"
  echo "Build XITE sequentially or in parallel on one or more remote hosts"
  echo "(different architectures)."
  echo "You must have remote access to the host(s)."
  echo "       -all        : Short for -c -d -e -i -l -v (default)."
  echo "       -c          : Clean first."
  echo "       -d          : Build documentation."
  echo "       -e          : Build executables."
  echo "       -h          : Print this text and exit."
  echo "       -i          : Install header-files."
  echo "       -l          : Build libraries."
  echo "       -p          : Build in parallel on several platforms."
  echo "       -n          : Don't build, just echo commands."
  echo "       -s <dir>    : Source dir to build. Default: All."
  echo "       -v          : Be verbose."
  echo "       -xe         : Build X-dependent executables only."
  echo "       -xite <dir> : Build under this XITE home-directory on"
  echo "                     remote host."
  echo "                     Default: Environment variable XITE_HOME."
  echo "       <host>      : Name of host."

  exit
fi

if test -z "$XITE_HOME"
then
  XITE_HOME=`pwd`
fi

if test -z "$build_dir"
then
  build_dir=$XITE_HOME
fi

for h in $hosts
do
  if test "$h" = "$h1"
  then
    if test "$verbose" = 1 -o "$dont" = 1
    then
      echo "rsh $h $build_dir/etc/makexite $targets \\"
      echo "  -xite $build_dir $src_dir > $build_dir/log_$h 2>&1"
    fi

    if test "$dont" = 0
    then
      rsh $h $build_dir/etc/makexite $targets \
        -xite $build_dir $src_dir > $build_dir/log_$h 2>&1 &
    fi
  else # first host
    if test "$verbose" = 1 -o "$dont" = 1
    then
      echo "rsh $h $build_dir/etc/makexite $tars \\"
      echo "  -xite $build_dir $src_dir > $build_dir/log_$h 2>&1"
    fi

    if test "$dont" = 0
    then
      rsh $h $build_dir/etc/makexite $tars \
        -xite $build_dir $src_dir > $build_dir/log_$h 2>&1 &
    fi
  fi # first host

  if test "$loc_dir" -eq 0; then
    wait # Wait until the build is done on the present host
  fi
done
