#!/bin/sh
 
#    Mar 17/2000 - F.Majaess

#id  fxmslpr  - replaces calls to "mslpr.dk"/"mslpr2.dk" call by either 
#id             "mslpr_pres.dk" or "mslpr_eta.dk".

#    AUTHOR  - F. Majaess

#hd  PURPOSE - "fxmslpr" processes a user's job by replacing calls to "mslpr.dk"
#hd            or "mslpr2.dk", by the entry to "rplcby" parameter described below, 
#hd            which's limited to  "mslpr_pres" or "mslpr_eta".

#pr  PARAMETERS:
#pr 
#pr    POSITIONAL
#pr 
#pr      fn1 fn2 ... fnm = list of m files that require fixing
#pr
#pr    PRIMARY
#pr
#pr      rplcby = the replacement of "mslpr" in "mslpr.dk", or "mslpr2" 
#pr               in "mslpr2.dk"which is limited to "mslpr_pres" or 
#pr               "mslpr_eta". 
#pr               (=none).


#ex  EXAMPLE
#ex  
#ex   fxmslpr rplcby=mslpr_pres *
#ex  
#ex   The above example replaces ". mslpr.dk" (or ". mslpr2.dk") occurences 
#ex   by ". mslpr_pres.dk" in all files in the current directory. 

# Capture the arguments in "dirctv":

dirctv=${dirctv:=$*}

#  * Reset field separators

IFS=' '
export IFS

#  * Ensure "rplcby" has a value...

unset rplcby filst


arg_list=$@
for arg in $arg_list
do
  case $arg in
            -*) set $arg                     ;;
      rplcby=*) eval "$arg"                  ;;
             *) filst="$filst $arg"
  esac
done

#  * Exit if no value set for rplcby.

if [ -z "$rplcby" ] ; then
  echo "No value specified for rplcby!"
  exit 1
else
  if [ "$rplcby" != 'mslpr_pres' -a "$rplcby" != 'mslpr_eta' ] ; then
    echo "Sorry invalid entry rplcby=${rplcby} !"
  exit 2
  fi
fi

#  * Loop over all files in the input list.

for file in $filst
do

  # Ensure that only existing ascii text files are operated on.

  info=`file $file 2>/dev/null `
  file_status=`expr "$info" : '.*\(script\).*'`

  if [ ! -f "$file" ] ; then
    echo "file $file does not exist: resuming execution with next file in list"
  elif [ -z "$file_status" ] ; then
    echo "file $file is not an ascii text file: file skipped"
  else
    if [ "$rplcby" = 'mslpr_pres' ] ; then
      sed 's/^ *\.  *mslpr.dk/. mslpr_pres.dk/
           s/^ *\.  *mslpr2.dk/. mslpr_pres.dk/
          '  $file > tmpfile$$
    else
      sed 's/^ *\.  *mslpr.dk/. mslpr_eta.dk/
           s/^ *\.  *mslpr2.dk/. mslpr_eta.dk/
          '  $file > tmpfile$$
    fi
    mv tmpfile$$ $file
  fi 
done

exit
