#!/usr/bin/ksh
#
# filename : error
# date     : 2006-Oct-27
# author   : herbert loevenich
#
# version : 2.0
#   first adaption for LinuxDSP's
#   local run with specified input file
#
# version : 1.0
#   first draft version  2003
#
#   source the a7 environment to be able to run as remsh command
#
# set -vx
#
  if [ $(set | grep -c -i a7) -lt 10 ]
  then
    . /opt/platform7/lbin/p7profile  # load environment
  fi
#
  s_OS=$(uname)
#
# fixes location/path depending on type of OS
#
  if [ $s_OS = "Linux" ]
  then
    c_cat=/bin/cat
    c_awk=/bin/gawk
  else
    c_cat=/usr/bin/cat
    c_awk=/sbin/awk
  fi
#
# get path to current location of the script
#
# echo $0 # for testing purpose
  s_path=$(echo $0 | $c_awk -F"/" ' { while (match (substr($0,i),"/") ) {
                                   i+=RSTART
                                   }
                                   print substr($0,1,i-2)
                                 }' )
#
# echo $s_path # for testing purpose
#
  if [ $# -eq 0 ]
  then
#
#  Start of recent local error logs
#
    s_id=$($c_cat /var/opt/platform7/conf/gf36_MCIDStorage)
    s_type=$(echo $s_id | cut -f 1,1 -d ":")
    s_site=$(echo $s_id | cut -f 2,2 -d ":")
    s_number=$(echo $s_id | cut -f 4,4 -d ":")
#
#   echo $s_type # for debugging
#
    case $s_type in
      Server)         s_pre=SVR ;;
      Site)           s_pre=$(echo "STE-$s_site") ;;
      Workstation)    s_pre=$(echo "WST-$s_number") ;;
      Datastore)      s_pre=$(echo "DSP-$s_number-S-$s_site") ;;
      ProbeProcessor) s_pre=$(echo "PRB-$s_number-S-$s_site") ;;
      SDBProc)        s_pre=$(echo "SDB-$s_number-S-$s_site") ;;
      *)              s_pre=$(echo "unknown-$s_number-S-$s_site") ;;
    esac
#
#   echo $s_pre # for debugging
#
# output file
#
    f_out=$s_path/${s_pre}_$(date +%m%d-%H%M).csv
    f_raw=$s_path/${s_pre}_$(date +%m%d-%H%M).raw
#
    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_patch/loc*.[cr][sa][vw]
    do
      /usr/contrib/bin/gzip $i 
    done 2> /dev/null
#
# echo $f_out
# echo $f_raw
#
    for i in $(ls -tr /var/opt/platform7/logs/local/local_error_log_?)
    do
      echo $i
      /opt/platform7/bin/p7dumperror $i >> $f_raw
      errno=$?
      if [ ! $errno -eq 0 ]
      then
        echo "p7dumperror $i failed with exit code : $errno"
      fi
    done
#
    $c_awk -f $s_path/error.awk $f_raw >> $f_out
    echo ""
#
#  END   of recent local error logs
#
    else
      if [ "$1" = "-i" ] && [ -r $2 ]
      then
        $c_awk -f $s_path/error.awk $2 > $2.csv
      fi
    fi
