#!/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]%/*}/..")"
mkdir -p "$BUILD_ROOT/release"

# 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_ROOT/release/:/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_ROOT/release"
