#
#  deluser    (4/2/88)
#-----------------------------------------------------------------------------
#  Deluser: delete a user entry from the password file.
#
#  NOTE:  This script will only work for single line entries in the password
#    file.  If you use the multi-line feature, which is unique to UX-DOS,
#    this script will not find the second line and will therefore run
#    incorrectly.  It is suggested, therefore, that you do not use the
#    multi-line feature of the password file if you plan to maintain that
#    file with adduser, chnguser, and deluser.  Adduser will set up single
#    line entries that will work with deluser without any problems
#
#  Start by making sure everything is set up right.
#-----------------------------------------------------------------------------
: ${PASSWD:=\\etc\\passwd}                       # make sure $PASSWD is set
if [ ! -f $PASSWD ]                              # look for password file
   then                                          # handle errors if not found
   echo "$PASSWD not found."
   exit 1
fi

clear                                            # clear the screen
echo "DELETE A USER DESCRIPTION FROM THE PASSWORD FILE:
"
case "$1" in                                     # look for name as argument
   ?*) USER=$1                                   # if present
       Echo "Deleting $USER.";;                  # set USER from it
    *) echo "Enter the name of the user to delete (no entry to quit)"
       echo -n "        :"                       # otherwise ask for user name
       read USER;;                               # and read USER from terminal
esac

case "$USER" in                                  # check for null entry
   "") echo "No user name entered, processing terminated."
       exit 1;;                                  # otherwise quit
esac

trap 'rm -fs ${TEMPDIR}temp[12]' 0               # set trap to remove files
grep "^${USER}:" $PASSWD > ${TEMPDIR}temp1       # look for existing user
case $? in                                       # and extract its description
   1) echo "$USER is not currently in $PASSWD, processing terminated."
      exit 1;;                                   # handle not found error
   2) echo "Cannot search $PASSWD for $USER, processing terminated."
      exit 1;;                                   # handle grep error
esac
#-----------------------------------------------------------------------------
#  Found an existing user in the password file.  Now use sed to change all
#  empty fields (::) into dummy fields (:~:) and then translate all quoting
#  characters (usually in encrypted passwords) to harmless characters (.) so
#  we can read them with the shell read command.  To do this, save the old
#  $IFS value, change $IFS to ":", read the reformatted password line
#  breaking words on the colon characters (:), and then restore the old
#  $IFS value.
#-----------------------------------------------------------------------------
sed -e 's/::/:~:/g' < ${TEMPDIR}temp1 > ${TEMPDIR}temp2
tr "\042\047\140" "..." < ${TEMPDIR}temp2 > ${TEMPDIR}temp1
OLDIFS="$IFS"                                    # save old $IFS
IFS=":"                                          # break "words" on :
read OLDUSER PWORD OLDCOMMENT OLDHOME OLDSUFFIX OLDCMD < "${TEMPDIR}temp1"
IFS="$OLDIFS"                                    # restore old $IFS
#-----------------------------------------------------------------------------
#  Now look at each word read.  If it is the dummy value (~), change it back
#  to an empty field.
#-----------------------------------------------------------------------------
for X in "OLDUSER" "PWORD" "OLDCOMMENT" "OLDHOME" "OLDSUFFIX" "OLDCMD"
   do                                            # if the value of a word is
   eval VALUE="\$$X"                             # the dummy value (~),
   case $VALUE in                                # change that variable to a
     ~) $X=;;                                    # null value
   esac                                          # otherwise leave it alone
done
#-----------------------------------------------------------------------------
#  Now we have our existing login entry.  If it has a password set, we cannot
#  delete the entry and must end.
#-----------------------------------------------------------------------------
case $PWORD in                                   # look for non-empty passwd
   ?*) echo "$USER is currently password protected.  Change the password to a
null entry, and then re-run this script (deluser).  If you do not know the
password, you cannot delete the user description.

Processing terminated."                          # print message
       exit 1;;                                  # and end
esac
#-----------------------------------------------------------------------------
#  Now we have an existing login entry we can delete.  Use the yesno script
#  to see if it is the proper one.  If so, remove it from the password file.
#  Note that we will not remove the old password file, but will leave it on
#  the disk as passwd.old.
#-----------------------------------------------------------------------------
                                                 # user we can delete
                                                 # show current descr
echo "Current user description:

        User login name:      $USER
        Descriptive comment:  $OLDCOMMENT
        Home directory:       $OLDHOME
        Path string suffix:   $OLDSUFFIX
        First command:        $OLDCMD
"
if sh yesno "Delete $USER"                       # ask if really want deletion
  then                                           # if okay, save the current
  mv -f $PASSWD ${PASSWD}.old                    # password file
  grep -v "^${USER}:" ${PASSWD}.old > $PASSWD    # make new one (w/o user)
  echo "$USER has been deleted."                 # report deletion
  exit 0                                         # then end
else                                             # if input is not okay
  echo "$USER not deleted."                      # do not do deletion
  exit 1
fi
