#!/bin/sh

set -e

if ! [ -r /etc/oci-poc/oci-poc.conf ] ; then
	echo "Cannot load /etc/oci-poc/oci-poc.conf: exiting."
	exit 1
fi

. /etc/oci-poc/oci-poc.conf


if ! ls debian-*-octavia-amphora-*.qcow2 2>/dev/null 1>/dev/null ; then
	if ! dpkg-query -W openstack-debian-images ; then
		apt-get install openstack-debian-images -y
	fi

	if ! dpkg-query -W lsb-release ; then
		apt-get install lsb-release -y
	fi

	CODE_NAME=$(lsb_release -c -s 2>/dev/null)
	if [ -n "${openstack_release}" ] ; then
		export OPENSTACK_RELEASE=${openstack_release}
	fi
	/usr/share/doc/openstack-debian-images/examples/octavia/amphora-build ${debian_release} ${DEBIAN_MIRROR} ${DEBIAN_SECURITY_MIRROR}
fi

AMPHORA_IMAGE_FILE=$(ls debian-*-octavia-amphora-*.qcow2)

. /root/oci-openrc
AMPHORA_IMAGE_ID=$(openstack image list --tag amphora -f value -c ID 2>/dev/null)
if [ -z "${AMPHORA_IMAGE_ID}" ] ; then
	openstack image create --container-format bare --disk-format qcow2 --file "${AMPHORA_IMAGE_FILE}" --tag amphora "${AMPHORA_IMAGE_FILE}"
	AMPHORA_IMAGE_ID=$(openstack image list --tag amphora -f value -c ID 2>/dev/null)
fi
