#!/bin/bash
# postinst script for yandex-stocks3
#
# see: dh_installdeb(1)

set -e

STOCKS_DIR="/opt/www/stocks3"
ENVIRONMENT_FILE="/etc/yandex/environment.type"
DEFAULT_CONFIGURATION="production"

if [ -f "$ENVIRONMENT_FILE" ]; then
    env_type=`cat ${ENVIRONMENT_FILE}`
else
    env_type=${DEFAULT_CONFIGURATION}
fi

if [[ "$env_type" == "production" || "$env_type" == "unstable" ]]; then
    cp ${STOCKS_DIR}/config/config.production.json ${STOCKS_DIR}/config/config.json
    cp ${STOCKS_DIR}/config/db.production.xml ${STOCKS_DIR}/config/db.xml
    cp ${STOCKS_DIR}/libs/planner/config.production.json ${STOCKS_DIR}/libs/planner/config.json
    cp ${STOCKS_DIR}/libs/planner/cron/plannerrunner /etc/cron.d/
else
    cp ${STOCKS_DIR}/config/config.dev.json ${STOCKS_DIR}/config/config.json
    cp ${STOCKS_DIR}/config/db.dev.xml ${STOCKS_DIR}/config/db.xml
    cp ${STOCKS_DIR}/libs/planner/config.dev.json ${STOCKS_DIR}/libs/planner/config.json
    cp ${STOCKS_DIR}/libs/planner/cron/plannerrunner /etc/cron.d/
fi


case "$1" in
    configure)
        adduser --quiet --system --home /home/morda --shell /bin/sh --group morda || echo "The user morda already exists."
        chown -R morda:morda /home/morda
        chown -R morda:morda /opt/www/stocks3
    ;;

    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
