#!/bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/ucb:/etc:/usr/etc:/usr/etc/install
export PATH

lowest_uid=500
highest_uid=699

new_users_dir=/home/u2

add_user()
{
 echo "Trying to add new user:   $2"
 echo "                 shell:   $3"
 echo "                  GCOS:   $4\\n"

 nismatch $2 passwd.org_dir
 if [ $? -eq 0 ]; then
	echo User already exists.
	return 1
 fi

 if [ -d /users/$2 ]; then
	echo /users/$2 already exists.
	return 1
 fi

 echo Searching for an uid...\\c
 uid=`niscat passwd.org_dir | nawk -F: \
      '{ if( $3 <= '$highest_uid' && last < $3 ) last = $3 } \
        END { if(last != '$highest_uid' ) print last; else print "uid_slut" }'`
 if [ $uid = "uid_slut" ]; then
	echo \\nNo uid found.
	return 1
 elif [ $uid -lt $lowest_uid ]; then
	uid=$lowest_uid
 else
	uid=`expr $uid + 1`
 fi
 echo OK.

 echo User will be assigned uid $uid.\\n

 expire=`date "+(%Y-1970)*366+(%m-1)*30+%d+365" | bc`

 echo Adding user to NIS+ table...\\c
 nistbladm -a -D access=o=r,gwn= name="$2" passwd="" uid=$uid gid=30 \
              gcos="$4" home="/users/$2" shell="/bin/true" \
              shadow="8600:0:-1:30:-1:$expire:0" passwd.org_dir
 echo OK.\\n

 echo Creating home-directory...
 cp -ri /home/u1/new-user $new_users_dir/$2
 chmod -R go-w $new_users_dir/$2
 chown -hR $2 $new_users_dir/$2
 chgrp -R lysator $new_users_dir/$2
 ln -s $new_users_dir/$2 /users/$2

 dom=`domainname`

 echo Adding local credentials...
 nisaddcred -P $2.$dom -p $uid local

 echo "Adding DES credentials..."
 nisaddcred -P $2.$dom -p unix.${uid}@lysator.liu.se -l "" des

 echo \\nPress RETURN at old-password-prompt.
 nispasswd $2

 echo Setting shell=$3...
 nistbladm -m shell="$3" "[name=$2],passwd.org_dir"

 echo Setting owner of passwd-entry to $2...
 nischown $2 "[name=$2],passwd.org_dir"

 echo \\nAll done.
}


rm_user()
{
 homedir=`nismatch $2 passwd.org_dir | cut -d: -f6`
 if [ -z "$homedir" ]; then
	echo "User ($2) not found."
	return 1
 fi

 echo "Are you sure that you want to remove $2. (y/n) \\c"
 read answer

 if [ "$answer" != "y" -a "$answer" != "yes" ]; then
	echo Aborting.
	return 1
 fi

 for dir in $homedir /users/$2 /home/u1/$2 /home/u2/$2 \
            /home/u3/$2 ; do
	if [ -d $dir -o -h $dir ]; then
		echo "$dir found. Remove it? (y/n) \\c"
		read answer
		if [ "$answer" = "y" -o "$answer" = "yes" ]; then
			echo Removing $dir...
			rm -rf $dir
		fi
	fi
 done

 if [ -f /usr/spool/mail/$2 ]; then
	rm -i /usr/spool/mail/$2
 fi

 dom=`domainname`

 echo \\nRemoving credentials...
 nisaddcred -r $2.$dom

 echo Removing $2 from passwd-table...
 nistbladm -r name=$2 passwd.org_dir

 echo \\nAll done.
}


disable_user()
{
 nismatch $2 passwd.org_dir > /dev/null
 if [ $? -ne 0 ]; then
	echo User not found.
	return 1
 fi

 echo "Are you sure that you want to disable $2. (y/n) \\c"
 read answer

 if [ "$answer" != "y" -a "$answer" != "yes" ]; then
	echo Aborting.
	return 1
 fi

 echo Setting passwd to "*"...
 nistbladm -m passwd="*" "[name=$2],passwd.org_dir"

 dom=`domainname`

 echo Removing credentials...
 nisaddcred -r $2.$dom

 echo \\nAll done.
}


