#!/bin/sh -ex


pre_install()
{
	# create users if needed
	USERLIST="
		user_tracing:1900
		user_agent:1901
	"
	HD="/nonexistent"

	# create group if needed
	GRP="group_tracing"
	GID=1900


	if ! getent group $GRP >/dev/null; then
		while getent group $GID >/dev/null; do
			GID=$(($GID + 1))
		done
		groupadd -g $GID $GRP
	else
		echo "$GRP group already exists"
	fi

	for USER in $USERLIST; do
		UserID=${USER#*:}
		USER=${USER%:*}
		if ! getent passwd $USER >/dev/null; then
			while getent passwd $UserID >/dev/null; do
				UserID=$(($UserID + 1))
			done
			useradd --uid $UserID --gid $GID --home $HD --shell /bin/false $USER
		else
			echo "$USER user already exists"
		fi
	done
}


case "$1" in
	install|upgrade)
		pre_install || true
		;;

	abort-upgrade)
		;;

	*)
		echo "preinst was called with unknown argument '$1'" >&2
		exit 1
		;;
esac
