#!/bin/bash

# Copyright (c) 2008 Huawei Technologies, C
# All rights reserved.
#
# Authors: Franko Fang <huananhu@huawei.com>
#

### BEGIN INIT INFO
# Provides:          HWActivator
# Required-Start:    $syslog $local_fs
# Should-Start: 
# Required-Stop:     
# Should-Stop: 
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: HWActivator  daemon providing fxz
# Description:       Start HWActivator to allow XY and provide YZ
#	continued on second line by '#<TAB>'
#	should contain enough info for the runlevel editor
#	to give admin some idea what this service does and
#	what it's needed for ...
#	(The Short-Description should already be a good hint.)
### END INIT INFO

HWAPP=CURPATH/driver/HWActivator

test -x $HWAPP || { echo "$HWAPP not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }


# Default: Assume sysvinit binaries exist
start_daemon() { /sbin/start_daemon ${1+"$@"}; }
killproc()     { /sbin/killproc     ${1+"$@"}; }
pidofproc()    { /sbin/pidofproc    ${1+"$@"}; }
checkproc()    { /sbin/checkproc    ${1+"$@"}; }
if test -e /etc/rc.status; then
    # SUSE rc script library
    . /etc/rc.status
else
    export LC_ALL=POSIX
    _cmd=$1
    declare -a _SMSG
    if test "${_cmd}" = "status"; then
	_SMSG=(running dead dead unused unknown reserved)
	_RC_UNUSED=3
    else
	_SMSG=(done failed failed missed failed skipped unused failed failed reserved)
	_RC_UNUSED=6
    fi
    if test -e /lib/lsb/init-functions; then
	# LSB    
    	. /lib/lsb/init-functions
	echo_rc()
	{
	    if test ${_RC_RV} = 0; then
		log_success_msg "  [${_SMSG[${_RC_RV}]}] "
	    else
		log_failure_msg "  [${_SMSG[${_RC_RV}]}] "
	    fi
	}
	# TODO: Add checking for lockfiles
	checkproc() { return pidofproc ${1+"$@"} >/dev/null 2>&1; }
    elif test -e /etc/init.d/functions; then
	# RHAT
	. /etc/init.d/functions
	echo_rc()
	{
	    #echo -n "  [${_SMSG[${_RC_RV}]}] "
	    if test ${_RC_RV} = 0; then
		success "  [${_SMSG[${_RC_RV}]}] "
	    else
		failure "  [${_SMSG[${_RC_RV}]}] "
	    fi
	}
	checkproc() { return status ${1+"$@"}; }
	start_daemon() { return daemon ${1+"$@"}; }
    else
	# emulate it
	echo_rc() { echo "  [${_SMSG[${_RC_RV}]}] "; }
    fi
    rc_reset() { _RC_RV=0; }
    rc_failed()
    {
	if test -z "$1"; then 
	    _RC_RV=1;
	elif test "$1" != "0"; then 
	    _RC_RV=$1; 
    	fi
	return ${_RC_RV}
    }
    rc_check()
    {
	return rc_failed $?
    }	
    rc_status()
    {
	rc_failed $?
	if test "$1" = "-r"; then _RC_RV=0; shift; fi
	if test "$1" = "-s"; then rc_failed 5; echo_rc; rc_failed 3; shift; fi
	if test "$1" = "-u"; then rc_failed ${_RC_UNUSED}; echo_rc; rc_failed 3; shift; fi
	if test "$1" = "-v"; then echo_rc; shift; fi
	if test "$1" = "-r"; then _RC_RV=0; shift; fi
	return ${_RC_RV}
    }
    rc_exit() { exit ${_RC_RV}; }
    rc_active() 
    {
	if test -z "$RUNLEVEL"; then read RUNLEVEL REST < <(/sbin/runlevel); fi
	if test -e /etc/init.d/S[0-9][0-9]${1}; then return 0; fi
	return 1
    }
fi

# Reset status of this service
rc_reset


case "$1" in
    start)
	echo -n "Starting HWActivator "
	$HWAPP parameter> /dev/null 2>&1
	rc_status -v
	;;
    stop)
	echo -n "Shutting down HWActivator "
	killproc -TERM $HWAPP
	rc_status -v
	;;
    restart)
	$0 stop
	$0 start

	rc_status
	;;
	
    force-reload)
	echo -n "Reload service HWActivator "
	killproc -HUP $HWAPP
	rc_status -v
	;;
	
    reload)
	echo -n "Reload service HWActivator "
	killproc -HUP $HWAPP
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|restart|force-reload|reload}"
	exit 1
	;;
esac
rc_exit
