#! /bin/sh -e

if [ "$1" = configure ]; then
    (
	cd /usr/lib/@PVER@
	files=$(dpkg -L @PVER@-minimal | sed -n '/\.py$/s,^/usr/lib/@PVER@/,,p')
	/usr/bin/@PVER@ /usr/lib/@PVER@/py_compile.py $files
	if grep -sq '^byte-compile[^#]*optimize' /etc/python/debian_config; then
	    /usr/bin/@PVER@ -O /usr/lib/@PVER@/py_compile.py $files
	fi
    )
    if [ -z "$2" ] || dpkg --compare-versions "$2" lt 2.5-3 \
        || [ -f /var/lib/python/@PVER@_installed ] \
        || grep -sq '^supported-versions[^#]*@PVER@' /usr/share/python/debian_defaults
    then
	# new installation or installation of first version with hook support
	if [ "$DEBIAN_FRONTEND" != noninteractive ]; then
	    echo "Linking and byte-compiling packages for runtime @PVER@..."
	fi
	version=$(dpkg -s @PVER@-minimal | awk '/^Version:/ {print $2}')
	for hook in /usr/share/python/runtime.d/*.rtinstall; do
	    [ -x $hook ] || continue
	    $hook rtinstall @PVER@ "$2" "$version"
	done
	if [ -f /var/lib/python/@PVER@_installed ]; then
	    rm -f /var/lib/python/@PVER@_installed
	    rmdir --ignore-fail-on-non-empty /var/lib/python 2>/dev/null
	fi
    fi
fi

case "$1" in
    configure)
        # Create empty directories in /usr/local
        if [ ! -e /usr/local/lib/@PVER@ ]; then
            mkdir -p /usr/local/lib/@PVER@ 2> /dev/null || true
            chmod 2775 /usr/local/lib/@PVER@ 2> /dev/null || true
            chown root:staff /usr/local/lib/@PVER@ 2> /dev/null || true
        fi
        if [ ! -e /usr/local/lib/@PVER@/site-packages ]; then
            mkdir -p /usr/local/lib/@PVER@/site-packages 2> /dev/null || true
            chmod 2775 /usr/local/lib/@PVER@/site-packages 2> /dev/null || true
            chown root:staff /usr/local/lib/@PVER@/site-packages 2> /dev/null || true
        fi
    ;;
esac

#DEBHELPER#

exit 0
