#!/bin/bash
source /root/envsetup/envsetup.conf

# Conf setup
sed -i "s@^NTP_SERVER1 = .*@NTP_SERVER1 = '${NTP}'@" /root/envsetup/conf.py
sed -i "s@^EMAIL_SMTP_SERVER = .*@EMAIL_SMTP_SERVER = '${EMAIL_SMTP_SERVER}'@" /root/envsetup/conf.py
sed -i "s@^MYSQL_ROOT_PWD = .*@MYSQL_ROOT_PWD = '${MYSQL_ROOT_PWD}'@" /root/envsetup/conf.py
# Wowza
sed -i "s@^wowza_server_name = .*@wowza_server_name = '${WOWZA}'@" /root/envsetup/conf.py
sed -i "s@^WOWZA_MANAGER_PWD = .*@WOWZA_MANAGER_PWD = '${WOWZA_MANAGER_PWD}'@" /root/envsetup/conf.py
sed -i "s@^WOWZA_LICENSE = .*@WOWZA_LICENSE = '${WOWZA_LICENSE}'@" /root/envsetup/conf.py
sed -i "s@^WOWZA_LIVE_PWD = .*@WOWZA_LIVE_PWD = '${WOWZA_LIVE_PWD}'@" /root/envsetup/conf.py
# MS
sed -i "s@^MS_SERVER_NAME = .*@MS_SERVER_NAME = '${MS_SERVER_NAME}'@" /root/envsetup/conf.py
sed -i "s@^MS_SUPERUSER_PWD = .*@MS_SUPERUSER_PWD = '${SUPERUSER_PWD}'@" /root/envsetup/conf.py
sed -i "s@^MS_ADMIN_PWD = .*@MS_ADMIN_PWD = '${ADMIN_PWD}'@" /root/envsetup/conf.py
if [ "${MS_ID}" = "" ]; then
	MS_ID=$(echo "$(hostname)_msuser")
fi
if [ "${MS_API_KEY}" = "" ]; then
	MS_API_KEY=$(echo "s$(pwgen 4)-$(pwgen 5)-$(pwgen 5)-$(pwgen 5)-$(pwgen 5)")
	# respect API pattern
	MS_API_KEY=$(echo $MS_API_KEY | sed "s@[iloILO]@$((${RANDOM} / 10000))@g")
fi
if [ "${MS_SECRET}" = "" ]; then
	MS_SECRET=$(echo "$(pwgen 40)")
fi
sed -i "s@^MS_ID = .*@MS_ID = '${MS_ID}'@" /root/envsetup/conf.py
sed -i "s@^MS_API_KEY = .*@MS_API_KEY = '${MS_API_KEY}'@" /root/envsetup/conf.py
sed -i "s@^MS_SECRET = .*@MS_SECRET = '${MS_SECRET}'@" /root/envsetup/conf.py
# SK
sed -i "s@^CM_SERVER_NAME = .*@CM_SERVER_NAME = '${CM}'@" /root/envsetup/conf.py
sed -i "s@^CM_SUPERUSER_PWD = .*@CM_SUPERUSER_PWD = '${CM_SUPERUSER_PWD}'@" /root/envsetup/conf.py
sed -i "s@^CM_ADMIN_PWD = .*@CM_ADMIN_PWD = '${CM_ADMIN_PWD}'@" /root/envsetup/conf.py
# Monitor
sed -i "s@^MONITOR_SERVER_NAME = .*@MONITOR_SERVER_NAME = '${MONITOR}'@" /root/envsetup/conf.py
sed -i "s@^MONITOR_SUPERUSER_PWD = .*@MONITOR_SUPERUSER_PWD = '${MONITOR_SUPERUSER_PWD}'@" /root/envsetup/conf.py
sed -i "s@^MONITOR_ADMIN_PWD = .*@MONITOR_ADMIN_PWD = '${MONITOR_ADMIN_PWD}'@" /root/envsetup/conf.py
# Worker
sed -i "s@^CELERITY_SIGNING_KEY = .*@CELERITY_SIGNING_KEY = '${CELERITY_SIGNING_KEY}'@" /root/envsetup/conf.py
# IP for whitelist
CELERITY_WORKER_IP=$(ip addr show | grep inet | grep -v 127 | grep -v ":" | awk -F " " '{print$2}' | awk -F "/" '{print$1}')
sed -i "s@^CELERITY_CELERITY_WORKER_IP = .*@CELERITY_CELERITY_WORKER_IP = '${CELERITY_WORKER_IP}'@" /root/envsetup/conf.py
