#!/usr/bin/ksh
#
# filename : collectfiles
# date     : 2003-Apr-10
# author   : herbert_loevenich
#
# Version 1.6
#   field delimiter as parameter introduced !! ";" not tested !!
#
# Version 1.5 
#   remove option to send emails
#   elm error messages appears when providing parameters
#
# Version 1.4
#   collect calls always alarm and/or error
#   gzip's and tar's the files, 
#   the csv and raw files will be deleted
#
# Version 1.3
#   be able to be start by remsh 
#
# Version 1.2
#   removed ":" from filename for Site, Workstation, DataStore
#
# Version 1.1
#   gzip files after use
#
# Version 1.0
#   first Parameter is treathed as an email address (works with attachments)
#   output from p7alarmview and p7dumperror are added as well to the logfiles
#   Customer Name is used for the tarfile 
#
# Version 0.9
#   if parameter provided they are treathed as email addresses
#   it could be aliases as well
#
# detect number of available csv files
#   output from alarm sysDDMMMHHhmm.csv  system status log
#   output from error locDDMMMHHhmm.csv  local exception log
#
#
# 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
#
# detect customer and save in
#
f_cust=$s_path/../.customer
#
if [ ! $f_cust -nt $0 ]
then
  s_type=$(cat /var/opt/platform7/conf/gf36_MCIDStorage | cut -f 1,1 -d ":")
  if [ $s_type = "Server" ]
  then
    echo "determine the customer name"
    /usr/sbin/swlist -l fileset -a revision -a state |
    awk -F\. '/^# P7Keys/ { print $2 ; exit } ' > $f_cust
  else
#
# replace ":" in Processor id against "-", less unix problems
#
    sed 's/\:/-/g' /var/opt/platform7/conf/gf36_MCIDStorage > $f_cust
  fi
fi
s_cust=$(cat $f_cust)
#
if [ -z "$s_cust" ]                             # in case something went wrong
then
  s_cust="access7"
fi
echo "Collect Information for $s_cust"
#
# check input parameter for field delimiter
#
  if [ $# -ne 0 ]
  then
    s_option="-fd $1"
  fi
#
# create new local exception logs
#
  if [ -x /opt/platform7/bin/p7dumperror ]
  then
#   echo "  Call   error"
    $s_path/error $s_option
#   echo "  Called error"
  fi
#
# tar latest logfiles
#
f_tar=$s_path/$s_cust.tar
# f_tar=$s_path/../data/$s_cust.tar
#
cd $s_path
tar -cvf $f_tar $(ls -tr *.csv 2> /dev/null | tail -1) \
                $(ls -tr *.raw 2> /dev/null | tail -1) 2> /dev/null
#
# cleanup csv and raw files
#
  rm $(ls -tr *.csv 2> /dev/null | tail -1) 2> /dev/null
  rm $(ls -tr *.raw 2> /dev/null | tail -1) 2> /dev/null
#
# create new system status logfiles
#
  if [ -d /var/opt/platform7/logs/current ]  # Only on Central Server
  then
#   echo "  Call   alarm"
    $s_path/alarm $s_option
#   echo "  Called alarm"
#
# tar latest logfiles
#
    cd $s_path
    tar -rvf $f_tar $(ls -tr *.csv 2> /dev/null | tail -1) \
                    $(ls -tr *.raw 2> /dev/null | tail -1) 2> /dev/null
#
# cleanup csv and raw files
#
    rm $(ls -tr *.csv 2> /dev/null | tail -1) 2> /dev/null
    rm $(ls -tr *.raw 2> /dev/null | tail -1) 2> /dev/null
  fi
#
# compress tarfile
#
gzip -f $f_tar
#
# cleanup csv and raw files
#
  rm $(ls -tr *.csv 2> /dev/null | tail -1) 2> /dev/null
  rm $(ls -tr *.raw 2> /dev/null | tail -1) 2> /dev/null
#
#--------------
#
  exit
#
#--------------
#
# No email addresses provided => exit
#
if [ $# -eq 0 ]
then 
  exit 
fi
#
#--------------  Part below to send email is working now, but needs testing
#
echo "Email contains from Customer $s_cust" > part1
echo "" >> part1
echo " - System Status log" >> part1
echo " - Central Server local exception log" >> part1
echo "" >> part1
echo "In both formats" >> part1
echo "  raw from p7alarmview and p7dumperror" >> part1
echo "  csv formated to be used within excel" >> part1
#
echo "[include $f_tar.gz application/octet-stream base64]" > part2
#
cat part* > message
#
elm -s "A7 Logfiles from $s_cust" $* < message
#
# cleanup
#
rm message
rm part*
