#!/usr/bin/env bash
# install.sh — Install the El SDK from the latest Gitea release.
#
# Usage:
#   bash install.sh
#   EL_VERSION=v1.0.0   bash install.sh     # pin a specific release tag
#   EL_PREFIX=/opt/el   bash install.sh     # custom install prefix
#
# Environment variables:
#   EL_VERSION   Release tag to download (default: latest)
#   EL_PREFIX    Install prefix           (default: /usr/local)

set -euo pipefail

REPO_BASE="https://git.neuralplatform.ai/neuron-technologies/el"
VERSION="${EL_VERSION:-latest}"
PREFIX="${EL_PREFIX:-/usr/local}"

BIN_DIR="${PREFIX}/bin"
LIB_DIR="${PREFIX}/lib/el"

RELEASE_BASE="${REPO_BASE}/releases/download/${VERSION}"

echo "==> Installing El SDK ${VERSION}"
echo "    prefix : ${PREFIX}"
echo "    bin    : ${BIN_DIR}"
echo "    lib    : ${LIB_DIR}"
echo

# Create directories
mkdir -p "${BIN_DIR}" "${LIB_DIR}"

# Download helper
download() {
    local url="$1"
    local dest="$2"
    echo "    Downloading $(basename "${dest}")..."
    if command -v curl >/dev/null 2>&1; then
        curl -fsSL "${url}" -o "${dest}"
    elif command -v wget >/dev/null 2>&1; then
        wget -q "${url}" -O "${dest}"
    else
        echo "Error: neither curl nor wget found" >&2
        exit 1
    fi
}

# Download assets
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "${TMP_DIR}"' EXIT

# The runtime is MULTI-FILE. el_runtime.c #includes six engram headers and makes
# hard cross-TU calls into all six sibling .c files, so installing el_runtime.c
# alone produces a lib/ that CANNOT LINK — `ld` fails with undefined
# engram_ground_json / engram_activate_inner / eg_find_relation / cog_assert_two_axis.
# This list mirrors lang/runtime/SOURCES (the in-repo source of truth); keep them
# in step. install.sh is standalone by design — it runs on machines with no repo
# checkout — so it cannot call scripts/el-runtime-sources.sh.
RUNTIME_SOURCES=(
    el_runtime.c el_seed.c
    engram_store.c engram_vindex.c engram_geometry.c
    engram_reason.c engram_verify.c engram_cognition.c
    engram_text.c
    eg_cosine_batch.c eg_cosine_batch_strategy_cpu.c
)
RUNTIME_HEADERS=(
    el_runtime.h el_seed.h
    engram_store.h engram_vindex.h engram_geometry.h
    engram_reason.h engram_verify.h engram_cognition.h
    engram_text.h
    eg_cosine_batch.h eg_cosine_batch_strategy.h
)

download "${RELEASE_BASE}/elc" "${TMP_DIR}/elc"
for f in "${RUNTIME_SOURCES[@]}" "${RUNTIME_HEADERS[@]}"; do
    download "${RELEASE_BASE}/${f}" "${TMP_DIR}/${f}"
done

# Install
install -m 755 "${TMP_DIR}/elc" "${BIN_DIR}/elc"
for f in "${RUNTIME_SOURCES[@]}" "${RUNTIME_HEADERS[@]}"; do
    install -m 644 "${TMP_DIR}/${f}" "${LIB_DIR}/${f}"
done

# Record the link set so downstream Makefiles can read it instead of hardcoding.
printf '%s\n' "${RUNTIME_SOURCES[@]}" > "${TMP_DIR}/SOURCES"
install -m 644 "${TMP_DIR}/SOURCES" "${LIB_DIR}/SOURCES"

echo
echo "==> El SDK installed successfully"
echo
echo "    elc binary : ${BIN_DIR}/elc"
echo "    runtime    : ${LIB_DIR}/ (${#RUNTIME_SOURCES[@]} .c files, ${#RUNTIME_HEADERS[@]} headers)"
echo "    link set   : ${LIB_DIR}/SOURCES"
echo
echo "Add the following to your Makefile to build El programs:"
echo
echo "    EL_LIB := ${LIB_DIR}"
echo "    ELC    := elc"
echo "    CC     := cc"
echo "    CFLAGS := -std=c11 -O2 -I\$(EL_LIB)"
echo "    LDLIBS := -lcurl -lssl -lcrypto -lpthread -lm"
echo
echo "    # The runtime is multi-file — link the whole set, not el_runtime.c alone."
echo "    EL_RUNTIME := \$(addprefix \$(EL_LIB)/,\$(shell cat \$(EL_LIB)/SOURCES))"
echo
echo "    dist/myapp.c: src/myapp.el"
echo "    \t\$(ELC) src/myapp.el > dist/myapp.c"
echo
echo "    dist/myapp: dist/myapp.c"
echo "    \t\$(CC) \$(CFLAGS) -o dist/myapp dist/myapp.c \$(EL_RUNTIME) \$(LDLIBS)"
echo
