#!/bin/sh
USAGE=\
'usage:\n'\
'clam-server [-unix | -inet] [-timeout <secs>] [-debug] [-emacs] <socket_file>'
TYPE='unix'
TIMEOUT='300'
DEBUG='no_debug'
EMACS='not_in_emacs'
#HOLCLAM=`echo $0 | sed -e s/clam-server$/hol-clam/`
HOLCLAM_DIR=`dirname $0`
if test -x $HOLCLAM_DIR/clam-engine
then HOLCLAM=$HOLCLAM_DIR/clam-engine;
     LD_LIBRARY_PATH=$HOLCLAM_DIR/sp-runtime:$LD_LIBRARY_PATH;
     export LD_LIBRARY_PATH
else HOLCLAM=$HOLCLAM_DIR/hol-clam
fi
#Prevent looping
if test "$HOLCLAM" = "$0"
then echo 'unexpected command name'; exit 1
fi

# Process command line arguments
while test $# -gt 0
do
   case $1 in
   -unix)
      TYPE='unix'
      ;;
   -inet)
      TYPE='inet'
      ;;
   -timeout)
      shift
      if test $# -eq 0
      then echo 'duration of timeout not specified'; echo $USAGE; exit 1
      else TIMEOUT=$1
      fi
      ;;
   -debug)
      DEBUG='debug'
      ;;
   -emacs)
      EMACS='in_emacs'
      ;;
   *)
      break
   esac
   shift
done

#Check format of timeout argument
if test -n "`echo $TIMEOUT | sed s%[0-9]%%g`"
then echo "invalid timeout value \`$TIMEOUT' -- should be a number";
     echo $USAGE; exit 1
fi

# Check remaining arguments
if test $# -eq 0
then echo 'socket filename not specified'; echo $USAGE; exit 1
else if test $# -gt 1
     then shift; echo "unrecognised arguments: $*"; echo $USAGE; exit 1
     fi
fi

# Make sure the socket file doesn't already exist
if test -s $1 -o -p $1
then echo "\`$1' already exists"; exit 1
fi

# Execute the server
$HOLCLAM $TYPE $1 $TIMEOUT $DEBUG $EMACS
