#!/bin/sh
# $OpenBSD: INSTALL,v 1.1.1.1 2004/05/13 06:52:36 grange Exp $
#
# Pre/post-installation setup of cnupm

# exit on errors, use a sane path and install prefix
#
set -e
PATH=/bin:/usr/bin:/sbin:/usr/sbin
PREFIX=${PKG_PREFIX:-/usr/local}
CNUPM_USER=_cnupm
CNUPM_GROUP=_cnupm
CNUPM_ID=531
CNUPM_DIR=/var/cnupm

do_usergroup_install()
{
	# Create cnupm user and group
	if groupinfo -e ${CNUPM_GROUP}; then
		echo "===> Using '${CNUPM_GROUP}' group for $1"
	else
		echo "===> Creating ${CNUPM_GROUP} group for $1"
		groupadd -g ${CNUPM_ID} ${CNUPM_GROUP}
	fi
	if userinfo -e ${CNUPM_USER}; then
		echo "===> Using '${CNUPM_USER}' user for $1"
	else
		echo "===> Creating '${CNUPM_USER}' user for $1"
		useradd -u ${CNUPM_ID} -g ${CNUPM_GROUP} -d ${CNUPM_DIR} -m \
		    -c "cnupm traffic collector" \
		    -s /sbin/nologin ${CNUPM_USER}
	fi
}

# verify proper execution
#
if [ $# -ne 2 ]; then
	echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
	exit 1
fi

# Verify/process the command
#
case $2 in
	PRE-INSTALL)
		do_usergroup_install $1
		;;
	POST-INSTALL)
		install -o ${CNUPM_USER} -g ${CNUPM_GROUP} -m 750 -d \
		    ${CNUPM_DIR}
		;;
	*)
		echo "usage: $0 distname { PRE-INSTALL | POST-INSTALL }" >&2
		exit 1
		;;
esac

exit 0
