#!/bin/bash

# Trigger "safe" lps output from the SAOS server.  This output will be
# produced without taking locks and without using a process slot in the
# SAOS server.
#
# This script will:
#   - run the script as root if it is not already running as root
#   - check that the SAOS server is running
#   - send a signal to the SAOS server to trigger lps output
#   - wait for the output file
#   - dump the output to stdout
#   - clean up the output file

source /ciena/scripts/saos_utils.sh

# Make sure we are running as root.  Try to promote ourselves if we are not.
if [ "$(whoami)" != "root" ] ; then
    exec root $0 $*

    # Fall-through if exec fails
    echo "ERROR: must run $(basename $0) as root"
    exit -1
fi

if ! saos_server_running ; then
    echo "ERROR: SAOS server is not running"
    exit -1
fi

# send a signal to the SAOS server to trigger lps output
kill -RTMIN $(saos_server_pid)
if [ "$?" -ne "0" ] ; then
    echo "ERROR: failed to send a signal to the SAOS server"
    exit -1
fi

# wait for the output file to appear
outfile=/tmp/lps.$$.txt
while [ ! -r "$outfile" ] ; do
    sleep 1
done

# - wait to ensure output is done
# - dump output to stdout
# - clean up the output file
sleep 1
cat $outfile
rm $outfile

