#!/bin/sh
#
# $FreeBSD: ports/net-mgmt/flow-tools/files/pkg-install.in,v 1.2 2007/04/10 19:32:35 stas Exp $
#

PATH=/bin:/usr/sbin:/usr/bin:/usr/sbin

FLOW_CAPTURE_SPOOL="%%FLOW_CAPTURE_SPOOL%%"
FLOW_CAPTURE_USER="%%FLOW_CAPTURE_USER%%"
FLOW_CAPTURE_GROUP="%%FLOW_CAPTURE_GROUP%%"
FLOW_CAPTURE_UID="%%FLOW_CAPTURE_UID%%"
FLOW_CAPTURE_GID="%%FLOW_CAPTURE_GID%%"
FLOW_CAPTURE_PIDDIR="%%FLOW_CAPTURE_PIDDIR%%"

if [ -x /usr/sbin/nologin ]; then
	NOLOGIN=/usr/sbin/nologin
else
	NOLOGIN=/sbin/nologin
fi

pre-install() {
#
# Create group
#
	pw groupshow ${FLOW_CAPTURE_GROUP} > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo -n "Creating '${FLOW_CAPTURE_GROUP}' group... "
		pw groupadd -n ${FLOW_CAPTURE_GROUP} -g ${FLOW_CAPTURE_GID} -q

		if  [ $? -eq 0 ]; then
			echo "ok"
		else
			echo "failed"
			echo "Cannot create group '${FLOW_CAPTURE_GROUP}'" >&2
			echo "Please, create it manually." >&2
			exit 1
		fi
	else
		echo "Using existing group '${FLOW_CAPTURE_GROUP}'"
	fi

#
# Create user
#
	pw usershow ${FLOW_CAPTURE_USER} > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo -n "Creating '${FLOW_CAPTURE_USER}' user... "
		pw useradd -n ${FLOW_CAPTURE_USER} -g ${FLOW_CAPTURE_GROUP} \
			-u ${FLOW_CAPTURE_UID} \
			-c "Flow-tools collector pseudo-user" \
			-d "/var/empty" -s "${NOLOGIN}" -h - -q

		if  [ $? -eq 0 ]; then
			echo "ok"
		else
			echo "failed"
			echo "Cannot create user '${FLOW_CAPTURE_USER}'" >&2
			echo "Please, create it manually." >&2
			exit 1
		fi
	else
		echo "Using existing group '${FLOW_CAPTURE_USER}'"
	fi
}

post-install() {
	/usr/bin/install -d -m 700 -o ${FLOW_CAPTURE_USER} \
		-g ${FLOW_CAPTURE_GROUP} ${FLOW_CAPTURE_PIDDIR}
	/usr/bin/install -d -m 1700 -o ${FLOW_CAPTURE_USER} \
		-g ${FLOW_CAPTURE_GROUP} ${FLOW_CAPTURE_SPOOL}
}


case $2 in
	PRE-INSTALL)
		pre-install
		;;
	POST-INSTALL)
		post-install
		;;
esac
