#!/bin/bash

set -e
export DOCKER_BUILDKIT=1

TAG=${TAG:-main}

build() {
  TEMPDIR=$(mktemp -d)
  mkdir ${TEMPDIR}/iso
  # passing a config file? Copy it to the target that will get copied inside the build
  if [[ -n "${CLOUD_CONFIG_FILE}" ]]; then
    echo "Using custom registration from ${CLOUD_CONFIG_FILE}"
    cp ${CLOUD_CONFIG_FILE} ${TEMPDIR}/iso/config
  fi
  curl https://raw.githubusercontent.com/rancher/elemental/${TAG}/Dockerfile.iso -fsSL -o ${TEMPDIR}/Dockerfile
  curl https://raw.githubusercontent.com/rancher/elemental/${TAG}/iso/manifest.yaml -fsSL -o ${TEMPDIR}/iso/manifest.yaml
  # Did we copy the custom cloud config? Then no need to download the default one
  if [[ -z "${CLOUD_CONFIG_FILE}" ]]; then
    echo "No custom registration set, using default"
    curl https://raw.githubusercontent.com/rancher/elemental/${TAG}/iso/config -fsSL -o ${TEMPDIR}/iso/config
  fi

  pushd ${TEMPDIR} || exit 1
  docker build -f ${TEMPDIR}/Dockerfile -t elemental/iso:latest .
  popd || exit 1

  rm -Rf ${TEMPDIR}
  ISO="elemental-teal.$(uname -m)-${TAG}-$(date "+%FT%TZ").iso"
  docker run --rm -v ${PWD}:/mnt elemental/iso:latest cp elemental-iso/elemental-teal.$(uname -m)-latest.iso /mnt/${ISO}
  echo "INFO: ISO generated at ${ISO}"
}

CLOUD_CONFIG_FILE=${1:-}

build
