#!/bin/ksh

# **************************** LICENSE START ***********************************
#
# Copyright 2012 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************

#                                                (vk Sep-95, rev 991011)
#
# This script was written only for ECMWF and it has not been
# designed to be portable...
#
# Script executes a metview module and reports crashes by mail.
# (within time this script has grown toooo looooooonnnngggggggg!)

set +e
Module=`basename $1`
start_date=`date`
running_bin="`ls -l $1 | cut -c43-`"
send_mail=NO

#-- execute module, with possible parameters
if [ $METVIEW_LOG = no_log -o "$METVIEW_LOG" = "-" ]
then
  "$@"
  SUCCESS_STATUS=$?
else
  MYLOG="$SCRATCH/$METVIEW_LOG.$Module.log"
  echo "[mvrun info] process $$ starting `date` with cmd:" > $MYLOG
  echo "[mvrun info] $@"                      >> $MYLOG
  echo "[mvrun info] mode is $METVIEW_MODE\n" >> $MYLOG
  "$@"                                        >> $MYLOG
  SUCCESS_STATUS=$?
  echo "\n[mvrun info] process $$ ended `date`" >> $MYLOG
  echo "[mvrun info] process exit status was $SUCCESS_STATUS" >> $MYLOG
fi

st_sig=`expr $SUCCESS_STATUS / 128`
st_signal=`expr $SUCCESS_STATUS % 128`
#-- (x:y) were used when divided by 16, now [x:y] when div 128
st_text="$SUCCESS_STATUS [$st_sig:$st_signal]"

current_bin="`ls -l $1 | cut -c43-`"
if [ "$running_bin" != "$current_bin" ]
then
  st_text="UPDATED!"
fi

MVUSERID=$USER
[ "$MVUSERID" = "" ] && MVUSERID=$LOGNAME
[ "$MVUSERID" = "" ] && MVUSERID="???"

if [ $st_sig -ne 0 ]
then
  send_mail=YES
  case $st_signal in
    1)
      send_mail=NO
      ;;
    2)
      send_mail=NO
      ;;
    4)
      st_reason="Illegal Instruction"
      ;;
    5)
      st_reason="Trace/Breakpoint Trap"
      ;;
    6)
      st_reason="Abort"
      ;;
    7)
      st_reason="Emulation Trap"
      ;;
    8)
      st_reason="Arithmetic Exception"
      ;;
    10)
      st_reason="Bus Error"
      ;;
    11)
      st_reason="Segmentation Fault"
      ;;
    12)
      st_reason="Bad System Call"
      ;;
    *)
      send_mail=NO
      st_reason="(reason not decoded)"

      MV_CRASH_LOG="/home/graphics/cgx/public/Metview_crashlog_nomail"
      if [ ! -f $MV_CRASH_LOG ]
      then
        touch $MV_CRASH_LOG
        chmod 666 $MV_CRASH_LOG
        #-- give file to cgx (not to decrease users available disk quota) --
        chown cgx $MV_CRASH_LOG
      fi
      #echo "`date -u '+%d.%m.%Y %R'`; $Module; `echo $METVIEW_MODE | cut -c1`; $st_text; $MVUSERID; `hostname`; `cat $METVIEW_DIR_SHARE/app-defaults/MvTitleBarVersionText`" >> $MV_CRASH_LOG
      echo "`date -u '+%d.%m.%Y %R'`; $MVUSERID; `echo $METVIEW_MODE | cut -c1`; $st_text; $Module; `hostname`; `cat $METVIEW_DIR_SHARE/app-defaults/MvTitleBarVersionText`" >> $MV_CRASH_LOG
      ;;
  esac
fi

