#!/bin/sh
# chkconfig: 345 71 29
# --------------------------------------------------------------------------
### BEGIN INIT INFO
# Provides:          artcom-boot-rc
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $ALL
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: Start ArtCom® Demons
# Description:       Optional way to start ArtCom® Demons without having
#                    to rely on somebody invoking a ArtCom program interactively
### END INIT INFO
#  ------------------------------------------------------------------------
#  Title        : Boot-Shell-Script, may be run from Linux system boot
#  ------------------------------------------------------------------------
#                  This is an UNPUBLISHED work COPYRIGHT by
#                                 ArtCom GmbH
#                    Haferwende 2; D-28357 Bremen; Germany
#       It may be used, copied, or distributed only as permitted in a
#                              written license.
#  ------------------------------------------------------------------------
#  Created      :       Wed, 22-Apr-92 / 09:28 / (pf)
#  Version      : $Id: artcom-boot-rc,v 1.6 2014-02-20 15:45:43 pf Exp $
# --------------------------------------------------------------------------
#I Usage        : Kann beim Booten aufgerufen werden.
#I Remarks      : In diesem Skript koennen Dinge untergebracht werden, die
#I                bei jedem Boot-Vorgang des Systems ausgefuehrt werden sollen.
#I                Dieses Skript verzoegert den Boot-Vorgang.  Alle langwierigen
#I                Aktivitaeten muessen deshalb mit '&' ausgeforkt werden.
# See also the specification here:
# http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
# Summary: Required subcommands:
#    start, stop, restart, status, force-reload
# The Subcommand status must provide the following exit codes:
# 0     program is running or service is OK
# 1     program is dead and /var/run pid file exists
# 2     program is dead and /var/lock lock file exists
# 3     program is not running
# 4     program or service status is unknown
#  ------------------------------------------------------------------------
# 
PATH=/sbin/:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/opt/artcom/current/bin
DESC="ArtCom® Demons"

if [ -L "$0" ]
then # we simply guess, because readlink does not work in this case:
     CALLED=/etc/init.d/artcom-boot-rc
else CALLED="$0"
fi
CALLED_AS=`basename $CALLED`
CALLED_IN=`dirname $CALLED`
# We need an absolute path name in $CALLED_IN:
CALLED_IN=`cd $CALLED_IN; pwd`
case "$CALLED_IN" in
    /etc/init.d|/etc/rc*.d) ACSHFUNCS=/opt/artcom/current/bin/.ac-sh-funcs
    ;;
    *) ACSHFUNCS=$CALLED_IN/../bin/.ac-sh-funcs
    ;;
esac
# --- prepare environment as needed by our application programs: ---
. $ACSHFUNCS
# now $PATH, $BITSLIB, $BITSDAT, $AC_CUSTOMER, $AC_NODE, $AC_PROJECTS,
#     $HOST, $LANGUAGE and $OS contain reasonable values.
#
if [ -r "$BITSDAT/defaultuser" ]
then ARTCOM_DEFAULTUSER=`cat $BITSDAT/defaultuser`
else ARTCOM_DEFAULTUSER=user # this is a questionable assumption
fi

case "$1" in
    stop)
        su --preserve-environment --command=stopArtCom $ARTCOM_DEFAULTUSER 
        rm -f $BITSDAT/LCK*
        rm -f $BITSDAT/scipc/* 2>&1 >/dev/null
    ;;
    start)
        if [ -r $BITSDAT/stopped ]
        then su --preserve-environment \
	        --command="stopArtCom -r" $ARTCOM_DEFAULTUSER
        fi
        if [ -r $AC_CUSTOMER/tcplp.conf ] 
        then $BITSLIB/bin/TCPLP
        fi
        # Alle benötigten ArtCom Demons starten:
        su --preserve-environment --command=startDemons $ARTCOM_DEFAULTUSER
    ;;
    status)
        # currently uses the Demon CCworker as a representative.
	# this is a preliminary solution.  Better and correct would 
	# require checking of all ArtCom demon processes.  Because
	# this depends on very complicated circumstances it is not
	# implemented yet. ### TODO: see bits/python/stopArtCom.py ###
        if su --preserve-environment \
	      --command="stopArtCom -t" $ARTCOM_DEFAULTUSER
        then exit 0
        else echo "ArtCom CCworker demon not running ($BITSDAT)"
             exit 3
        fi
    ;;
    force-reload)    
        # does currently the same as restart
        $CALLED stop
        echo "waiting 5 sec for shutdown completion..." 1>&2
        sleep 5
        $CALLED start
    ;;
    restart)
        $CALLED stop
        echo "waiting 5 sec for shutdown completion..." 1>&2
        sleep 5
        $CALLED start
    ;;
    help)
        echo "usage: $0 (start|stop|restart|force-reload|status|help)" 1>&2
    ;;
    *)  echo "$0 $1 failed" 1>&2
        echo "usage: $0 (start|stop|restart|force-reload|status|help)" 1>&2
        exit 1
    ;;
esac
