#! /bin/sh

set -eu

# Let's find out if a temporary build directory exists already
BUILD_DIR=$(find . -type d -name "tmp*" -print -quit 2>/dev/null)

# Create it if it doesn't
if ! echo "$BUILD_DIR" | grep -q .; then
  BUILD_DIR=$(mktemp --tmpdir=. --directory tmp.build.XXXXXXXXXX)
fi

# Preprocess the dotenv file
./src/esh -o "$BUILD_DIR"/.env ./.env

# Load the contents of the preprocessed dotenv file in order to
# use the values for init stage below
# https://www.shellcheck.net/wiki/SC1090
# shellcheck source=/dev/null
. "$BUILD_DIR"/.env

echo "
RELEASE_BASENAME=alpine-rpi-$ALPINE_VER_MAJOR.$ALPINE_VER_MINOR.$ALPINE_VER_PATCH-$ALPINE_ARCH
RELEASE_VERSION=v$ALPINE_VER_MAJOR.$ALPINE_VER_MINOR
ALPINE_DOCKER_IMAGE=alpine:$ALPINE_VER_MAJOR.$ALPINE_VER_MINOR.$ALPINE_VER_PATCH

wlan0_cidr=$wlan0_base.0/24
wlan0_address=$wlan0_base.1
wlan0_range_start=$wlan0_base.2
wlan0_range_end=$wlan0_base.254

wg0_cidr=$wg0_base.0/24
wg0_address=$wg0_base.12
wg0_gateway=$wg0_base.1" >> "$BUILD_DIR"/.env

echo "$BUILD_DIR"
