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


case "$1" in
    configure)
        CONF=/etc/nntsc/nntsc.conf

        echo "============================================================"
        echo "Debian has old python packages, if you haven't already:"
        echo
        echo "pip install influxdb"
        if [ `lsb_release -c -s` = "wheezy" ]; then
            echo "pip install --upgrade pika"
        fi
        echo "============================================================"

        # ensure the cuz system user exists
        if ! getent passwd cuz > /dev/null; then
            adduser --quiet --system --group --no-create-home cuz
        fi

        # ensure the cuz database user exists
        su postgres -c "psql -tAc \"SELECT 1 FROM pg_roles WHERE rolname='cuz'\" | grep -q 1 || createuser cuz"

        # ensure the nntsc database exists
        su postgres -c "psql --list | cut -d ' ' -f 2 | grep -qx nntsc || \
            createdb -O cuz nntsc"

        # ensure the influx database exists
        influx -execute "show databases" | grep -qx nntsc || \
            influx -execute "CREATE DATABASE nntsc"

        # apply the schema and config to the nntsc database
        su cuz -s /bin/bash -c "build_nntsc_db -C $CONF"

        # ensure the nntsc rabbitmq user exists and the password matches the
        # configuration file
        PASSWD=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1`
        /usr/sbin/rabbitmqctl list_users | cut -f 1 | grep -qx nntsc || \
            (/usr/sbin/rabbitmqctl add_user nntsc $PASSWD && \
             /usr/sbin/rabbitmqctl set_permissions nntsc ".*" ".*" ".*" && \
             sed -i "s/username = guest/username = nntsc/" $CONF && \
             sed -i "s/password = guest/password = $PASSWD/" $CONF)

        if [ -n $2 ]; then
            if dpkg --compare-versions "$2" le-nl "2.11-1"; then
                # add new protocol column and update table constraints
                su postgres -c "psql -q -c \
                    \"ALTER TABLE streams_amp_throughput \
                    ADD COLUMN protocol \
                    VARCHAR NOT NULL DEFAULT 'default'\" -d nntsc || true"

                su postgres -c "psql -q -c \
                    \"ALTER TABLE streams_amp_throughput DROP CONSTRAINT \
                    streams_amp_throughput_source_destination_direction_address_key; \
                    \" -d nntsc || true"

                su postgres -c "psql -q -c \
                    \"ALTER TABLE streams_amp_throughput ADD CONSTRAINT \
                    streams_amp_throughput_source_destination_direction_address_key \
                    UNIQUE (            \
                        source,         \
                        destination,    \
                        direction,      \
                        address,        \
                        duration,       \
                        writesize,      \
                        tcpreused,      \
                        protocol);\" -d nntsc || true"
            fi
        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
