#!/bin/bash

# Если заполнить FORCE_TO_PRESTABLE_SECRET, то можно будет ходить на prestable или stable принудительно.
# Для этого в браузере нужно будет добавить заголовок:
# X-Force-To-Prestable: <FORCE_TO_PRESTABLE_SECRET> для prestable
# X-Force-To-Prestable: stop для stable

APPLICATION_PROBABILITY=${APPLICATION_PROBABILITY:-100}
PRESTABLE_HOST=${PRESTABLE_HOST:-prestable.morda-front.rasp.common.yandex.ru}
TOUCH_PRESTABLE_HOST=${TOUCH_PRESTABLE_HOST:-t.prestable.morda-front.rasp.common.yandex.ru}
FORCE_TO_PRESTABLE_SECRET=${FORCE_TO_PRESTABLE_SECRET:-prestable}

if [ "$YENV_TYPE" = "production" ]; then
    MORDA_OLD_URL=${MORDA_OLD_URL:-"https://production.old-morda-python.rasp.common.yandex.ru"}
    TOUCH_OLD_URL=${TOUCH_OLD_URL:-"https://production.touch.rasp.common.yandex.ru"}
    RASP_PUBLIC_BUCKET_URL=${RASP_PUBLIC_BUCKET_URL:-"http://rasp.s3.yandex.net"}
fi

# testing and others
MORDA_OLD_URL=${MORDA_OLD_URL:-"https://testing.old-morda-python.rasp.common.yandex.ru"}
TOUCH_OLD_URL=${TOUCH_OLD_URL:-"https://testing.touch.rasp.common.yandex.ru"}
RASP_PUBLIC_BUCKET_URL=${RASP_PUBLIC_BUCKET_URL:-"http://rasp-test-bucket.s3.mdst.yandex.net"}

function replace_env() {
    env_name=$1
    file=/etc/nginx/$2
    env $env_name="${!env_name}" \
    envsubst '$'"$1" < $file > /tmp/nginx_file && mv /tmp/nginx_file $file
}

replace_env MORDA_OLD_URL includes/morda-old
replace_env TOUCH_OLD_URL includes/touch-old
replace_env RASP_PUBLIC_BUCKET_URL includes/sitemaps
replace_env APPLICATION_PROBABILITY sites-enabled/50-application.conf
replace_env PRESTABLE_HOST sites-enabled/50-application.conf
replace_env TOUCH_PRESTABLE_HOST sites-enabled/50-application.conf
replace_env FORCE_TO_PRESTABLE_SECRET sites-enabled/50-application.conf
