#!/bin/bash
# strata-docker - run Strata's Intel Arc (SYCL) engine in Docker on Slackware
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Thin wrapper over upstream's documented Intel Arc route (docs/INTEL.md).
# Upstream's strata-sycl.sh mounts the checkout's parent dir at /work, so the
# checkout (Strata/) and the data dir (Strata-data/) both live in this repo.
set -euo pipefail

here=$(cd "$(dirname "$0")" && pwd)
src=$here/Strata
ref=${STRATA_REF:-v0.2.2}       # upstream tag to build
aot=${STRATA_AOT:-bmg-g21}      # B580, B570, Pro B60
image=strata-sycl-dev           # upstream's runner hardcodes this default

case "${1:-}" in
fetch)
    [ -d "$src/.git" ] || git clone https://github.com/Niko1221/Strata "$src"
    git -C "$src" fetch --tags -q
    git -C "$src" checkout -q "$ref"
    git -C "$src" log -1 --format='Strata at %h (%D)'
    ;;
image)
    docker build --pull -t "$image" "$src/sycl/tools"
    ;;
build)
    docker run --rm -u "$(id -u):$(id -g)" -v "$here:/work" \
        -e AOT="$aot" -e REPO=/work/Strata -e BUILD_DIR=/work/Strata/build-sycl-aot \
        "$image" "cd /work/Strata && bash sycl/tools/build.sh strata"
    ;;
setup)
    shift
    cd "$src"
    exec ./setup.sh --backend sycl --family gyro --model Gyro-S \
        --data-dir "$here/Strata-data" "$@"
    ;;
*)
    echo "usage: ${0##*/} fetch|image|build|setup [setup.py args]" >&2
    exit 1
    ;;
esac
