#!/bin/sh
 
#   May 25/05 - F.Majaess (Revised to use "-L" instead of parsing "file" command output)

#    Dec 06/04 - F.Majaess

#id  ffxdiag  - Changes "oldiag=..." occurences in each targeted file to "oldiag=diag4".

#    AUTHOR  - F. Majaess

#hd  PURPOSE - "ffxdiag" script is used to change any "oldiag" settings to
#hd            target "diag4" library.
#hd            Note: Only active lines are checked (ie. commented lines skipped).
#hd                  The target file(s) must maps into "text" and/or "script"
#hd                  file type upon invoking "file" command.
#hd                  The original date/time stamp is preserved in any massaged file.
#hd

#pr  PARAMETERS:
#pr 
#pr    POSITIONAL
#pr 
#pr      fn1 fn2 ... fnm = list of m files that require fixing.

#ex  EXAMPLE
#ex  
#ex   ffxdiag $HOME/jobs/myjob
#ex  
#ex   The above example changes "oldiag" setting (if present) in
#ex   $HOME/jobs/myjob to target "diag4".
#ex  

# set -x
if [ "$OS" != 'AIX' -a "$HOSTHW" != 'SX-6' ] ; then
 DiffSwtch='-q'
else
 unset DiffSwtch
fi
TmStmp=`date '+%Y%j%H%M%S'`


#  * Loop over all files in the input list.

for file
do
  if [ -d "${file}/." ] ; then
   # echo "ffxdiag - ${file} subdirectory skipped since only file(s) can be processed!"
   :
  else
   # echo "ffxdiag - processing file $file"
   # Ensure that only existing ascii text files are operated on.
 
   info=`file $file 2>/dev/null `
   file_statush=`expr "$info" : '.*\( commands text\).*'`
   file_statush=${file_statush:-`expr "$info" : '.*\( script\).*'`}
   TmPlate=`cat $file | egrep '^#MARKER:' | tail -1 | sed -e 's/^#MARKER:.*$/ script/'`
   file_statush=${file_statush:-$TmPlate}
   file_status=`echo $file_statush | sed -n -e 's/^.*text.*$/text/gp'`
   file_status=${file_status:-`echo $file_statush | sed -n -e 's/^.*script.*$/text/gp'`}
   # file_status=`echo $file_status | sed -n -e '/^text$/p'`
 
   if [ ! -f "$file" ] ; then
     echo "ffxdiag - file $file does not exist: resuming execution with next file in list"
   elif [ -L "$file" ] ; then
     echo "ffxdiag - Link $file is skipped"
   elif [ -z "$file_status" -o \( -n "$file_status" -a "$file_status" != 'text' \) ] ; then
     echo "ffxdiag - file $file is not a suitable file for massaging: file skipped"
   else
     umask 077
     chmod u+w $file
     Tfile=`basename $file`
     Tpath=`dirname $file`
     TmpDir="$HOME/tmp/tmp_ffxdiag_dir"
     if [ "$Tpath" = "$TmpDir" ] ; then
      echo "ffxdiag - $file skipped since it resides in $TmpDir "
     else 
      (mkdir $TmpDir || : )  > /dev/null 2>/dev/null
      (\rm -f $TmpDir/${Tfile}_${TmStmp}_0 $TmpDir/${Tfile}_${TmStmp}_1 || : ) > /dev/null 2>/dev/null
      cp -p $file $TmpDir/${Tfile}_${TmStmp}_0
      touch $TmpDir/${Tfile}_${TmStmp}_1
      chmod go-rwx $TmpDir/${Tfile}_${TmStmp}_1
  
      cat ${file} | sed '/^ *#/b
      /oldiag=/{
       s/diag4t/diag4/g
       s/diag3a/diag4/g
       s/newdiag3/diag4/g
       }
       ' | sed '/^ *#/b
       /oldiag=/{
       s/diag3/diag4/g
       }
       ' > $TmpDir/${Tfile}_${TmStmp}_1
  
      diff $DiffSwtch $file $TmpDir/${Tfile}_${TmStmp}_1 > /dev/null && \rm -f $TmpDir/${Tfile}_${TmStmp}_0  || ( \cp $TmpDir/${Tfile}_${TmStmp}_1 $file ; touch -r $TmpDir/${Tfile}_${TmStmp}_0 $file ; echo "\nffxdiag - +++> $file <+++ MASSAGED\n" || : ) || : 
      \rm $TmpDir/${Tfile}_${TmStmp}_1 
      # (\rmdir $TmpDir || : ) > /dev/null 2>/dev/null
     fi
   fi 
  fi
done

exit
