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

    useradd lunapark || true

    	if [ ! -e /var/lib/tankapi ]
		then
			mkdir -p /var/lib/tankapi
			chown lunapark:users /var/lib/tankapi
		fi

		if [ -e /var/lib/tankapi/server.log ]
		then
		    chown lunapark:users /var/lib/tankapi/server.log
		fi

		mkdir -p /var/lib/tankapi/tests
		chown lunapark:users /var/lib/tankapi/tests

		mkdir -p /var/lib/tankapi/tests/stpd-cache
		chown lunapark:users /var/lib/tankapi/tests/stpd-cache
		chmod a+rwx,+t /var/lib/tankapi/tests/stpd-cache

    if [ ! -e /var/lib/bdk ] 
    then
      mkdir -p /var/lib/bdk
      chown lunapark:users /var/lib/bdk
    fi
    
    mkdir -p /var/lib/bdk/tests
    chown lunapark:users /var/lib/bdk/tests

    mkdir -p /var/lib/bdk/tests/stpd-cache
    chown lunapark:users /var/lib/bdk/tests/stpd-cache
    chmod a+rwx,+t /var/lib/bdk/tests/stpd-cache


  		while ls /var/lock/lunapark_*.lock 2>/dev/null ; do
  		    echo Waiting for existing lock files...
  		    sleep 1
  		done

    ;;

    triggered)
  		echo Restarting Yandex.Tank API Server...
  		while ls /var/lock/lunapark_*.lock 2>/dev/null ; do
  		    echo Waiting for existing lock files...
  		    sleep 1
  		done

	;;

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

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

# This will only remove masks created by d-s-h on package removal.
deb-systemd-helper unmask tankapi.service >/dev/null || true

# was-enabled defaults to true, so new installations run enable.
if deb-systemd-helper --quiet was-enabled tankapi.service; then
        # Enables the unit on first installation, creates new
        # symlinks on upgrades if the unit file has changed.
        deb-systemd-helper enable tankapi.service >/dev/null || true
else
        # Update the statefile to add new symlinks (if any), which need to be
        # cleaned up on purge. Also remove old symlinks.
        deb-systemd-helper update-state tankapi.service >/dev/null || true
fi

# This will only remove masks created by d-s-h on package removal.
deb-systemd-helper unmask bdk.service >/dev/null || true

# was-enabled defaults to true, so new installations run enable.
if deb-systemd-helper --quiet was-enabled bdk.service; then
        # Enables the unit on first installation, creates new
        # symlinks on upgrades if the unit file has changed.
        deb-systemd-helper enable bdk.service >/dev/null || true
else
        # Update the statefile to add new symlinks (if any), which need to be
        # cleaned up on purge. Also remove old symlinks.
        deb-systemd-helper update-state bdk.service >/dev/null || true
fi

# This will only remove masks created by d-s-h on package removal.
deb-systemd-helper unmask tankapi.service >/dev/null || true

# was-enabled defaults to true, so new installations run enable.
if deb-systemd-helper --quiet was-enabled tankapi.service; then
        # Enables the unit on first installation, creates new
        # symlinks on upgrades if the unit file has changed.
        deb-systemd-helper enable tankapi.service >/dev/null || true
else
        # Update the statefile to add new symlinks (if any), which need to be
        # cleaned up on purge. Also remove old symlinks.
        deb-systemd-helper update-state tankapi.service >/dev/null || true
fi

# This will only remove masks created by d-s-h on package removal.
deb-systemd-helper unmask tankapi.service >/dev/null || true

# was-enabled defaults to true, so new installations run enable.
if deb-systemd-helper --quiet was-enabled tankapi.service; then
        # Enables the unit on first installation, creates new
        # symlinks on upgrades if the unit file has changed.
        deb-systemd-helper enable tankapi.service >/dev/null || true
else
        # Update the statefile to add new symlinks (if any), which need to be
        # cleaned up on purge. Also remove old symlinks.
        deb-systemd-helper update-state tankapi.service >/dev/null || true
fi

if [ -d /run/systemd/system ]; then
        systemctl --system daemon-reload >/dev/null || true
        deb-systemd-invoke start bdk.service tankapi.service >/dev/null || true
fi

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