#!/bin/sh

# This script is called if the watchdog is killing the system.  It's purpose
# is to collect data from SAOS, and then force a system reboot.

SAOS_PIDFILE=/tmp/fifo/leos/LEOS_IPC_PID

/ciena/bin/evt_log ALERT "$0: activated"

# take a snapshot of the logs before attempting to kill SAOS
/ciena/bin/evt_copy ram0 flash0

# Find it and kill it.
if [ -f "$SAOS_PIDFILE" ] ; then
    saos_pid=$(cat $SAOS_PIDFILE)

    # Make sure we have a SAOS server to bring down
    if [ -d /proc/$saos_pid ] ; then

        # Try to be nice - send a TRAP signal and wait for something to happen
        kill -TRAP $saos_pid
        sleep 5

        # If it doesn't seem like SAOS is dying, then hit the server with a
        # TRAP signal until we see evidence of something crashing, or until
        # the kill command starts failing.
        until [ -e /tmp/core.in.progress ] ; do
            kill -TRAP $saos_pid
            if [ "$?" -ne "0" ] ; then
                break;
            fi
        done

        # Now wait for the core dump to complete
        while [ -e /tmp/core.in.progress ] ; do
            sleep 5
        done
    fi
fi

/ciena/bin/evt_log ALERT "$0: rebooting"
/sbin/reboot
