#!/bin/ksh
###
# Test for indoubt transactions, checking every 2 minutes.
# Trap to cleanup if user presses <ctrl-c>.
# Display to stdout and logfile if an indoubt transaction is found, then exit.

# Note that the connect iterates within the do loop, this is necessary to refresh the cache. 
# Substitute or export the $INSTANCE and $DBALIAS of your choice. 
# Alex Levy, Nov. 2002. Sustainable 	Software Ltd. http://www.sustainablesoftware.net
# With grateful thanks to James A'Court of IBM for the original stub.
###
#-----------------------------------------------------------
#
# syntax: test_indoubt.ksh dbalias
#
#-----------------------------------------------------------

function cleanup {
db2 "connect reset" > /dev/null
db2 "terminate"     > /dev/null
find . -name "list_indoubt_*.log" -mtime +14 -exec rm {} \; > /dev/null
exit
}

trap cleanup 2

if test $# = 0
   then echo "No parameters entered. Syntax is test_indoubt.ksh dbalias"
   exit 9
else
   print "Hit <ctrl-c> to end"
fi

dbalias=$1
logpath=.

#do forever
while [ 0 -eq 0 ];do

  logfile="list_indoubt_tran_$dbname_`date +'%y%m%d%H%M%S'.log"

  db2 connect to $dbalias > /dev/null

  db2 list indoubt transactions | grep SQL1251W > /dev/null
  rc=$?


  if [ $rc -eq 0 ]
    then echo "No indoubt transactions" `date`
    else echo `date`  > $logpath/$logfile
         echo "Indoubt transaction found for database $dbname" \
	             >> $logpath/$logfile
         db2 list indoubt transactions  >> $logpath/$logfile
	 echo "Process indoubt transaction using command:"     \
		     >> $logpath/$logfile
	 echo "db2 list indoubt transactions with prompting"   \
		     >> $logpath/$logfile
   echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++  INDOUBT TRANSACTIONS FOUND FOR $dbname"
	 echo "+++  CHECK LOGFILE $logpath/$logfile"      
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
	 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
   exit
  fi

  db2 terminate > /dev/null

  sleep 120
done
