#!/bin/bash
#
# Init file for the NorduGrid httpsd
#
# chkconfig: 2345 55 25
# description: NorduGrid httpsd
#
# config: /etc/sysconfig/globus
# config: /etc/sysconfig/nordugrid
# config: /opt/nordugrid/etc/nordugrid.conf
# config: /etc/nordugrid.conf

# source function library
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
else
  success() { echo -n "OK" 
  } 
  failure() { echo -n "FAILURE"
  }
  status() {
    pid=`pidof -o $$ -o $PPID -o %PPID -x $1`
    if test "x$pid" != "x"; then
      echo "$1 (pid $pid) is running..."
      return 0
    fi

    if test -f $PID_FILE; then
      read pid < $PID_FILE
      if test "$pid" != ""; then
        echo "$1 stopped but pid file exists"
        return 1
      fi
    fi
    if test -f $LOCKFILE; then
      echo "$1 stopped but lockfile exist"
      return 2
    fi
    echo "$1 is stopped"
    return 3
  }
fi

# sysconfig files
if [ -f /etc/sysconfig/globus ]; then
    . /etc/sysconfig/globus
fi
if [ -f /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
fi

prog=`basename $0`

# GLOBUS_LOCATION
GLOBUS_LOCATION=${GLOBUS_LOCATION:-/opt/globus}
if [ ! -d "$GLOBUS_LOCATION" ]; then
  echo "GLOBUS_LOCATION not found" 
  exit 1
fi

# NORDUGRID_LOCATION
NORDUGRID_LOCATION=${NORDUGRID_LOCATION:-/opt/nordugrid}
if [ ! -d "$NORDUGRID_LOCATION" ]; then
  echo "NORDUGRID_LOCATION not found" 
  exit 1
fi
export NORDUGRID_LOCATION

CMD="$NORDUGRID_LOCATION/sbin/$prog"
if ! test -x "$CMD"; then
    failure "Missing executable"
    echo
    exit 1
fi

# NORDUGRID_CONFIG
if test "x$NORDUGRID_CONFIG" = "x"; then
  if [ -r $NORDUGRID_LOCATION/etc/nordugrid.conf ]; then
    NORDUGRID_CONFIG=$NORDUGRID_LOCATION/etc/nordugrid.conf
  elif [ -r /etc/nordugrid.conf ]; then
    NORDUGRID_CONFIG=/etc/nordugrid.conf
  fi
fi
if [ ! -r "$NORDUGRID_CONFIG" ]; then
   if [ ! -r $NORDUGRID_LOCATION/etc/$prog.conf ]; then
     echo "NorduGrid configuration not found (usually /etc/nordugrid.conf)"
     exit 1
   else
     echo "NorduGrid common configuration not found (usually /etc/nordugrid.conf)"
     echo "  using low level configuration instead."
   fi
fi

# VOMS_LOCATION
VOMS_LOCATION=${VOMS_LOCATION:-/opt/voms}

# NORDUGRID_ETC_LOCATION
NORDUGRID_ETC_LOCATION=${NORDUGRID_ETC_LOCATION:-$NORDUGRID_LOCATION/etc}

LD_LIBRARY_PATH=$NORDUGRID_LOCATION/lib:$GLOBUS_LOCATION/lib:$VOMS_LOCATION/lib:$LD_LIBRARY_PATH
SASL_PATH=${SASL_PATH:-$GLOBUS_LOCATION/lib/sasl}
export GLOBUS_LOCATION LD_LIBRARY_PATH SASL_PATH

if [ `id -u` = 0 ] ; then
  # Debian does not have /var/lock/subsys
  if test -d /var/lock/subsys; then
    LOCKFILE=/var/lock/subsys/$prog
  else
    LOCKFILE=/var/lock/$prog
  fi
  PID_FILE=/var/run/$prog.pid
else
  LOCKFILE=$HOME/$prog.lock
  PID_FILE=$HOME/$prog.pid
fi

start()
{
    echo -n "Starting $prog: "

    # Check if we are already running
    if test -f $PID_FILE; then
      read pid < $PID_FILE
      if test "x$pid" != "x"; then 
        failure "Error: already running ($pid)"
        echo
        return 1
      fi
    fi

    # Creating configuration directory if it does not exist
    if test ! -d "$NORDUGRID_ETC_LOCATION"; then
      echo "Warning: creating $NORDUGRID_ETC_LOCATION" 1>&2
      if ! mkdir -p "$NORDUGRID_ETC_LOCATION"; then
        failure "Can not create directory $NORDUGRID_ETC_LOCATION"
        echo
        return 1
      fi
    fi

    # Create the low-level configuration file
    config="$NORDUGRID_ETC_LOCATION/${prog}.conf"
    if test -x $NORDUGRID_LOCATION/libexec/config-mk-${prog}; then
      if ! $NORDUGRID_LOCATION/libexec/config-mk-${prog} > $config; then
        failure "Failed to create low level configuration file"
        echo
        return 1
      fi
    fi
    CMD="$CMD -c $config"

    # Create Globus environment file
    environment="$NORDUGRID_ETC_LOCATION/${prog}.environment"
    if test -x $NORDUGRID_LOCATION/libexec/server-environment; then
      $NORDUGRID_LOCATION/libexec/server-environment $prog > $environment
    fi

    # Add server specific options
    test "x$HTTPSD_CONNECTIONS" = "x" || CMD="$CMD -n $HTTPSD_CONNECTIONS"

    eval env `cat $environment` $CMD
    RETVAL=$?

    if [ $RETVAL -ne 0 ]; then
        failure "$prog startup"
    else
       touch $LOCKFILE
       success "$prog startup"
    fi
    echo
    return $RETVAL
}

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

    if test -f $PID_FILE; then
      kill `cat $PID_FILE` && success "$prog shutdown" || failure "$prog shutdown"
      RETVAL=$?
      rm -f $PID_FILE $LOCKFILE
    else
      failure "$prog shutdown - pidfile missing"
    fi
    echo
    return $RETVAL
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
	status $prog
        ;;
  restart|reload)
        stop
        start
        ;;
  condrestart)
        test -f $LOCKFILE && restart || :
        ;;
  *)
        echo "Usage: $0 {start|stop|status|restart|reload|condrestart}"
        exit 1
esac

exit $?