#-- do the hard work if really crashed
if [ $send_mail = YES ]
then
  UserMsg=/tmp/crash_$$.$Module.UserMsg
  MailMsg=/tmp/crash_$$.$Module.MailMsg
  Person=`finger $LOGNAME | grep "name: $LOGNAME" | cut -f3 -d: | head -1`

  #-- log into logfile --
  MV_CRASH_LOG="/home/graphics/cgx/public/Metview_crashlog_`cat $METVIEW_DIR_SHARE/MvVersion`"
  if [ ! -f $MV_CRASH_LOG ]
  then
    touch $MV_CRASH_LOG
    chmod 666 $MV_CRASH_LOG
    #-- give file to cgx (not to decrease users available disk quota) --
    chown cgx $MV_CRASH_LOG
  fi
  #-- new crash file format 25.3.1998, add METVIEW_MODE first letter 6.8.1998 --
  #echo "`date` $Module; $MVUSERID; `hostname`; $st_text; `cat $METVIEW_DIR_SHARE/app-defaults/MvTitleBarVersionText`" >> $MV_CRASH_LOG
  #echo "`date -u '+%d.%m.%Y %R'`; $Module; `echo $METVIEW_MODE | cut -c1`; $st_text; $MVUSERID; `hostname`; `cat $METVIEW_DIR_SHARE/app-defaults/MvTitleBarVersionText`" >> $MV_CRASH_LOG
  #-- 2000-08-18: make column widths more even i.e. constant width values first --
  echo "`date -u '+%d.%m.%Y %R'`; $MVUSERID; `echo $METVIEW_MODE | cut -c1`; $st_text; $Module; `hostname`; `cat $METVIEW_DIR_SHARE/app-defaults/MvTitleBarVersionText`" >> $MV_CRASH_LOG

  #-- inform user about crash and core --
  echo "\nThis is an automatic message to Metview user $Person:" > $UserMsg
  echo "a module '$Module' just crashed in your Metview session !">>$UserMsg

  if [ "$running_bin" != "$current_bin" ]
  then
    echo "________\n\nTHIS CRASH WAS CAUSED BY A METVIEW UPDATE !!!!!" >> $UserMsg
  fi
  echo "________\n" >> $UserMsg

  if [ -s core ]
  then
    echo "You have the following core file in your directory:" >> $UserMsg
    ls -l core >> $UserMsg
    file core >> $UserMsg
    echo "________\n" >> $UserMsg
    #-- ecquota is an ECMWF modification for HP servers --
    ecquota -v >> $UserMsg
  else
    echo "No core file was produced or the core file is empty !" >> $UserMsg
  fi
  echo " " >> $UserMsg

  if [ "$running_bin" = "$current_bin" ]
  then
   echo "+---------------------------------------------------------+">>$UserMsg
   echo "|   If you have the power to crash Metview repeatedly,    |">>$UserMsg
   echo "|  please inform Graphics Group how you manage to do it!  |">>$UserMsg
   echo "+---------------------------------------------------------+">>$UserMsg
   echo " " >> $UserMsg
  fi 

  [ -s core ] && echo "Check your disk quota" >> $UserMsg
  echo "  M e t v i e w" >> $UserMsg

  if [ $METVIEW_MODE = batch ]
  then
    /usr/bin/mail $MVUSERID < $UserMsg
  else
    xterm -title "Metview Crash Info" -bg gray -fg red4 -b 12 -e view $UserMsg &
    #-- try to make sure window appears even when network is slow...
    sleep 15
  fi

  #-- inform Metview group by mail --
  echo "Subject: Metview $METVIEW_RELEASE: $Module crashed!" > $MailMsg
  cat $METVIEW_DIR_SHARE/app-defaults/MvTitleBarVersionText >> $MailMsg
  echo "\n$Module: $st_reason! (status=$SUCCESS_STATUS: code=$st_sig, signal=$st_signal)" >> $MailMsg
  echo "User is $MVUSERID:$Person on `hostname` (informed)" >> $MailMsg

  #-- phone number is an ECMWF command!!!
  pn "\[$MVUSERID" >> $MailMsg

  echo " " >> $MailMsg
  echo "Started: $start_date" >> $MailMsg
  echo "Crashed: `date`" >> $MailMsg
  echo " " >> $MailMsg
  echo "Crashed binary: $running_bin" >> $MailMsg
  echo "Current binary: $current_bin" >> $MailMsg
  if [ "$running_bin" != "$current_bin" ]
  then
    echo "\n!!!!!! CRASH DUE TO EXECUTABLE UPDATE !!!!!" >> $MailMsg
  fi
  echo " " >> $MailMsg

  if [ -s core ]
  then
    CoreOk=`file core | grep $Module`
    file core >> $MailMsg
    echo "file etc.: `ls -l core | cut -c17-`" >> $MailMsg
    echo "directory: `pwd`" >> $MailMsg
    chmod 777 core

    if [ "$CoreOk" != "" ]
    then
      #-- examine call stack, redirect & tee to catch crash reason
      echo " " >> $MailMsg
      echo "where;quit" | dbx $1 core 2>&1 | tee -a $MailMsg
    else
      echo "\nWrong core file !" >> $MailMsg
    fi
  else
    echo "No core file or core file empty !" >> $MailMsg
  fi

  /usr/bin/mail $METVIEW_SUPPORT_MAIL < $MailMsg
  #mail cgh < $MailMsg

  rm -f $MailMsg
  rm -f $UserMsg
fi
exit $SUCCESS_STATUS
