#!/bin/bash
#
# fpprep.sh version 0.90
# 
#
# Wrapper script for administering FrontPage Server Extenstions 2002 
# It concatenates Debian Apache2 config files into one monolithic httpd.conf
# and sets up symlinks necessary for building mod_frontpage.so
#
# run ./fpprep.sh withough arguments for usage info
#

checkconfig()
{
# First test to make sure we actually have what looks like a complete 
#  Debian Apache2 configuration in /etc/apache2:
echo "Checking for existing Debian Apache2 configuration layout"
echo
if [[ ! ( \
    -f /etc/apache2/apache2.conf && \
    -f /etc/apache2/httpd.conf && \
    -f /etc/apache2/ports.conf && \
    `ls -a "/etc/apache2/mods-enabled" | wc -l` -gt 2 && \
    `ls -a "/etc/apache2/sites-enabled" | wc -l` -gt 2 \
    ) ]]; then
    echo "I cannot find what looks like a normal Debian Apache2"
    echo "configuration setup"
    echo "Aborting."
    exit 
else
    echo "Debian Apache2 configuration layout looks okay"
    echo "Proceeding"
    echo
fi
}

setbuildenv()
{
echo "Creating temporary symlinks to enable build of frontpage module"
echo
if [ -e /usr/bin/apxs ]; then
    mv /usr/bin/apxs /usr/bin/apxs-fpse.install.tmp 
elif [ -e /usr/sbin/apachectl ]; then
    mv /usr/sbin/apachectl /usr/sbin/apachectl-fpse.install.tmp
fi

cd /usr/bin
ln -s apxs2 apxs
cd /usr/sbin
ln -s ../bin/apxs2 apxs
ln -s apache2ctl apachectl
}

setconfig()
{
checkconfig

echo "Creating temporary monolithic httpd.conf in /etc/apache2"

cd /etc/apache2

# I can do this better.  Should check first to see if existing renamed
#  file exists and then number incrementally.
mv httpd.conf httpd.conf-fpse.install.tmp

cp apache2.conf httpd.conf
mv apache2.conf apache2.conf-fpse.install.tmp

# owsadm.exe fails without a DocumentRoot in the main section of httpd.conf
#  (outside of any that might occur in VirtualHost stanzas) so we must
#  put one there
sed --in-place -e 's,^Group www-data,Group www-data&\
\
DocumentRoot \/var\/www/,' httpd.conf

cat mods-enabled/*.load >> httpd.conf
cat mods-enabled/*.conf >> httpd.conf
cat ports.conf >> httpd.conf
cat sites-enabled/* >> httpd.conf
sed  --in-place=-orig -e 's/^Include/## Include/g' httpd.conf
sed  --in-place=-orig -e 's/AllowOverride None/AllowOverride All/g' httpd.conf
ln -s httpd.conf apache2.conf
}

restoreconfig()
{
echo "Restoring Debian Apache2 config files to their original state"
echo
cd /etc/apache2
if [ -e httpd.conf-fpse.install.tmp ]; then
    echo "Saving copy of modified httpd.conf as httpd.conf.fpse"
    mv httpd.conf httpd.conf.fpse
    mv httpd.conf-fpse.install.tmp httpd.conf
fi
if [ -e apache2.conf-fpse.install.tmp ]; then
    mv apache2.conf-fpse.install.tmp apache2.conf
fi
}

restorebuildenv()
{
echo "Removing temporary symlinks needed to build frontpage module"
echo
if [ -e /usr/bin/apxs-fpse.install.tmp ]; then
    mv /usr/bin/apxs-fpse.install.tmp /usr/bin/apxs
elif [ -h /usr/bin/apxs ]; then
    rm -rf /usr/bin/apxs
    if [ -h /usr/sbin/apxs ]; then
	rm -rf /usr/sbin/apxs
    fi
fi

if [ -e /usr/sbin/apachectl-fpse.install.tmp ]; then
    mv /usr/sbin/apachectl-fpse.install.tmp /usr/sbin/apachectl
elif [ -h /usr/sbin/apachectl ]; then
    rm -rf /usr/sbin/apachectl
fi
}


case "$1" in
    install)
	setconfig
	setbuildenv
#	restorebuildenv
#	restoreconfig
    ;;

    restore)
	restorebuildenv
	restoreconfig
    ;;

    check)
	checkconfig
    ;;


    wipe)
	echo "This will WIPE OUT all the FrontPage Server extension"
	echo "Directories from your DocumentRoot!"
	echo
	echo "NOTE: If you have any other directories under /var/www"
	echo "whose names begin with _ they will be wiped by this script"
	echo
	echo "Do you really want to do this? (y|N)?"
	read ANSWER
	if [ $ANSWER != y ]; then
	    exit 0
	else
	    find /var/www/ -type d -name _* -exec rm -rf {} \;
	    rm -rf /usr/local/frontpage
	    echo
	    echo
	    echo "The FrontPage Server Extensions have been wiped"
	fi
    ;;
    *)
	echo "Usage: fpprep.sh {install|restore|check|wipe}"
	exit 1
    ;;
esac



