#!/bin/bash
# start/stop script for censhare local serviceclient
#
# 2015-May-19 pmo  corrected profile invocation to bash standard
# 2014-Aug-21 pmo  added start/stop/status for s2s as corpus user
# 2014-Jun-26 pmo  stop start now also executable as non root user
# 2014-Mai-14 pmo  added option to set JAVA_OPTIONS for serviceclient
# 2014-Mai-13 pmo  fixed status check as corpus and non home dir
# 2014-Apr-23 pmo  removed 'm' at su for Darwin
# 2013-Nov-14 pmo  status now also executable as non root user
# 2013-Feb-07 ahu  changed sudo -m only for Darwin
# 2012-Aug-23 pmo  unset LD_LIBRARY_PATH to ensure imagemagick is working
# 2012-Mar-28 pmo  initial version
#
# LSB run levels and dependencies
### BEGIN INIT INFO
# Provides:          css_serviceclient
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: censhare serviceclient
# Description:       censhare serviceclient
### END INIT INFO
### BEGIN
# RedHat run levels
# chkconfig: 345 83 12
### END

test -z "$BASH" && exec /bin/bash $0 "$@"		# needed by Solaris: force bash

progname="${0##*/}"								# cut path
progname=${progname#rc}
progname=${progname#[KS][0-9][0-9]}
progname="${progname%%.*}"						# cut filetype

action="$1"										# save action
ec=0											# default: success
 ret_other=(success error)						# return values
ret_status=(running error dead_but_lock unused    unknown reserved5 not_configured)
 ret_start=(started   error inv_args not_implemented insuff_privileges not_installed not_configured not_running)
  ret_stop=(stopped  error inv_args not_implemented insuff_privileges not_installed not_configured not_running)
rc_status () {									# inform user
    local s
    case "$action" in
	start)  s=${ret_start[$ec]};;
	stop)   s=${ret_stop[$ec]};;
	status) s=${ret_status[$ec]};;
        *)      test "$ec" = 0 && s=${ret_other[0]} || s=${ret_other[1]};;
    esac
    echo ": ${s:-(unknown exit code $ec)}"
}

# read configuration
ConfErr () { echo -n >&2 "$progname: $*"; exit 6; }
ChkCfgfile () {                                         # first try /etc/sysconfig then /etc/s2s/sysconfig
    local cf="$1"; test -z "$cf" && ConfErr "$FUNCNAME(): filename missing."
    CONFIG_FILE=/etc/sysconfig/$cf;     cfglist="$cfglist $CONFIG_FILE"; test -r "$CONFIG_FILE" && return 0
    CONFIG_FILE=/etc/s2s/sysconfig/$cf; cfglist="$cfglist $CONFIG_FILE"; test -r "$CONFIG_FILE" && return 0
    CONFIG_FILE=/etc/$cf; cfglist="$cfglist $CONFIG_FILE"; test -r "$CONFIG_FILE" && return 0
    return 1                                            # failed
}
test -z "$CONFIG_FILE" && {                             # if no config file defined by environment, look for
    ChkCfgfile $progname.$HOSTNAME || {                 #     host specific one or
        ChkCfgfile $progname || {                       #     general one or error
            ConfErr "configuration missing, searched: '${cfglist# }'."; }; }
}                                                       # endif
test -n "$CONFIG_FILE" -a -s "$CONFIG_FILE" && {
    . $CONFIG_FILE
}

# unset LD_LIBRARY_PATH to ensure imagemagick is working
unset LD_LIBRARY_PATH

# check for mandatory config vars
test -z "$SERVICECLIENT_USER" && ConfErr "sysconfig var 'SERVICECLIENT_USER' empty."
test -z "$SERVICECLIENT_BASE" && ConfErr "sysconfig var 'SERVICECLIENT_BASE' empty."

# ensure serviceclient user exists
id "$SERVICECLIENT_USER" >/dev/null 2>&1 || ConfErr "SERVICECLIENT_USER '$SERVICECLIENT_USER' not existent"

