#!/usr/bin/ksh
#
# filename : alarm
# date     : 2003-Apr-10
# author   : herbert loevenich
#
#
# Version 1.0
#   older csv files are gzip'ed
#   raw p7alarmview files are kept
#
#   source the a7 environment to be able to run as remsh command
#
  if [ $(set | grep -c -i a7) -lt 10 ]
  then
    . /opt/platform7/lbin/p7profile  # load environment
  fi
#
# get path to current location of the script
#
# echo $0 # for testing purpose
  s_path=$(echo $0 | awk -F"/" ' { while (match (substr($0,i),"/") ) {
                                   i+=RSTART
                                   }
                                   print substr($0,1,i-2)
                                 }' )
#
# echo $s_path # for testing purpose
#
# global Variables
#
  b_cleanup=TRUE   # default value temporary files will be deleted
# f_out=$s_path/sys$(date +%d%b%Hh%M)     
# f_raw=$s_path/sys$(date +%d%b%Hh%M).raw 
# wm  f_tm_out=$s_path/TM_Sys_$(date +%m%d-%H%M).txt # TM statistik
# wm  f_out=$s_path/Sys_$(date +%m%d-%H%M).csv  # formated output
# wm  f_raw=$s_path/Sys_$(date +%m%d-%H%M).raw  # RAW output
  f_tm_out=$s_path/TM_late_messages.txt # TM statistik
  f_out=$s_path/alarm_logs.csv  # formated output
  f_raw=$s_path/alarm_logs.raw  # RAW output
  s_fd=","                                 # default field delimiter
  s_option=""                              # awk  Option / Parameter string
#
  while [ $# -gt 0 ]
  do
#   echo "Parameter 1: $1 2: $2"
    case $1 in
    +c)  b_cleanup=TRUE ;;  # default value
    -c)  b_cleanup=FALSE ;;
    -e)  s_mail=$(echo "$s_mail $2")
         shift ;;
    -fd) s_option=$(echo "$s_option -v s_fd=$2")
         shift ;;
    -i)  if [ -r $2 ]
         then
           f_in=$2
           echo "Use input file $f_in"
           f_out=$(echo "$f_in" | cut -f 1,1 -d ".").csv
         else
           echo "Error $0:  Can not read file $2"
         fi
         shift ;;
    -o)  f_out=$2
         shift ;;
    *)   echo "Error $0 Wrong Parameter $1"
         echo ""
         exit ;;
    esac
    shift
  done
# 
#
#
#
  if [ -r $f_out ]
  then
    echo "remove previous output file"
    rm $f_out
  fi
  if [ -r $f_raw ]
  then
    echo "remove previous raw file"
    rm $f_raw
  fi
#
# compress older files
#
# for i in $s_path/sys*.[cr][sa][vw]
# do
#   gzip $i 
# done 2> /dev/null
#
# echo $f_out  # for testing purpose
# echo $f_raw  # for testing purpose
#
if [ -z "$f_in" ]                      # default analyse recent alarmlog files
then
  for i in $(ls -tr /var/opt/platform7/logs/current/alarm_log_?)
  do
    echo $i
    /opt/platform7/bin/p7alarmview $i >> $f_raw
  done 
else                                   # check input file type
  s_type=$(file $f_in | awk -F \  '{ print $NF }')
  echo "given input file  type : >$s_type<"
  if [ "$s_type" = "text" ]            # already dumped using p7alarmview
  then
    f_raw=$f_in
  else                                 # archived files 
    s_filename=$(echo $f_in | awk -F \/ '{ print $NF }')
    f_raw=$s_path/${s_filename}.raw
    f_out=$s_path/${s_filename}.csv
    /opt/platform7/bin/p7alarmview $f_in >> $f_raw
  fi
fi
# wm  echo "Analyse File : $f_raw"
# wm  echo "Output  File : $f_out"
#
  if [ -z "$s_option" ]
  then
    s_option=$(echo "$s_option -v s_fd=,")
  fi
#
# wm  echo "used awk options are $s_option"
#
  /sbin/awk -f $s_path/alarm.awk $s_option $f_raw >> $f_out.$$
#
# remove debug information
#
  grep "^#TM" $f_out.$$ |\
  sort -n -t, -k 5,5 > ${f_tm_out} # Traffic Monitor Statistik
  grep -v "^#" $f_out.$$ > ${f_out}
#
# cleanup temporary files
#
  if [ "$b_cleanup" = "TRUE" ] 
  then 
    rm $f_out.$$
  fi
