#!/bin/sh

# $Id$

#Dump the STK log information.

#The important funcion for getting the logs.
get_log()
{
    tl_name=$1

    #Create the variables that point to the files to output to.
    fname=${tl_name}-log
    output_file=$log_dir/$fname
    temp_file=$temp_dir/$fname.temp
    temp2_file=$temp_dir/$fname.temp.temp

    #If we write to a temp file, and swap in it when we are done, there will
    # not any time when the page is empty becuase the scipt is still writing
    # the file.
    rm -f $temp_file

    #Get the name of the tape library.
    tape_library=`enstore conf $timeout --show $mc tape_library 2> /dev/null`
    if [ $? -ne 0 ]; then
        #Use the entire line from the --list-media-changers command if
        # the tape_library was not found in the media_changer configuration.
        tape_library=$line
    fi

    #Get the name of the acls node.  This node is specific to STK.
    silo_node=`enstore conf $timeout --show $mc acls_host 2> /dev/null`
    if [ $? -ne 0 ]; then
	echo "Could not find name of ACLS node for $mc."
	continue
    fi

    #Make sure we know how up-to-date this is.
    echo -e $tape_library Log Report: `date` >> $temp_file 2>&1
    echo -e Brought to You by: `basename $0` "\n" >> $temp_file 2>&1
    #Get the last 1000 lines of the stk log file.
    #Note: The "<&-" for the rsh command closes standard input so it
    #      won't munch any lines from the main while loop!!!
    #Note: Send errors from /usr/bin/rsh (via 2>> $temp_file)
    #      to the output file so that are not 'lost' to standard out.
    /usr/bin/rsh $silo_node -l acsss 'tail -1000 log/acsss_event.log' <&- > $temp2_file 2>> $temp_file
    #Filter the result.
    #Note: The tac program reverses the order.
    awk '/20[0-9][0-9]/ { printf("%s",$0) ; getline ;  getline; printf("\t%s\n",$0)}' $temp2_file | tac >> $temp_file 2>&1
    echo -e "\n" >> $temp_file 2>&1
    #Give an end time so we know how long it took.
    echo -e $fname Report Completed: `date` "\n" >> $temp_file 2>&1

    #Move the file into its correct location.
    if [ $is_web_server -eq 1 ]; then
	mv $temp_file $output_file    #Do the temp file swap.
	#echo mv $temp_file $output_file
    else
	enrcp $temp_file $web_server:$output_file && rm -f $temp_file
	#echo enrcp $temp_file $web_server:$output_file && rm -f $temp_file
    fi
}

#Make sure to set a timeout.
timeout='--timeout 10 --retries 3'

#First, obtain the host the web server runs on.
web_server=`enstore conf $timeout --show web_server ServerHost`
if [ $? -ne 0 ]; then
    echo Web server hostname not obtained.
    exit 1
fi
#Determine if we are running this script on the web server.
web_shortname=`echo $web_server | cut -f 1 -d "."`
this_shortname=`hostname | cut -f 1 -d "."`
if [ $web_shortname == $this_shortname ]; then
    is_web_server=1
else
    is_web_server=0
fi

#Next, obtain the directory to write the output.
log_dir=`enstore conf $timeout --show crons log_dir`
if [ $? -ne 0 ]; then
    echo Log directory not obtained.
    exit 1
fi

#Next, obtain the directory to write the temporary output.
temp_dir=`enstore conf $timeout --show crons tmp_dir`
if [ $? -ne 0 ]; then
    echo Temp directory not obtained.
    exit 1
fi
if [ ! -d $temp_dir ]; then
    echo Temp directory $log_dir not found.
    exit 1
fi

#Loop over all the media changers (remember to delete the header line).
enstore conf $timeout --list-media-changers | sed '1d' |
while read line; do
    mc=`echo $line | awk '{print $1}'`

    #Look for STK media changers.
    echo $line | egrep "STK_MediaLoader" > /dev/null 2> /dev/null
    if [ $? -ne 0 ]; then
        continue
    fi

    #Use just the short name of the media changer converted to upper case.
    tl_name=`echo $mc | tr "." " " | cut -f 1 -d " " | tr '[a-z]' '[A-Z]'`

    get_log $tl_name
done

