#!/bin/sh
#
#	$FreeBSD: ports/www/apache13-fp/pkg-install,v 1.7 2002/06/20 04:35:36 ijliao Exp $
#
# Created by: hetzels@westbend.net

PKG_BATCH=${BATCH:=NO}
PKG_USER=${PKG_USER:=www}
PKG_GROUP=${PKG_GROUP:=www}

PKG_PREFIX=${PKG_PREFIX}
HOST_NAME=`/bin/hostname`

AP_CGI=${PKG_PREFIX}/www/cgi-bin
AP_CONF=${PKG_PREFIX}/etc/apache
AP_DATA=${PKG_PREFIX}/www/data
AP_SHARE=${PKG_PREFIX}/share/doc/apache

FPINSTALL=${PKG_PREFIX}/frontpage/version5.0/fp_install.sh
FPCNF=${PKG_PREFIX}/frontpage/version5.0/frontpage.cnf
IMAGES_DIR=${AP_SHARE}/manual/images
IMAGES_VTI=${AP_DATA}/images/_vti_cnf

create_user()
{
	if [ ! -x /usr/sbin/pw ]; then
		echo "*** Please add a user and a group name \`${PKG_GROUP}' before installing this package."
		exit 69
	fi

	oldgid=0
	if pw show group apache -q > /dev/null ; then
		oldgid=`pw show group apache 2> /dev/null | cut -d: -f3`
		if [ ${oldgid} -eq 80 ] ; then
			echo "*** Group changed from apache to ${PKG_GROUP}"
			pw groupdel apache
		fi
	fi

	if ! pw show group ${PKG_GROUP} -q > /dev/null; then
		gid=80
		if ! pw add group ${PKG_GROUP} -g ${gid}; then
			e=$?
			echo "*** Failed to add group \`${PKG_GROUP}'.  Please add it manually."
			exit ${e}
		fi
		echo "*** Added group \`${PKG_GROUP}' (id ${gid})."
	else
		gid=`pw show group ${PKG_GROUP} 2> /dev/null | cut -d: -f3`
	fi

	if [ -x /sbin/nologin ]; then
		shell="/sbin/nologin"
	else
		shell="/nonexistent"
	fi

	if pw show user apache -q > /dev/null ; then
		olduid=`pw show user apache 2> /dev/null | cut -d: -f3`
		if [ ${olduid} -eq 80 ]; then
			echo "*** User name changed from apache to ${PKG_USER}"
			pw del user apache
		fi
	fi

	if ! pw show user ${PKG_USER} -q > /dev/null; then
		uid=80
		if ! pw add user ${PKG_USER} -u ${uid} -g ${gid} \
				-d "/nonexistent" \
				-c "World Wide Web Owner" \
				-s "${shell}" -p "*" ; then
			e=$?
			echo "*** Failed to add user \`${PKG_USER}'. Please add it manually."
			exit ${e}
		fi
		echo "*** Added user \`${PKG_USER}' (id ${uid})"
	fi
}
		
create_apache_doc_root ()
{
    if [ ! -d ${AP_CGI} ]; then
                /bin/cp -rp ${AP_CGI}.default ${AP_CGI}
    fi

    if [ ! -d ${AP_DATA} ]; then
	/bin/mkdir -p ${AP_DATA}/images
	for file in apache_pb.gif fplogo.gif powerlogo.gif
	{
	    /bin/cp -rp ${IMAGES_DIR}/${file} ${AP_DATA}/images
	}
	/bin/cp -rp ${AP_SHARE}/index.html.en ${AP_DATA}/index.html.en
    fi
}

fix_httpd_conf ()
{
    if [ ! -f ${AP_CONF}/httpd.conf ] ; then
	/bin/cat ${AP_CONF}/httpd.conf.default | \
	    /usr/bin/sed -e 's;@@HOSTNAME@@;'${HOST_NAME}';' \
	    > ${AP_CONF}/httpd.conf
    fi
}

# Add SendMailCommand to frontpage.cnf
fix_frontpage_cnf ()
{
    if [ ! "`grep SendMailCommand ${FPCNF}`" ] ; then
	echo "SendMailCommand:/usr/sbin/sendmail" >> ${FPCNF}
    fi
}

#Add the appropriate comment to the images/_vti_cnf file.
comment_files ()
{
    if [ -d ${IMAGES_VTI} ]; then
	if [ -f ${IMAGES_VTI}/apache_pb.gif ] && \
	   [ ! "`grep description ${IMAGES_VTI}/apache_pb.gif`" ] ; then
	    /bin/echo "vti_description:SW|Apache Webserver" >> ${IMAGES_VTI}/apache_pb.gif
	fi
	if [ -f ${IMAGES_VTI}/fplogo.gif ] && \
	   [ ! "`grep description ${IMAGES_VTI}/fplogo.gif`" ] ; then
	    /bin/echo "vti_description:SW|Created with Microsoft FrontPage" >> ${IMAGES_VTI}/fplogo.gif
	fi
	if [ -f ${IMAGES_VTI}/powerlogo.gif ] && \
	   [ ! "`grep description ${IMAGES_VTI}/powerlogo.gif`" ] ; then
	    /bin/echo "vti_description:SW|Powered by FreeBSD" >> ${IMAGES_VTI}/powerlogo.gif
	fi
    fi
}

case $2 in
    PRE-INSTALL)
	create_user
	;;
    POST-INSTALL)
	create_apache_doc_root
	fix_httpd_conf
	# If we are not in batch mode
	if [ "${PKG_BATCH}" = "NO" ]; then
	    ${FPINSTALL}
	    fix_frontpage_cnf
	    comment_files
	fi
	;;

esac
