#!/bin/bash

. /etc/sysconfig/rc.conf
. /bin/network-functions
. /etc/image_features

PIDFILE=/var/run/ecmonclient.pid

SERVICE=ecmonclient
PROCESS_NAME=ecmonclient

do_start ()
{
	if [ ! -f /etc/fiad-conf/ecmonclient.cfg ] ; then
		cp -f /etc/default-conf/ecmonclient.cfg /etc/fiad-conf/ecmonclient.cfg
		if [ "$FEATURE_CLOUD_IPPBX_OWN_SERVICE" = "1" ] || [ "$FEATURE_QX1500_IPPBX_OWN_SERVICE" = "1" ] ; then
			sed -i 's/enabled=false/enabled=true/g' /etc/fiad-conf/ecmonclient.cfg
		fi
	fi

	if ! ServiceCheckRunning1 ; then
		return $?
	fi

	echo -n "Starting EC Monitoring client service: "
	/bin/setsid /bin/$PROCESS_NAME > /dev/null 2>&1 &
	echo $! > $PIDFILE

	ServiceCheckRunning2
	return $?
}

do_stop ()
{
	/etc/init.d/softdogd wait
	echo -n "Shut down EC Monitoring client service: "
	ServiceStop
	return $?
}

do_status ()
{
	ServiceStatus
	return $?
}

case "$1" in
	start)
		do_start
		;;
	stop)
		do_stop
		;;
	restart)
		do_stop
		do_start
		;;
	status) 
		do_status
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
esac

exit $?
