#!/bin/sh
if [ ! -r /usr/local/etc/openfwtk.conf ]; then
    echo "Error: /usr/local/etc/openfwtk.conf file is not exist or access to it is denied" 1>&2
    exit 1
fi
. /usr/local/etc/openfwtk.conf
if [ "$OFWTKPATH" = "" ]; then
    echo "Error: OFWTKPATH is empty" 1>&2
    exit 1
fi

AUTHSRV="$OFWTKPATH/bin/authsrv -s"
#AUTHSRV="authsrv -s"
while true; do
	echo -n "Enter Userid: "
	read userid || exit 1
	echo -n "Full Name: "
	read fullname || exit 1
	echo -n "Group: "
	read group || exit 1
	echo -n "Initial Password: "
	read pass || exit 1

	echo
	sure=0
	while [ $sure = 0 ]; do
		echo -n "Add user $user? (y/n)"
		read whyen || exit 1
		case $whyen in
			y*|Y*)
			(
				echo adduser $userid \"$fullname\"
				echo group $userid "$group"
				echo password $userid "$pass"
				echo enable $userid
				echo quit
			) | $AUTHSRV >/dev/null
			if [ $? -ne 0 ]; then
				echo "user not added correctly!"
			fi
			sure=1;
			;;
			n*|N*)
			sure=1;
			echo "User discarded"
			;;
			*)
			echo "Invalid response"
			;;
		esac
		echo -n "Add another user? (y/n)"
		read whyen || exit 1
		case $whyen in
			n*|N*)
			exit 0;
		esac
	done
done
