#!/bin/sh
# ============================================================================
# Generated by AgentScript (ASL) Target Generator (asl-target-sh).
# DO NOT EDIT THIS FILE BY HAND.
# Single Source of Truth (SSOT): asl/packages/asl-target-sh/src/distrib.asl & asl/grammar/rules.asn
# All modifications must be made to the ASL source and compiled via: asl target distrib
# ============================================================================
# AgentScript Universal Installer bootstrap: it only finds or builds asl-host and runs the ASL installer
# Canonical URL: https://aslang.dev/install.sh
# Usage: curl -fsSL https://aslang.dev/install.sh | sh
set -eu
REPO_URL="${REPO_URL:-https://github.com/GenSEAM/asl.git}"
export REPO_URL
FROM=pipe
ROOT=""
case "$0" in
  *install.sh) if [ -f "$0" ]; then FROM=file; ROOT="$(cd -P "$(dirname "$0")/.." && pwd)"; fi ;;
esac
die() { echo "✗ $*" >&2; exit 1; }
pkg_of() {
  if [ -f "$1/asl/packages/asl-target-sh/src/distrib.asl" ]; then echo "$1/asl"
  elif [ -f "$1/packages/asl-target-sh/src/distrib.asl" ]; then echo "$1"
  else return 1; fi
}
host_of() {
  if [ ! -x "$1/host/out/asl-host" ] || [ -n "$(find "$1/host/src/" "$1/prelude/" "$1/host/build.sh" -newer "$1/host/out/asl-host" 2>/dev/null | head -n 1)" ]; then
    sh "$1/host/build.sh" >&2 || [ -x "$1/host/out/asl-host" ] || return 1
  fi
  echo "$1/host/out/asl-host"
}
case "${1:-}" in
  --tier|--render-rules|--check-rules|--render-home|--render-lexicon|--render-installers|--check-installers)
    [ -n "$ROOT" ] || die "install.sh $1 runs from a checkout, not from a pipe"
    PKG="$(pkg_of "$ROOT")" || die "no ASL installer under $ROOT"
    HOST="$(host_of "$PKG")" || die "cannot build asl-host in $PKG/host (needs a C compiler)"
    exec "$HOST" run "$PKG/packages/asl-target-sh/src/rules.asl" "$ROOT" "$@" ;;
esac
OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$OS" in
  *mingw*|*cygwin*|*msys*|windows_nt)
    echo "Windows environment detected via $OS. Delegating to PowerShell installer..."
    PS1_SCRIPT="$(cd "$(dirname "$0")" 2>/dev/null && pwd)/install.ps1"
    if [ "$FROM" = file ] && [ -f "$PS1_SCRIPT" ]; then
      powershell.exe -ExecutionPolicy Bypass -File "$PS1_SCRIPT" || powershell -ExecutionPolicy Bypass -File "$PS1_SCRIPT"
    else
      powershell.exe -ExecutionPolicy Bypass -Command "irm https://aslang.dev/install.ps1 | iex"
    fi
    exit 0 ;;
esac
SRC_ARG=""; HOME_ARG="${HOME:-}"; DRY=0; PREV=""
for A in "$@"; do
  case "$PREV" in --source) case "$A" in -*) ;; *) SRC_ARG="$A" ;; esac ;; --home) HOME_ARG="$A" ;; esac
  case "$A" in --source=*) SRC_ARG="${A#*=}" ;; --home=*) HOME_ARG="${A#*=}" ;; --dry-run) DRY=1 ;; esac
  PREV="$A"
done
TREE=""
if [ -n "$ROOT" ] && pkg_of "$ROOT" >/dev/null; then TREE="$ROOT"
else
  FROM=pipe
  if [ -n "$SRC_ARG" ] && [ -d "$SRC_ARG" ] && pkg_of "$SRC_ARG" >/dev/null; then TREE="$(cd -P "$SRC_ARG" && pwd)"
  else
    TREE="$HOME_ARG/.asl/repo"
    if [ ! -d "$TREE/.git" ]; then
      [ "$DRY" -eq 0 ] || die "--dry-run needs a local tree: run install.sh from a checkout or pass --source DIR"
      command -v git >/dev/null 2>&1 || die "ERR_TOOL_ABSENT git: needed to fetch $REPO_URL"
      git clone --depth 1 "$REPO_URL" "$TREE" >&2
    fi
  fi
fi
PKG="$(pkg_of "$TREE")" || die "no ASL installer in $TREE"
HOST="$(host_of "$PKG")" || die "cannot build asl-host in $PKG/host (needs a C compiler)"
export ASL_HOST="$HOST"
TTY=none
if [ -t 0 ]; then TTY=tty
elif [ -e /dev/tty ]; then
  if (: </dev/tty) 2>/dev/null; then TTY=devtty; else TTY=devttyFail; fi
fi
INSTALLER="$PKG/packages/asl-target-sh/src/distrib.asl"
if [ "$DRY" -eq 0 ] && [ -e "$TREE/.git" ] && git -C "$TREE" rev-parse --git-dir >/dev/null 2>&1; then
  git -C "$TREE" config --local merge.asnRows.driver "\"$HOST\" run \"$PKG/tools/mergeRows/mergeRows.asl\" %O %A %B %P"
  git -C "$TREE" config --local merge.asnRows.name 'ASN row-keyed merge (MRG1)'
fi
if [ "$TTY" = devtty ]; then exec "$HOST" run "$INSTALLER" "$TREE" "$FROM" "$TTY" "$OS" "$ARCH" "$@" </dev/tty; fi
exec "$HOST" run "$INSTALLER" "$TREE" "$FROM" "$TTY" "$OS" "$ARCH" "$@"
