#!/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="ap-ssl"

PKG_USERS=""
PKG_GROUPS=""

SPECIAL_PERMS=""

CONF_FILES=""
CONF_FILES_PERMS=""
SUPPORT_FILES=" /usr/pkg/share/examples/mod_ssl/ssl.crl/Makefile.crl /usr/pkg/etc/httpd/ssl.crl/Makefile.crl /usr/pkg/share/examples/mod_ssl/ssl.crl/README.CRL /usr/pkg/etc/httpd/ssl.crl/README.CRL /usr/pkg/share/examples/mod_ssl/ssl.crt/Makefile.crt /usr/pkg/etc/httpd/ssl.crt/Makefile.crt /usr/pkg/share/examples/mod_ssl/ssl.crt/README.CRT /usr/pkg/etc/httpd/ssl.crt/README.CRT /usr/pkg/share/examples/mod_ssl/ssl.crt/ca-bundle.crt /usr/pkg/etc/httpd/ssl.crt/ca-bundle.crt /usr/pkg/share/examples/mod_ssl/ssl.crt/README.CSR /usr/pkg/etc/httpd/ssl.crt/README.CSR /usr/pkg/share/examples/mod_ssl/ssl.key/README.KEY /usr/pkg/etc/httpd/ssl.key/README.KEY /usr/pkg/share/examples/mod_ssl/ssl.prm/README.PRM /usr/pkg/etc/httpd/ssl.prm/README.PRM"
SUPPORT_FILES_PERMS=""
RCD_SCRIPTS=""
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/etc/httpd/ssl.crl /usr/pkg/etc/httpd/ssl.crt /usr/pkg/etc/httpd/ssl.csr /usr/pkg/etc/httpd/ssl.prm"
OWN_DIRS_PERMS="/usr/pkg/etc/httpd/ssl.key			 root root 0700"

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 deinstall
#
# $NetBSD: deinstall,v 1.16 2002/11/21 10:11:57 jlam Exp $

eval set -- ${PKG_USERS}
for userset; do
	user=`
		IFS=":"
		set -- ${userset}
		${ECHO} "$1"
	`
	ALL_USERS="${ALL_USERS} \"${user}\""
done
eval set -- ${PKG_GROUPS}
for groupset; do
	group=`
		IFS=":"
		set -- ${groupset}
		${ECHO} "$1"
	`
	ALL_GROUPS="${ALL_GROUPS} \"${group}\""
done

eval set -- ${CONF_FILES} ${SUPPORT_FILES}
for file; do
	ALL_FILES="${ALL_FILES} \"${file}\""
done
if [ "${_PKG_RCD_SCRIPTS}" = "YES" ]; then
	eval set -- ${RCD_SCRIPTS}
	for script; do
		samplefile="${RCD_SCRIPTS_EXAMPLEDIR}/${script}"
		file="${RCD_SCRIPTS_DIR}/${script}"
		shift
		ALL_FILES="${ALL_FILES} \"${samplefile}\" \"${file}\""
	done
fi
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 
	ALL_FILES="${ALL_FILES} \"${samplefile}\" \"${file}\""
done

eval set -- ${RCD_SCRIPTS_DIR} ${MAKE_DIRS}
for dir; do
	ALL_MAKE_DIRS="${ALL_MAKE_DIRS} \"${dir}\""
done
eval set -- ${MAKE_DIRS_PERMS}
while [ $# -gt 0 ]; do
	dir="$1"; owner="$2"; group="$3"; mode="$4"
	shift; shift; shift; shift
	ALL_MAKE_DIRS="${ALL_MAKE_DIRS} \"${dir}\""
done
eval set -- ${ALL_MAKE_DIRS} ${OWN_DIRS}
for dir; do
	ALL_DIRS="${ALL_DIRS} \"${dir}\""
done
eval set -- ${OWN_DIRS_PERMS}
while [ $# -gt 0 ]; do
	dir="$1"; owner="$2"; group="$3"; mode="$4"
	shift; shift; shift; shift
	ALL_DIRS="${ALL_DIRS} \"${dir}\""
done
ALL_DIRS=`
	( eval set -- ${ALL_DIRS}
	  for dir; do
		${ECHO} "\"${dir}\""
	  done
	) | ${SORT} -r
`

case ${STAGE} in
DEINSTALL)
	# Remove configuration files if they don't differ from the default
	# config file.
	#
        if [ "${_PKG_CONFIG}" = "YES" ]; then
		eval set -- ${ALL_FILES}
		while [ $# -gt 0 ]; do
			samplefile="$1"; file="$2"
			shift; shift

			if [ "${file}" != "${samplefile}" -a		\
			     -e "${file}" -a -e "${samplefile}" ]; then
				if ${CMP} -s "${file}" "${samplefile}"; then
					${RM} -f "${file}"
				fi
			fi
		done
	fi
	if [ -n "${PKG_SHELL}" -a "${PKG_REGISTER_SHELLS}" = "YES" ]; then
		${ECHO} "===> Updating /etc/shells"
		${CP} /etc/shells /etc/shells.pkgsrc."$$"
		(${GREP} -v ${PKG_SHELL} /etc/shells.pkgsrc."$$" || ${TRUE}) > /etc/shells
		${RM} /etc/shells.pkgsrc."$$"
	fi
	;;

POST-DEINSTALL)
	modified_files=''
	eval set -- ${ALL_FILES}
	while [ $# -gt 0 ]; do
		samplefile="$1"; file="$2"
		shift; shift

		if [ -e "${file}" ]; then
			modified_files="${modified_files} \"${file}\""
		fi
	done

	existing_dirs=''
	eval set -- ${ALL_DIRS}
	for dir; do
        	if [ "${_PKG_CONFIG}" = "YES" ]; then
			if [ -f "${dir}/.pkgsrc" ]; then
				dirowner=`${HEAD} -1 "${dir}/.pkgsrc"`
				if [ "${dirowner}" = "${PKGBASE}" ]; then
					${RM} -f "${dir}/.pkgsrc"
					${RMDIR} -p "${dir}" 2>/dev/null || ${TRUE}
				fi
			fi
			is_make_dir=`					\
				eval set -- ${ALL_MAKE_DIRS};		\
				is_make_dir=0;				\
				for make_dir; do			\
					case "${make_dir}" in		\
					${dir}) is_make_dir=1; break ;;	\
					esac;				\
				done;					\
				${ECHO} ${is_make_dir}			\
			`
			if [ ${is_make_dir} -eq 0 -a -d "${dir}" ]; then
				existing_dirs="${existing_dirs} \"${dir}\""
			fi
		else
			case "${dir}" in
			${PKG_PREFIX}/*)
				${RMDIR} -p "${dir}" 2>/dev/null || ${TRUE}
				;;
			esac
			case "${dir}" in
			#
			# Don't bother the admin about the following dirs
			# if they still exist.
			#
			${PKG_SYSCONFBASE}|${RCD_SCRIPTS_DIR})
				;;
			*)	
				if [ -d "${dir}" ]; then
					existing_dirs="${existing_dirs} \"${dir}\""
				fi
				;;
			esac
		fi
	done

	if [ -n "${ALL_USERS}" -o -n "${ALL_GROUPS}" -o			\
	     -n "${modified_files}" -o -n "${existing_dirs}" ]; then
		${CAT} << EOF
===========================================================================
If you won't be using ${PKGNAME} any longer, you may want to remove
EOF
		if [ -n "${ALL_USERS}" ]; then
			${ECHO} ""
			${ECHO} -n "  * the following users"
			sep=":   "
			eval set -- ${ALL_USERS}
			for user; do
				${ECHO} -n "${sep}${user}"
				sep=", "
			done
			${ECHO} ""
		fi
		if [ -n "${ALL_GROUPS}" ]; then
			${ECHO} ""
			${ECHO} -n "  * the following groups"
			sep=":  "
			eval set -- ${ALL_GROUPS}
			for group; do
				${ECHO} -n "${sep}${group}"
				sep=", "
			done
			${ECHO} ""
		fi
		if [ -n "${modified_files}" ]; then
			${CAT} << EOF

  * the following files:

EOF
			eval set -- ${modified_files}
			for file; do
				${ECHO} "	${file}"
			done
		fi
		if [ -n "${existing_dirs}" ]; then
			${CAT} << EOF

  * the following directories:

EOF
			eval set -- ${existing_dirs}
			for dir; do
				${ECHO} "	${dir}"
			done
		fi
		if [ -n "${RCD_SCRIPTS}" ]; then
			${CAT} << EOF

You many also want to remove any settings in rc.conf that you may have
made in order to use ${PKGNAME}.
EOF
		fi
		${CAT} << EOF
===========================================================================
EOF
	fi
	;;

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

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

exit 0

# end of footer
