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

case "$1" in
    configure)
	# Match pip layout: Apache django vhosts expect
	# /var/www/acme2certifier/acme2certifier/... (symlink into site-/dist-packages).
	if command -v python3 >/dev/null 2>&1; then
		A2C_PKG=$(python3 -c "import acme2certifier, pathlib; print(pathlib.Path(acme2certifier.__file__).parent)" 2>/dev/null || true)
		if [ -n "$A2C_PKG" ] && [ -d "$A2C_PKG" ]; then
			# Replace a real directory left by ≤0.44 django layout; ln -sfn alone
			# would nest the symlink inside the old tree.
			if [ -e /var/www/acme2certifier/acme2certifier ] && [ ! -L /var/www/acme2certifier/acme2certifier ]; then
				rm -rf /var/www/acme2certifier/acme2certifier
			fi
			ln -sfn "$A2C_PKG" /var/www/acme2certifier/acme2certifier
		fi
	fi
	mkdir -p /var/www/acme2certifier/acme_srv
	# Core lives in dist-packages; deploy tree (incl. default SQLite dbfile) owned by web user.
	if id www-data >/dev/null 2>&1; then
		chown -R www-data:www-data /var/www/acme2certifier
	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
