#!/bin/sh
#sl             Wrapper for showlock. Written by Jerry Rains 1/11/01
#               This program runs showlock for every file in $PFDATA$PFDIR
#               every 15 seconds.  Any locked records found will print out
#               the showlock data and in addition the ps -ef | grep PID 
#               for the process that has the record locked and the time it
#               was locked.  The date is printed out only once per day.
#               You can tee the output into a file to make a permanent 
#               record if you like.
#		02/20/01 - changed it to use fuser instead of ps -ef thanks
#		to Joe Chasan's persistence.  fuser will find locked keys
#		when  'ps -ef | grep clerk'  won't.
# 

# Determine form of echo to use.
if [ `echo "x\c"` = "x" ]
then
    # Then the standard command will work just fine.
    ECHO="echo"
else
    # Otherwise we need to enable the 'backslash' processing.
    ECHO="echo -e"
fi
export ECHO

[ $tmpdir ] || tmpdir=/tmp

# Use defaults if path vars not set.
dir=`pwd`
nix=`uname`
[ $PFPROG ] || PFPROG=`sed -n "1 p" /etc/default/fppath`;export PFDATA 
[ $PFDATA ] || PFDATA=`sed -n "2 p" /etc/default/fppath`;export PFDATA 
[ $PFDIR ] || PFDIR=`sed -n "3 p" /etc/default/fppath`;export PFDIR
type=`type showlock`
nf="not found"
case $type in
  *$nf) : nothing ;;
     *) instdir=`echo "$type" | sed -e "s,\(.* \)\(.*/\)\(.*\),\2,"`;;
esac
instdir=`dirname "$instdir"showlock`
[ -z "$instdir" ] && {
    if [ -f $dir/showlock ]
    then
        instdir=$dir
    elif [ -f $PFPROG/fp/showlock ]
    then
        instdir=$PFPROG/fp
    elif [ -f $PFPROG/fp/util/bin/showlock.$nix ]
    then
        cp -p $PFPROG/fp/util/bin/showlock.$nix $PFPROG/fp/showlock
        instdir=$PFPROG/fp
    else
        $ECHO
        $ECHO "'showlock' must be in the PATH."
        $ECHO
        read key
        exit 1
    fi
    }
$ECHO 
$ECHO "Monitoring locked records in $PFDATA$PFDIR/filepro."
$ECHO "There will be no output unless a record is locked."
this_date=`date "+%m/%d/%Y"`
date
cd $PFDATA$PFDIR/filepro
$ECHO "0" > $tmpdir/count
# 02/20/01 - Change to user fuser.  Build file.
$ECHO */key* | xargs ls > $tmpdir/j
chown filepro $tmpdir/j
key=n
while [ $key != x ]
do
  # If the date changes print it.
  [ "$this_date" = `date "+%m/%d/%Y"` ] || {
   this_date=`date "+%m/%d/%Y"`
    export this_date
    date
    }
  while read j
  do
    PID=`fuser $j* 2>/dev/null`
    [ -n "$PID" ] && {
      # Put the showlock output into a variable.
      tst=`showlock $j `
      # Get rid of hex 0a's.
      tst0=`$ECHO $tst | tr -d "\012"`
      # Parse the output into its 4 parts.
      tst1=`$ECHO "$tst0" | sed -e "s,\([^:]*:.\) *\([^ ]*\) *\([^ ]*\) *\(.*\),\1,"`
      tst2=`$ECHO "$tst0" | sed -e "s,\([^:]*:.\) *\([^ ]*\) *\([^ ]*\) *\(.*\),\2,"`
      tst3=`$ECHO "$tst0" | sed -e "s,\([^:]*:.\) *\([^ ]*\) *\([^ ]*\) *\(.*\),\3,"`
      tst4=`$ECHO "$tst0" | sed -e "s,\([^:]*:.\) *\([^ ]*\) *\([^ ]*\) *\(.*\),\4,"`
      # If there are no locked records then tst4 will have tst1 info in it.
      # Look for the : in the filename in tst1 and ignore if so.
      tst5=`$ECHO "$tst4" | grep :`
      [ -n "$tst5" ] && {
        tst4=""
        }
      # If tst4 is still valid then it is a valid PID, print the output.
      [ -n "$tst4" ] && {
        $ECHO "$PFDATA$PFDIR/filepro/$tst1 $tst2 $tst3 $tst4 - \c";date +%H:%M:%S
        ps -ef | grep $tst4 | sed "/grep/d" | awk ' { printf "%7s%6s%6s%3s%9s%7s%9s%14s%-11.11s",$1,$2,$3,$4,$5,$6,$7," "$8," "$9 }'
        $ECHO
        # Print 'date' every 24 lines.
        count=`cat $tmpdir/count`
        count=`expr $count + 1`
        $ECHO $count > $tmpdir/count
        [ `expr $count` = 12 ] && {
          count=0
          date
          }
        }
      }
  done < $tmpdir/j
  sleep 15
  $ECHO -n "."
done
