#!/bin/bash
#
# Init file for the NorduGrid/ARC Grid Information Index Server (EGIIS)
#
# chkconfig: - 56 44
# description: NorduGrid/ARC Grid Information Index Server
#
# config: /etc/sysconfig/nordugrid
# config: /etc/sysconfig/nordugrid-arc-egiis
# config: /etc/arc.conf
#
######################################################################

### BEGIN INIT INFO
# Provides:          nordugrid-arc-egiis
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Stop:      0 1 2 3 4 5 6
# Short-Description: NorduGrid/ARC Grid Information Index Server
# Description:       Init script for the NorduGrid/ARC Grid Information
#                    Index Server (EGIIS)
### END INIT INFO

# Helper functions
if [ -r /etc/init.d/functions ]; then
    . /etc/init.d/functions
    log_success_msg() {
	echo -n "$@"
	success "$@"
	echo
    }
    log_warning_msg() {
	echo -n "$@"
	warning "$@"
	echo
    }
    log_failure_msg() {
	echo -n "$@"
	failure "$@"
	echo
    }
elif [ -r /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
else
    echo "Error: Cannot source neither init.d nor lsb functions"
    exit 1
fi

RETVAL=0
prog=nordugrid-arc-egiis
RUN=yes

# sysconfig files
if [ -r /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
elif [ -r /etc/default/nordugrid ]; then
    . /etc/default/nordugrid
fi
if [ -r /etc/sysconfig/$prog ]; then
    . /etc/sysconfig/$prog
elif [ -r /etc/default/$prog ]; then
    . /etc/default/$prog
fi

if [ "x$RUN" != "xyes" ]; then
    log_warning_msg "$prog disabled, please adjust the configuration to your"
    log_warning_msg "needs and then set RUN to 'yes' in /etc/default/$prog to enable it."
    exit 0
fi

[ -n "$ARC_LOCATION" ] && export ARC_LOCATION
[ -n "$ARC_CONFIG" ]   && export ARC_CONFIG

ARC_LOCATION=${ARC_LOCATION:-/usr}
if [ ! -d "$ARC_LOCATION" ]; then
    log_failure_msg "ARC_LOCATION ($ARC_LOCATION) not found"
    exit 1
fi

# Source the config parsing routines
if [ -r "$ARC_LOCATION/share/arc/config_parser_compat.sh" ]; then
    . $ARC_LOCATION/share/arc/config_parser_compat.sh || exit $?
else
    log_failure_msg "Could not find $ARC_LOCATION/share/arc/config_parser_compat.sh"
    exit 1
fi

ARC_CONFIG=${ARC_CONFIG:-/etc/arc.conf}
if [ ! -r "$ARC_CONFIG" ]; then
    log_failure_msg "arc.conf was not found. If this file is in a non-standard place,"
    log_failure_msg "  it can be set with the ARC_CONFIG environment variable"
    exit 1
fi

# Read arc.conf
config_parse_file $ARC_CONFIG || exit $?

# Check for infosys-block
if ! config_match_section infosys; then
    log_warning_msg "Missing infosys configuration block"
    exit 0
fi

config_hide_all
config_import_section common

# These options need to come from the infosys-block, not from common
unset CONFIG_logfile
unset CONFIG_user
unset CONFIG_port

config_import_section infosys

ldap_user=`getent passwd | grep ldap | sed 's/\([az]*\):.*/\1/g'`
if [ ! "xldap" = "x$ldap_user" ] && [ ! "xopenldap" = "x$ldap_user" ]; then
    log_warning_msg "Warning, could not find ldap or openldap user"
    log_warning_msg "resorting to using the root user."
    ldap_user=root
fi
bdii_user=${CONFIG_user:-$ldap_user}

# Use this command to change user
if [ -x /sbin/runuser ]; then
    RUNUSER=runuser
else
    RUNUSER=su
fi

# If missing, we have a problem
USERSHELL=${USERSHELL:-"/bin/sh"}
if [ ! -x ${USERSHELL} ]; then
    log_failure_msg "Could not find /bin/sh"
    exit 1
fi

giis_location=${CONFIG_giis_location:-$ARC_LOCATION}

# Debian does not have /var/lock/subsys
if [ -d /var/lock/subsys ]; then
    lockfile=/var/lock/subsys/$prog
else
    lockfile=/var/lock/$prog
fi

arc_runtime_config="/var/run/arc/infosys"
mkdir -p ${arc_runtime_config}
chown ${bdii_user}: ${arc_runtime_config}

giis_fifo=${CONFIG_giis_fifo:-$arc_runtime_config/giis-fifo}

start () {
    if [ -r "${lockfile}" ]; then
	log_success_msg "$prog already started"
	RETVAL=0
	return ${RETVAL}
    fi

    if [ ! "`config_subsections infosys/index`" ]; then
	log_failure_msg "No $prog defined if $ARC_CONFIG"
	exit 1
    fi

    $RUNUSER -s "$USERSHELL" -c "${giis_location}/sbin/arc-infoindex-server -c $ARC_CONFIG -f $giis_fifo" ${bdii_user}

    touch ${lockfile}

    log_success_msg "$prog started"
}

stop () {
    if [ ! -r "${lockfile}" ]; then
	log_success_msg "$prog already stopped"
	RETVAL=0
	return ${RETVAL}
    fi

    [ -p $giis_fifo ] && echo STOP > $giis_fifo

    rm -f ${lockfile}
    log_success_msg "$prog stopped"
}

status ()  {
    if [ ! -r "${lockfile}" ]; then
	log_success_msg "$prog is stopped"
	RETVAL=3
	return ${RETVAL}
    fi

    log_success_msg "$prog is running"
    RETVAL=0
    return ${RETVAL}
}

case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart | force-reload)
	stop
	# avoid race
	sleep 3
	start
	;;
    reload)
	;;
    status)
	status
	;;
    condrestart | try-restart)
	if [ -r ${lockfile} ]; then
	    stop
	    # avoid race
	    sleep 3
	    start
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|restart|force-reload|reload|condrestart|try-restart|status}"
	exit 1
	;;
esac

exit $RETVAL
