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

HORIZON_USER=horizon
HORIZON_GROUP=horizon

OPENSTACK_DASHBOARD_DIR=/usr/share/openstack-dashboard

MURANO_DASHBOARD_CACHE=/var/cache/muranodashboard-cache
MURANO_LOG_DIR=/var/log/murano


configure_dashboard() {
    mkdir -p $MURANO_DASHBOARD_CACHE
    chown -R $HORIZON_USER:$HORIZON_GROUP $MURANO_DASHBOARD_CACHE

    MURANO_DASHBOARD_CACHE=$MURANO_DASHBOARD_CACHE /usr/bin/modify-horizon-config.sh install

    if [ ! -d "$MURANO_LOG_DIR" ]; then
        mkdir -p $MURANO_LOG_DIR
    fi

    touch $MURANO_LOG_DIR/murano-dashboard.log
    chown $HORIZON_USER:$HORIZON_GROUP $MURANO_LOG_DIR/murano-dashboard.log

    mkdir -p $OPENSTACK_DASHBOARD_DIR/static/floppyforms
    chown -R $HORIZON_USER:$HORIZON_GROUP $OPENSTACK_DASHBOARD_DIR/static/floppyforms

    mkdir -p $OPENSTACK_DASHBOARD_DIR/static/muranodashboard
    chown -R $HORIZON_USER:$HORIZON_GROUP $OPENSTACK_DASHBOARD_DIR/static/muranodashboard
    service apache2 restart || (sleep 60 && service apache2 restart)
}

case "$1" in
    configure)
        configure_dashboard
    ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        exit 0
    ;;
    *)
        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.

exit 0
