#!/bin/bash
#
# Init file for the NorduGrid gridsshd
#
# chkconfig: - 55 25
# description: NorduGrid gridsshd
#
# config: /etc/sysconfig/globus
# config: /etc/sysconfig/nordugrid

# 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
else
    echo /etc/sysconfig/globus not found - ignoring
fi
if [ -f /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
else
   echo /etc/sysconfig/nordugrid not found - ignoring
fi

prog=gridsshd

# 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

# 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

    # Use simple commandline configuration
    CMD_OPTS="-d 3"
    eval $CMD $CMD_OPTS
    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 $?
