#! /bin/sh
# postinst script for yaws
#
# 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'.

case "$1" in
    configure)
	# Create yaws user
	adduser --quiet --system --group --home /var/cache/yaws yaws
	# Moving home directory if it's still in /var/run
	usermod --home /var/cache/yaws --move-home yaws >/dev/null 2>/dev/null

	# Fixing config directory permissions
	for d in yaws yaws/conf.avail yaws/conf.d ; do
	    if ! dpkg-statoverride --list /etc/$d >/dev/null; then
		chown root:yaws /etc/$d
		chmod 750 /etc/$d
	    fi
	done

	# Yaws configs may contain sensitive information (passwords)
	for f in /etc/yaws/yaws.conf /etc/yaws/conf.avail/*.conf ; do
	    if ! dpkg-statoverride --list $f >/dev/null; then
		chown root:yaws $f
		chmod 640 $f
	    fi
	done

	# Create config symlinks
	# Do it only if it's a fresh install or upgrade from version
	# less than 1.66-2 (which is the first version with multiple
	# configs), allowing admin to safely remove it.

	if dpkg --compare-versions "$2" lt "1.66-2" ; then
	    for f in localhost.conf localhost-ssl.conf ; do
		if [ ! -f /etc/yaws/conf.d/$f ] ; then
		    ln -s ../conf.avail/$f /etc/yaws/conf.d/
		fi
	    done
	fi


	# Adding yaws user to the ssl-cert group to use the snakeoil
	# SSL certificate

	if getent group ssl-cert >/dev/null ; then
	    adduser --quiet yaws ssl-cert
	fi

	# Create the homedir if it doesn't exist for some reason
	mkdir -p /var/cache/yaws

	# Only user yaws and root are allowed to create yaws control files
	chown yaws:yaws /var/cache/yaws
	chmod 750 /var/cache/yaws

	install -o yaws -g adm  -m 750 -d /var/log/yaws

	# Finish moving /usr/lib/yaws/custom/* to /usr/local/lib/yaws-appmods/
	dira=/usr/lib/yaws-custom.dpkg-backup
	dirb=/usr/local/lib/yaws-appmods
	if [ -d $dira ] ; then
	    # If the /usr/lib/yaws/custom.dpkg-backup directory exists then we
	    # are upgrading from an older version and have to move it to
	    # /usr/local/lib/yaws-appmods

	    echo "Relocating /usr/lib/yaws/custom into $dirb..."

	    (
		while read line; do
		    set -- $line
		    dir="$1"; mode="$2"; user="$3"; group="$4"
		    if [ ! -e "$dir" ]; then
			if mkdir "$dir" 2>/dev/null; then
			    chown "$user":"$group" "$dir"
			    chmod "$mode" "$dir"
			fi
		    fi
		done
	    ) << DATA
/usr/local/lib 2775 root staff
/usr/local/lib/yaws-appmods 2775 root staff
DATA
	    for f in $(ls -A $dira) ; do
		mv -f $dira/$f $dirb/
	    done
	    rmdir $dira
	fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        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

