#! /bin/sh
# set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2020.  All Rights Reserved.
#
# Name - printlogs.sh
#
# Purpose - Script to print tranlog data.  
#           See "help" text for details.
#
# $Id$
#
#=========================================================================

usage() {
  cat <<EOF
====================================================================

Usage:
`basename $0` [-h]|[<filters>][<keywords>][<tlogA>..<tlogZ>]

Purpose

  To print out contents of designated tranlogs in current directory.

  WARNING: This can produce vast amounts of output!!

  Make sure \$GEMSTONE is set.

Options

  -h (or no arguments): 

      print this usage message and exit.

  Filters: 

      Only print records that match any of the filtering criteria.
      One or more of the following pairs:

      user      username  - filter by GS UserProfile name 
      host      hostname  - filter by gem/topaz process host 
      client    X.X.X.X   - filter by client IP Address 
      euid      <integer> - filter by gem's effective UNIX user ID
      ruid      <integer> - filter by gem's real UNIX user ID
      luid      <integer> - filter by gem's login UNIX user ID
      euidstr   <string>  - filter by gem's effective UNIX user name
      ruidstr   <string>  - filter by gem's real UNIX user name
      luidstr   <string>  - filter by gem's login UNIX user name
      gempid    <integer> - filter by gem's process ID
      sessionid <integer> - filter by gem's session ID

  Keywords: 
      full - More detailed logs are produced (VERY LARGE!).

      all  - Print out contents of all tranlogs in this directory.
           You must list specific tranlogs on the command line or
           give the keyword 'all'.  Tranlogs are assumed to be named 
           'tranlogXXX.dbf'.  Override by setting \$GS_TRANLOG_PREFIX 
           to the prefered value.

      nostrings  - Suppress the printing of String data in the output.
                   Useful for security when sharing tranlog output.

      nouserinfo - Suppress the printout of user information.

  WARNING: a maximum of 256 tranlogs can be analyzed in one set.

====================================================================
EOF
}

# check for -h or no args; print usage and exit
if [ "x$1" = "x" ] || [ "$1" = "-h" ]; then
  usage
  exit 0
fi 

# check that $GEMSTONE is set
if [ "x$GEMSTONE" = "x" ]; then
  echo "ERROR:  \$GEMSTONE not set" 1>&2
  exit 1
fi

# determine tranlog naming convention
if [ "x$GS_TRANLOG_PREFIX" = "x" ]; then
  GS_TRANLOG_PREFIX="tranlog"
fi

# debug
# echo "Using tranlog prefix $GS_TRANLOG_PREFIX"

# Check for $GEMSTONE/sys/pgsvrslow
if [ -f $GEMSTONE/sys/pgsvrslow ]; then
  pgsvrExe=$GEMSTONE/sys/pgsvrslow
else
  echo "ERROR:  \$GEMSTONE/sys/pgsvrslow not found" 1>&2
  exit 1;
fi

# debug
# echo "Using $pgsvrExe"

# initialize variables
filters=""
pFilter=""
tranlogs=""
dumpCmd="dumplog"
tmpfile="/tmp/gsprintlog$$"

# debug
# echo "tmpfile = $tmpfile"

# create temporary file to hold pgsvr commands
touch $tmpfile
echo "quiet " >>$tmpfile
echo "printusernames " >>$tmpfile

# loop over arguments..
while [ "x$1" != "x" ] 
do

# check for filtering keywords

if [ $1 = "user" ]; then
  echo "'$2' username" >>$tmpfile
  filters="$filters user $2"
  pFilter="$pFilter user $2"
  shift
  shift

elif [ $1 = "host" ]; then
  echo "'$2' gemhost" >>$tmpfile
  filters="$filters host $2"
  pFilter="$pFilter host $2"
  shift
  shift

elif [ $1 = "client" ]; then
  echo "'$2' clientip" >>$tmpfile
  filters="$filters client $2"
  pFilter="$pFilter client $2"
  shift
  shift

elif [ $1 = "euid" ]; then
  echo "$2 findeuid" >>$tmpfile
  filters="$filters findeuid $2"
  pFilter="$pFilter euid $2"
  shift
  shift

elif [ $1 = "ruid" ]; then
  echo "$2 findruid" >>$tmpfile
  filters="$filters findruid $2"
  pFilter="$pFilter ruid $2"
  shift
  shift

elif [ $1 = "luid" ]; then
  echo "$2 findluid" >>$tmpfile
  filters="$filters findluid $2"
  pFilter="$pFilter luid $2"
  shift
  shift

elif [ $1 = "euidstr" ]; then
  echo "'$2' findeuidstr" >>$tmpfile
  filters="$filters findeuidstr $2"
  pFilter="$pFilter euidstr $2"
  shift
  shift

elif [ $1 = "ruidstr" ]; then
  echo "'$2' findruidstr" >>$tmpfile
  filters="$filters findruidstr $2"
  pFilter="$pFilter ruidstr $2"
  shift
  shift


elif [ $1 = "luidstr" ]; then
  echo "'$2' findluidstr" >>$tmpfile
  filters="$filters findluidstr $2"
  pFilter="$pFilter luidstr $2"
  shift
  shift

elif [ $1 = "gempid" ]; then
  echo "$2 findgempid" >>$tmpfile
  filters="$filters findgempid $2"
  pFilter="$pFilter gempid $2"
  shift
  shift

elif [ $1 = "sessionid" ]; then
  echo "$2 findsessionid" >>$tmpfile
  filters="$filters findsessionid $2"
  pFilter="$pFilter sessionid $2"
  shift
  shift

# check for keyword full
elif [ $1 = "full" ]; then
  dumpCmd=dumplogfull
  shift

# check for keyword all
elif [ $1 = "all" ]; then
  tranlogs=`ls -1rt *$GS_TRANLOG_PREFIX*.dbf`
  shift

# check for keyword nostrings
elif [ $1 = "nostrings" ]; then
  echo "nostrings" >>$tmpfile
  filters="$filters nostrings"
  shift

# check for keyword nouserinfo
elif [ $1 = "nouserinfo" ]; then
  echo "noprintusername " >>$tmpfile
  shift

# check if it's a tranlog name
elif [ -r $1 ]; then
  tranlogs="$tranlogs $1"
  shift

# otherwise don't know what it is
else
  echo "Unrecognized keyword $1"
  shift
fi

# done with argument processing loop..
done


# construct pgsvr commands
for each in $tranlogs
do
  echo "'$each' openlog 0 100000000 $dumpCmd closedbf" >>$tmpfile
done

# debug: print out contents of temporary file
# echo "PGSVRSLOW command file contents:"
# cat $tmpfile
# exit

# print summary info
echo "Tranlog Analysis Print Log"
echo
echo "Printing out tranlogs:"
echo
for each in $tranlogs
do
  echo "  $each"
done
echo
if [ "$filters" != "" ]; then
  echo "Filtering on: $pFilter"
fi

# now execute..
$pgsvrExe < $tmpfile
pStatus=$?
if [ $pStatus -ne 0 ]; then
  echo "pgsvr returned $pStatus";
  exit $pStatus
fi

# delete temporary file
rm -f $tmpfile

#
# end of script


