#!/bin/sh
#
#
#    Copyright (c) 1996-2001 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   burninlogcat
#    Module name: diag/scripts
#
#    This file concatinates logs between dual CP systems into
#	  one cronological log file.
#
# NAME
#    burninlogcat
#
# SYNOPSIS
#    burninlogcat
#
# AVAILABILITY
#    swt_mfg
#
# DESCRIPTION
#    This command determint if there is a second CP on the system.
#    If there is not one, then the script exits since there is 
#    nothing to concatinate.
#
#    If one exists, then a subdirectory /var/log/cp_slot# is 
#    created, and the /var/log/*.log files from the other CP are 
#    copied into this directory. each file in the /var/log/*.log is
#    processed with the corresponding file in /var/log/cp_slot#
#    subdirectory. The file in the /var/log directory is sliced
#    into segments of activity between reboots. Each segment is 
#    named in incrementing order from first to last. The same in
#    done to the from the other CP in the newly created local
#    directory.
#
#    After the segmentation is complete, the segments are joined
#    cronologically into a .sum file. The script then cleans-up the
#    temporary files, and finally removes the /var/log/cp_slot#
#    subdirectory.
#
# OPERANDS
#
#    NONE
#
##########################

##########################
# sort_files
#
#  This routine will segment the input file into segments that
#  identify activity between power cycles. The boundry condition
#  is the appearence of the "Sending logs to" output. This is the
#  first output for all burnin scripts. The line numbers of the
#  occurence is tagged and the list of their occurences is saved
#  in $1/$file.segments.
#
#  The list is read back in, and head and tail are used to slice
#  up the original file in to smaller .tmp.segment# files.
#
##########################
sort_file()
{
    /bin/rm -f $1/$file.tmp.*
    /bin/grep -n "Sending logs to" $1/$file > $1/$file.segments
    start_line=1
    last_line=0
    segment=1
    /bin/echo $segment > $1/$file.tmp.num
    /bin/cat $1/$file.segments | while read lin
    do
	last_line=`/bin/echo $lin | /usr/bin/cut -d":" -f1`
	if [ $last_line -eq 1 ]
	then
	    continue
	else
	    end_line=`/usr/bin/expr $last_line - $start_line`
	    /usr/bin/tail +$start_line $1/$file > $1/$file.tmp.$segment
	    /usr/bin/head -$end_line $1/$file.tmp.$segment >  $1/$file.tmp.$segment.1
	    /bin/mv $1/$file.tmp.$segment.1 $1/$file.tmp.$segment
	    segment=`/usr/bin/expr $segment + 1`
	    start_line=$last_line
	    /bin/echo $segment > $1/$file.tmp.num
	fi
    done
    segment=`/bin/cat $1/$file.tmp.num`
    start_line=`/usr/bin/tail -1 $1/$file.segments |  /usr/bin/cut -d":" -f1`
    /usr/bin/tail +$start_line $1/$file > $1/$file.tmp.$segment
}

##########################
#  get_date
#
#  This routine extracts the first occurence of a date string
#  in a file. It assumes that the burnin run was able to begin
#  the run. If there is no Begin line in the file, then the
#  run never actually tested anything.
#
#  The results are echo'ed out
#
##########################
get_date()
{
    if [ -f $1 ]
    then
	result=`/bin/grep Begin $1 | /usr/bin/head -n 1 | /usr/bin/tr -s " " " " | /usr/bin/cut -d" " -f13-15,17 | sed "s/ /_/g"`
	if [ "$result" = "" ]
	then
	    /bin/echo NODATE
	else
	    /bin/echo $result
	fi
    else
	/bin/echo DONE
    fi
}

##########################
#  get_num_month
#
#  This routine converts month names to numberical values
#
##########################
get_num_month()
{
    case $1 in
    Jan) /bin/echo 1;;
    Feb) /bin/echo 2;;
    Mar) /bin/echo 3;;
    Apr) /bin/echo 4;;
    May) /bin/echo 5;;
    Jun) /bin/echo 6;;
    Jul) /bin/echo 7;;
    Aug) /bin/echo 8;;
    Sep) /bin/echo 9;;
    Oct) /bin/echo 10;;
    Nov) /bin/echo 11;;
    Dec) /bin/echo 12;;
    *) /bin/echo 13;;
    esac
}

