#!/bin/sh
set -e

if [ ! -e /etc/apache/mime.types ]
then
    ln -s ../mime.types /etc/apache/mime.types
fi

if [ "$1" != configure ]
then
    exit 0
fi

chmod -R go=rX /usr/lib/apache /usr/doc/apache
update-rc.d apache defaults 91 > /dev/null

if [ -d /etc/httpd ]
then
    echo "Directory /etc/httpd is way obsolete, and should be removed."
fi

if [ -d /usr/lib/httpd/cgi-bin ]
then
    echo "Copying CGI files to /usr/lib/cgi-bin."
    cp -ia /usr/lib/httpd/cgi-bin/* /usr/lib/cgi-bin || true
fi

if [ -d /usr/lib/httpd ]
then
    echo "Directory /usr/lib/httpd is now obsolete, and should be removed."
    echo "(Icons are in /usr/doc/apache/icons, CGI in /usr/lib/cgi-bin.)"
    echo
fi

if  [ -d /var/log/apache-httpd ]
then
    echo
    echo 'Copying log files to their new location...'
    (cd /var/log/apache-httpd; tar cf - .) |
	(cd /var/log/apache; tar xpf -) && rm -rf /var/log/apache-httpd
    (cd /var/log/apache; for f in `find . \( -name \*access_log\* -o \
	-name \*error_log\* \) -print`; do
	new=`echo $f | sed 's,_log$,.log,'`;
	if [ "$new" != "$f" -a -f "$new" ]
	then
	    tmpl=$TMPDIR/`basename $new`.$$
	    cat "$f" "$new" >$tmpl && mv -f "$tmpl" "$new" && rm -f "$f"
	else
	    mv -f $f "`echo $f | sed 's,_log,.log,'`";
	fi; done)
fi

if  [ -d /etc/httpd -o /var/log/apache-httpd -o -d /usr/lib/httpd ]
then
    echo
fi

if grep assert-perl /usr/sbin/apacheconfig > /dev/null 2>&1 \
    && /usr/sbin/apacheconfig --assert-perl > /dev/null 2>&1
then
    if ! /usr/sbin/apacheconfig --update
    then
	echo
	echo -n "Configuration failed!  "
	echo "Run \"apacheconfig\" to try this again later."
	echo
    fi
else
    echo
    echo ERROR: apacheconfig could not be run.  It may be the wrong
    echo version, or perl may not be fully configured yet.  When
    echo fixed, run \"dpkg --configure apache\" or \"apacheconfig\".
    echo
    echo -n "Press Enter to continue: "
    read
    echo
    exit 1;
fi

cat <<EOF

Apache can be reconfigured at any time with the "apacheconfig" command.
EOF

if [ -e /etc/suid.conf -a -x /usr/sbin/suidregister ]
then
	suidregister -s apache /usr/bin/htpasswd root root 755
else
	chown root.root /usr/bin/htpasswd
	chmod 755 /usr/bin/htpasswd
fi

exit 0
# Debmake then adds the suidregister commands and /etc/init.d/apache start.
