#!/usr/bin/sh
# --------------------------------------------------------------------
# This script is used to install USER 'user' and GROUP 'group'
# and ensure that the user has access to the callout devices
# through supplementary membership to the 'uucp' group
# --------------------------------------------------------------------

USER=sms
GROUP=sms

# --------------------------------------------------------------------
# User uucp traditionally has read/write permissions to callout
# devices such as modems.
# --------------------------------------------------------------------

uucp="uucp"

# --------------------------------------------------------------------
# Add Group if it does not already exist
# --------------------------------------------------------------------

echo "----------------------------------------"
echo "Checking for group '$GROUP'..."

group=`awk -F: "\\$1 == \"$GROUP\" { print \\$1 }" /etc/group`
if [ "X-$group" = "X-" ] 
then
	echo "Group '$GROUP' NOT Found."
	echo "Do you whish to add group '$GROUP' [y/n] ?"

	read ans
	case $ans in
	y*|Y*)
		echo "Adding new group '$GROUP'..."

		/usr/sbin/groupadd $GROUP 2>/dev/null
		if [ $? -ne 0 ]
		then
			echo "Error: Could not add group '$GROUP'"
			exit 1
		else
			echo "Added group '$GROUP'"
		fi

		;;
	*)
		echo "Skipping..."
		;;
	esac
else
	echo "Group '$GROUP' Found"
fi


# --------------------------------------------------------------------
# Add User if it does not already exist
# --------------------------------------------------------------------

echo "----------------------------------------"
echo "Checking for user '$USER'..."

user=`awk -F: "\\$1 == \"$USER\" { print \\$1 }" /etc/passwd`
if [ "X-$user" = "X-" ] 
then
	echo "User '$USER' NOT Found."
	echo "Do you whish to add user '$USER' [y/n] ?"

	read ans
	case $ans in
	y*|Y*)
		echo "Adding new user '$USER'..."

		while :
		do
			nuucp=`awk -F: "\\$1 == \"$uucp\" { print \\$1 }" /etc/group`
			if [ "X-$nuucp" = "X-" ] 
			then
				echo 
				echo "Warning: Cannot find group '$uucp'"
				echo 

				if [ "X-$uucp" = "X-uucp" ] 
				then 
					echo "Traditionally the group 'uucp' is given"
					echo "read/write access to callout devices such as modems"
					echo 
				fi

				echo "If your system has another group that"
				echo "has read/write access to callout devices such"
				echo "as modems please enter it's name:"

				read uucp
			else
				break
			fi
		done

		/usr/sbin/useradd -g $GROUP -G $uucp $USER 2>/dev/null
		if [ $? -ne 0 ]
		then
			echo "Error: Could not add user '$USER'"
			exit 1
		else
			echo "Added user '$USER'"
			echo 
			echo "NOTE - User '$USER' was given supplementary membership to"
			echo "       group '$uucp'. This has been done to allow read/write"
			echo "       access to callout devices such as modems."
		fi

		;;
	*)
		echo "Skipping..."
		;;
	esac
else
	echo "User '$USER' Found"
fi

# --------------------------------------------------------------------

echo "----------------------------------------"
echo "Done."

exit 0
