# /etc/Addusr shell script
# Copyright 1986 L/F Technologies, Carson City, Nevada
# Author: Rodger Lakey
# File Creation Date: Dec 10, 1986 by RJL
# Last Revision Date: Apr 16, 1987 by ACW
#
# Modification History:
# 	1.0 - Dec 10, 1986 - Original creation by RJL
#	1.1 - Apr 16, 1987 - Modified grep for other login names to ensure that
#		only one name would be matched. Added sort function to order
#		/etc/passwd according to user id number. Added mutual exclusion
#		on /etc/passwd.
#
PASSFILE=/etc/passwd
GROUPFILE=/etc/group
echo "Add User Program"
echo "Version 1.1"
echo
while :
do
   echo "Enter new user's login name: \c"
   read LOGNAME
   if [ "$LOGNAME" = "" ]
   then 
      continue
   fi
   EXISTS=`grep -c "^$LOGNAME:" /etc/passwd`
   if [ "$EXISTS" = "0" ]
   then
      echo ""
      break
   else
      echo "That name is already in use."
      echo "Please try another."
      echo ""
   fi
done

while :
do
   echo "Enter new user's full name: \c"
   read FULLNAME
   if [ "$FULLNAME" = "" ]
   then
      continue
   fi
   echo ""
   break
done

while :
do
   echo "Enter new user's home directory as a complete pathname: \c"
   read HOMEDIR
   if [ "$HOMEDIR" = "" ]
   then
      continue
   fi
   if [ -x $HOMEDIR ]
   then
      echo ""
      echo "That directory already exists."
      echo "Its ownership and permissions will not be changed."
      echo "Is this correct? (y or n) \c"
      read ANS
      if [ "$ANS" = "y" ]
      then
         break
      fi
   elif [ ! -x `dirname $HOMEDIR` ]
   then
      echo "The parent directory of $HOMEDIR does not exist."
      echo "Please select a different pathname."
      continue
   else
      break
   fi
   echo ""
done

while :
do
   read NEXTID < /etc/.nextid
   echo "The next available userid is " $NEXTID
   echo "Is this ok? (y or n) \c"
   read ANS
   if [ "$ANS" = "y" ]
   then
      break
   fi
   if [ "$ANS" = "n" ]
   then
      break
   fi
done

if [ "$ANS" = "y" ]
then
   UID=$NEXTID
   NEXTID=`awk '{ printf("%d",$1+1) }' /etc/.nextid`
   echo $NEXTID > /etc/.nextid
else
   echo ""
   echo "Enter the userid you wish to assign to this user: \c"
   read UID
fi

while :
do
   EXISTS=`awk -F: '{ if ( $3 == x ) printf("%s ",$1) }' x=$UID /etc/passwd`
   if [ "$EXISTS" != "" ]
   then
      echo "That userid is already in use by user(s)" $EXISTS.
      echo "Is this correct? (y or n) \c"
      read ANS
      if [ "$ANS" = "y" ]
      then
         break
      fi
      if [ "$ANS" = "n" ]
      then
         echo "Enter the userid you wish to assign to this user: \c"
         read UID
      fi
   else
      break
   fi
done

while :
do
   echo ""
   echo "The default group name is 'user'. If this is correct press"
   echo "<cr>. If not enter the group name."
   echo "Enter the group name you wish to assign this user to: \c"
   read GNAME
   if [ "$GNAME" = "" ]
   then
      GNAME=user
   fi
   GID=`grep $GNAME /etc/group | awk -F: '{ printf("%d",$3) }'`
   if [ "$GID" = "" ]
   then
      while :
      do
         echo "That group does not exist."
         echo "Do you wish to add it? (y or n) \c"
         read ANS
         if [ "$ANS" = "y" ]
         then
            echo "What id number do you wish to assign the group? \c"
            read GID
            GRPNAME=`grep $GID /etc/group | awk -F: '{ printf("%s",$1) }'`
            if [ "$GRPNAME" != "" ]
            then
               echo "That group id number already belongs to"
               echo "group" $GRPNAME.
               echo "Please either use a different group name or" $GRPNAME.
               break
            else
               echo $GNAME::$GID >> $GROUPFILE
               break 2
            fi
         fi
         if [ "$ANS" = "n" ]
         then
            break
         fi
      done
   else
      break
   fi
done

while :
do
   echo ""
   echo "The default shell program is '/bin/sh'. If this is correct press"
   echo "<cr>. If not enter the shell program name."
   echo "Enter the shell program you wish to assign to this user: \c"
   read LSHELL
   if [ "$LSHELL" = "" ]
   then
      LSHELL=/bin/sh
   fi
   if [ -x $LSHELL ]
   then
      break
   else
      echo "That program does not exist. You must select"
      echo "an existing program."
   fi
done

while :
do
  if mkdir /etc/ptmp 	# fails if passwd file in use
  then
    cp /etc/passwd /etc/opasswd
    chmod 444 /etc/opasswd
    chmod 600 /etc/passwd
    echo $LOGNAME::$UID:$GID:$FULLNAME:$HOMEDIR:$LSHELL |
	sort -t: +2n /etc/opasswd - > /etc/passwd
    chmod 444 /etc/passwd
    rmdir /etc/ptmp
    break
  else
    echo "/etc/passwd is currently in use."
    echo "If you wish to try adding the new user's account again,"
    echo "please type "y", otherwise type "n" and rerun this script later."
    read ANSWER
    if [ "$ANSWER" = "y" ]
    then
      continue
    else
      echo "The new account has not been added."
      exit 1
    fi
  fi
done

if [ ! -x $HOMEDIR ]
then
   mkdir $HOMEDIR
   chown $UID $HOMEDIR
   chgrp $GID $HOMEDIR
   chmod 755 $HOMEDIR
fi

if [ ! -x $HOMEDIR/.profile ]
then
   cp /etc/basicprofile $HOMEDIR/.profile
   chown $UID $HOMEDIR/.profile
   chgrp $GID $HOMEDIR/.profile
   chmod 755 $HOMEDIR/.profile
fi

echo "Done."
