#!/bin/sh
USAGE=\
'usage: clam-server [-unix | -inet] [-timeout <secs>] [-debug] <socket_file>'
TYPE='unix'
TIMEOUT='300'
DEBUG='no_debug'
#HOLCLAM=`echo $0 | sed -e s/clam-server$/hol-clam/`
HOLCLAM=`dirname $0`/hol-clam
#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'
      ;;
   *)
      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
