#!/bin/bash
# start/stop script for aws
#
# 2015-Feb-19 pmo  changed error handling on start
# 2013-Dez-05 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

# 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

# handle action
case "$action" in
    start)
        echo -n "    Starting $progname"
        while :; do
            echo -n "."
            sleep 1
	done &
	echo_pid=$!
	disown $echo_pid
	test -f /var/run/"$progname".startlock && {
	    kill "$echo_pid"
	    echo ": Already started before."
	    exit 0
	}
	IFS_BAK=$IFS
        IFS=$'\n'
        for start_list in $(sed -n "/Start/,/Stop/ {/#.*/ D; p; }" "$CONFIG_FILE"); do
            IFS=$IFS_BAK
            eval "$start_list"
 	    test "$ec" = 0 || {
                kill "$echo_pid"
                echo ": CMD '$start_list' failed with ec='$ec'."
                exit 1
            }
            IFS=$'\n'
        done
        rm /var/run/"$progname".stoplock 2>/dev/null
	touch /var/run/"$progname".startlock
	kill "$echo_pid"
        echo ": started"
	;;
    stop)
        echo -n "    Shutting down $progname"
        while :; do
            echo -n "."
            sleep 1
	done &
	echo_pid=$!
	disown $echo_pid
        test -f /var/run/"$progname".stoplock && {
            kill "$echo_pid"
            echo ": Already stopped before."
            exit 0
        }
	IFS_BAK=$IFS
        IFS=$'\n'
	for stop_list in $(sed -n "/Stop/,/*/ {/#.*/ D; p; }" "$CONFIG_FILE"); do
	    IFS=$IFS_BAK
	    eval "$stop_list" || {
	        kill "$echo_pid"
                echo ": CMD '$start_list' failed with ec='$ec'."
                exit 1
            }
            IFS=$'\n'
        done
	IFS=$IFS_BAK
	touch /var/run/"$progname".stoplock
	rm /var/run/"$progname".startlock 2>/dev/null
	kill "$echo_pid" 2>/dev/null
	echo ": stopped"
	;;
    status)
	echo -n "    Checking for $progname: "
	if test -f /var/run/"$progname".startlock; then
	    echo "Already started before"
	    exit 0
	else
	    echo "not started"
	    exit 3
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|status}"
	ec=1
	;;
esac
# Local Variables:
# mode:                 shell-script
# mode:                 font-lock
# comment-column:       56
# tab-width:            8
# End:
