#!/bin/sh
#
# $FreeBSD: ports/www/squid31/files/pkg-install.in,v 1.6 2010/05/02 10:52:12 miwi Exp $
#

PATH=/bin:/usr/bin:/usr/sbin
pkgname=$1
squid_base="/var/squid"
squid_cachedir="${squid_base}/cache"
squid_confdir="${PKG_PREFIX:-/usr/local}/etc/squid"
squid_logdir="/var/log/squid"
squid_rundir="/var/run/squid"
if [ -x /usr/sbin/nologin ]; then
	nologin=/usr/sbin/nologin
else
	nologin=/sbin/nologin
fi
squid_user="squid"
squid_group="squid"
squid_gid=100
squid_uid=100
case $2 in
PRE-INSTALL)
	echo "===> Pre-installation configuration for ${pkgname}"
	if ! pw groupshow ${squid_group} -q >/dev/null ; then
		echo "There is no group '${squid_group}' on this system, so I will try to create it (using group id ${squid_gid}):"
		if ! pw groupadd ${squid_group} -g ${squid_gid} -q ; then
			echo "Failed to create group \"${squid_group}\"!" >&2
			echo "Please create it manually." >&2
			exit 1
		else
			echo "Group '${squid_group}' created successfully:"
		fi
	else
		echo "I will use the existing group '${squid_group}':"
	fi
	pw groupshow ${squid_group}

	if ! pw usershow ${squid_user} -q >/dev/null ; then
		echo "There is no account '${squid_user}' on this system, so I will try to create it (using user id ${squid_uid}):"
		if ! pw useradd -q -n ${squid_user} \
		    -u ${squid_uid} -g ${squid_group} \
	  	    -c "Squid caching-proxy pseudo user" \
		    -d "${squid_base}" -s "${nologin}" \
		    -h - ; then
			echo "Failed to create user '${squid_user}'!" >&2
			echo "Please create it manually." >&2
			exit 1
		else
			echo "User '${squid_user}' created successfully:"
		fi
	else
		echo "I will use the existing user '${squid_user}':"
	fi
	pw usershow ${squid_user}
	# Since we usually start the Squid master process as ${squid_user}
	# instead of root make sure that ${squid_base} is writable for it.
	if [ ! -d ${squid_base} ]; then
		echo "Creating ${squid_base}..."
		install -d -o root -g ${squid_group} \
		    -m 0775 ${squid_base}
	else
		chgrp ${squid_group} ${squid_base}
		chmod g+w ${squid_base}
	fi
	if [ ! -d ${squid_cachedir} ]; then
		echo "Creating ${squid_cachedir} ..."
		install -d -o ${squid_user} -g ${squid_group} \
		    -m 0750 ${squid_cachedir}
	fi
	if [ ! -d ${squid_confdir} ]; then
		echo "Creating ${squid_confdir}..."
		install -d -o root -g ${squid_group} \
		    -m 0755 ${squid_confdir}
	fi
	if [ ! -d ${squid_logdir} ]; then
		echo "Creating ${squid_logdir}..."
		install -d -o ${squid_user} -g ${squid_group} \
		    -m 0750 ${squid_logdir}
	fi
	if [ ! -d ${squid_rundir} ]; then
		echo "Creating ${squid_rundir}..."
		install -d -o ${squid_user} -g ${squid_group} \
		    -m 0755 ${squid_rundir}
	fi
	;;
POST-INSTALL)
	for file in cachemgr.conf errorpage.css mime.conf msntauth.conf squid.conf; do
		if [ ! -f ${squid_confdir}/${file} \
		    -a -f ${squid_confdir}/${file}.default ]; then
			echo "Creating ${file} from default..."
			install -c -o root -g ${squid_group} -m 0640 \
		    	    ${squid_confdir}/${file}.default \
			    ${squid_confdir}/${file}
		fi
	done
	;;
*)
	exit 64
	;;
esac
exit 0
