#!/bin/bash
#<pre><b>
# Script     : myderchk
# Version    : 1.1
# Author     : Roland Rashleigh-Berry
# Date       : 11-Mar-2007
# Purpose    : Scans runderived.chk to show you details that belong to your
#              own program's log files (based on .sas file ownership)
# SubScripts : none
# Notes      : none
# Usage      : myderchk
#              myderchk -u rrash
#===============================================================================
# OPTIONS:
#-opt- -------------------------------description-------------------------------
#  u   (value)  User                                              
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
# N/A  Do not supply any parameters
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# rrb  11Mar07         Use "less" as the viewer
#===============================================================================
# This is public domain software. No guarantee as to suitability or accuracy is
# given or implied. User uses this code entirely at their own risk.
#===============================================================================

# Define give-usage-message-then-exit function
usage () {
  echo "Usage: myderchk" 1>&2
  exit 1
}


# Default values
valueu=$(whoami)                  # Default value for User                                              


# "case" statement for action to take on selected options
while getopts "u:" param ; do
  case $param in
   "?") # bad option supplied
        usage
        ;;
   "u") # (value) User
        valueu=$OPTARG
        ;;
  esac
done


# shift to bring first parameter to position 1
shift $(($OPTIND - 1))



# No parameters allowed
if [ $# -ne 0 ] ; then
  echo "Error: (myderchk) No parameters needed" 1>&2
  usage
fi


# Get the list of log files in runderived.chk owned by the user.
# (the ownership of the corresponding .sas file is inspected)
owner $(sed 's/\..*$/.sas/' runderived.chk | sort -u) | \
  grep ^$valueu | sed -e "s/^$valueu //" -e 's/\.sas$/.log/' | \
  sort > $HOME/myderchk.tmp


# sort runderived.chk ready for a join
sort runderived.chk > $HOME/runderived.tmp


# join the two files and save to a file
join $HOME/myderchk.tmp $HOME/runderived.tmp > $HOME/myderchk.tmp2


# delete the two files joined
rm -f $HOME/myderchk.tmp $HOME/runderived.tmp


# if the final file contains something then view it
if [ -s $HOME/myderchk.tmp2 ] ; then
  # view final file using "less"
  less $HOME/myderchk.tmp2
  rm -f $HOME/myderchk.tmp2
else
  echo "(myderchk) No matching lines found in \"runderived.chk\"" 1>&2
  rm -f $HOME/myderchk.tmp2
fi
