#!/bin/bash
#
# Author: Martin Bruchanov, bruxy at regnet.cz
# Date:   Sun Mar  1 21:33:04 CET 2015
#
# chkconfig: 2345 90 90
# description: EUMETCast reception software 
#
 
ETOKEND=/usr/local/etoken/aksrte-3-15.84p3/etokend
PCSCD=/usr/local/sbin/pcscd
PCSCD_DEBUG_LOG=0 # set to 1 for verbose log
TELLICAST=/usr/local/bin/tc-recv
MANAGEMENT_IP=192.168.0.2
INTERFACE=enp0s25
PRIORITY=-20 # -20 max priority ... +20 lowest
RESET=/usr/local/bin/eku_reset

# FUNCTIONS ###################################################################

function process_status() {
	local name=$(basename $1)
	local pid=$(pgrep $name)
	
	if [ ! -z "$pid" ] ; then
		echo $name is running with PID = $pid.
		return 0
	else
		echo $name is not running!
		return 1
	fi
}

function ayecka_status() {
	# Send 1 ping with timeout 1 sec
	ping -c 1 -W 1 $MANAGEMENT_IP >/dev/null
	if [ $? -eq 0 ] ; then
		echo "Host $MANAGEMENT_IP is on."
		# get RX Status info from Ayecka
		local TMP=/tmp/tmp_output
		exec 3<>/dev/tcp/$MANAGEMENT_IP/23
		sleep 1
		echo -e "telnet\r21" 1>&3
		sleep 2
		( cat <&3 > $TMP ) &
		read_pid=$!  
		sleep 2
		# suppress message 'Terminated'
		kill $read_pid 
		wait $read_pid 2>/dev/null
		exec 3>&-
		sed -ne '/RX Status/,$p' $TMP
	else
		echo "Cannot ping management IP $MANAGEMENT_IP!"
		return 1			
	fi	
}

function eku_status() {
	URL=http://localhost:2517/www/client/info.html
	echo -n "License status of "
	local host_key=$(elinks -dump http://localhost:2517/www/client/info.html | grep host_key_4)
	echo "${host_key// /}"
}

function ethernet_status() {
	echo Checking $INTERFACE
	ifconfig $INTERFACE
	local proc_file="/proc/sys/net/ipv4/conf/$INTERFACE/rp_filter"
	local rp_filter=$(cat < "$proc_file" )
	if [ $rp_filter -ne 0 ] ; then
		echo "0" > "$proc_file"
	fi
}

function process_stop() {
	local name=$(basename $1)
	local retval

	echo Swithing off process $name.
	pkill $name
	retval=$?

	if [ $retval -eq 1 ] ; then
		echo "No proccess matched criteria."
		return 1
	elif [ $retval -eq 0 ] ; then
		return 0
	else
		echo "Error when pkill of $name."
		return 1
	fi
}

function start_etokend() {
	local link="/dev/bus/usb/devices"
	local point="/proc/bus"
	echo "Starting $(basename $ETOKEND)"
	# create mount point /proc/bus
	if ! mount | grep $point > /dev/null ; then
		echo "Creating mount point $point."
		mount --bind /dev/bus $point
	fi
	# create link to old USB device
	if [ ! -f $link ] ; then 
		ln -s /sys/kernel/debug/usb/devices $link
	fi
	nice -n $PRIORITY $ETOKEND
	sleep 0.5
	if ! process_status $ETOKEND ; then
		echo "Unable to start $ETOKEND"
		exit 1
	fi
}

function start_pcsd() {
	local retval
	echo "Starting $(basename $PCSCD)"
	if [ $PCSCD_DEBUG_LOG -eq 1 ] ; then
		nice -n $PRIORITY $PCSCD -c /etc/reader.conf -a -d syslog
		retval=$?
	else
		nice -n $PRIORITY $PCSCD -c /etc/reader.conf
		retval=$?
	fi
	sleep 0.5
	if [ $retval -ne 0 ] || ! process_status $PCSCD ; then
		echo "Unable to start $PCSCD! Exit code: '$retval'."
		exit 1
	fi
}

function start_tellicast() {
	local retval
	echo "Starting $(basename $TELLICAST)"
	nice -n $PRIORITY $TELLICAST -c /etc/recv.ini > /dev/null &
	retval=$?
	sleep 1
	if [ $retval -ne 0 ] || ! process_status $TELLICAST ; then
		echo "Unable to start $TELLICAST"
		exit 1
	fi
}

# MAIN #######################################################################

LOCK=/var/lock/subsys/eumetcast

case "$1" in
	start)
		if [ -f $LOCK ] ; then
			echo "Lock file $LOCK exist!"
			process_status $ETOKEND
			process_status $PCSCD
			process_status $TELLICAST
			exit 1
		fi
		touch $LOCK
		ethernet_status
		start_etokend
		start_pcsd
		start_tellicast
		;;
	stop)
		process_stop $TELLICAST
		process_stop $PCSCD
		process_stop $ETOKEND
		rm -f $LOCK
		;;
	status)
		process_status $ETOKEND
		process_status $PCSCD
		process_status $TELLICAST
		ayecka_status
		eku_status
		ethernet_status
		;;
	restart|reload|condrestart)
		process_stop $TELLICAST
		process_stop $PCSCD
		process_stop $ETOKEND
		sleep 1
		$RESET
		start_etokend
		start_pcsd
		start_tellicast
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
		;;
esac

