#! /bin/sh
# postinst script for caudium
# 
# $Id: postinst,v 1.29.2.9 2003/11/20 17:49:39 grendel Exp $
#
# see: dh_installdeb(1)
EXTVER=

set -e

. /usr/share/debconf/confmodule

db_version 2.0
db_capb

# 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/share/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'.
CAUDIUM_PIKE=/usr/lib/caudium${EXTVER}/base_server/caudium.pike
PIKE=/usr/lib/caudium${EXTVER}/bin/caudium

case "$1" in
    configure)

    db_get caudium${EXTVER}/config_port
    CFGPORTNO="$RET"
    if [ -n "$RET" -a "$RET" != "22202" ]; then
        CONFIG_PORT="$RET"
	sed -e "s/\(ConfigPorts.*\)\(22202\)\(.*\)/\1$RET\3/" $CAUDIUM_PIKE > $CAUDIUM_PIKE.new && \
	mv $CAUDIUM_PIKE.new $CAUDIUM_PIKE
    fi
    
    db_get caudium${EXTVER}/listen_on
    PORTNO="$RET"

    if [ ! -f /etc/caudium${EXTVER}/servers/Global_Variables ]; then    
     touch /var/state/caudium${EXTVER}/virgin.install
     db_get caudium${EXTVER}/config_login
     CONFIGADMIN="$RET"

     db_get caudium${EXTVER}/config_password
     
     CONFIGPASS="`echo $RET | $PIKE -e 'string p; p=Stdio.stdin->gets(); write(crypt(p));'`"

     cat <<EOF | sed -f - /etc/caudium${EXTVER}/Global_Variables.sample > \
         /etc/caudium${EXTVER}/servers/Global_Variables.new && mv /etc/caudium${EXTVER}/servers/Global_Variables.new \
         /etc/caudium${EXTVER}/servers/Global_Variables
s¤@CFGPORT@¤$CFGPORTNO¤g
s¤@CONFIGADMIN@¤$CONFIGADMIN¤g
s¤@CONFIGPASS@¤$CONFIGPASS¤g
EOF
#     sed -e "s;@CFGPORT@;$CFGPORTNO;" /etc/caudium${EXTVER}/Global_Variables.sample > \
#         /etc/caudium${EXTVER}/servers/Global_Variables.new && mv /etc/caudium${EXTVER}/servers/Global_Variables.new \
#         /etc/caudium${EXTVER}/servers/Global_Variables

     sed -e "s;@PORTNUM@;$PORTNO;" /etc/caudium${EXTVER}/localhost.sample > \
         /etc/caudium${EXTVER}/servers/localhost.new && mv /etc/caudium${EXTVER}/servers/localhost.new \
         /etc/caudium${EXTVER}/servers/localhost
    elif [ -f /var/state/caudium${EXTVER}/virgin.install ]; then
     rm /var/state/caudium${EXTVER}/virgin.install
    fi
    
    db_get caudium${EXTVER}/start_options
    START_OPTIONS=

    for o in `echo $RET | sed -e 's/,//g'`; do
	case $o in
	    threads) START_OPTIONS="$START_OPTIONS --with-threads" ;;
	    debug) START_OPTIONS="$START_OPTIONS --with-debug" ;;
	    once) START_OPTIONS="$START_OPTIONS --once" ;;
	    profile) START_OPTIONS="$START_OPTIONS --with-profile" ;;
	    fd-debug) START_OPTIONS="$START_OPTIONS --with-fd-debug" ;;
	    keep-alive) START_OPTIONS="$START_OPTIONS --with-keep-alive" ;;
	esac
    done
    db_get caudium${EXTVER}/performance
    if [ "$RET" = "true" ]; then
        START_OPTIONS="$START_OPTIONS -DMAX_PERFORMANCE"
    fi

    if [ ! -f /etc/default/caudium${EXTVER} ]; then
	cat <<EOF > /etc/default/caudium${EXTVER}
#
# This variable will be joined with the default
# Caudium startup options in the init script
#
# This file has been generated automatically the first
# time Caudium was installed on this machine. You can
# modify the START_OPTIONS variable below at your will
# as this file won't ever be modified by the Caudium
# package.
#
# For available options type
# /usr/lib/caudium${EXTVER}/start --help
#
START_OPTIONS="$START_OPTIONS"

#
# Stuff for caudiumctl(1)
#
caudiumhome=/usr/lib/caudium${EXTVER}
pidfile=/var/run/caudium${EXTVER}/caudium.pid
killallwww=no
wwwuser=www-data
flags="$START_OPTIONS"
EOF
    fi

##
## HTTP2 is the default now
##

#    db_get caudium/experimental_http
#    if test "$RET" = "true"; then
#	PROTOCOL=http2
#    else
#	PROTOCOL=http
#    fi
#    
#    if [ -f /etc/caudium/servers/localhost ]; then
#	sed -e "s;@PROTOCOL@;$PROTOCOL;" /etc/caudium/servers/localhost > \
#         /etc/caudium/servers/localhost.new && mv /etc/caudium/servers/localhost.new \
# 	 /etc/caudium/servers/localhost
#    fi

    
# -----------------------
    if [ ! -f /var/www/index.html ]; then
	ln -sf ../../usr/share/doc/caudium/www/index.html /var/www/index.html
    fi
    db_stop

    # Now let's take care of permissions
    # if it is the first time we run Caudium
    #if [ -f /var/state/caudium${EXTVER}/virgin.install ]; then    
    #	DIRS="/var/run/caudium${EXTVER} /var/state/caudium${EXTVER} /var/cache/caudium${EXTVER} /etc/caudium/servers${EXTVER} /var/log/caudium${EXTVER}"
    #	for d in $DIRS; do
    #	    if test -d $d; then
    #		chown -R www-data.www-data $d
    #	    fi
    #	done
    rm -f /var/state/caudium${EXTVER}/virgin.install
    #fi

    DIRS="/var/run/caudium${EXTVER} /var/state/caudium${EXTVER} /var/cache/caudium${EXTVER} /var/cache/caudium${EXTVER}/args /etc/caudium${EXTVER}/servers /var/log/caudium${EXTVER}"
    for d in $DIRS; do
	dpkg-statoverride --remove $d > /dev/null 2>&1 || true
	dpkg-statoverride --update --add www-data www-data 0751 $d
    done
    
    if [ -d /usr/lib/caudium/caudium-images -a ! -L /usr/lib/caudium/caudium-images ]; then
	rm -r /usr/lib/caudium/caudium-images
    fi
    ln -sf ../../share/caudium/caudium-images /usr/lib/caudium/caudium-images
    
    if [ ! -d /usr/local/share/caudium/modules/ ]; then
	install -d -m 755 -o root -g root /usr/local/share/caudium/modules/
    fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
	db_stop
	exit 0
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


