#! /bin/sh
# postinst script for apache
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

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

if [ "$2" != "" ]
then
    CONFFLAGS="--update"
else
    CONFFLAGS="--fullauto"
fi

# adduser stuffs moved from preinst
if [ -e /usr/sbin/adduser ]
then
    if ! grep -q "^www" /etc/group;
    then
	echo -n "The www-data group is missing!  Attempt to fix? "
	read X
	if [ "$X" = "Y" -o "$X" = "y" ];
	then
	    echo "Here goes nothing ... "
            addgroup --gid 33 --force-badname www-data
            echo
	fi
    fi

    if ! grep -q "^www" /etc/passwd;
    then
	echo -n "The www-data user is missing!  Attempt to fix? "
	read X
	if [ "$X" = "Y" -o "$X" = "y" ];
	then
            adduser --system --home /var/www --no-create-home \
            --uid 33 --gid 33 --disabled-password --force-badname www-data
            echo
	fi
    fi
fi

if [ ! -e /etc/apache/conf ] && [ "$2" = "" ]
then
    ln -s . /etc/apache/conf
fi

if [ -f /etc/apache/httpd.conf ] \
    && ! grep -q LoadModule: /etc/apache/httpd.conf \
    && ! grep -q ClearModuleList: /etc/apache/httpd.conf
then
    echo -n "Adding magic LoadModule line to httpd.conf ... "
    cat > /etc/apache/httpd.conf.dpkg-inst.$$ <<EOF

# Please keep this LoadModule: line here, it is needed for installation.


EOF
    cat /etc/apache/httpd.conf >> /etc/apache/httpd.conf.dpkg-inst.$$
    mv /etc/apache/httpd.conf.dpkg-inst.$$ /etc/apache/httpd.conf
    echo "done."
fi

# If the 1.3.9 Debian Package's srm.conf is still around, comment out
# this directive or the newly-installed web server (currently 1.3.14)
# won't start.
if [ -f /etc/apache/srm.conf ]
then
    if grep -q "^[ 	]*AddDefaultCharsetName" /etc/apache/srm.conf
    then
        sed '/[^	]*AddDefaultCharsetName/s/^/# Obsolete # /' < /etc/apache/srm.conf > /etc/apache/srm.conf.dpkg-inst.$$ &&
	    cat /etc/apache/srm.conf.dpkg-inst.$$ > /etc/apache/srm.conf
    fi
fi

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

if ! grep -qi "^webmaster:" /etc/aliases 2> /dev/null
then
    echo "Adding webmaster to /etc/aliases ..."
    echo "webmaster: root" >> /etc/aliases
    if [ -e /usr/sbin/newaliases ]
    then
        newaliases
    fi
fi

update-rc.d apache defaults 91 20 > /dev/null

NEED_ECHO=0

if [ -d /etc/httpd ]
then
    echo "Directory /etc/httpd is way obsolete, and should be removed."
    NEED_ECHO=1
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
    NEED_ECHO=1
fi

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

if  [ -d /var/log/apache-httpd ]
then
    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)
    NEED_ECHO=1
fi

# Check for log files.
set -- /etc/apache/*.conf
if [ -e $1 ]; then
    while [ $# -gt 0 ]; do
	CONFS=${CONFS:+$CONFS }$1
	set -- "$@" $(awk '$1 ~ /^\s*[Ii]nclude$/ && $2 ~ /^\// {print $2}' $1 | sort -u )
	shift
    done
    SERVERROOT=$(awk '$1 == "ServerRoot" { print $2; exit }' $CONFS)
    LOGS=$(awk '$1 ~ /^\s*[A-Za-z]*Log$/ {print $2}' $CONFS | grep -v '|' | sort -u)
    BAD_LOG=0
    for i in $LOGS; do
	# remove quotes from log
	i=$(echo $i)
	# relative logs are bad
	case $i in
	    /*) ;;
	    *) BAD_LOG=1; i=$SERVERROOT/$i ;;
	esac
	i=${i##/var/log/apache/}
	case $i in
	    */*) BAD_LOG=1 ;;
	esac
	if [ $BAD_LOG = 1 ]; then
	    break;
	fi
    done
    if [ $BAD_LOG = 1 ]; then
	echo Apache has switched to using logrotate.  However, some of your logs
	echo are stored outside the /var/log/apache directory, so you should
	echo edit /etc/logrotate.d/apache to have them automatically rotated.
	NEED_ECHO=1
    fi
fi
if [ $NEED_ECHO = 1 ]
then
    echo
fi

if [ -f /etc/cron.daily/apache ]; then
    rm -f /etc/cron.daily/apache
fi

if [ -f /etc/apache/cron.conf ]; then
    rm -f /etc/apache/cron.conf
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 $CONFFLAGS
    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 REPLY
    echo
    exit 1;
fi

# Automatically added by dh_installdocs
if [ "$1" = "configure" ]; then
        if [ -d /usr/doc -a ! -e /usr/doc/apache -a -d /usr/share/doc/apache ]; then
                ln -sf ../share/doc/apache /usr/doc/apache
        fi
fi

/etc/init.d/apache restart || true

exit 0

#FIXME DEBHELPER#
