#!/bin/bash -f

#  Remove the archive files FILE1,...,FILEn  from the archive ARC if files
#  are present

#  syntax rmpyarfiles ar-command grep-command arc-name file1 ... filen ,

#  where ar-command   is the appropriate "ar" command,
#        grep-command is the appropriate "grep" command,
#        arc-name     is the name of the archive, and
#        file1,...,filen-name are the names of the files to be removed

#  Nick Gould, for GALAHAD production
#  This version: 2023-05-11

AR=$1
GREP=$2
ARC=$3
END=$#
if [[ -e $ARC ]] ; then
 for i in $(seq 4 $END); do
   FILE=${!i}.o
#   echo " removing file $FILE"
   [[  `$AR -t $ARC | $GREP -w $FILE` == $FILE  ]] && $AR -d $ARC $FILE
 done
fi

exit 0
