#!/bin/bash
#

#
# iris    File for automatic startup of Vaisala IRIS, on Linux platforms
#
# chkconfig: 345 99 01
# description: Vaisala IRIS startup file

### BEGIN INIT INFO
# Provides: iris $iris
# Required-Start: antennad
# Required-Stop: antennad
# Default-Stop: 0 1 6
# Short-Description: Start IRIS daemon
# Description: Starts Vaisala IRIS daemon
### END INIT INFO

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

#PATH=/usr/sbin:/usr/bin:/sbin:/bin

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

RETVAL=0


start() {
  # Execute the commands to start iris
    if [ ! -f /var/lock/subsys/iris ] ; then
	echo -n $"Starting iris: "
	${install_root}/bin/start_iris -auto 1>/usr/iris_data/log/iris_out 2>/usr/iris_data/log/iris_err
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
	    echo_success
	    touch /var/lock/subsys/iris
	else
	    echo_failure
	fi
	echo
    else
	echo -n $"Starting iris: Lock file indicates already running"
        RETVAL=2
        echo_failure
        echo
    fi
    return $RETVAL
}

stop() {
  # Execute the commands to stop iris
    echo -n $"Stopping iris: "
    ${install_root}/bin/stop_iris -auto
    RETVAL=$?
    echo
    rm -f /var/lock/subsys/iris
    return $RETVAL
}

colors() {
    echo -n $"Reloading color setup tables: "
    ${install_root}/bin/start_iris -auto -colors &>/usr/iris_data/log/restart_err
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
	echo_success
    else
	echo_failure
    fi
    echo
    return $RETVAL
}

refork() {
  # Execute the commands to refork iris
    echo -n $"Restarting all iris daemons: "
    ${install_root}/bin/start_iris -auto -refork &>/usr/iris_data/log/restart_err
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
	echo_success
    else
	echo_failure
    fi
    echo
    return $RETVAL
}

resurrect() {
  # Execute the commands to resurrect iris
    echo -n $"Restarting crashed iris daemons: "
    ${install_root}/bin/start_iris -auto -resurrect &>/usr/iris_data/log/restart_err
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
	echo_success
    else
	echo_failure
    fi
    echo
    return $RETVAL
}

scan() {
    echo -n $"Rescanning product directories: "
    ${install_root}/bin/start_iris -auto -scan &>/usr/iris_data/log/restart_err
    RETVAL=$?
    if [ $RETVAL -eq 0 ] ; then
	echo_success
    else
	echo_failure
    fi
    echo
    return $RETVAL
}


case $1 in

'start')
	start
	;;

'stop')
	stop
	;;

'colors')
	colors
	;;

'restart')
	stop
	start
	;;

'refork')
	refork
	;;

'resurrect')
	resurrect
	;;

'scan')
	scan
	;;

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

exit $?
