#!/bin/sh
#      *************************************************************
#      *                                                           *
#      *  Script to Search the IRIS Development Tree Source Files  *
#      *                                                           *
#      *************************************************************
# utils/misc/iris_grep
#
#                     COPYRIGHT (c) 1998, 2002 BY
#         SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
# 
# THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
# ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
# INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
# COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
# OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
# TRANSFERED.

TMP1=/tmp/$$_tmp1 ; rm -f ${TMP1}
trap "rm -f ${TMP1}" INT EXIT

GREPARGS="-E" ; ALLFILES="false" ; HELP="false"

while [ ! "$1" = "" ] ; do
  if [ "$1" = "-all" ] ; then
    ALLFILES="true"
  elif [ "$1" = "-help" -o  "$1" = "-?" ] ; then
    HELP="true"
  else
    GREPARGS="${GREPARGS} '$1'"
  fi
  shift
done

if [ "${HELP}" = "true" ] ; then
  echo ""
  echo "'iris_grep' performs a pattern search on IRIS/RDA source files."
  echo "All command line args are passed through to 'grep' except for:"
  echo ""
  echo "      -all : Search scripts and other obscure sources"
  echo "  -? -help : This list of options"
  echo ""
  echo "Note: to actually search for one of these special strings please"
  echo "      use something like: iris_grep '-e -help'"
  exit
fi
if [ "${GREPARGS}" = "" ] ; then exit ; fi

alias XARGSGREP="xargs grep ${GREPARGS}"

# If the current directory ends in 'sigmet', then just search from here.
# Otherwise try home directory option and /usr standard root points,
# lastly IRIS_ROOT.
#
if [ ".${PWD##*/}" != ".sigmet" ] ; then
  if   [ -r    ~/sigmet         ] ; then cd    ~/sigmet ;
  elif [ -r /usr/sigmet         ] ; then cd /usr/sigmet ;
  elif [ ".${IRIS_ROOT}" != "." ] ; then cd ${IRIS_ROOT}
  fi
  echo "Searching in ${PWD}..." 1>&2
fi

# Make up the directory list from all the ones that are sensible to
# search and which are present on this machine.
#
DIRLIST=""
for DIR in config_template dpolapp include iris libs local rda ts utils web ; do
  if [ -d ${DIR} ] ; then DIRLIST="${DIRLIST} ${DIR}" ; fi
done

# Search within files whose names we recognize, as well as those that
# appear to contain shellscript text.
#
find ${DIRLIST} \
  "("  -name "*.[vcCh]"       \
    -o -name "*.vh"           \
    -o -name "*.rf"           \
    -o -name "*.op"           \
    -o -name "*.prj"          \
    -o -name "Makefile"       \
    -o -name "messages.msg"   \
  ")" -print >> ${TMP1}

if [ "${ALLFILES}" = "true" ] ; then
  find ${DIRLIST} -perm -110 -type f -print | \
    xargs file | grep text | sed 's/:.*//' >> ${TMP1}
fi

cat ${TMP1} | sort -u | XARGSGREP
