#!/bin/sh
#-----------------------------------------------------------------------------
# squiz: Search for and examine AIPS++ sources
#-----------------------------------------------------------------------------
#
#   Copyright (C) 1994,1995,1996
#   Associated Universities, Inc. Washington DC, USA.
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   Correspondence concerning AIPS++ should be addressed as follows:
#          Internet email: casa-feedback@nrao.edu.
#          Postal address: AIPS++ Project Office
#                          National Radio Astronomy Observatory
#                          520 Edgemont Road
#                          Charlottesville, VA 22903-2475 USA
#
#-----------------------------------------------------------------------------
# Usage: squiz [-c <category>] [-e|-E <expression>] [-F] [-p <package>] [-l]
#              [file]
#-----------------------------------------------------------------------------
# squiz searches for AIPS++ source files and optionally searches for strings
# inside the files it finds.  Wildcarding is allowed in the file specification
# and the search is case insensitive by default.  If no file is specified the
# default is to search for (i.e. list) all files.
#
# Options:
#   -c <category>
#          Restrict the search to a certain category: ftn, imp, & tst.
#          The default is to search all categories.
#   -e <expression>
#          Perform a case-insensitive search for the specified expression in
#          all selected files.  grep-type regular expressions are allowed.
#   -E <expression>
#          Case-sensitive version of the -e option.
#   -F     Do a case-sensitive search on the file specification rather than
#          the default case-insensitive search.
#   -l     List filenames that contain a pattern match; this is less verbose
#          than the default behaviour.
#   -p <package>
#          Restrict the search to a particular package.  The default is to
#          search all packages.
#
# Status returns:
#    0:  success
#    1:  initialization error
#
# Notes:
#    1)
#
# Original: 1994/07/28 by Mark Calabretta, ATNF
# $Id$
#=============================================================================
# Fundamentals.
  if [ "$CASAPATH" = "" ]
  then
     echo "ai: CASAPATH is not defined, abort!" 1>&2
     exit 1
  fi

  AIPSROOT=`echo $CASAPATH | awk '{ print $1 }'`
  cd $AIPSROOT/code 


# Parse arguments.
  CATEGORY=""
  EXPRESSION=""
  FILE=""
  FILECASE=0
  LISTMATCH=0
  PACKAGE="*"

  while [ "$#" -gt 0 ]
  do
     case $1 in
     -c*)
        case $1 in
        -c)
           shift
           CATEGORY="$1"
           ;;
        *)
           CATEGORY=`echo $1 | sed -e 's/^-c//'`
           ;;
        esac
        ;;
     -e* | -E*)
        case $1 in
        -e*)
           GREP="grep -i"
           ;;
        -E*)
           GREP="grep"
           ;;
        esac

        case $1 in
           -?)
              shift
              EXPRESSION="$1"
              ;;
           *)
              EXPRESSION=`echo $1 | sed -e 's/^-.//'`
              ;;
        esac
        ;;
     -F)
        FILECASE=1
        ;;
     -l)
        LISTMATCH=1
        ;;
     -p*)
        case $1 in
        -p)
           shift
           PACKAGE="$1"
           ;;
        *)
           PACKAGE=`echo $1 | sed -e 's/^-p//'`
           ;;
        esac

        PACKAGE=`echo $PACKAGE | sed -e 's/::*/ /g'`
        ;;
     -*)
        exec 1>&2
        echo ""
        echo "Usage: squiz [-c <category>] [-e|-E <expression>] [-F] [-l]" \
             "[-p <package>] [file]"
        exit 1
        ;;
     *)
        [ "$FILE" = "" ] && FILE="$1"
        ;;
     esac

     [ "$2" = "" ] && break

     shift
  done

# Any files specified?
  [ "$FILE" = "" ] && FILE="*"

# Was a case-sensitive search requested?
  if [ "$FILECASE" = 0 ]
  then
     iFILE="$FILE"
     IFILE=`echo "$iFILE" | tr '[a-z][A-Z]' '[A-Z][a-z]'`

     J=0
     FILE=""
     while true
     do
        J=`expr $J + 1`

        Ci=`echo "$iFILE" | cut -c $J`
        [ "$Ci" = "" ] && break
        CI=`echo "$IFILE" | cut -c $J`

        if [ "$CI" = "$Ci" ]
        then
           FILE=$FILE$Ci
        else
           FILE=$FILE[$Ci$CI]
        fi
     done
  fi

# Check the package.
  if [ "$PACKAGE" != "*" ]
  then
     if [ ! -d "$PACKAGE" ]
     then
        exec 1>&2
        echo "squiz: \"$PACKAGE\" is not a recognized AIPS++ package, abort!"
        exit 1
     fi
  fi


# Search by category?
  case $CATEGORY in
  "")
#    No restriction by category.
     ;;
  app*)
     PACKAGE="$PACKAGE/apps"
     ;;
  ftn | for*)
     PACKAGE="$PACKAGE/fortran"
     ;;
  imp*)
     PACKAGE="$PACKAGE/implement"
     ;;
  tst | test)
     PACKAGE="$PACKAGE/test"
     ;;
  *)
     exec 1>&2
     echo "squiz: \"$CATEGORY\" is not a recognized AIPS++ category, abort!"
     exit 1
     ;;
  esac

# List filenames only?
  ACTION="-print"
  if [ $LISTMATCH -eq 1 ] ; then
     if [ "$EXPRESSION" != "" ] ; then
	GREP="$GREP -l"
	ACTION=""
     fi
  fi

# Do it.
  if [ "$EXPRESSION" = "" ]
  then
     find $PACKAGE -type f -name "$FILE" $ACTION
  else
     find $PACKAGE -type f -name "$FILE" $ACTION \
        -exec $GREP "$EXPRESSION" {} \;
  fi


  exit 0
