#!/bin/ksh
#
# start of header
#
# $NetBSD: header,v 1.17 2002/11/17 08:58:50 salo Exp $

PKGNAME=$1
STAGE=$2

AWK="/usr/bin/nawk"
BASENAME="/usr/bin/basename"
CAT="/usr/bin/cat"
CHGRP="/usr/bin/chgrp"
CHMOD="/usr/bin/chmod"
CHOWN="/usr/bin/chown"
CMP="cmp"
CP="/usr/bin/cp"
DIRNAME="/usr/bin/dirname"
ECHO="/usr/ucb/echo"
EGREP="/usr/xpg4/bin/egrep"
EXPR="/usr/bin/expr"
FALSE="/usr/bin/false"
FIND="/usr/bin/find"
GREP="/usr/xpg4/bin/grep"
GROUPADD="/usr/sbin/groupadd"
GTAR="/usr/pkg/bin/tar"
HEAD="/usr/bin/head"
ID="/usr/xpg4/bin/id"
LN="/usr/bin/ln"
MKDIR="/usr/bin/mkdir -p"
MV="/usr/bin/mv"
PERL5="/usr/pkg/bin/perl"
RM="/usr/bin/rm"
RMDIR="/usr/bin/rmdir"
SED="/usr/xpg4/bin/sed"
SH="/bin/ksh"
SORT="/usr/bin/sort"
SU="/usr/bin/su"
TOUCH="/usr/bin/touch"
TR="/usr/bin/tr"
TRUE="/usr/bin/true"
USERADD="/usr/sbin/useradd"
XARGS="/usr/bin/xargs"

PKGBASE="apache6"

PKG_USERS="www:www::Apache\ user"
PKG_GROUPS="www"

SPECIAL_PERMS=""

CONF_FILES="/usr/pkg/share/examples/httpd/httpd.conf.default /usr/pkg/etc/httpd/httpd.conf"
CONF_FILES_PERMS=""
SUPPORT_FILES="/usr/pkg/share/examples/httpd/magic.default /usr/pkg/etc/httpd/magic /usr/pkg/share/examples/httpd/mime.types.default /usr/pkg/etc/httpd/mime.types"
SUPPORT_FILES_PERMS=""
RCD_SCRIPTS="apache"
RCD_SCRIPTS_DIR="/etc/rc.d"
RCD_SCRIPTS_EXAMPLEDIR="/usr/pkg/etc/rc.d"

MAKE_DIRS="/usr/pkg/etc/httpd "
MAKE_DIRS_PERMS=""
OWN_DIRS="/usr/pkg/lib/httpd /var/httpd /var/log/httpd"
OWN_DIRS_PERMS=""

PKG_SYSCONFBASE="/usr/pkg/etc"
PKG_SYSCONFDIR="/usr/pkg/etc/httpd"

PKG_REGISTER_SHELLS="YES"
PKG_SHELL=""

ALL_USERS=
ALL_GROUPS=
ALL_FILES=
ALL_MAKE_DIRS=
ALL_DIRS=

case "${PKG_CREATE_USERGROUP:-YES}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	_PKG_CREATE_USERGROUP=YES
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	_PKG_CREATE_USERGROUP=NO
	;;
esac

case "${PKG_CONFIG:-YES}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	_PKG_CONFIG=YES
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	_PKG_CONFIG=NO
	;;
esac

case "${PKG_RCD_SCRIPTS:-NO}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	_PKG_RCD_SCRIPTS=YES
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	_PKG_RCD_SCRIPTS=NO
	;;
esac

msginit()
{
	_msgn=0
	_msgempty=1

}

msgempty()
{
	if [ $_msgempty -gt 0 ]; then
		${TRUE}
	else
		${FALSE}
	fi
}

msgadd()
{
        eval _msg$_msgn=\"$1\"
        _msgn=`${EXPR} $_msgn + 1`
	_msgempty=0
}

msgprint()
{
	i=0
	while [ $i -lt $_msgn ]; do
		eval _msg=_msg$i
		eval ${ECHO} "\$$_msg" | ${SED} \
			-e "s,^###,			," \
			-e "s,^##,		," \
			-e "s,^#,	,"
                i=`${EXPR} $i + 1`
        done
}

# end of header
# start of install
#
# $NetBSD: install,v 1.19 2003/01/29 19:47:29 jlam Exp $

