#!/bin/sh
#
BASD=/usr/share/xtds/packages/STDS
#
SCRD=$BASD/prog/exe
SCRATCH=/tmp/EXASG__$$
#
echo ""
echo "EXASG  : "`date`
if [ $# -eq 0 ] ; then
  echo ""
  echo "-> list of the files (of the current directory and his subdirectories)"
  echo "   containing the specified string."
  echo ""
  echo "            CALL : exasg 'string'"
  echo ""
  echo "-> extraction of the records containing 'string' (30 characters)"
  echo "   from all the specified files."
  echo "   Ordered records are saved in ASG_EXP (input file of eq_int or eq_tds)."
  echo ""
  echo "             CALL : exasg 'string'     'files'"
  echo ""
  echo "         examples : exasg 'P1mP0'   '*'"
  echo "                    exasg 'P1mP0'   'subdir/*'"
  echo "                    exasg 'P1mP0'   'KPNO* subdir/KPNO*'"
  echo ""
  exit 1
#
elif [ $# -eq 1 ] ; then
  echo " EXASG  -> files containing : <$1>"
  echo ""
  FICHIERS=`find . -type f -print`
  grep -l "$1" $FICHIERS /dev/null
  echo ""
  exit 0
else
  if [ -f ASG_EXP ] ; then
    \rm ASG_EXP
  fi
  echo " EXASG  -> files containing : <$1>"
  FICHIERS=`grep -l "$1" $2 /dev/null`
  for i in $FICHIERS
  do
    echo " <  "$i
  done
  echo " >> ASG_EXP"
  if [ "$FICHIERS" = "" ] ; then
    touch ASG_EXP
  else
    if [ -f $SCRATCH ] ; then rm $SCRATCH ; fi
    for i in $FICHIERS
    do
      grep "$1" $i >>$SCRATCH
    done
    $SCRD/passx triasg "$1" $SCRATCH
    <$SCRATCH cut -c 1-72 >ASG_EXP
  fi
  echo ""
fi
if [ -f $SCRATCH ] ; then rm $SCRATCH ; fi
