#!/bin/sh
#
# Script to compress the fault log file.
#
# Accepts a single parameter to specify the file to be compressed.  If no
# parameter is specified, the default fault file name is compressed.
#

source /etc/profile
source /ciena/scripts/fault.sh

if [ "$#" -lt "1" ] ; then
    fault_log="$FAULTLOG"
else
    fault_log="$1"
fi

if [ ! -f "$fault_log" ] ; then
    echo "ERROR: $fault_log is not a valid file"
    exit 1
fi

# use a unique tempfile name to avoid collisions
tempfile="/tmp/log_$$.gz"

# track if we encounter a failure, but continue running
rc=0

# put a gziped version of the fault log into tmpfs
gzip <$fault_log > $tempfile || rc=1

# remove the current flash logs and replace them with a note of what we did
echo "*** PREVIOUS LOG COMPRESSED: $(date)"        >  $fault_log
echo "***                          $fault_log-.gz" >> $fault_log
echo ""                                            >> $fault_log

# move the gziped logs into flash
mv $tempfile $fault_log-.gz || rc=1

# Regardless of the result of the move, erase the temporary file
rm -f $tempfile || rc=1

exit $rc
