#!/bin/bash
# postinst script for yandex-bugbounty-internal
#
# 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

if [ -e /etc/yandex/environment.type ]; then
    yandex_environment=$(cat /etc/yandex/environment.type)
fi

DJANGO_USER="www-data"
DJANGO_GROUP="www-data"
LOG_DIR="/var/log/yandex/bugbounty-internal"
RUN_DIR="/var/run/yandex/bugbounty-internal"

old_settings_py="/usr/lib/yandex/yandex-bugbounty-internal/settings/local_settings.py"
settings_py="/etc/yandex/bugbounty-internal/local_settings.py"

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

#DEBHELPER#

case "$1" in
    configure)
	for DIR in $LOG_DIR $RUN_DIR; do
		chown -R $DJANGO_USER:$DJANGO_GROUP $DIR
		chmod -R g+w $DIR
	done
	# local_settings contains template for passwords
	cp /usr/share/doc/yandex-bugbounty-internal/examples/local_settings.py "${settings_py}.example"
	# DB migrations
	chmod +x /usr/lib/yandex/yandex-bugbounty-internal/manage.py
	# no needed anymore - we do it in init script
	#supervisorctl reread
	#supervisorctl update
	#if [ -n "${2}" ]
	#then
	#else
	#	invoke-rc.d yandex-bugbounty-internal start
	#fi
    ;;

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

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

exit 0
