: $Workfile:   SecurityServer.sh  $ $Revision:   1.0  $
#**********************************************************************#
#*                                                                    *#
#* Copyright (c) 2002 by Sun Microsystems, Inc.                       *#
#* All rights reserved.                                               *#
#*                                                                    *#
#**********************************************************************#

USAGE="usage: msfserver {-s|-r|-t|-p} [{-n|username password}]"

help() {
echo "MSF Security Server administration tool"
echo $USAGE
echo "   -s    start the Security Server in a background process"
echo "   -r    refresh the active Security Server process"
echo "   -t    terminate the active Security Server process"
echo "   -p    print statistics from the active Security Server process"
echo "   -n    don't prompt for USERNAME & PASSWORD; use Unix userid"
echo "   USERNAME to be authenticated / authorized for this action"
echo "   PASSWORD for authenticating that username"
}

#---
# This script assumes that it is located in the bin directory of the
# installed MSF product. If it is moved, then the MSF_HOME environment
# variable can be set to override the calculations here.
#---

#
# Determine where the installation of MSF is.
#
if [ "$MSF_HOME"A = "A" ]; then
    D=`dirname \`which $0\``
    B=`basename \`which $0\``
    abspath="`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`/$B"
    tmp1=`dirname $abspath`
    MSF_HOME=`dirname $tmp1`
fi
if [ ! -f "${MSF_HOME}/lib/secrt.jar" ]; then
    echo "Could not find runtime under '"${MSF_HOME}"'"
    exit 1
fi
if [ "$MSF_INSTANCE"A = "A" ]; then
    echo "Must specify value for environment variable MSF_INSTANCE"
    exit 1
else
    if [ ! -f "${MSF_HOME}/config/${MSF_INSTANCE}/MSFconfig.properties" ]; then
        echo "No configuration under '"${MSF_HOME}/config/${MSF_INSTANCE}"'"
        exit 1
    fi
fi

set -- `getopt hnprst $*`

if test  "$1" != "--"
then
  optset=""
  noprompt=""
  while test "$1" != "--"
  do
    case $1 in
    -h)
	help
	exit 0
	;;
    -n)
        if test  "$noprompt"A != "A"; then echo $USAGE; exit 1; fi
        noprompt="n"
        shift;;
    -s)
        if test  "$optset"A != "A"; then echo $USAGE; exit 1; fi
        echo starting Security Server...
        optset="s"
        shift;;
    -r)
        if test  "$optset"A != "A"; then echo $USAGE; exit 1; fi
        echo refreshing Security Server...
        optset="r"
        shift;;
    -t)
        if test  "$optset"A != "A"; then echo $USAGE; exit 1; fi
        echo terminating Security Server...
        optset="t"
        shift;;
    -p)
        if test  "$optset"A != "A"; then echo $USAGE; exit 1; fi
        echo printing Security Server statistics...
        optset="p"
        shift;;
    *)
# illegal option encountered
	echo $USAGE
        exit 1
    esac
  done
else
# no option specified
	echo $USAGE
	exit 2
fi

# make sure some valid option was requested
if test "$optset"A = "A"
then
  echo $USAGE
  exit 1
fi

# only pick up username and password from command line if "-n" not specified
if test "$noprompt"A = "A"
then
  PARAM2=$2
  PARAM3=$3
fi

# make sure any unexpected 'kill' in PasswordReader resets terminal echo, etc.
saveterm=`stty -g`
trap "stty $saveterm" 0

java -DMSF_HOME=$MSF_HOME -DMSF_INSTANCE=$MSF_INSTANCE \
 -Djava.security.properties=$MSF_HOME/config/java.security \
 -Djava.library.path=$MSF_HOME/lib:$LD_LIBRARY_PATH \
 -classpath \
 $MSF_HOME/lib/secrt.jar:$MSF_HOME/lib/secrtpa.jar:$MSF_HOME/config/$MSF_INSTANCE \
 com.sun.emp.security.server.DirectSecurityServer -$noprompt$optset $PARAM2 $PARAM3

RET=$?
if test $RET -ne 0
then echo SecurityServer command terminated with error
fi
exit $RET

