#!/bin/sh

set -e

if [ -x ./openstack-tempest-ci-build-live-image-clean ] ; then
	./openstack-tempest-ci-build-live-image-clean
else
	openstack-tempest-ci-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
        echo "No --pxe-server-ip given, using 192.168.100.1 as default." > /dev/stderr
        OTCI_PXE_SERVER_IP=192.168.100.1
fi

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

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

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

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 "openssh-server
openssh-client
ntp
pciutils
tcpdump
net-tools
nmap
joe
wget
kbd" > config/package-lists/openstack.list.chroot

cp -auxf /usr/share/live/build/bootloaders config
	
lb clean
lb config -b netboot --bootappend-live "boot=live systemd.show_status=true components url=http://${OTCI_PXE_SERVER_IP} fetch=http://${OTCI_PXE_SERVER_IP}/tempest-ci/filesystem.squashfs" --net-root-path /var/lib/tempest-ci --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

lb build

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