# Script file to enable db2trc and run node.js script to collect 
# db2trace files to diagnose the issue and make sure setup is proper.
# This script is only for non-Windows platform.

testfile="$1"
if [ $# -eq 0 ]; then
  echo "test file to execute is missing. Usage: trace test.js"
  exit 1;
fi

if [ "$IBM_DB_HOME" == "" ]
then
  IBM_DB_HOME=`pwd`/../installer/clidriver
fi
OS=`uname`

export PATH=$IBM_DB_HOME/bin:$IBM_DB_HOME/adm:$IBM_DB_HOME/lib:$PATH
if [ "$OS" == "Darwin" ]
then
  export DYLD_LIBRARY_PATH=$IBM_DB_HOME/lib:$DYLD_LIBRARY_PATH
else
  export LD_LIBRARY_PATH=$IBM_DB_HOME/lib:$LD_LIBRARY_PATH
fi

rm -rf 1.trc 1.flw 1.fmt 1.fmtc 1.cli
if [ "$OS" == "Darwin" ]
then
  db2trc on -l 2m
else
  db2trc on -f 1.trc
fi
sleep 5

node $testfile
# You can use either above db2cli command or below node command to run .js file.
# Keep only one and comment other. Better to use above validate command first.
#node ../defect/t.js

if [ "$OS" == "Darwin" ]
then
db2trc dump 1.trc
fi
db2trc off
db2trc flw -t 1.trc 1.flw
db2trc fmt 1.trc 1.fmt
db2trc fmt -c 1.trc 1.fmtc
db2trc fmt -cli 1.trc 1.cli

echo "Generated trace files:"
echo "`ls 1.*`"

