#!/bin/bash
#
#
# File for automatic startup of Vaisala Bite Export daemon
#
# chkconfig: 345 96 03 
# description: Vaisala Bite Export startup file

### BEGIN INIT INFO
# Provides: bite_export $bite_export
# Required-Start: 
# Required-Stop: 
# Default-Stop: 0 1 6
# Short-Description: Start Bite Export daemon
# Description: Starts Vaisala Bite Export daemon
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sigmet/profile.conf
. /etc/profile.d/sigmet.sh

if [ ! -x ${install_root}/bin/bite_export ] ; then
    echo "ERROR: ${install_root}/bin/bite_export file missing"
    exit 0
fi

RETVAL=0

start(){
  # Execute the commands to start rcp8
    PROCESSCOUNT=$(ps -ef | grep -v grep | grep -v start | grep -cw bite_export)
    if [[ -f /var/lock/subsys/bite_export ]] && [[ ${PROCESSCOUNT} -eq 0 ]] ; then
        rm /var/lock/subsys/bite_export
    fi

    if [ ! -f /var/lock/subsys/bite_export ]; then
	echo -n $"Starting bite_export: "

	${install_root}/bin/bite_export -port 30785 -interval 2 -output /usr/iris_data/log/bite_ascii.csv &
	RETVAL=$?
	if [ $RETVAL ] ; then
	    echo_success
	    touch /var/lock/subsys/bite_export
	else
	    echo_failure
	fi
	echo
    fi
    return $RETVAL
}

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


case $1 in

'start')
	start
	;;

'stop')
	stop
	;;

'restart')
        stop
        start
	;;

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

exit $?
