#!/bin/bash

[ $UID == 0 ] || { echo "Wrong user! Execute this script as root";  exit 1; }
[[ `hostname` =~ ^lab-(sec|net)- ]] || { echo "Wrong lab! Execute this script only in lab-sec and lab-net";  exit 1; }
(( `hostname|cut -d- -f3-` <= 16 )) || { echo "Wrong OS! Execute this script only in Local Linux"; exit 1; }

while read -p "Input the    domain name you will configure: " DOMAIN JUNK ; do
	[[ $JUNK ]] && { echo "  domain names cannot contain whitespaces"; continue; }
	[[ $DOMAIN != *.put ]]  && { echo "  your domain must be a subdomain of .put"; continue; }
	break
done

while read -p "Input the subdomain name you will configure: " SUBDOM JUNK; do
	[[ $JUNK ]] && { echo "  domain names cannot contain whitespaces"; continue; }
	[[ $SUBDOM != *$DOMAIN ]]  && { echo "  your domain must be a subdomain of $DOMAIN"; continue; }
	break
done

N=`hostname|cut -d- -f3-`

ip -br -4 address sh dev br0 | grep -q 192.168.$N.1 || {
	echo "Assigning address 192.168.$N.1 to br0"
	ip address add 192.168.$N.1/16 dev br0
}

[[ `grep ^nameserver /etc/resolv.conf | head -n1` == "nameserver 192.168.0.1" ]] || {
	echo "Changing system default DNS server to 192.168.0.1"
	sed -i 's/NETCONFIG_DNS_STATIC_SERVERS="[^"]*"/NETCONFIG_DNS_STATIC_SERVERS="192.168.0.1"/' /etc/sysconfig/network/config
	netconfig update -f
}

if [ -e /var/lib/lxc/www ] && [ -e /var/lib/lxc/mx ] && [ -e /var/lib/lxc/www2 ]; then
	echo "Cleaning old LXC images"
	lxc-stop -q -k -n www; lxc-stop -q -k -n mx; lxc-stop -q -k -n www2;
	rm -rf /var/lib/lxc/{www,mx,www2}/rootfs-overlay/*
else
	echo "Fetching & unpacking LXC images"
	( cd /var/lib/lxc && curl --silent https://www.cs.put.poznan.pl/jkonczak/_media/sk2:dns-lab-lxc.tar.xz | tar xJ )
fi

while read VM IP NAME; do
ed -s /var/lib/lxc/$VM/config <<- EOF
	/^lxc.net.0.ipv4.address/c
	lxc.net.0.ipv4.address = 192.168.$N.$IP/16
	.
	/^lxc.uts.name/c
	lxc.uts.name = $NAME
	.
	w
	EOF
done <<- EOF
	www  2 $DOMAIN
	www2 3 $SUBDOM
	mx   4 $DOMAIN
	EOF

echo "Starting (or restarting) LXC containers"
systemctl --quiet restart lxc
lxc-start -n www; lxc-start -n mx; lxc-start -n www2;

box(){
	TEXT=$1 POS=$2 SYM=${3:-}
	read C < <(awk '{l=length();m=m>l?m:l}END{print m}' <<< "$TEXT")
	printf "\e[${POS}G┬%${C}s┐\n" "" | sed 's/ /─/g'
	while read; do
		printf "$SYM\e[${POS}G│%-${C}s│\n" "$REPLY"
	done <<< "$TEXT"
	printf "$SYM"
	printf "\e[${POS}G└%${C}s┘\n" "" | sed 's/ /─/g'
}

cat << EOF
__________________________________________
The following network has been configured:

 (internet)
     ┆
┌────┴─────┐     local subnet: 192.168.0.0/16
│ ⇇switch⇉ │
└┬──┬──┬──┬┘
EOF
printf " ┆  ┆  ┆  └┄┄┄"
box $'Your computer      IP addres: '192.168.$N.1$'\nRole: DNS server for domain: '$DOMAIN 15 " ┆  ┆  ┆"
printf " ┆  ┆  └┄┄┄┄┄┄"
box $'Computer "www"     IP addres: '192.168.$N.2$'\nRole: HTTP server\nWants to host the website at http://'$DOMAIN 15 " ┆  ┆"
printf " ┆  └┄┄┄┄┄┄┄┄┄"
box $'Computer "www2"    IP addres: '192.168.$N.3$'\nRole: HTTP server\nWants to host the website at http://'$SUBDOM 15 " ┆"
printf " └┄┄┄┄┄┄┄┄┄┄┄┄"
box $'Computer "mx"      IP addres: '192.168.$N.4$'\nRole: mail server\nWants to handle e-mails sent to addresses: ...@'$DOMAIN 15 ""
