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

echo -e "
${RED}Customer parameters will be applied after rebooting. Note that you won't be able to login to the machine after this operation !${NC}"	

# set customer IP (/etc/network/interfaces)
sed -i "s@192\.168\.40\.10.*@${NETWORK_IP}@" /etc/network/interfaces
sed -i "s@255\.255\.252\..*@${NETWORK_MASK}@" /etc/network/interfaces
sed -i "s@192\.168\.40\..*@${NETWORK_GATEWAY}@" /etc/network/interfaces

# backup-server specific
if ( test -f /usr/sbin/burp )
then
# changer burp status IP
sed -i "s@^BURP_CLIENT_MAIL_DEST=.*@BURP_CLIENT_MAIL_DEST=${BURP_CLIENT_MAIL_DEST}@" /root/burp-custom.sh
sed -i "s@admin: .*@admin: ${BURP_CLIENT_MAIL_DEST}" /etc/aliases
newaliases
fi

# set customer DNS
echo "nameserver ${NETWORK_DNS1}" > /etc/resolv.conf
echo "nameserver ${NETWORK_DNS2}" >> /etc/resolv.conf

# set customer NTP
sed -i "s@^server .*@@" /etc/ntp.conf
echo "" >> /etc/ntp.conf
echo "server ${NTP_SERVER1}" >> /etc/ntp.conf

if ( ! test -z ${NTP_SERVER2} )
then
echo "server ${NTP_SERVER2}" >> /etc/ntp.conf
fi

if ( ! test -z ${NTP_SERVER3} )
then
echo "server ${NTP_SERVER3}" >> /etc/ntp.conf
fi
service ntp restart

# proxy
if [ ${PROXY} = "1" ]
then
	if [ ${PROXY_AUTHENTICATION} = "1" ]
	then
	# APT
	echo "Acquire::http::Proxy \"http://${PROXY_USER}:${PROXY_PASSWD}@${PROXY_HTTP}:${PROXY_PORT}\";" > /etc/apt/apt.conf.d/proxy
	echo "Acquire::https::Proxy \"http://${PROXY_USER}:${PROXY_PASSWD}@${PROXY_HTTP}:${PROXY_PORT}\";" >> /etc/apt/apt.conf.d/proxy
	# /etc/environment
	echo "http_proxy=\"http://${PROXY_USER}:${PROXY_PASSWD}@${PROXY_HTTP}:${PROXY_PORT}\"" >> /etc/environment
	echo "https_proxy=\"http://${PROXY_USER}:${PROXY_PASSWD}@${PROXY_HTTP}:${PROXY_PORT}\"" >> /etc/environment
	else
	# APT
	echo "Acquire::http::Proxy \"http://${PROXY_HTTP}:${PROXY_PORT}\";" > /etc/apt/apt.conf.d/proxy
	echo "Acquire::https::Proxy \"http://${PROXY_HTTP}:${PROXY_PORT}\";" >> /etc/apt/apt.conf.d/proxy
	# /etc/environment
	echo "http_proxy=\"http://${PROXY_HTTP}:${PROXY_PORT}\"" >> /etc/environment
	echo "https_proxy=\"http://${PROXY_HTTP}:${PROXY_PORT}\"" >> /etc/environment
	fi
fi

# MS specific
if ( test -d /usr/local/WowzaStreamingEngine )
then
if [ ${PROXY} = "1" ]
then
# wowza
NB=$(grep -n 'Properties defined here will be added to the IServer.getProperties()' /usr/local/WowzaStreamingEngine/conf/Server.xml | awk -F ":" '{print$1}')
NB_1=$(( ${NB} + 1 ))
NB_MAX=$(wc -l /usr/local/WowzaStreamingEngine/conf/Server.xml | awk '{print$1}')
	if [ ${PROXY_AUTHENTICATION} = "1" ]
	then
	sed -i "${NB_1},${NB_MAX}d" /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Properties>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Name>licenseServerProxyAddress</Name>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Value>${PROXY_HTTP}</Value>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Name>licenseServerProxyPort</Name>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Value>${PROXY_PORT}</Value>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Type>Integer</Type>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Name>licenseServerProxyUsername</Name>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Value>${PROXY_USER}</Value>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Name>licenseServerProxyPassword</Name>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Value>${PROXY_PASSWD}</Value>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Properties>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Server>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Root>	" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	else
	sed -i "${NB_1},${NB_MAX}d" /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Properties>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Name>licenseServerProxyAddress</Name>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Value>${PROXY_HTTP}</Value>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Name>licenseServerProxyPort</Name>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Value>${PROXY_PORT}</Value>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "<Type>Integer</Type>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Property>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Properties>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Server>" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	echo "</Root>	" >> /usr/local/WowzaStreamingEngine/conf/Server.xml
	fi
fi
fi

# set email sender
if ( ! test -z ${EMAIL_SENDER} )
then
echo "#DEFAULT_FROM_EMAIL = '${EMAIL_SENDER}'" >> /etc/mediaserver/msconf.py
echo "#DEFAULT_FROM_EMAIL = '${EMAIL_SENDER}'" >> /home/skyreach/htdocs/skyreach_site/settings_override.py
fi

# set locale
echo -e "${CYAN}You will now change the locales.${NC}"
locale-gen en_GB.UTF-8
dpkg-reconfigure locales
update-locale LANG=en_GB.UTF-8 LANGUAGE=en_GB.UTF-8 LC_ALL=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
#dpkg-reconfigure console-data
#dpkg-reconfigure keyboard-configuration

# set TZ
echo -e "${CYAN}You will now change the time zone.${NC}"
dpkg-reconfigure tzdata
