:
#!/bin/sh
#
# Filename:  meshfilt
#
# Usage:
# 	meshfilt {-d | -f <Error_File> | -l} [-h]
#
# Description:
#   This utility is provided as a way to 'filter' the sometimes voluminous
#   error and warnings reported by the mesh tests that run under PSD. 
#   For further information about this utility consult the meshfilt man
#   page on the Diagnostics Station.
#
# Modification History:
#
# $Log: meshfilt,v $
# Revision 1.2  1995/03/28  00:16:08  chowell
# chowell - added version string
#
# Revision 1.1  1994/11/09  23:31:08  chowell
# Initial revision
#
#
# Initial revision:
#
#   09-Nov-94      Chuck Howell   Initial version
#
#######################################################################
#
# What string:
#
# @(#) meshfilt DIAG_REL_2.0 Thu Jan  5 12:04:09 PST 1995
#
##############################################################################

TRUE=1
FALSE=0

D=${FALSE}
F=${FALSE}
F_OPT=""
L=${FALSE}

def_dpath=/u/paragon/diag
def_psd=psd.def
errlog=meshck.err
#error_out=meshfilt.diag
in_file=""
log_psd=psd.log
mesh_awk=meshfilt.awk

#
# usage()
#
usage()
{
   echo ""
   echo "$0 {-d | -f Error_File | -l} [-h]"
   echo ""
   echo "   -d              specifies /u/paragon/diag/psd.def to parse for errors"
   echo "   -f Error_File   specifies the file to parse for errors"
   echo "   -h              to get this usage message"
   echo "   -l              specifies /u/paragon/diag/psd.log to parse for errors"
   exit 1
}

#
# cleanup()
#
cleanup()
{
   echo "$0: aborting..."
}

########
# main #
########

# trap for a h/w or s/w interrupt
#
trap 'cleanup' 2 15

# Check for valid arguments
#
if [ $# = 0 ] ; then
   usage
fi

while getopts df:lh c
do
   case ${c} in
      d)
          D=${TRUE};
          in_file=$def_dpath/$def_psd;;
      f)
          F=${TRUE};
          F_OPT=${OPTARG};
          in_file=${F_OPT};;
      h)
          usage;;
      l)
          L=${TRUE};
          in_file=${def_dpath}/${log_psd};;
      ?)
          usage;;
   esac
done

# Cannot have any combinations of the switches -d, -f, and -l
#
if [ \( ${D} -eq ${TRUE} \) -a \( ${F} -eq ${TRUE} \) ] ; then
      echo "$0: invalid combination of switches -- d f"
      usage
fi

if [ \( ${D} -eq ${TRUE} \) -a \( ${L} -eq ${TRUE} \) ] ; then
      echo "$0: invalid combination of switches -- d l"
      usage
fi

if [ \( ${F} -eq ${TRUE} \) -a \( ${L} -eq ${TRUE} \) ] ; then
      echo "$0: invalid combination of switches -- f l"
      usage
fi

# Check for existance of files and directories
#
if [ \( ${D} -eq ${TRUE} \) -o \( ${L} -eq ${TRUE} \) ] ; then
   if [ ! -d ${def_dpath} ] ; then
      echo "$0: the directory ${def_dpath} does not exist"
      exit 1
   fi
fi

if [ ! -f ${mesh_awk} ] ; then
   echo "$0: the file ./${mesh_awk} is missing"
   exit
fi

if [ ! -f ${in_file} ] ; then
   echo "$0: the file ${in_file} is missing"
   exit 1
fi

# Check to see if the file is a text file
#
txt=`file ${in_file} | grep -c text`

if [ ${txt} -eq 0 ] ; then
   echo "$0: the input file ${in_file} is not a text file"
   exit 1
fi

# Now process the error information into to include the meaningful output
#
#echo "awk -f ./${mesh_awk} errfile=${errlog} ${in_file} > ${error_out}"

awk -f ./${mesh_awk} errfile=${errlog} ${in_file}

exit 0
