#!/bin/sh

set -e
set -x

PXE_NIC_NAME=tempestnic0
PXE_VM_NIC_USER=zigo
PXE_VM_VIRTAP_NAME=tempesttap

for i in $@ ; do
	case "${1}" in
	"--pxe-server-ip")
		if [ -z "${2}" ] ; then echo "Parameter for option --pxe-server-ip is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_PXE_SERVER_IP=${2}
		shift
		shift
		;;
	"--configure-dummy-nick")
		OTCI_CONFIGURE_DUMMY_NICK=yes
		shift
		;;
	"--undo-dummy-nick-setup")
		OTCI_UNCONFIGURE_DUMMY_NICK=yes
		;;
	*)
		;;
	esac
done

if [ "${OTCI_UNCONFIGURE_DUMMY_NICK}" = "yes" ] ; then
	brctl delif tempestbr ${PXE_NIC_NAME} || true
	brctl delif tempestbr ${PXE_VM_VIRTAP_NAME} || true
	brctl delbr tempestbr || true
	ip link delete ${PXE_VM_VIRTAP_NAME} || true
	rmmod dummy
	exit 0
fi

if [ -z "${OTCI_PXE_SERVER_IP}" ] ; then
	echo "No --pxe-server-ip given, using 192.168.100.1 as default." > /dev/stderr ; DO_EXIT="yes"
	OTCI_PXE_SERVER_IP=192.168.100.1
fi

otci_enable_nested_virt () {
	if [ -e /sys/module/kvm_intel/parameters/nested ] ; then
		NESTED_FLAG=$(cat /sys/module/kvm_intel/parameters/nested)
		if [ "${NESTED_FLAG}" = "N" ] ; then
			rmmod kvm-intel
			sh -c "echo 'options kvm-intel nested=y' >> /etc/modprobe.d/dist.conf"
			modprobe kvm-intel
		fi
	fi
}

otci_calc_network_config () {
	OTCI_PXE_NETWORK=$(ipcalc ${OTCI_PXE_SERVER_IP}/24 | grep Network | awk '{print $2}' | cut -d/ -f1)
	OTCI_PXE_NETMASK=$(ipcalc ${OTCI_PXE_SERVER_IP}/24 | grep Netmask | awk '{print $2}')
	OTCI_PXE_DIGIT1=$(echo ${OTCI_PXE_SERVER_IP} | cut -d. -f1)
	OTCI_PXE_DIGIT2=$(echo ${OTCI_PXE_SERVER_IP} | cut -d. -f2)
	OTCI_PXE_DIGIT3=$(echo ${OTCI_PXE_SERVER_IP} | cut -d. -f3)
	OTCI_PXE_DIGIT4=$(echo ${OTCI_PXE_SERVER_IP} | cut -d. -f4)
	OTCI_PXE_PREFIX=${OTCI_PXE_DIGIT1}.${OTCI_PXE_DIGIT2}.${OTCI_PXE_DIGIT3}
	OTCI_PXE_LIVE_IP=${OTCI_PXE_DIGIT1}.${OTCI_PXE_DIGIT2}.${OTCI_PXE_DIGIT3}.$((${OTCI_PXE_DIGIT4} + 1))
}

otci_setup_dummy_nic () {
	# Create a dummy virtual interface
	modprobe dummy
	ip link set name ${PXE_NIC_NAME} dev dummy0
	ifconfig ${PXE_NIC_NAME} hw ether 00:22:22:ff:ff:ff

	# Create a bridge and bridge that interface to it
	ip tuntap add dev ${PXE_VM_VIRTAP_NAME} mode tap user ${PXE_VM_NIC_USER}
	brctl addbr tempestbr
	brctl addif tempestbr ${PXE_NIC_NAME}
	brctl addif tempestbr ${PXE_VM_VIRTAP_NAME}
	ifconfig tempestbr ${OTCI_PXE_SERVER_IP} netmask ${OTCI_PXE_NETMASK} up
	ip link set tempestbr up
	ip link set ${PXE_NIC_NAME} up
	ip link set ${PXE_VM_VIRTAP_NAME} up
}

otci_setup_tftp_server () {
	sed -i 's/[ \t]*TFTP_ADDRESS[ \t]*=.*/TFTP_ADDRESS="'${OTCI_PXE_SERVER_IP}':69"/' /etc/default/tftpd-hpa
	sed -i 's|[ \t]*TFTP_DIRECTORY[ \t]*=.*|TFTP_DIRECTORY="/var/lib/tempest-live-booter/tftp"|' /etc/default/tftpd-hpa
}

otci_setup_dhcpd () {
	echo "interface=tempestbr
dhcp-range=${OTCI_PXE_PREFIX}.10,${OTCI_PXE_PREFIX}.99,255.255.255.0,12h
dhcp-option=option:netmask,255.255.255.0
dhcp-option=option:router,${DHCP_SERVER_SUBNET}.1
dhcp-option=option:dns-server,8.8.8.8

dhcp-vendorclass=BIOS,PXEClient:Arch:00000
dhcp-vendorclass=UEFI,PXEClient:Arch:00007
dhcp-vendorclass=UEFI64,PXEClient:Arch:00009

dhcp-boot=net:BIOS,pxelinux.0,,${OTCI_PXE_SERVER_IP}
dhcp-boot=net:UEFI,shimx64.efi.signed,,${OTCI_PXE_SERVER_IP}
dhcp-boot=net:UEFI64,shimx64.efi.signed,,${OTCI_PXE_SERVER_IP}
" > /etc/dnsmasq.conf

	mkdir -p /var/lib/tempest-live-booter/tftp
	cp /usr/lib/PXELINUX/lpxelinux.0 /var/lib/tempest-live-booter/tftp
}

otci_setup_scratch_disk () {
	qemu-img create /var/lib/tempest-live-booter/tempest-scratch-disk.dat 100G
}

otci_enable_nested_virt
otci_calc_network_config
if [ "${OTCI_CONFIGURE_DUMMY_NICK}" = "yes" ] ; then
	otci_setup_dummy_nic
fi
otci_setup_tftp_server
otci_setup_dhcpd
otci_setup_scratch_disk
service isc-dhcp-server restart
service tftpd-hpa restart
