#!/bin/bash
#
# Init file for the NorduGrid httpsd server
#
# chkconfig: 2345 55 25
# description: HHTP(S|GSI) Server
#
# processname: httpsd
# config: /etc/httpsd.conf
# config: /opt/nordugrid/etc/httpsd.conf
# pidfile: /var/run/httpsd.pid

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0

export NORDUGRID_LOCATION=${NORDUGRID_LOCATION:-/opt/nordugrid}
export GLOBUS_LOCATION=${GLOBUS_LOCATION:-/opt/globus}
export VOMS_LOCATION=${VOMS_LOCATION:-/opt/voms}

export TMP_DIR=${TMP_DIR:-/tmp}

HTTPSD_USER=${HTTPSD_USER:-""}
HTTPSD_PORT=${HTTPSD_PORT:-8000}

SWITCH_USER=yes
if [ '0' != `id -u` ] ; then 
  HTTPS_USER=`id -un`
  SWITCH_USER=
fi

if [ -z "$HTTPSD_CONFIG" ] ; then
  HTTPSD_CONFIG=/etc/httpsd.conf
  if [ ! -r "$HTTPSD_CONFIG" ]; then
    HTTPSD_CONFIG=$NORDUGRID_LOCATION/etc/httpsd.conf
  fi
fi
if [ ! -r "$HTTPSD_CONFIG" ]; then
  echo "Missing configuration file"
  exit 1
fi

HTTPSD_CONNECTIONS=${GFS_CONNECTIONS:-}

export LD_LIBRARY_PATH=$NORDUGRID_LOCATION/lib:$GLOBUS_LOCATION/lib:$VOMS_LOCATION/lib
unset X509_USER_CERT
unset X509_USER_KEY
unset X509_USER_PROXY
unset X509_SERVER_CERT
unset X509_SERVER_KEY
export X509_RUN_AS_SERVER=1

prog=httpsd

PID_FILE=/var/run/$prog.pid
LOCKFILE=/var/lock/subsys/$prog
LOGFILE=/var/log/$prog.log

if [ ! -z "$HTTPSD_USER" ] ; then
  mkdir -p "$NORDUGRID_LOCATION/var"
  PID_FILE="$NORDUGRID_LOCATION/var/$prog.pid"
  LOCKFILE="$NORDUGRID_LOCATION/var/$prog"
  LOGFILE="$NORDUGRID_LOCATION/var/$prog.log"
  export X509_USER_CERT="$GLOBUS_LOCATION/etc/hostcert.pem"
  export X509_USER_KEY="$GLOBUS_LOCATION/etc/hostkey.pem"
  export X509_CERT_DIR="$GLOBUS_LOCATION/share/certificates"
  export GRIDMAP="$GLOBUS_LOCATION/etc/grid-mapfile"
fi

start()
{
  echo -n "Starting $prog    "
  if [ ! -x $NORDUGRID_LOCATION/bin/httpsd ] ; then
    echo "Missing executable"
    exit 1
  fi
  
  CMD="$NORDUGRID_LOCATION/bin/httpsd"
  if [ ! -z "$HTTPSD_PORT" ] ; then
    CMD="$CMD -p $HTTPSD_PORT"
  fi
  if [ ! -z "$HTTPSD_CONFIG" ] ; then
    CMD="$CMD -c $HTTPSD_CONFIG"
  fi
  if [ ! -z "$HTTPSD_CONNECTIONS" ] ; then
    CMD="$CMD -n $HTTPSD_CONNECTIONS"
  fi
  
#        ulimit -c 0

  if [ ! -z "$HTTPSD_USER" ] && [ ! -z "$SWITCH_USER" ] ; then 
    su - $HTTPSD_USER -c "$CMD 1>>$LOGFILE 2>&1 &"
    echo $! > ${PID_FILE}
    RETVAL=$?
  else 
    $CMD 1>>$LOGFILE 2>&1 &
    echo $! > ${PID_FILE}
    RETVAL=$?
  fi
  
  [ "$RETVAL" = 0 ] && success

  [ "$RETVAL" = 0 ] && touch $LOCKFILE

  echo
}

stop()
{
  echo -n "Stopping $prog"

  if [ -f $PID_FILE ] ; then
    kill `cat $PID_FILE` && success "$prog shutdown" || failure "$prog shutdown"
    rm -f $PID_FILE $LOCKFILE
  else
    failure "$prog shutdown"
  fi

  echo
}

case "$1" in
  	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		stop
		start
		;;
	condrestart)
		if [ -f $LOCKFILE ] ; then
			stop
			# avoid race
			sleep 3
			start
		fi
		;;
	status)
		status $prog
		RETVAL=$?
		;;
	*)
		echo "Usage: $0 {start|stop|restart|reload|condrestart|status}"
		RETVAL=1
esac
exit $RETVAL
