#! /bin/sh
#set -x
#
# /etc/init.d/shibd for Solaris
#
# start/stop script for Shibboleth ServiceProvider 1.3 daemon 
#
# Created:  20050602 - Valery Tschopp - SWITCH
# Modified: 20050916 - Dominique Petitpierre at adm.unige.ch: Solaris port
#           20080108 - Valery Tschopp - SWITCH: Shibboleth 1.3.1 update
#
# HOWTO INSTALL: 
# cp shibd /etc/init.d/shibd
# chmod u+x /etc/init.d/shibd
# ln -s ../init.d/shibd /etc/rcS.d/K28shibd
# ln -s ../init.d/shibd /etc/rc0.d/K28shibd
# ln -s ../init.d/shibd /etc/rc1.d/K28shibd
# ln -s ../init.d/shibd /etc/rc2.d/K28shibd
# ln -s ../init.d/shibd /etc/rc3.d/S15shibd
#  

PATH=/bin:/usr/bin:/sbin:/usr/sbin

#
# Shibboleth 1.3
#
SHIB_HOME=/opt/shibboleth-1.3
SHIB_ETC=/etc/shibboleth

SHIB_CONFIG=$SHIB_ETC/shibboleth.xml

LD_LIBRARY_PATH=$SHIB_HOME/lib

NAME=shibd
DAEMON=$SHIB_HOME/sbin/$NAME
DESC="Shibboleth 1.3 SP Daemon"

if [ ! -x $DAEMON -o ! -f $SHIB_CONFIG ] ; then
   exit
fi

case "$1" in
  start)
    # pgrep return 0 on success
    if pgrep -f $DAEMON > /dev/null ; then 
       echo "$DESC: $NAME is already running"
    else
       echo "Starting $DESC: $NAME"
       $DAEMON -fc $SHIB_CONFIG &
       sleep 1
       if pgrep -f $DAEMON >/dev/null ; then
           :
       else
           echo "$DESC: $NAME has not started"
           exit 1
       fi
    fi  
    ;;
  stop)
    if pgrep -f $DAEMON > /dev/null ; then
        echo "Stopping $DESC: $NAME"
        pkill -x $NAME
        sleep 1
        if pgrep -x $DAEMON >/dev/null ; then
           echo "$DESC: $NAME has not stopped"
           exit 1
        fi
    else
       echo "$DESC: $NAME is not running"
    fi
    ;;
  restart)
    if $DAEMON -tc $SHIB_CONFIG >/dev/null 2>&1 ; then
        # Restart
        $0 stop
        $0 start
    else
       echo "$DESC: $NAME configuration broken, not restarting"
    fi
    ;;
  configtest)
    echo "Checking config $SHIB_CONFIG for $DESC: $NAME"
    $DAEMON -tc $SHIB_CONFIG
    echo "Done."
    ;;
  version)
    version=`$DAEMON -v`
    echo "Version of $DESC: $NAME: $version"
    ;;
  *)
    echo "Usage: - $0 {start|stop|restart|configtest}" >&2
    exit 1
    ;;
esac

exit 0
