#!/bin/sh -e

APP_ROOT="/Berkanavt/yasm/gateway"
LOGS_DIR="${APP_ROOT}/../logs"

USER="user_yasmgateway"
GROUP="group_solomon"
SVC="yandex-yasm-gateway"
CONFFILE="/etc/nginx/nginx-solomon.conf"

case "$1" in
    configure)
        /bin/chown ${USER}:${GROUP} ${APP_ROOT}

        if [ ! -e ${LOGS_DIR} ]; then
            /usr/bin/install -g ${GROUP} -m 775 -d ${APP_ROOT}/../logs
        fi

        if [ -x /bin/systemctl -a -f /etc/systemd/system/${SVC}.service ] ; then
            if ! /bin/systemctl restart ${SVC} ; then
                echo "Cannot restart ${SVC} service!"
            fi
        elif [ -x /usr/bin/service -a -f /etc/init/${SVC}.conf ] ; then
            if ! /usr/bin/service ${SVC} restart ; then
                echo "Cannot restart ${SVC} service!"
            fi
        fi

        ln -sf /etc/nginx/sites-available/yasm-front.conf /etc/nginx/sites-enabled/yasm-front.conf
        if which nginx >/dev/null && pgrep -ox nginx >/dev/null && nginx -qt -c $CONFFILE ; then
            nginx -s reload -c $CONFFILE
        else
            echo "Ningx configuration failed"
        fi
    ;;
esac
