#! /bin/bash -f
##
## "@(#) $Id: ferosDRSds9cmd,v 1.2 2005/10/25 13:51:29 jpritcha Exp $"

usage() {
  printf "\
Usage: ferosDRSds9cmd [-h|vw] <command>  [-d <DRS-directory>]
                          [--maxWait <sec>]

e.g.   ferosDRSds9cmd file fero0001.mt
"
  exit ${exstat:-0}
}

while [ -n "$1" ]; do
  case $1 in
  -h)        usage;;
  -v)        vbose="-v"; shift;;
  --help)    usage;;
  --xpa-options) xpaOptions="${xpaOptions} ${2}"; shift; shift;;
  *)         cmd="$cmd $1"; shift;;
  esac
done

if [ -z "${cmd}" ]; then
  exstat=1
  usage
fi

# Does the ferosDRSds9 command exist?
which ferosDRSds9 > /dev/null 2>&1
if [ $? -ne 0 ]; then
## Does the ds9 command exist?
  which ds9 > /dev/null 2>&1
  if [ $? -eq 0 ]; then
    ln -s `which ds9` $FEROS_DRS/bin/ferosDRSds9
  else
    echo "Error!!! ds9 can not be found in the PATH"
    exit 1
  fi
fi

# Do we have xpa...
which xpaaccess > /dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "Error!!! xpaaccess can not be found in the PATH"
  exit 2
fi

if [ "`xpaaccess ${xpaOptions} ferosDRSds9`" != "yes" ]; then
  echo "Error!!! ferosDRSds9 is not running"
  exit 4
fi

[ ! -z "${vbose}" ] && echo "ferosDRSds9cmd: xpaset ${xpaOptions} -p ferosDRSds9 $cmd"
xpaset ${xpaOptions} -p ferosDRSds9 $cmd
