#!/bin/bash
#
# openpbx		This script starts up the openpbx
#
# chkconfig: 345 95 05
# description: 	This loads up the kernel driver module and then \
#		starts up the middleware and scripts.

#set -x
export VPB_MODEL=V12PCI
VPB_MODULE="vpbhp"
BINPATH="/usr/bin"
SBINDIR="/usr/sbin/"
PLPATH="/usr/share/vpb/openpbx"
CTSERVEROPTS="-d -nv --log $PLPATH/log/"

function check_driver(){
	CNT=`/sbin/lsmod | awk '{print $1}' | grep -c "^${VPB_MODULE}$" `;
	if [ ${CNT} -ne 1 ]; then
		echo "Loading ${VPB_MODULE} module...";
		/sbin/modprobe ${VPB_MODULE}
	else
		echo "Module ${VPB_MODULE} already loaded...";
	fi
	CNT=`/sbin/lsmod | awk '{print $1}' | grep -c "^${VPB_MODULE}$" `;
	if [ ${CNT} -ne 1 ]; then
		echo "Module ${VPB_MODULE} module could not be loaded";
		exit
	fi
}

function scan(){
	SCAN=`${SBINDIR}/vpbscan`
	BOARDS=`echo "$SCAN"|grep BOARDS|cut -f2 -d:`
	echo "Found $BOARDS boards"
	if [ "x${BOARDS}" = "x" ]; then
		echo "Voicetronix card not detected!"
		exit
	fi
	echo "$SCAN"               
}

function check_ports(){
	CTSVR=`ps -deaf | grep "ctserver -d" | grep -v "grep" | awk '{print $2}'`
	if [ -n "$CTSVR" ];then
		echo "ctserver already running! Exiting...."
		exit
	fi
	if [ -f ${PLPATH}/cards.conf ];then
		echo "Reading ${PLPATH}/cards.conf"
		CONF=`cat ${PLPATH}/cards.conf`
	else
		echo "${PLPATH}/cards.conf not present, detecting ports..."
		CONF=`${SBINDIR}/vpbconf`
	fi
	TRUNKS=`echo "$CONF"|grep TRUNKS|cut -f2 -d:|xargs echo`
	STATIONS=`echo "$CONF"|grep STATIONS|cut -f2 -d:|xargs echo`
	echo "$CONF"
	if [ "x$STATIONS" = "x" -o "x$TRUNKS" = "x" ];then
		if [ "x$STATIONS" = "x" ];then
			echo -n "No station ports! "
		fi
		if [ "x$TRUNKS" = "x" ];then
			echo -n "No trunk ports! "
		fi
		echo "Aborting!"
		exit
	fi
}

function start_ctserver(){
	export VPB_MODEL 
	rm -f /var/run/ctserver.pid
        rm -f /var/lock/subsys/openpbx
	ulimit -c 1000000
	pushd /
	${BINPATH}/ctserver ${CTSERVEROPTS} 
	popd
	echo -n "Waiting ctserver to start";
	tries=0
	until [ -f /var/run/ctserver.pid ]; do
		echo -n ".";
		tries=`expr $tries + 1`
		if [ $tries -lt 110 ];then
			sleep 1
		else 
			echo "ctserver could not start!"
			exit
		fi
	done;
        touch /var/lock/subsys/openpbx
	echo
}

function start_perl(){
        pushd ${PLPATH} 
	for I in ${STATIONS}; do
                echo "Starting Station ${I}..."
		perl ${PLPATH}/station.pl -p ${I} -d 2>&1
	done;
	for I in ${TRUNKS}; do
                echo "Starting Trunk ${I}..."
		perl ${PLPATH}/trunk.pl -p ${I} -d 2>&1
	done;
        popd 
}


function start(){
	check_driver;
	scan;
	check_ports;
	start_ctserver;
	start_perl;
}

function stop(){
	PLSTAT=`ps -deafwww | grep "perl.*station.pl" | grep -v "grep" | awk '{print $2" "}'`
	PLTRUNK=`ps -deafwww | grep "perl.*trunk.pl" | grep -v "grep" | awk '{print $2" " }'`
	if [ -f /var/run/ctserver.pid ]; then
		kill -TERM `cat /var/run/ctserver.pid` 2>/dev/null
		kill $PLSTAT $PLTRUNK
		sleep 3
	else
		NOPID=1
	fi
	if [ -f /var/run/ctserver.pid ]; then
		rm -f /var/run/ctserver.pid
	fi
        if [ -f /var/lock/subsys/openpbx ]; then
                rm -f /var/lock/subsys/openpbx
        fi
	CTSVR=`ps -deaf | grep "ctserver -d" | grep -v "grep" | awk '{print $2}'`
	if [ -n "$CTSVR" ];then
		echo "ctserver could not be stopped, trying harder..."
		echo $CTSVR|xargs -n1 kill -9
		sleep 3
	fi
	CTSVR=`ps -deaf | grep "ctserver -d" | grep -v "grep" | awk '{print $2}'`
	if [ -n "$CTSVR" ];then
		echo "Some ctserver processes still running - sorry!"
		exit
	else
		if [ "x${NOPID}" = "x1" ];then
			echo "ctserver is not running"
		else
			echo "ctserver stopped"
		fi
	fi
}

function status(){
	PLSTAT=`ps -deafwww | grep "perl.*station.pl" | grep -v "grep" | awk '{print $2" "}'`
	PLTRUNK=`ps -deafwww | grep "perl.*trunk.pl" | grep -v "grep" | awk '{print $2" " }'`
	CTSVR=`ps -deaf | grep "ctserver -d" | grep -v "grep" | awk '{print $2}'`
	if [ -z "${CTSVR}" ];  then
		echo "ctserver not running!"
	else
		echo ${CTSVR}|xargs echo "ctserver:"
	fi;
	if [ -z "${PLSTAT}" ] && [ -z "${PLTRUNK}" ]; then
		echo "No perl scripts!"
	else
		echo ${PLSTAT}|xargs echo "Station scripts:"
		echo ${PLTRUNK}|xargs echo "Trunk scripts:"
	fi
}

case "$1" in
	start)
		start
		;;
	start_perl)
		start_perl
		;;
	start_ctserver)
		start_ctserver
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	reload)
		stop
		start
		;;
	status)
		status
		;;
	scan)
		scan
		;;
	portconfig)
		scan
		check_ports
		;;
	*)
	echo $"Usage: $0 {start|start_ctserver|start_perl|stop|restart|status|scan|portconfig}"
	RETVAL=1
esac
exit $RETVAL