enable_user()
{

 shell=`nismatch "[name=$2],passwd.org_dir" | cut -f7 -d:`
 if [ -z "$shell" ]; then
	echo "User ($2) does not exist."
	return 1
 fi

 expire=`date "+(%Y-1970)*366+(%m-1)*30+%d+365" | bc`

 echo Clearing old passwd, setting expire, setting shell=/bin/true...
 nistbladm -m shell="/bin/true" passwd="" shadow="8600:0:-1:30:-1:$expire:0" \
              "[name=$2],passwd.org_dir"

 dom=`domainname`
 uid=`nismatch "[name=$2],passwd.org_dir" | cut -f3 -d:`

 echo Adding local credentials...
 nisaddcred -P $2.$dom -p $uid local

 echo "Adding DES credentials..."
 nisaddcred -P $2.$dom -p unix.${uid}@lysator.liu.se -l "" des

 echo \\nPress RETURN at old-password-prompt.
 nispasswd $2

 echo Setting shell=$shell...
 nistbladm -m shell="$shell" "[name=$2],passwd.org_dir"

 echo \\nAll done.
}


update_expire()
{
 nismatch $2 passwd.org_dir > /dev/null
 if [ $? -ne 0 ]; then
	echo User not found.
	return 1
 fi

 expire=`date "+(%Y-1970)*366+(%m-1)*30+%d+365" | bc`

 echo Setting expire to one year from now...
 nistbladm -m shadow="8600:0:-1:30:-1:$expire:0" "[name=$2],passwd.org_dir"

 echo \\nAll done.
}


call_func()
{
 argno=1
 arg1=
 arg2=
 arg3=
 arg4=

 function=$1
 shift 1

 while [ $# -ne 0 ]; do
	echo $1 \\c
	read arg$argno
	argno=`expr $argno + 1`
	shift 1
 done

 $function -foo "$arg1" "$arg2" "$arg3" "$arg4"
}


print_help_text()
{
 echo "These options are valid:"
 echo "               add user:        -a user shell GCOS"
 echo "               remove user:     -r user"
 echo "               disable user:    -d user"
 echo "               enable user:     -e user"
 echo "               update expire:   -x user"
 echo "               show this text:  -h"
 echo
 echo "Interactive mode is entered if no options are given."
}


print_interactive_help_text()
{
 echo
 echo "Useradm menu:"
 echo
 echo "     a  Add user"
 echo "     r  Remove user"
 echo "     d  Disable user (passwd=\"*\")"
 echo "     e  Enable user"
 echo "     x  Update expire (one year from now)"
 echo "     h  Display help for non-interactive use"
 echo "     ?  Display this help"
 echo "     q  Quit"
}


interactive()
{
 while true; do

  echo "\\nYour choice: \\c"
  read choice
  case "$choice" in
	a)
	  echo Add user
	  call_func add_user "Username of new user: " "Shell: " "GCOS: ";;
	r)
	  echo Remove user
	  call_func rm_user "Username: ";;
	d)
	  echo Disable user
	  call_func disable_user "Username: ";;
	e)
	  echo Enable user
	  call_func enable_user "Username: ";;
	x)
	  echo Update expire
	  call_func update_expire "Username: ";;
	h)
	  print_help_text;;
	"?")
	  print_interactive_help_text;;
	q)
	  exit 0;;
  esac

 done
}

#
# MAIN program
#

if [ $# -eq 0 ]; then
	print_interactive_help_text
	interactive
	exit 0
fi

case "$1" in
	-a)
	   if [ $# -ne 4 ]; then
		echo `basename $0` -a user shell GCOS
		exit 1
	   fi
	   add_user "$@"
	   exit;;

	-r)
	   if [ $# -ne 2 ]; then
		echo `basename $0` -r user
		exit 1
	   fi
	   rm_user "$@"
	   exit;;

	-d)
	   if [ $# -ne 2 ]; then
		echo `basename $0` -d user
		exit 1
	   fi
	   disable_user "$@"
	   exit;;

	-e)
	   if [ $# -ne 2 ]; then
		echo `basename $0` -e user
		exit 1
	   fi
	   enable_user "$@"
	   exit;;

	-x)
	   if [ $# -ne 2 ]; then
		echo `basename $0` -x user
		exit 1
	   fi
	   update_expire "$@"
	   exit;;
esac

print_help_text
exit 1