#!/bin/bash
source /root/envsetup/global-conf.sh

# install cm
apt-get install -y campus-manager
# configure nginx
sed -i "s@server_name skyreach;@server_name ${CM_SERVER_NAME};@" /etc/nginx/sites-available/skyreach.conf
service nginx restart

# install apt cacher
apt-get install -y apt-cacher-ng
# secure it
if ( ! rgrep 'AdminAuth: admin:' /etc/apt-cacher-ng/security.conf >/dev/null ); then
	APT_CACHER_PWD="$(pwgen 12)"
	echo "AdminAuth: admin:${APT_CACHER_PWD}" >> /etc/apt-cacher-ng/security.conf
	service apt-cacher-ng restart
fi

# iptables rules for port 3142
DEBIAN_FRONTEND=noninteractive apt-get install -y iptables-persistent
iptables -A INPUT -p tcp -s localhost --destination-port 3142 -j ACCEPT
iptables -A INPUT -p tcp --destination-port 3142 -j DROP
iptables-save
# the save using "service" command fails on VM
if ( rgrep '14.04' /etc/lsb-release >/dev/null ); then
	update-rc.d iptables-persistent enable
	service iptables-persistent save || true
else
	update-rc.d netfilter-persistent enable
	service netfilter-persistent save || true
fi

LIG=$(grep -n "^iface lo inet loopback" /etc/network/interfaces | awk -F ":" '{print$1}')
LIG=$(( ${LIG} + 1 ))

sed -i "${LIG}i\pre-up iptables-restore < /etc/iptables/rules.v4" /etc/network/interfaces
