#!/bin/sh
#
# $FreeBSD: 
#

PATH=/bin:/usr/bin:/usr/sbin
pkgname=$1                  
c_icap_base="${PKG_PREFIX:-/usr/local}/c-icap"

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

c_icap_user="_c_icap"
c_icap_group="netpolice"
c_icap_gid=1344
c_icap_uid=1344

LOG_DIR=/var/log/c-icap
TMP_DIR=/var/cache/c-icap
RUN_DIR=/var/run/c-icap
NETPOLICELOG_DIR=/var/log/netpolice


if [ "$2" = "PRE-INSTALL" ]; then
    if ! pw groupshow ${c_icap_group} -q >/dev/null ; then
	echo "there is no group '${c_icap_group}' on this system, so i will try to create it (using group id ${c_icap_gid}):"
	if ! pw groupadd ${c_icap_group} -g ${c_icap_gid} -q ; then
		echo "failed to create group \"${c_icap_group}\"!" >&2
		echo "please create it manually." >&2
		exit 1
	else
		echo "group '${c_icap_group}' created successfully:"
	fi
    else
	echo "i will use the existing group '${c_icap_group}':"
    fi
    pw groupshow ${c_icap_group}
    if ! pw usershow ${c_icap_user} -q >/dev/null ; then
	echo "there is no account '${c_icap_user}' on this system, so i will try to create it (using user id ${c_icap_uid}):"
	if ! pw useradd -q -n ${c_icap_user} \
	-u ${c_icap_uid} -g ${c_icap_group} \
	-c "_c_icap  pseudo user" \
	-d "${c_icap_base}" -s "${nologin}" \
	-h - ; then
		echo "failed to create user '${c_icap_user}'!" >&2
		echo "please create it manually." >&2
		exit 1
	else
		echo "user '${c_icap_user}' created successfully:"
	fi
    else
	echo "i will use the existing user '${c_icap_user}':"
    fi
    pw usershow ${c_icap_user}
elif [ "$2" = "POST-INSTALL" ]; then
    if [ ! -d "$LOG_DIR" ]; then
	install -d -o ${c_icap_user} -g ${c_icap_group} "$LOG_DIR"
    fi
    if [ ! -d "$TMP_DIR" ]; then
	install -d -o ${c_icap_user} -g ${c_icap_group} "$TMP_DIR"
    fi
    if [ ! -d "$RUN_DIR" ]; then
	install -d -o ${c_icap_user} -g ${c_icap_group} "$RUN_DIR"
    fi
    if [ ! -d "$NETPOLICELOG_DIR" ]; then
	install -d -o ${c_icap_user} -g ${c_icap_group} "$NETPOLICELOG_DIR"
    fi
fi

exit 0

