#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MAIN_BRANCH="${SWARMFORGE_SCRIPTS_BRANCH:-main}"
ARCHIVE_URL="${SWARMFORGE_SCRIPTS_URL:-https://github.com/unclebob/swarm-forge/archive/refs/heads/${MAIN_BRANCH}.tar.gz}"
TMP_DIR=""

cleanup() {
  if [[ -n "$TMP_DIR" ]]; then
    rm -rf "$TMP_DIR"
  fi
}
trap cleanup EXIT

if [[ ! -d "$SCRIPT_DIR/swarmforge/scripts" || ! -d "$SCRIPT_DIR/swarmforge/scripts/shared-articles" ]]; then
  TMP_DIR="$(mktemp -d)"
  mkdir -p "$SCRIPT_DIR/swarmforge"
  curl -L "$ARCHIVE_URL" | tar -xz --strip-components=1 -C "$TMP_DIR"
  if [[ ! -d "$SCRIPT_DIR/swarmforge/scripts" ]]; then
    cp -R "$TMP_DIR/swarmforge/scripts" "$SCRIPT_DIR/swarmforge/scripts"
  fi
  if [[ -d "$TMP_DIR/swarmforge/constitution/articles" ]]; then
    mkdir -p "$SCRIPT_DIR/swarmforge/scripts/shared-articles"
    cp -R "$TMP_DIR/swarmforge/constitution/articles/." "$SCRIPT_DIR/swarmforge/scripts/shared-articles/"
  fi
fi

exec "$SCRIPT_DIR/swarmforge/scripts/swarmforge.sh" "$@"
