#!/bin/sh

set -e

if ! [ -r /etc/openstack-cluster-installer/openstack-cluster-installer.conf ] ; then
	echo "Could not load /etc/openstack-cluster-installer/openstack-cluster-installer.conf"
	exit 1
else
	TMPFILE=$(mktemp -t openstack-cluster-installer.XXXXXX)
	cat /etc/openstack-cluster-installer/openstack-cluster-installer.conf | grep -v '^\[' >${TMPFILE}
	. ${TMPFILE}
	rm ${TMPFILE}
fi

if [ -x ./openstack-cluster-installer-build-live-image-clean ] ; then
	./openstack-cluster-installer-build-live-image-clean
else
	openstack-cluster-installer-build-live-image-clean
fi

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
		;;
	"--debian-mirror-addr")
		if [ -z "${2}" ] ; then echo "Parameter for option --debian-mirror-addr is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
		OTCI_DEB_MIRROR_ADDR=${2}
		shift
		shift
		;;
	*)
		;;
	esac
done
if [ -z "${OTCI_PXE_SERVER_IP}" ] ; then
	OTCI_PXE_SERVER_IP=$(ipcalc ${OPENSTACK_CLUSTER_NETWORK} | grep HostMin | awk '{print $2}')
        echo "No --pxe-server-ip given, using ${OTCI_PXE_SERVER_IP} as default." > /dev/stderr
        OTCI_PXE_SERVER_IP=${OTCI_PXE_SERVER_IP}
fi

if [ "${DO_EXIT}" = "yes" ] ; then
	echo "Parameters not validated: will exit now!" > /dev/stderr
	echo "Example call: $0 --pxe-server-ip ${OTCI_PXE_SERVER_IP} --debian-mirror-addr http://${OTCI_PXE_SERVER_IP}:9999/debian" > /dev/stderr
	exit 1
fi

# Manage ssh keys
if [ -e /etc/openstack-cluster-installer/id_rsa.pub ] ; then
	echo "Will use existing /etc/openstack-cluster-installer/id_rsa.pub file"
else
	echo "No ssh key found, generating one"
	( echo "" ; echo "" ) | ssh-keygen -t rsa -f /etc/openstack-cluster-installer/id_rsa
	chown www-data:www-data /etc/openstack-cluster-installer/id_rsa*
fi

mkdir -p config/includes.chroot/root/.ssh/
chmod 700 config/includes.chroot/root/.ssh/
cp /etc/openstack-cluster-installer/id_rsa.pub config/includes.chroot/root/.ssh/authorized_keys
chmod 600 config/includes.chroot/root/.ssh/authorized_keys

# Add our repository with updated openstack-debian-images
mkdir -p config/archives/
echo "deb http://stretch-queens.debian.net/debian stretch-queens-backports main
deb http://stretch-queens.debian.net/debian stretch-queens-backports-nochange main
" >config/archives/stretch-queens.list.chroot
cp /etc/openstack-cluster-installer/pubkey.gpg config/archives/stretch-queens.key.chroot

# Copy the agent
mkdir -p config/includes.chroot/usr/bin
cp /usr/bin/openstack-cluster-installer-agent config/includes.chroot/usr/bin
sed -i s/__OCI__GATEWAY__REPLACE__ME__/${OCI_IP}/ config/includes.chroot/usr/bin/openstack-cluster-installer-agent

# Install it in crontab
mkdir -p config/includes.chroot/etc/cron.d
echo "# Discovery agent

* * * * * root if [ -x /usr/bin/openstack-cluster-installer-agent ] ; then /usr/bin/openstack-cluster-installer-agent ; fi
" >config/includes.chroot/etc/cron.d/openstack-cluster-installer-agent

mkdir -p config/includes.chroot/etc/network
echo "auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp" >config/includes.chroot/etc/network/interfaces

mkdir -p config/package-lists
echo "bc
bind9-host
curl
dmidecode
debootstrap
dosfstools
extlinux
ipcalc
ipmitool
joe
kbd
kpartx
lshw
mbr
net-tools
nmap
ntp
openssh-server
openssh-client
openstack-debian-images
parted
pciutils
qemu-utils
syslinux-common
tcpdump
util-linux
wget" > config/package-lists/openstack.list.chroot

cp -auxf /usr/share/live/build/bootloaders config
	
lb clean
lb config --mirror-binary http://${OTCI_PXE_SERVER_IP}:9999/debian -b netboot --bootappend-live "boot=live systemd.show_status=true biosdevname=0 net.ifnames=0 components url=http://${OTCI_PXE_SERVER_IP} fetch=http://${OTCI_PXE_SERVER_IP}/openstack-cluster-installer/filesystem.squashfs" --net-root-path /var/lib/openstack-cluster-installer --net-root-server ${OTCI_PXE_SERVER_IP}

# Change the default mirror in the config
if [ -n "${OTCI_DEB_MIRROR_ADDR}" ] ; then
	for i in LB_PARENT_MIRROR_BOOTSTRAP LB_PARENT_MIRROR_CHROOT LB_PARENT_MIRROR_DEBIAN_INSTALLER LB_MIRROR_BOOTSTRAP LB_MIRROR_CHROOT LB_MIRROR_DEBIAN_INSTALLER \
		LB_PARENT_MIRROR_BINARY LB_MIRROR_BINARY ; do
		sed -i 's|^'${i}'=.*|'${i}'="'${OTCI_DEB_MIRROR_ADDR}'"|' config/bootstrap
	done
fi
sed -i 's/^LB_BOOTLOADERS=.*/LB_BOOTLOADERS="syslinux"/' config/binary

# Fix the default syslinux timeout to 5 seconds
sed -i "s/timeout 0/timeout 5/" config/bootloaders/isolinux/isolinux.cfg
sed -i "s/timeout 0/timeout 5/" config/bootloaders/pxelinux/pxelinux.cfg/default
sed -i "s/timeout 0/timeout 5/" config/bootloaders/syslinux/syslinux.cfg
sed -i "s/timeout 0/timeout 5/" config/bootloaders/extlinux/extlinux.conf

sed -i s/ftp.debian.org/${OTCI_PXE_SERVER_IP}:9999/ config/bootstrap

lb build

# Copy the tftp stuff
mkdir -p /var/lib/openstack-cluster-installer/tftp
cp -r tftpboot/* /var/lib/openstack-cluster-installer/tftp
mkdir -p /var/lib/openstack-cluster-installer/tftp/live
cp -auxf tftpboot/live/vmlinuz* tftpboot/live/initrd* /var/lib/openstack-cluster-installer/tftp/live
cp binary/live/filesystem.squashfs /var/lib/openstack-cluster-installer

# Create the ipxe-boot-script, needed for ipxe support.
echo "#!ipxe

chain tftp://${OTCI_PXE_SERVER_IP}/lpxelinux.0
" > /var/lib/openstack-cluster-installer/tftp/ipxe-boot-script

cp /usr/lib/PXELINUX/lpxelinux.0 /var/lib/openstack-cluster-installer/tftp

# dhcptd.conf must have something like this:
#subnet 192.168.111.0 netmask 255.255.255.0 {
#  range 192.168.111.2 192.168.111.2;
#  option routers 192.168.111.1;
#  next-server 192.168.111.1;
#  if exists user-class and option user-class = "iPXE" {
#    filename "http://192.168.111.1/openstack-cluster-installer/tftp/ipxe-boot-script";
#  } else {
#    filename "pxelinux.0";
#  }
#}
#
#host cluster-installer.debian.net { hardware ethernet 08:00:27:06:CC:DF; fixed-address 192.168.111.2; }
