#!/bin/bash

# Abort if anything fails
set -e

# Ensure we have the submodule checked out
git submodule update --init --recursive --jobs=8

# Prepare build repo
BUILD_ROOT=$(realpath "${BASH_SOURCE[0]%/*}/..")
BUILD_RELEASE_PATH="$BUILD_ROOT/release"
mkdir -p "$BUILD_RELEASE_PATH"

# Determine which build script to use
if [[ $1 == "--offline" ]]; then
  BUILD_SCRIPT="builder/offline.sh"
else
  BUILD_SCRIPT="builder/online.sh"
fi

# Run the builder
docker run --rm \
  --privileged \
  -v "$BUILD_RELEASE_PATH/:/out/" \
  -v "$BUILD_ROOT/$BUILD_SCRIPT:/$BUILD_SCRIPT:ro" \
  -v "$BUILD_ROOT/archiso:/archiso:ro" \
  -v "$BUILD_ROOT/builder:/builder:ro" \
  archlinux/archlinux:latest /$BUILD_SCRIPT

# Make iso accessible to all
sudo chmod 777 -R "$BUILD_RELEASE_PATH"

# Offer to boot the new build
echo
latest_iso=$(\ls -t "$BUILD_RELEASE_PATH"/*.iso | head -n1)
gum confirm "Boot $latest_iso?" && omarchy-iso-boot "$latest_iso"
