#! /bin/sh
 
#    Oct 07/91 - F.Majaess
 
#id  edfil   - Used to list/change password occurences in a file.
 
#    AUTHOR  - F.Majaess
 
#hd  PURPOSE - "edfil" script is used to list/change password occurences
#hd            if the new password "newpw" is same_as/different_from the
#hd            old password "oldpw".
 
#pr  PARAMETERS:
#pr
#pr    POSITIONAL     
#pr
#pr      pathfln = path/fln to be processed.
#pr      oldpw   = user's account old password.
#pr      newpw   = user's account new password.
#pr      oldapw  = acrnall account old password.
#pr      newapw  = acrnall account new password.
#pr
#pr
#ex  EXAMPLE: 
#ex
#ex    edfil $HOME/.cshrc_usr oldpw=xxx newpw=yyy oldapw=aaa newapw=bbb
 
#   * code used to set switches and deal with parameter=value arguments 
#   * as well as setting parameters to their primary/secondary defaults.
#   * The list of other arguments (if any) is returned in "prmtrl" var...
 
. $SUBPROC/check_set_swtches_prmtrs
  
#   * The following code deals with checking/setting positional 
#   * parameters ...
#   * Note: script will abort if too many arguments are specified.
 
 if [ -n "$prmtrl" ] ; then
  i=1
  for prmtr in $prmtrl
  do
   case $i in
     1) pathfln=$prmtr;;
     2) oldpw=$prmtr;;
     3) newpw=$prmtr;;
     4) oldapw=$prmtr;;
     5) newapw=$prmtr;;
     *) eval "echo '$0 : too many parameters !' "; exit;;
   esac
   i=`expr $i + 1`
 done
 fi
 
#   * Prompt for a positional parameter value(s) if none was/were
#   * specified...
 
while [ -z "$pathfln" ] ; do
  echo "please enter (path)/file name to be processsed ? > \\c"
  read pathfln
done
 
while [ -z "$oldpw" ] ; do
  echo "please enter account old password ? > \\c"
  read oldpw
done
 
while [ -z "$newpw" ] ; do
  echo "please enter account new password ? > \\c"
  read newpw
done
 
while [ -z "$oldapw" ] ; do
  echo "please enter acrnall account old password ? > \\c"
  read oldapw
done
 
while [ -z "$newapw" ] ; do
  echo "please enter acrnall account new password ? > \\c"
  read newapw
done

# * if file is a text file then
#      setup needed parameters which accomodate upper/lower case password
#      occurences ...
# * otherwise display an error message and quit. 
 
info=`file $pathfln 2>/dev/null `
infox=`expr "$info" : '.*\(data.*\).*'`
if [ "$infox" != 'data or International Language text' ] ; then
 file_status=`expr "$info" : '.*\(text\).*'`
 file_status=${file_status:=`expr "$info" : '.*\(character\).*'`}
fi

if [ -n "$file_status" ] ; then 
  eval "echo edfil processing $pathfln file :"
  loldpw=`echo $oldpw | tr '[A-Z]' '[a-z]' `
  Uoldpw=`echo $oldpw | tr '[a-z]' '[A-Z]' `
  loldapw=`echo $oldapw | tr '[A-Z]' '[a-z]' `
  Uoldapw=`echo $oldapw | tr '[a-z]' '[A-Z]' `
  if [ "$oldpw" != "$newpw" ] ; then
     eval "echo changing $oldpw,$loldpw,$Uoldpw to $newpw "
     eval "echo and      $oldapw,$loldapw,$Uoldapw to $newapw "
     noprtn="s/$oldpw/$newpw/gp" 
     loprtn="s/$loldpw/$newpw/gp" 
     Uoprtn="s/$Uoldpw/$newpw/gp" 
     naoprtn="s/$oldapw/$newapw/gp" 
     laoprtn="s/$loldapw/$newapw/gp" 
     Uaoprtn="s/$Uoldapw/$newapw/gp" 
  else
     eval "echo listing all occurences of : "
     eval "echo $oldpw,$loldpw,$Uoldpw,$oldapw,$loldapw,$Uoldapw "
     noprtn="p"
     loprtn="p" 
     Uoprtn="p" 
     naoprtn="p" 
     laoprtn="p" 
     Uaoprtn="p" 
  fi
else
  eval "echo $pathfln file is not a valid text file !"
fi
 
#   ****   Task of the script...   ****
 
# cat << woof
ed -s $pathfln << woof
g/$oldpw/$noprtn
g/$loldpw/$loprtn
g/$Uoldpw/$Uoprtn
g/$oldapw/$naoprtn
g/$loldapw/$laoprtn
g/$Uoldapw/$Uaoprtn
w
q
woof
