#!/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.

source /ciena/scripts/saos_utils.sh

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

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

# Make sure we have a SAOS server to bring down
if $(saos_server_running) ; then

    saos_pid=$(saos_server_pid)

    # 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

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