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

# Conf setup
sed -i "s@^system_ntp = .*@system_ntp = '${NTP}'@" /root/envsetup/conf.py
sed -i "s@^system_smtp = .*@system_smtp = '${CUST_SMTP}'@" /root/envsetup/conf.py
sed -i "s@^mysql_root_pwd = .*@mysql_root_pwd = '${MYSQLD_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_PASSWD}'@" /root/envsetup/conf.py
# MS
sed -i "s@^ms_server_name = .*@ms_server_name = '${MS}'@" /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
MS_ID=$(echo "$(hostname)_msuser")
MS_API=$(echo "s$(pwgen 4)-$(pwgen 5)-$(pwgen 5)-$(pwgen 5)-$(pwgen 5)")
# respect API pattern
MS_API=$(echo $MS_API | sed "s@[iloILO]@$((${RANDOM} / 10000))@g")
sed -i "s@^ms_id = .*@ms_id = '${MS_ID}'@" /root/envsetup/conf.py
sed -i "s@^ms_api_key = .*@ms_api_key = '${MS_API}'@" /root/envsetup/conf.py
MS_SECRET=$(echo "$(pwgen 40)")
sed -i "s@^ms_secret = .*@ms_secret = '${MS_SECRET}'@" /root/envsetup/conf.py
# SK
sed -i "s@^skyreach_server_name = .*@skyreach_server_name = '${CM}'@" /root/envsetup/conf.py
sed -i "s@^skyreach_superuser_pwd = .*@skyreach_superuser_pwd = '${CM_SUPERUSER_PWD}'@" /root/envsetup/conf.py
sed -i "s@^skyreach_admin_pwd = .*@skyreach_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 = '${CELERIY_SIGNING_KEY}'@" /root/envsetup/conf.py
# IP for whitelist
if [ "${IS_WORKER}" = "1" ]; then
	WORKER_IP=$(ip addr show | grep inet | grep -v 127 | grep -v ":" | awk -F " " '{print$2}' | awk -F "/" '{print$1}')
fi
sed -i "s@^celerity_worker_ip = .*@celerity_worker_ip = '${WORKER_IP}'@" /root/envsetup/conf.py