case ${STAGE} in
PRE-INSTALL)
	msginit
	_pkg_exit=0
	if [ -n "${PKG_GROUPS}" ]; then
		_print_group_header=1
		eval set -- ${PKG_GROUPS}
		for groupset; do
			save_IFS="${IFS}"; IFS=":"
			set -- ${groupset}
			group="$1"; groupid="$2"
			IFS="${save_IFS}"
			
			# We need to check that ${PKG_GROUP} exists before
			# adding the user.  Do it with chgrp to be able to
			# use NIS.
			#
	          	${TOUCH} /tmp/grouptest.$$
		  	if ${CHGRP} ${group} /tmp/grouptest.$$ >/dev/null 2>&1; then
				${ECHO} "Group '${group}' already exists."
			elif [ "${_PKG_CREATE_USERGROUP}" = "NO" ]; then
				if [ ${_print_group_header} -gt 0 ]; then
					_print_group_header=0
					msgadd ""
					msgadd "The following groups need to be created for ${PKGNAME}:"
					msgadd ""
				fi
				if [ -z "${groupid}" ]; then
					groupid_str=
				else
					groupid_str=" (${groupid})"
				fi
				msgadd "#${group}${groupid_str}"
				_pkg_exit=1
			else
				groupid_option=
				if [ -n "${groupid}" ]; then
					groupid_option="-g"
				fi
				${ECHO} "Creating group: ${group}"
				${GROUPADD} ${groupid_option} ${groupid} "${group}"
			fi
			${RM} -f /tmp/grouptest.$$
		done
	fi
	if [ -n "${PKG_USERS}" ]; then
		_print_user_header=1
		eval set -- ${PKG_USERS}
		for userset; do
			save_IFS="${IFS}"; IFS=":"
			set -- ${userset}
			user="$1"; group="$2"; userid="$3"
			descr="${4:-${PKGBASE} ${user} user}"
			home="${5:-/nonexistent}"
			shell="${6:-/usr/bin/false}"
			IFS="${save_IFS}"

			# Use `id' to be able to use NIS.
			if ${ID} "${user}" >/dev/null 2>&1; then
				${ECHO} "User '${user}' already exists."
			elif [ "${_PKG_CREATE_USERGROUP}" = "NO" ]; then
				if [ ${_print_user_header} -gt 0 ]; then
					_print_user_header=0
					msgadd ""
					msgadd "The following users need to be created for ${PKGNAME}:"
					msgadd ""
				fi
				if [ -z "${userid}" ]; then
					userid_str=
				else
					userid_str=" (${userid})"
				fi
				msgadd "#${user}${userid_str}: ${group}, ${home}, ${shell}"
				_pkg_exit=1
			else
				userid_option=
				if [ -n "${userid}" ]; then
					userid_option="-u"
				fi
				${ECHO} "Creating user: ${user}"
				${USERADD} -c "${descr}"		\
					${userid_option} ${userid}	\
					-g "${group}"			\
					-d "${home}"			\
					-s "${shell}"			\
					"${user}"
			fi
		done
	fi

	# Create package directories at pre-install time.
	#
	if [ -n "${MAKE_DIRS}" -o -n "${OWN_DIRS}" -o			\
	     -n "${MAKE_DIRS_PERMS}" -o -n "${OWN_DIRS_PERMS}" ]; then
		if [ -z "${RCD_SCRIPTS}" -o "${_PKG_RCD_SCRIPTS}" = "NO" ]; then
			_RCD_SCRIPTS_DIR=
		else
			_RCD_SCRIPTS_DIR="${RCD_SCRIPTS_DIR}"
		fi
		eval set -- ${_RCD_SCRIPTS_DIR} ${MAKE_DIRS} ${OWN_DIRS}
		for dir; do
			if [ -d "${dir}" ]; then
				continue
			fi
			if [ "${_PKG_CONFIG}" = "YES" ]; then
				${MKDIR} "${dir}"
				${ECHO} "${PKGBASE}" > "${dir}/.pkgsrc"
			fi
		done
		eval set -- ${MAKE_DIRS_PERMS} ${OWN_DIRS_PERMS}
		while [ $# -gt 0 ]; do
			dir="$1"; owner="$2"; group="$3"; mode="$4"
			shift; shift; shift; shift
			if [ "${_PKG_CONFIG}" = "YES" ]; then
				if [ ! -d "${dir}" ]; then
					${MKDIR} "${dir}"
					${ECHO} "${PKGBASE}" > "${dir}/.pkgsrc"
				fi
				${CHOWN} -R "${owner}" "${dir}"
				${CHGRP} -R "${group}" "${dir}"
				${CHMOD} -R "${mode}" "${dir}"
			fi
		done
	fi
	if ! msgempty; then
		${ECHO} "==========================================================================="
		msgprint
		${ECHO} ""
		${ECHO} "==========================================================================="
	fi
	if [ ${_pkg_exit} -gt 0 ]; then
		exit ${_pkg_exit}
	fi
        ;;

POST-INSTALL)
	#
	# Note any missing package directories.
	#
	msginit
	if [ -n "${MAKE_DIRS}" -o -n "${OWN_DIRS}" -o			\
	     -n "${MAKE_DIRS_PERMS}" -o -n "${OWN_DIRS_PERMS}" ]; then
		_print_dir_header=1
		if [ -z "${RCD_SCRIPTS}" -o "${_PKG_RCD_SCRIPTS}" = "NO" ]; then
			_RCD_SCRIPTS_DIR=
		else
			_RCD_SCRIPTS_DIR="${RCD_SCRIPTS_DIR}"
		fi
		eval set -- ${_RCD_SCRIPTS_DIR} ${MAKE_DIRS} ${OWN_DIRS}
		for dir; do
			if [ -d "${dir}" ]; then
				continue
			fi
			if [ "${_PKG_CONFIG}" = "NO" ]; then
				if [ ${_print_dir_header} -gt 0 ]; then
					_print_dir_header=0
					msgadd ""
					msgadd "The following directories should be created for ${PKGNAME}:"
					msgadd ""
				fi
				msgadd "#${dir}"
			fi
		done
		eval set -- ${MAKE_DIRS_PERMS} ${OWN_DIRS_PERMS}
		while [ $# -gt 0 ]; do
			dir="$1"; owner="$2"; group="$3"; mode="$4"
			shift; shift; shift; shift
			if [ "${_PKG_CONFIG}" = "NO" ]; then
				if [ ${_print_dir_header} -gt 0 ]; then
					_print_dir_header=0
					msgadd ""
					msgadd "The following directories should be created for ${PKGNAME}:"
					msgadd ""
				fi
				msgadd "#${dir} (o=${owner}, g=${group}, m=${mode})"
			fi
		done
	fi

	if [ "${_PKG_CONFIG}" = "YES" ]; then
		if [ -n "${CONF_FILES}" -o				\
		     -n "${CONF_FILES_PERMS}" -o			\
		     -n "${SUPPORT_FILES}" -o				\
		     -n "${SUPPORT_FILES_PERMS}" ] ||			\
		   [ "${_PKG_RCD_SCRIPTS}" = "YES" -a			\
		     -n "${RCD_SCRIPTS}" ]; then
			${CAT} << EOF
Installing configuration files for ${PKGNAME}:

EOF
		fi
	fi
	_print_file_header=1
	eval set -- ${CONF_FILES}
	while [ $# -gt 0 ]; do
		samplefile="$1"; file="$2"
		shift; shift

		if [ "${_PKG_CONFIG}" = "NO" ]; then
			if [ ${_print_file_header} -gt 0 ]; then
				_print_file_header=0
				msgadd ""
				msgadd "The following files should be created for ${PKGNAME}:"
				msgadd ""
			fi
			msgadd "#${file} (m=0644)"
		elif [ -e "${file}" ]; then
			${ECHO} "    ${file} already exists, example file is"
			${ECHO} "        ${samplefile}"
		else
			if [ -e "${samplefile}" ]; then
				${ECHO} "    ${file}"
				${CP} "${samplefile}" "${file}"
				${CHMOD} 0644 "${file}"
			fi
		fi
	done
	eval set -- ${SUPPORT_FILES}
	while [ $# -gt 0 ]; do
		samplefile="$1"; file="$2"
		shift; shift

		if [ "${_PKG_CONFIG}" = "NO" ]; then
			if [ ${_print_file_header} -gt 0 ]; then
				_print_file_header=0
				msgadd ""
				msgadd "The following files should be created for ${PKGNAME}:"
				msgadd ""
			fi
			msgadd "#${file} (m=0644)"
		elif [ -e "${file}" ]; then
			${ECHO} "    ${file} already exists, example file is"
			${ECHO} "        ${samplefile}"
		else
			if [ -e "${samplefile}" ]; then
				${ECHO} "    ${file}"
				${CP} "${samplefile}" "${file}"
				${CHMOD} 0644 "${file}"
			fi
		fi
	done
	eval set -- ${CONF_FILES_PERMS} ${SUPPORT_FILES_PERMS}
	while [ $# -gt 0 ]; do
		samplefile="$1"; file="$2"
		owner="$3"; group="$4"; mode="$5"
		shift; shift; shift; shift; shift

		if [ "${_PKG_CONFIG}" = "NO" ]; then
			if [ ${_print_file_header} -gt 0 ]; then
				_print_file_header=0
				msgadd ""
				msgadd "The following files should be created for ${PKGNAME}:"
				msgadd ""
			fi
			msgadd "#${file} (o=${owner}, g=${group}, m=${mode})"
		elif [ -e ${file} ]; then
			${ECHO} "    ${file} already exists, example file is"
			${ECHO} "        ${samplefile}"
		else
			if [ -e "${samplefile}" ]; then
				${ECHO} "    ${file}"
				${CP} "${samplefile}" "${file}"
				${CHOWN} "${owner}" "${file}"
				${CHGRP} "${group}" "${file}"
				${CHMOD} ${mode} "${file}"
			fi
		fi
	done
	eval set -- ${RCD_SCRIPTS}
	for script; do
		samplefile="${RCD_SCRIPTS_EXAMPLEDIR}/${script}"
		file="${RCD_SCRIPTS_DIR}/${script}"

		if [ "${_PKG_CONFIG}" = "NO" -o				\
		     "${_PKG_RCD_SCRIPTS}" = "NO" ]; then
			if [ ${_print_file_header} -gt 0 ]; then
				_print_file_header=0
				msgadd ""
				msgadd "The following files should be created for ${PKGNAME}:"
				msgadd ""
			fi
			msgadd "#${file} (m=0755)"
		elif [ -e "${file}" ]; then
			${ECHO} "    ${file} already exists, example file is"
			${ECHO} "        ${samplefile}"
		else
			if [ -e "${samplefile}" ]; then
				${ECHO} "    ${file}"
				${CP} "${samplefile}" "${file}"
				${CHMOD} 0755 "${file}"
			fi
		fi
	done
	_print_special_header=1
	eval set -- ${SPECIAL_PERMS}
	while [ $# -gt 0 ]; do
		file="$1"; owner="$2"; group="$3"; mode="$4"
		shift; shift; shift; shift

		if [ ${_print_special_header} -gt 0 ]; then
			_print_special_header=0
			msgadd ""
			msgadd "The following files and directories have special permissions:"
			msgadd ""
		fi
		msgadd "#${file} (o=${owner}, g=${group}, m=${mode})"
		${CHOWN} "${owner}" "${file}"
		${CHGRP} "${group}" "${file}"
		${CHMOD} ${mode} "${file}"
	done
	if ! msgempty; then
		${ECHO} "==========================================================================="
		msgprint
		${ECHO} ""
		${ECHO} "==========================================================================="
	fi
	if [ "${_PKG_CONFIG}" = "YES" ]; then
		if [ -n "${CONF_FILES}" -o -n "${CONF_FILES_PERMS}" ]; then
			${CAT} << EOF

===========================================================================
You may wish to customize the following files for ${PKGNAME}:

EOF
			eval set -- ${CONF_FILES}
			while [ $# -gt 0 ]; do
				samplefile="$1"; file="$2"
				shift; shift

				${ECHO} "	${file}"
		       	done
			eval set -- ${CONF_FILES_PERMS}
			while [ $# -gt 0 ]; do
				samplefile="$1"; file="$2"
				owner="$3"; group="$4"; mode="$5"
				shift; shift; shift; shift; shift

				${ECHO} "	${file}"
			done
			${CAT} << EOF
===========================================================================
EOF
		fi
	fi
	if [ -n "${PKG_SHELL}" -a "${PKG_REGISTER_SHELLS}" = "YES" ]; then
		${ECHO} "===> Updating /etc/shells"
		${TOUCH} /etc/shells
		${CP} /etc/shells /etc/shells.pkgsrc."$$"
		(${GREP} -v ${PKG_SHELL} /etc/shells.pkgsrc."$$" || ${TRUE}; ${ECHO} ${PKG_SHELL}) > /etc/shells
		${RM} /etc/shells.pkgsrc."$$"
	fi
	;;

*)
	${ECHO} "Unexpected argument: ${STAGE}"
	exit 1
	;;
esac

# end of install
# start of footer
#
# $NetBSD: footer,v 1.1 2001/11/19 16:18:44 jlam Exp $

exit 0

# end of footer