# test serviceclient base
test -d "$SERVICECLIENT_BASE" || ConfErr "SERVICECLIENT_BASE '$SERVICECLIENT_BASE' not existent"

# handle action
case "$action" in
    start)
    	echo -n "    Starting $progname"
	if test "$UID" -eq 0; then
    	    sys_os="`uname`"
    	    case "$sys_os" in
    	        SunOS)
    	            su - "$SERVICECLIENT_USER" -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; export SERVICECLIENT_JAVA_OPTIONS=\"$SERVICECLIENT_JAVA_OPTIONS\"; $SERVICECLIENT_BASE/serviceclient.sh start" >/dev/null 2>&1; ec=$?
    	            ;;
    	        Darwin)
    	            su - "$SERVICECLIENT_USER" -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; export SERVICECLIENT_JAVA_OPTIONS=\"$SERVICECLIENT_JAVA_OPTIONS\"; $SERVICECLIENT_BASE/serviceclient.sh start" >/dev/null 2>&1; ec=$?
    	            ;;
    	        *)
    	            su - "$SERVICECLIENT_USER" -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; export SERVICECLIENT_JAVA_OPTIONS=\"$SERVICECLIENT_JAVA_OPTIONS\"; $SERVICECLIENT_BASE/serviceclient.sh start" ; ec=$?
    	            ;;
    	    esac
	else
	    export SERVICECLIENT_JAVA_OPTIONS="$SERVICECLIENT_JAVA_OPTIONS"
	    $SERVICECLIENT_BASE/serviceclient.sh start >/dev/null 2>&1; ec=$?
	fi
	rc_status
	;;
    stop)
    	echo -n "    Shutting down $progname"
	if test "$UID" -eq 0; then
    	    sys_os="`uname`"
    	    case "$sys_os" in
    	        SunOS)
    	            su - "$SERVICECLIENT_USER" -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; $SERVICECLIENT_BASE/serviceclient.sh stop" >/dev/null 2>&1; ec=$?
    	            ;;
    	        Darwin)
    	            su - "$SERVICECLIENT_USER" -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; $SERVICECLIENT_BASE/serviceclient.sh stop" >/dev/null 2>&1; ec=$?
    	            ;;
    	        *)
    	            su - "$SERVICECLIENT_USER" -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; $SERVICECLIENT_BASE/serviceclient.sh stop" >/dev/null 2>&1; ec=$?
    	            ;;
    	    esac
	else
	    $SERVICECLIENT_BASE/serviceclient.sh stop >/dev/null 2>&1; ec=$?
	fi
    	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop; $0 start; ec=$?
	;;
    status)
    	echo -n "    Checking for $progname"
        if test "$UID" -eq 0; then
    	    sys_os="`uname`"
    	    case "$sys_os" in
    	        SunOS)
    	            su - "$SERVICECLIENT_USER" -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; $SERVICECLIENT_BASE/serviceclient.sh status" >/dev/null 2>&1; ec=$?
    	            ;;
    	        Darwin)
    	            su - "$SERVICECLIENT_USER" -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; $SERVICECLIENT_BASE/serviceclient.sh status" >/dev/null 2>&1; ec=$?
    	            ;;
    	        *)
    	            su - "$SERVICECLIENT_USER" -c "if test -r .bash_profile; then . .bash_profile; elif test -r .bash_login; then . .bash_login; elif test -r .profile; then . .profile; fi; $SERVICECLIENT_BASE/serviceclient.sh status" >/dev/null 2>&1; ec=$?
    	            ;;
    	    esac
        else
            $SERVICECLIENT_BASE/serviceclient.sh status >/dev/null 2>&1; ec=$?
        fi
    	rc_status
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart}"
	ec=1
	;;
esac
exit $ec

# Local Variables:
# mode:                 shell-script
# mode:                 font-lock
# comment-column:       56
# tab-width:            8
# End:
