#!/bin/bash
# postinst script for dump1090
#
# 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>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

NAME=dump1090-fa
RUNAS=dump1090
CRONFILE=/etc/cron.d/$NAME
TEMPLATECRON=/usr/share/$NAME/cron-template

case "$1" in
    configure)
        . /usr/share/debconf/confmodule

        if ! getent passwd "$RUNAS" >/dev/null
        then
            adduser --system --home /usr/share/$NAME --no-create-home --quiet "$RUNAS"
        fi

        # plugdev required for bladeRF USB access
        adduser "$RUNAS" plugdev

        # set up lighttpd
        if dpkg --compare-versions "$2" lt "3.1.0"
        then
            echo "Enabling lighttpd integration.." >&2
            lighty-enable-mod dump1090-fa || true

            # only enable the statcache config if there is nothing else around that already
            # configures it, because lighttpd fails if it's configured twice
            if ! grep -q -E '^\S*server.stat-cache-engine' /etc/lighttpd/conf-enabled/*.conf
            then
                echo "Enabling lighttpd integration (stat cache).." >&2
                lighty-enable-mod dump1090-fa-statcache || true
            fi
        fi

        # on upgrade, add an ENABLED line if it's not already present
        if dpkg --compare-versions "$2" lt-nl "3.7.0"
        then
            if [ -f /etc/default/dump1090-fa ]
            then
                if ! grep -q -E '^ENABLED=' /etc/default/dump1090-fa
                then
                    echo "Setting ENABLED=yes in /etc/default/dump1090-fa.." >&2
                    echo "# Automatically added by upgrade from $2" >>/etc/default/dump1090-fa
                    echo "ENABLED=yes" >>/etc/default/dump1090-fa
                fi
            fi
        fi

        # on upgrade from pre-6.0, update the defaults file to the new syntax
        if dpkg --compare-versions "$2" lt-nl "6.0"
        then
            if [ -f /etc/default/dump1090-fa ]
            then
                echo "Trying to upgrade existing config to new syntax.." >&2
                /usr/share/dump1090-fa/upgrade-config /etc/default/dump1090-fa /usr/share/dump1090-fa/dump1090-fa.default \
                    || echo "Something went wrong upgrading the config; your config may be broken. Sorry!" >&1
            fi
        fi

        if dpkg --compare-versions "$2" le "5.0"
        then
            echo "Enabling lighttpd skyaware module.." >&2
            lighty-enable-mod skyaware || true

        fi

        # dump1090-fa lighttpd module deprecated in 7.2
        if dpkg --compare-versions "$2" lt "7.2"
        then
	        if [ -e /etc/lighttpd/conf-enabled/89-dump1090-fa.conf ]
                then
	            echo "Disabling deprecated lighttpd dump1090-fa module..." >&2
	            lighty-disable-mod dump1090-fa || true
	        fi
	fi

        echo "Restarting lighttpd.." >&2
        invoke-rc.d lighttpd restart || true
    ;;

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

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

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

#DEBHELPER#

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