#!/bin/bash

set -e

# It is not a good idea to include inc/releng_getopts.inc with the
# master script as it calls the subordinate scripts and they include
# this file too, and we want to avoid sharing variable with subshells

SELF=$(basename "$0")
GEN_DATA_DIR=releng_generated_data
CLOG=${GEN_DATA_DIR}/log.${SELF}
PASSED_OPTIONS="$@"
RELENG_XORRISO=
CLEANUP_LOG=0

#############################################
while getopts “x:k:c:f:h” OPT
do
  case $OPT in
       x)
         RELENG_XORRISO=$OPTARG
         ;;
       c)
         CLEANUP_LOG=1
         ;;
       *)
         ;;
  esac
done

if [ ! "${1}" ]; then
cat << HLP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
${SELF} runs executables from CWD starting 
with releng_*, passing them its own options.
stdout/stderr output stored in:
 ${CLOG}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
self generated data are stored in ${GEN_DATA_DIR}.
required space for these data is about 300 megabytes.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
examples:
# run xorriso and keep the self generated data
 $ ./${SELF} -x path/to/xorriso [-k1]

# clean up self generated data from previous run
 $ ./${SELF} -c1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HLP
 exit 31
fi

########################################################
 if [ -f "${CLOG}" ]; then
   mv "${CLOG}" "${CLOG}".prev
 fi
 > ${CLOG}
 if [ -x "${RELENG_XORRISO}" ]; then
    echo -e "_OVERVIEW_______________________________________________________________" >> ${CLOG}
    date --utc                                                                         >> ${CLOG}
    ${RELENG_XORRISO} --version                                                        >> ${CLOG}
    echo -e "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> ${CLOG}
 fi
 DSTART=`date --utc`
 echo -ne "${SELF}: Started at ${DSTART}" | tee -a ${CLOG}
 E1=`date '+%s'`
 # require ^releng_, avoid running (your)self explicitly
 for s in `ls | grep ^releng_ | grep -v ${SELF} | sort -n`; do
  if [ -x ${s} -a ! -d ${s} ]; then
    echo -ne "\n\n_STARTING_TEST_________________________________________________________"      >> ${CLOG}
    echo -ne "\n${SELF}: Running ./${s} ${PASSED_OPTIONS}..."                             | tee -a ${CLOG}
    T1=`date '+%s'`
    set +e
    ./${s} ${PASSED_OPTIONS} &>> ${CLOG}
    RET=$?
    T2=`date '+%s'`
    let TS="${T2} - ${T1}"
    case ${RET} in
         0)
           printf "done in ${TS} sec. ok."
           ;;
         *)
	   printf "done in ${TS} sec. "
           which tput >/dev/null 2>&1 && tput smso
           printf "FAIL -> EXIT CODE $RET"
           which tput >/dev/null 2>&1 && tput rmso
           ;;
    esac
    set -e
  fi
 done

 DEND=`date --utc`
 echo -ne "\n${SELF}: Stopped at ${DEND}." | tee -a ${CLOG}
 if   [ "${CLEANUP_LOG}" -eq 1 ]; then
      if [ -f "${CLOG}" ]; then
	 rm -f "${CLOG}"
         echo -e "\n${SELF}: Removed my own log ${CLOG}." | tee -a ${CLOG}
      fi
      if [ -f "${CLOG}".prev ]; then
         rm -f "${CLOG}".prev
         echo -e "\n${SELF}: Removed my own log ${CLOG}.prev." | tee -a ${CLOG}
      fi
 else
      E2=`date '+%s'`
      if [ ${E2} -eq ${E1} ]; then
        echo -e " Total elapsed 0 sec." | tee -a ${CLOG}
      else
        let ES="${E2} - ${E1}"
        echo -e " Total elapsed ${ES} sec." | tee -a ${CLOG}
      fi
      #####
      echo -e "\n_SUMMARY________________________________________________________________" >> ${CLOG}
      echo -e "${SELF}: Trivial log examination: ${CLOG}" | tee -a ${CLOG}
      echo -e "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | tee -a ${CLOG}
      # severity classes of libdax_msgs.h in libburn and libisofs
      # List of boring keywords:
      #   'UPDATE' A pacifier message during long running operations.
      # List of interesting keywords:
        # thrown by xorriso and underlying libraries
      LIST_KWD="(NEVER|ABORT|FATAL|FAILURE|MISHAP|SORRY|WARNING|HINT|NOTE|DEBUG|ALL"
        # thrown by others
      LIST_KWD+="|FAIL|ERROR|WRONG)"

      if [ -f "${CLOG}" ]; then
          set +e
	  # lines, perl regex, leading tabs
          grep -n -PT "${LIST_KWD}" "${CLOG}" | tee -a ${CLOG}
	  RET_GREP="$?"
          case ${RET_GREP} in
              0) # found
                 ;;
              1) # not found
		 echo -e "\n${SELF}: Log file looks clear.\n" | tee -a ${CLOG}
                 ;;
	      *) #
                 echo -e "\n${SELF}: grep returned EXIT CODE: ${RET_GREP}.\n" | tee -a ${CLOG}
		 ;;
          esac
	  set -e
      fi
      echo -e "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" | tee -a ${CLOG}

      ##### TODO: work out a less noisy diff'ing technique!
      if [ -f "${CLOG}".prev -a -f "${CLOG}" ]; then
        echo -e "${SELF}: See diff against previous log file (might be long):" | tee -a ${CLOG}
	echo -e "diff -Naur ${CLOG}.prev ${CLOG} | less" | tee -a ${CLOG}
      fi
 fi
 
 #
 which tput >/dev/null 2>&1 && tput smso
 echo -e "${SELF}: Leaving the following cruft in ${GEN_DATA_DIR}:" | tee -a ${CLOG}
 which tput >/dev/null 2>&1 && tput rmso

 ls -lth "${GEN_DATA_DIR}" | tee -a ${CLOG}

 # Fin
 exit 0