#!/bin/sh -e

SOLOMON_ROOT="/Berkanavt/solomon"
CONFDIR="/etc/yandex/statbox-push-client"
SEC_FILE="/Berkanavt/keys/solomon/push_tvm.secret"

ENV_FILE="/etc/solomon/env"

case "$1" in
    configure)
        if [ -r "$ENV_FILE" ] ; then
            ENV="$(cat $ENV_FILE)"
        else
            echo "Cannot read environment file $ENV_FILE"
            exit 1
        fi

        case $ENV in
            cloud-preprod)
                ln -sf ${CONFDIR}/push-client.yaml_preprod ${CONFDIR}/push-client.yaml
                ;;
            cloud-prod)
                ln -sf ${CONFDIR}/push-client.yaml_prod ${CONFDIR}/push-client.yaml
                ;;
            *)
                echo "Cannot determine environment: $ENV"
                exit 1
                ;;
        esac
        chown statbox $SEC_FILE

        if ! systemctl is-active statbox-push-client >/dev/null 2>&1 ; then
            systemctl restart statbox-push-client || true
        fi
        ;;
    *)
        ;;
esac
