#!/bin/sh
#
# See "man chkconfig" for information on next two lines (RedHatCompatible Only)
# chkconfig: 2345 90 10
# description: iRODS
#
#
# Following lines are in conformance with LSB 1.2 spec
### BEGIN INIT INFO
# Provides:          irods
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manages the iRODS service
# Description:       Manages the iRODS service
### END INIT INFO

set -e

# import service account information
. /etc/irods/service_account.config

IRODS_HOME=/var/lib/irods/iRODS
IRODS_CTL=./irodsctl

cd $IRODS_HOME

DETECTEDOS=`../packaging/find_os.sh`

if [ "$DETECTEDOS" = "MacOSX" ] ; then
    SU_SHELL_OPTS=""
else
    SU_SHELL_OPTS="--shell=/bin/bash"
fi

case $1 in
status)
    su $SU_SHELL_OPTS $IRODS_SERVICE_ACCOUNT_NAME -c "$IRODS_CTL status"
    ;;
start)
    su $SU_SHELL_OPTS $IRODS_SERVICE_ACCOUNT_NAME -c "$IRODS_CTL start"
    if [ "$DETECTEDOS" = "RedHatCompatible" -o "$DETECTEDOS" = "SuSE" ] ; then
        touch /var/lock/subsys/irods
    fi
    ;;
stop)
    su $SU_SHELL_OPTS $IRODS_SERVICE_ACCOUNT_NAME -c "$IRODS_CTL stop"
    if [ "$DETECTEDOS" = "RedHatCompatible" -o "$DETECTEDOS" = "SuSE" ] ; then
        rm /var/lock/subsys/irods
    fi
    ;;
restart)
    su $SU_SHELL_OPTS $IRODS_SERVICE_ACCOUNT_NAME -c "$IRODS_CTL restart"
    if [ "$DETECTEDOS" = "RedHatCompatible" -o "$DETECTEDOS" = "SuSE" ] ; then
        touch /var/lock/subsys/irods
    fi
    ;;
force-reload)
    su $SU_SHELL_OPTS $IRODS_SERVICE_ACCOUNT_NAME -c "$IRODS_CTL restart"
    if [ "$DETECTEDOS" = "RedHatCompatible" -o "$DETECTEDOS" = "SuSE" ] ; then
        touch /var/lock/subsys/irods
    fi
    ;;
*)
    echo "/etc/init.d/irods :: [$1] is not supported." 1>&2
    echo "Usage: /etc/init.d/irods {start|stop|status|restart|force-reload}" 1>&2
    exit 1
esac