##########################
#  compare_date
#
#  This routine compares two input strings to determin
#  which date was earlier.
#
#  RETURN
#  1 means the first string was earlier
#  2 means the second string was earlier
#  3 means neither string was a valid date
#
##########################
compare_date()
{
    one=$1
    two=$2

    if [ $one = DONE ]
    then
	/bin/echo 2
	return
    fi

    if [ $two = DONE ]
    then
	/bin/echo 1
	return
    fi

    if [ $one = NODATE ]
    then
	if [ $two = NODATE ]
	then
	    /bin/echo 3
	    return
	else
	    /bin/echo 2
	    return
	fi
    else
	if [ $two = NODATE ]
	then
	    /bin/echo 1
	    return
	fi
    fi

    year1=`/bin/echo $one | /usr/bin/cut -d"_" -f4`
    year2=`/bin/echo $two | /usr/bin/cut -d"_" -f4`
    if [ $year1 -ne $year2 ]
    then
	if [ $year1 -lt $year2 ]
	then
	    /bin/echo 1
	    return
	else
	    /bin/echo 2
	    return
	fi
    fi

    month1=`/bin/echo $one | /usr/bin/cut -d"_" -f1`
    month2=`/bin/echo $two | /usr/bin/cut -d"_" -f1`
    if [ $month1 != $month2 ]
    then
	month1=`get_num_month $month1`
	month2=`get_num_month $month2`
	if [ $month1 -lt $month2 ]
	then
	    /bin/echo 1
	    return
	else
	    /bin/echo 2
	    return
	fi
    fi

    day1=`/bin/echo $one | /usr/bin/cut -d"_" -f2`
    day2=`/bin/echo $two | /usr/bin/cut -d"_" -f2`
    if [ $day1 -ne $day2 ]
    then
	if [ $day1 -lt $day2 ]
	then
	    /bin/echo 1
	    return
	else
	    /bin/echo 2
	    return
	fi
    fi

    tim1=`/bin/echo $one | /usr/bin/cut -d"_" -f3`
    tim2=`/bin/echo $two | /usr/bin/cut -d"_" -f3`
    hr1=`/bin/echo $tim1 | /usr/bin/cut -d":" -f1`
    hr2=`/bin/echo $tim2 | /usr/bin/cut -d":" -f1`
    if [ $hr1 -ne $hr2 ]
    then
	if [ $hr1 -lt $hr2 ]
	then
	    /bin/echo 1
	    return
	else
	    /bin/echo 2
	    return
	fi
    fi

    min1=`/bin/echo $tim1 | /usr/bin/cut -d":" -f2`
    min2=`/bin/echo $tim2 | /usr/bin/cut -d":" -f2`
    if [ $min1 -ne $min2 ]
    then
	if [ $min1 -lt $min2 ]
	then
	    /bin/echo 1
	    return
	else
	    /bin/echo 2
	    return
	fi
    fi
    sec1=`/bin/echo $tim1 | /usr/bin/cut -d":" -f3`
    sec2=`/bin/echo $tim2 | /usr/bin/cut -d":" -f3`
    if [ $sec1 -ne $sec2 ]
    then
	if [ $sec1 -lt $sec2 ]
	then
	    /bin/echo 1
	    return
	else
	    /bin/echo 2
	    return
	fi
    fi
    /bin/echo ERROR
}

