#!/bin/bash
#
# File for automatic startup of tsexport (TS export utility)
#
# chkconfig: 345 98 02
# description: SIGMET tsexport utility
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sigmet/profile.conf
. /etc/profile.d/sigmet.sh

#-----------USAGE NOTES
# TSEXPORT
#
# tsexport can take the following options:
# tsexport -broadcast -port -debug -daemon
#
# SIGMET RECOMMENDS THE FOLLOWING PORTS:
#   RVP8:
#     TSEXPORT: 30780
#     TSIMPORT: 30781
#
#   ARCHIVE HOST:
#     TSEXPORT: 30781
#     TSIMPORT: 30780
#
if [ ${tsexport_port} ]; then
  PORT=${tsexport_port}
else
  PORT="30780"
fi
# NOTE: THE PORTS FOR EXPORT AND IMPORT HAVE TO MATCH (CONNECT)
#       BETWEEN THE RVP8 AND THE ARCHIVE HOST
#
# tsexport can used in two ways: 
#    
#    Subnet broadcast:
#	tsexport -broadcast:192.168.76.255 -port:30780
#    Targeted broadcast:
#       tsexport -broadcast:192.168.76.36  -port:30780
#
if [ ${tsexport_ip} ]; then
 IPADDRESS=${tsexport_ip}
else
 IPADDRESS="192.168.76.36"
fi

TSEXPORT_PATH="${install_root}/bin/tsexport"

if [ ! -x $TSEXPORT_PATH ] ; then
    echo "ERROR: ${TSEXPORT_PATH} file missing"
    exit 0
fi
RETVAL=0

start(){
  # Execute the commands to start tsexport
    if [ ! -f /var/lock/subsys/tsexport ]; then
	echo -n $"Starting tsxport: "
	${TSEXPORT_PATH} -broadcast:${IPADDRESS} -port:${PORT} -daemon
	RETVAL=$?
	if [ $RETVAL ] ; then
	    echo_success
	    touch /var/lock/subsys/tsexport
	else
	    echo_failure
	fi
	echo
    fi
    return $RETVAL
}

stop() {
    echo -n $"Stopping tsexport: "
    killproc tsexport -TERM
    RETVAL=$?
    echo
    if [ $RETVAL ] ; then
	rm -f /var/lock/subsys/tsexport
    fi
    return $RETVAL
}

case $1 in
    
'start')
	start
	;;
    
'stop')
	stop
	;;

'restart')
        stop
        start
	;;
    
    *)
	echo "usage: $0 {start|stop|restart}"
	;;
esac

exit $?
