#!/usr/bin/env bash

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

PROGNAME=$0;

ARGUMENT=$1;

PROJ_DIR="/var/www";

PROJ_NAME="grizzlyhockey";

PROJ_FULLNAME="${PROJ_DIR}/${PROJ_NAME}";

BACKEND_START_COMMAND="start.sh -r";

BACKEND_START_SCRIPT="sh ${PROJ_FULLNAME}/${BACKEND_START_COMMAND}";

NGINX_ENABLED_DIR="/etc/nginx/sites-enabled";

NGINX_AVAILABLE_DIR="/etc/nginx/sites-available";

NGINX_TARGET_FILE="001-grizzlyhockey-nginx";

NGINX_ENABLED_FILE="${NGINX_ENABLED_DIR}/${NGINX_TARGET_FILE}";

NGINX_AVAILABLE_FILE="${NGINX_AVAILABLE_DIR}/${NGINX_TARGET_FILE}";

NGINX_HOST=$(cat /etc/hostname);

NGINX_HOST=${NGINX_HOST%.yandex.ru};

NGINX_HOST=${NGINX_HOST%.yandex.net};

NGINX_FULLHOSTNAME=$(hostname -f);

NGINX_HOST_PATTERN="%HOST%";

NGINX_FULLHOSTNAME_PATTERN="%FULLHOSTNAME%";

warn () {
    echo "(${PROGNAME}):WARN: $@" >&2 ;
}

error () {
    echo "(${PROGNAME}):ERROR: $@ (X_x)" >&2 ; exit 1 ;
}

info() {
    echo "(${PROGNAME}):INFO: $@" >&2;
}

infod() {
    echo "(${PROGNAME}):INFO:       $@" >&2;
}

infoc() {
    echo "(${PROGNAME}):INFO:       $> $@" >&2;
}

config_backend(){
    info "Config backend ... ";
    info "Backend configured!";
}

start_backend(){
    info  "Starting backend ... ";
    infoc "${BACKEND_START_SCRIPT}";
    eval  "${BACKEND_START_SCRIPT}"     \
        && info "Backend up!"           \
        || error "Backend  down!"
}

config_available_file(){
    infod "Place actual hostname.";
    
    infoc "sed -i -e \"s/${NGINX_HOST_PATTERN}/${NGINX_HOST}/g\"";
    sed -i -e                                                   \
        "s/${NGINX_HOST_PATTERN}/${NGINX_HOST}/g"               \
        "${NGINX_AVAILABLE_FILE}";
}

config_enabled_file(){
    if [ ! -h "${NGINX_ENABLED_FILE}" ]; then
        infod "There is no '${NGINX_ENABLED_FILE}'. So create it!";
        infoc "ln -s '${NGINX_AVAILABLE_FILE}' '${NGINX_ENABLED_FILE}'";
        ln -s "${NGINX_AVAILABLE_FILE}" "${NGINX_ENABLED_FILE}";
    else
        infod "'${NGINX_AVAILABLE_FILE}' is changed...";
        infod "... So '${NGINX_ENABLED_FILE}' is also changed";
    fi
}

config_frontend(){
    info "Config frontend ... ";
    config_available_file;
    config_enabled_file;
    info "Frontend configured!";
}

start_frontend(){
    info "Starting frontend ...";
    if [ -f /var/run/nginx.pid ]; then
        infod "Trying to reload frontend ..."
        infoc "/etc/init.d/nginx reload "
        /etc/init.d/nginx reload            \
            && info "Backend reloaded!"     \
            || warn "Unable to reload frontend"
    else
        infod "Trying to restart frontend ... "
        infoc "/etc/init.d/nginx restart"
        /etc/init.d/nginx restart           \
            && info "Backend up!"           \
            || error "Unable to start frontend"
    fi
}

case "${ARGUMENT}" in
    configure)
        config_backend;  start_backend;
        config_frontend; start_frontend;
        ;;
    abort-upgrade|abort-remove|abort-deconfigure)   
        break;;
    *)
        error "postinst called with unknown argument \`${ARGUMENT}'";
    ;;
esac

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

#DEBHELPER#

exit 0;
