#!/bin/sh

UID=240
GID=${UID}
DAEMON_NAME="aprsd"
UID_NAME=${DAEMON_NAME}
GID_NAME=${DAEMON_NAME}

TNC_UID=241
TNC_GID=${TNC_UID}
TNC_UID_NAME="tnc"
TNC_GID_NAME="tnc"

if [ "x$2" != "xPRE-INSTALL" ]; then
	exit 0;
fi

ask() {
	local question default answer

	question=$1
	default=$2
	if [ -z "${PACKAGE_BUILDING}" ]; then
		read -p "${question} [${default}]? " answer
	fi
	if [ x${answer} = x ]; then
		answer=${default}
	fi
	echo ${answer}
}

yesno() {
	local dflt question answer

	question=$1
	dflt=$2
	while :; do
		answer=$(ask "${question}" "${dflt}")
		case "${answer}" in
		[Yy]*)          return 0;;
		[Nn]*)          return 1;;
		esac
		echo "Please answer yes or no."
	done
}

adduser() {
	local uid gid uid_name gid_name daemon_name

	uid=$1
	gid=$2
	uid_name=$3
	gid_name=$4
	daemon_name=$5
	if which -s pw ; then
		:
	else
		cat <<EOF
Your system does not include the "pw" utility.  You should upgrade
to a newer version of FreeBSD.  Without "pw" this script will not
run.
EOF
		exit 1
	fi

	echo ""
	if pw groupshow ${gid_name} 2> /dev/null ; then
		echo "You already have a group \"${gid_name}\", so I will use it."
	else
		if pw groupshow ${gid} 2> /dev/null ; then
			echo "You already have a gid \"${gid}\".  Please create a user ${gid_name}"
			echo "with a default group of \"${gid_name}\"."
			exit 1
		fi
		echo "You need a group \"${gid_name}\"."
		if which -s pw && yesno "Would you like me to create it" y; then
		    pw groupadd ${gid_name} -g ${gid} || exit
		    echo "Done."
		else
			echo "Please create it, and try again."
			if ! pw usershow ${uid_name} 2> /dev/null ; then
			    echo "While you're at it, please create a user \"${uid_name}\""
				echo 'too, with a default group of "${gid_name}".'
			fi
			exit 1
		fi
	fi

	if pw usershow ${uid_name} 2> /dev/null ; then
		echo "You already have a user \"${uid_name}\", so I will use it."
	else
	        if pw usershow ${uid} 2> /dev/null ; then
        	        echo "You already have a uid \"${uid}\".  Please create a user \"${uid_name}\""
                	echo "with a default group of \"${gid_name}\"."
	                exit 1
		fi
		echo "You need a user \"${uid_name}\"."
		if which -s pw && yesno "Would you like me to create it" y; then
		    pw useradd ${uid_name} -g ${gid_name} -u ${uid} -h - -d /nonexistent \
				-s /bin/sh -c "${daemon_name}" || exit
			echo "Done."
		else
			echo "Please create it, and try again."
			exit 1
		fi
	fi
}

(adduser ${UID} ${GID} ${UID_NAME} ${GID_NAME} ${DAEMON_NAME})

echo `pw groupshow dialer`|grep -q ${UID_NAME}
if [ $? -eq 0 ]; then
	echo "You already have \"${UID_NAME}\" in the dialer group, so I will use this."
else
cat<<EOF
${DAEMON_NAME} needs access to serial ports to talk to an
external tnc. You will be asked if you wish to add ${DAEMON_NAME}
to the dialer group for that purpose.
EOF
	if pw usershow ${UID_NAME} 2> /dev/null ; then
		if which -s pw && yesno "Would you like to add \"${UID_NAME}\" to the dialer group?" y; then
			pw groupmod -n "dialer" -m ${UID_NAME} || exit
			echo "Done."
		fi
	fi
fi

if ! pw usershow ${TNC_UID_NAME} 2> /dev/null ; then
cat<<EOF
"${DAEMON_NAME}" has an optional login "${TNC_UID_NAME}",
(This is described in the aprsd docs.) if you
wish to allow remote logins. "${DAEMON_NAME}" needs to run as
root for this to work, if you do not plan to run
"${DAEMON_NAME}" ever as root, you can skip this step.
You can always add it manually later if you change your mind.
EOF
        if which -s pw && yesno "Would you like to add a ${TNC_UID_NAME} login" y; then
		(adduser ${TNC_UID} ${TNC_GID} ${TNC_UID_NAME} ${TNC_GID_NAME} ${DAEMON_NAME})
        fi
else
	echo "You already have an user \"${TNC_UID_NAME}\" so I will use it."
fi