##########################
#  join_the_files
#
#  This routine interleaves two sets of file segments into
#  a single file. The segments are sorted cronologically
#  until all segments are accounted for.
#
##########################
join_the_files()
{
    first_idx=1
    second_idx=1
    sum_file=$first_dir/$file.sum
    /bin/rm -f $sum_file
    /bin/touch $sum_file
    first_file=$first_dir/$file.tmp
    second_file=$second_dir/$file.tmp
    more=`/usr/bin/expr $first_seg + $second_seg`
    first_date="`get_date $first_file.$first_idx`"
    second_date="`get_date $second_file.$second_idx`"
    while [ $more -gt 0 ]
    do
	first=`compare_date "$first_date" "$second_date"`
	if [ $first = ERROR ]
	then
	    /bin/echo "There was an error with the dates in $first_file.$first_idx or $second_file.$second_idx"
	    exit 3
	fi
	case $first in
	1)
#	    /bin/echo "Adding $first_file.$first_idx"
	    /bin/echo "Adding $first_file.$first_idx" >> $sum_file
	    /bin/cat $sum_file $first_file.$first_idx >> $sum_file.1
	    first_idx=`/usr/bin/expr $first_idx + 1`
	    first_date="`get_date $first_file.$first_idx`"
	    ;;
	2)
#	    /bin/echo "Adding $second_file.$second_idx"
	    /bin/echo "Adding $second_file.$second_idx" >> $sum_file
	    /bin/cat $sum_file $second_file.$second_idx >> $sum_file.1
	    second_idx=`/usr/bin/expr $second_idx + 1`
	    second_date="`get_date $second_file.$second_idx`"
	    ;;
	3)
	    /bin/echo "UNSURE OF ORDER HERE, NO DATE FOUND" >> $sum_file
#	    /bin/echo "Adding FIRST $first_file.$first_idx"
	    /bin/echo "Adding $first_file.$first_idx" >> $sum_file
	    /bin/cat $sum_file $first_file.$first_idx >> $sum_file.1
	    first_idx=`/usr/bin/expr $first_idx + 1`
	    first_date="`get_date $first_file.$first_idx`"
	    /bin/mv $sum_file.1 $sum_file
	    more=`/usr/bin/expr $more - 1`
#	    /bin/echo "Adding SECOND $second_file.$second_idx"
	    /bin/echo "Adding $second_file.$second_idx" >> $sum_file
	    /bin/cat $sum_file $second_file.$second_idx >> $sum_file.1
	    second_idx=`/usr/bin/expr $second_idx + 1`
	    second_date="`get_date $second_file.$second_idx`"
	    ;;
	esac
	/bin/mv $sum_file.1 $sum_file
	more=`/usr/bin/expr $more - 1`
    done
    /bin/echo "resulting file is in $sum_file"
}


##########################
# MAIN body ot the script
##########################
##########################
#
# Is there an other CP ?
#
##########################

other_stat=`hashow | /bin/grep Remote | /bin/grep -e Standby -e Active`
if [ "$other_stat" != "" ]
then
    other_cp=TRUE
    other_cp_slot=`hashow | /bin/grep Remote | /usr/bin/cut -d" " -f4 | /usr/bin/cut -d"," -f1`
else
    /bin/echo "Other CP is not available. Nothing to concatinate"
    exit 1
fi

##########################
#
# Get other CP files
#
##########################
if [ ! -d  /var/log/cp_slot$other_cp_slot ]
then
  /bin/mkdir /var/log/cp_slot$other_cp_slot
fi
/bin/rcp 10.0.0.$other_cp_slot:/var/log/*.log /var/log/cp_slot$other_cp_slot

first_dir=/var/log
second_dir=/var/log/cp_slot$other_cp_slot

##########################
#
# do all the log files
#
##########################
cd $first_dir
for file in *.log
do
    sort_file $first_dir
    first_seg=`/bin/cat $first_dir/$file.tmp.num`
    /bin/echo "Found $first_seg reboots in $first_dir/$file"
    sort_file $second_dir
    second_seg=`/bin/cat $second_dir/$file.tmp.num`
    /bin/echo "Found $second_seg reboots in $second_dir/$file"
    join_the_files
    /bin/rm -f $first_dir/$file.tmp* $first_dir/$file.segments
    /bin/rm -f $second_dir/$file.tmp* $second_dir/$file.segments
done
##########################
#
# remove the other CP data
#
##########################
/bin/rm -r /var/log/cp_slot$other_cp_slot
exit 0
