#!/bin/bash
# start/stop script for local office service
#
# 2014-Jun-27 pmo  start stop status now also possible as non root user
# 2013-Nov-14 pmo  status now also executable as non root user
# 2012-Aug-28 pmo  initial version
#
# LSB run levels and dependencies
### BEGIN INIT INFO
# Provides:          startoffice
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: startoffice
# Description:       startoffice
### 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
}

# check for mandatory config vars
test -z "$OFFICE_USER" && ConfErr "sysconfig var 'OFFICE_USER' empty."
eval test -d "~$OFFICE_USER" || ConfErr "'OFFICE_USER' '$OFFICE_USER' not existent or not a directory."
test -z "$OFFICE_LRDIR" && ConfErr "sysconfig var 'OFFICE_LRDIR' empty."
test -z "$OFFICE_PORT" && ConfErr "sysconfig var 'OFFICE_PORT' empty."
test -z "$OFFICE_ALLOWFROM" && ConfErr "sysconfig var 'OFFICE_ALLOWFROM' empty."
test -z "$OFFICE_EXEC" && ConfErr "sysconfig var 'OFFICE_EXEC' empty."
test -z "$OFFICE_OPTS" && ConfErr "sysconfig var 'OFFICE_OPTS' empty."

# ensure office user exists
id "$OFFICE_USER" >/dev/null 2>&1 || ConfErr "OFFICE_USER '$OFFICE_USER' nonexistent"

# test office executable
test -x "$OFFICE_EXEC" || ConfErr "OFFICE_EXEC '$OFFICE_EXEC' nonexistent or non executable."

# handle action
case "$action" in
    start)
    	echo -n "    Starting $progname"
        if test "$UID" -eq 0; then
    	    su - "$OFFICE_USER" -c "office.sh start" >/dev/null 2>&1; ec=$?
	else
	    office.sh start >/dev/null 2>&1; ec=$?
	fi
	rc_status
	;;
    stop)
    	echo -n "    Shutting down $progname"
        if test "$UID" -eq 0; then
    	    su - "$OFFICE_USER" -c "office.sh stop" >/dev/null 2>&1; ec=$?
	else
	    office.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
    	    su - "$OFFICE_USER" -c "office.sh status" >/dev/null 2>&1; ec=$?
        else
            office.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:
