#!/bin/sh
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

. /usr/share/debconf/confmodule
trap "db_stop || true" EXIT HUP INT QUIT TERM

PACKAGE=landscape-client

# Use the default installed Python 3.  Running just "python" might run
# something from /usr/local/bin, which doesn't necessarily support
# running the landscape client.
PYTHON=/usr/bin/python3

case "$1" in
    configure)

        CONFIG_FILE=/etc/landscape/client.conf
        if [ ! -f $CONFIG_FILE ]; then
            # Create new configuration, with private mode
            TEMPFILE=$(mktemp -p /etc/landscape)
            cat > $TEMPFILE <<END
[client]
log_level = info
url = https://landscape.canonical.com/message-system
ping_url = http://landscape.canonical.com/ping
data_path = /var/lib/landscape/client
END
            chown landscape $TEMPFILE
            mv $TEMPFILE $CONFIG_FILE

            # Get configuration values from debconf
            db_get $PACKAGE/computer_title
            COMPUTER_TITLE="${RET}"
            db_get $PACKAGE/account_name
            ACCOUNT_NAME="${RET}"
            db_get $PACKAGE/registration_key
            REGISTRATION_KEY="${RET}"
            db_get $PACKAGE/url
            URL="${RET}"
            db_get $PACKAGE/exchange_interval
            EXCHANGE_INTERVAL="${RET}"
            db_get $PACKAGE/urgent_exchange_interval
            URGENT_EXCHANGE_INTERVAL="${RET}"
            db_get $PACKAGE/ping_url
            PING_URL="${RET}"
            db_get $PACKAGE/ping_interval
            PING_INTERVAL="${RET}"
            db_get $PACKAGE/http_proxy
            HTTP_PROXY="${RET}"
            db_get $PACKAGE/https_proxy
            HTTPS_PROXY="${RET}"
            db_get $PACKAGE/tags
            TAGS="${RET}"

            # If we got the needed information, actually do the registration.
            if [ -n "$ACCOUNT_NAME" -a -n "$COMPUTER_TITLE" ]; then
                landscape-config --silent --ok-no-register \
                    --computer-title "$COMPUTER_TITLE" \
                    --account-name "$ACCOUNT_NAME" \
                    --registration-key "$REGISTRATION_KEY" \
                    --url "$URL" \
                    --exchange-interval "$EXCHANGE_INTERVAL" \
                    --urgent-exchange-interval "$URGENT_EXCHANGE_INTERVAL" \
                    --ping-url "$PING_URL" \
                    --ping-interval "$PING_INTERVAL" \
                    --http-proxy "$HTTP_PROXY" \
                    --https-proxy "$HTTPS_PROXY" \
                    --tags "$TAGS"
            fi
        else
            # Fix non-private permissions
            chmod 0600 $CONFIG_FILE
            chown landscape $CONFIG_FILE
        fi

        # Remove statoverride for smart-update, if there's one
        smart_update=/usr/lib/landscape/smart-update
        if dpkg-statoverride --list $smart_update >/dev/null 2>&1; then
            dpkg-statoverride --remove $smart_update
        fi

        # Add the setuid flag to apt-update, and make it be executable by
        # users in the landscape group (that normally means landscape itself)
        apt_update=/usr/lib/landscape/apt-update
        if ! dpkg-statoverride --list $apt_update >/dev/null 2>&1; then
            dpkg-statoverride --update --add root landscape 4754 $apt_update
        fi

        # In response to bug 1508110 we need to trigger a complete update of
        # user information.  The flag file will be removed by the client when
        # the update completes.
        DATA_PATH="`grep ^data_path /etc/landscape/client.conf | cut -d= -f2 | tr -d '[[:space:]]'`"
        if [ -z "$DATA_PATH" ]; then
            DATA_PATH=/var/lib/landscape/client
        fi
        install --owner=landscape --directory $DATA_PATH
        USER_UPDATE_FLAG_FILE="$DATA_PATH/user-update-flag"
        install --owner=landscape /dev/null $USER_UPDATE_FLAG_FILE
        echo "This file indicates that the Landscape client needs to send updated user information to the server." >> $USER_UPDATE_FLAG_FILE

        # To work around bug #1735100 we rewrite file-local landscape sources
        # with the trusted flag, as they have no release file, thus are
        # unsigned repositories. It exists while package profile is applying.
        LANDSCAPE_INTERNAL_SOURCES=/etc/apt/sources.list.d/_landscape-internal-facade.list
        if [ -f "$LANDSCAPE_INTERNAL_SOURCES" ]; then
            if grep -q -e "^deb file:" $LANDSCAPE_INTERNAL_SOURCES; then
                sed -i 's/^deb file:/deb [ trusted=yes ] file:/' $LANDSCAPE_INTERNAL_SOURCES
            fi
        fi

        # XXX handle regression with systemd 1.19.3 (disco+) refusing to stop
        # old sysvinit service if pid file is not root-owned. Thus we
        # chown it for the upggrade.
        if [ -f /etc/init.d/landscape-client ]; then
            chown -f root:root /var/run/landscape/landscape-client.pid || true
            invoke-rc.d landscape-client stop >/dev/null
            update-rc.d landscape-client remove >/dev/null
            rm -f /etc/init.d/landscape-client
        fi

        # Migrate old sysvinit config to systemd
        DEFAULTS=/etc/default/landscape-client
        if [ -f $DEFAULTS ]; then
            RUN=$(. $DEFAULTS; echo $RUN)
            if [ "$RUN" = "1" ]; then
                systemctl enable landscape-client.service
            fi
            rm -f $DEFAULTS
        fi
    ;;

    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#

exit 0
