#!/bin/sh
#
BASD=/usr/share/xtds/packages/C4vTDS
#
SCRD=$BASD/prog/exe
SCRATCH=/tmp/XASG_SCRATCH_$$
#
# extract and format assignments
# saved in ASG_EXP by xasg0
#
echo ""
echo "XASG   : "`date`
#
if [ $# -lt 4 ] ; then
  echo ""
  echo "!!! BAD NUMBER OF PARAMETERS"
  echo ""
  echo "            CALL : xasg 'string'  'files'              Pup Plo [MHz/GHz] [TEMP [pol [Rxxx]] [fpvib FPVIB] [abund ABUND]]"
  echo ""
  echo "        examples : xasg 'P0mP0'   '*'                  P0  P0"
  echo "                   xasg 'P1mP1'   'subdir/*'           P1  P1     296"
  echo "                   xasg 'P1mP0'   'KPNO* subdir/KPNO*' P1  P0 MHz"
  echo ""
  exit 1
fi
# list of files containing the string
echo " XASG   -> files containing : <$1>"
FICHIERS=`grep -l "$1" $2 /dev/null`
if [ -f $SCRATCH ] ; then rm $SCRATCH ; fi
touch $SCRATCH
# for each file
# extract all lines containing the string in one file
if [ "$FICHIERS" != "" ] ; then
  for i in $FICHIERS ; do
    echo " <  "$i
    grep "$1" $i >>$SCRATCH
  done
  CHAINE="$1"
  shift
  shift
echo  $SCRD/passx xasg0 "$CHAINE" $*
# call xasg0 to format lines
  $SCRD/passx xasg0 "$CHAINE" $* <$SCRATCH
fi
# delete temporary file
if [ -f $SCRATCH ] ; then rm $SCRATCH ; fi
echo ""
