#!/usr/bin/env bash
# install.sh — install El as a proper local framework tool.
#
# Copies the compiler binary, runtime source files, headers, and stdlib
# from the local source tree into the install prefix. After install,
# El programs can be built against an installed, stable copy of the runtime.
#
# Usage:
#   ./tools/install.sh
#   ./tools/install.sh --prefix /opt/el
#
# Environment:
#   EL_HOME   Override install prefix (same as --prefix)

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EL_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

PREFIX="${EL_HOME:-/usr/local/el}"

# Parse --prefix flag
while [[ $# -gt 0 ]]; do
    case "$1" in
        --prefix)
            PREFIX="$2"
            shift 2
            ;;
        --prefix=*)
            PREFIX="${1#--prefix=}"
            shift
            ;;
        *)
            echo "Unknown argument: $1" >&2
            echo "Usage: $0 [--prefix <path>]" >&2
            exit 1
            ;;
    esac
done

BIN_DIR="${PREFIX}/bin"
RUNTIME_DIR="${PREFIX}/runtime"
INCLUDE_DIR="${PREFIX}/include"
LIB_DIR="${PREFIX}/lib"

ELC_SRC="${EL_ROOT}/dist/platform/elc"
RUNTIME_SRC="${EL_ROOT}/runtime"
STDLIB_SRC="${EL_ROOT}/runtime"

echo "==> Installing El framework to ${PREFIX}"
echo "    bin:     ${BIN_DIR}"
echo "    runtime: ${RUNTIME_DIR}"
echo "    include: ${INCLUDE_DIR}"
echo "    lib:     ${LIB_DIR}"
echo

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

# 1. Install elc binary
if [[ ! -f "${ELC_SRC}" ]]; then
    echo "Error: elc binary not found at ${ELC_SRC}" >&2
    echo "Build it first or run from the el repo root." >&2
    exit 1
fi
install -m 755 "${ELC_SRC}" "${BIN_DIR}/elc"
echo "    installed: ${BIN_DIR}/elc"

# 2. Install runtime .el files
for f in "${STDLIB_SRC}"/*.el; do
    [[ -f "$f" ]] || continue
    install -m 644 "$f" "${RUNTIME_DIR}/$(basename "$f")"
done
echo "    installed: ${RUNTIME_DIR}/*.el  ($(ls "${STDLIB_SRC}"/*.el | wc -l | tr -d ' ') files)"

# 3. Install headers
for header in el_seed.h el_runtime.h; do
    src="${RUNTIME_SRC}/${header}"
    if [[ -f "${src}" ]]; then
        install -m 644 "${src}" "${INCLUDE_DIR}/${header}"
        echo "    installed: ${INCLUDE_DIR}/${header}"
    fi
done

# 4. Build libel.a from el_seed.c + el_runtime.c
echo "    compiling libel.a..."
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "${TMP_DIR}"' EXIT

cc -std=c11 -O2 -I"${RUNTIME_SRC}" -c "${RUNTIME_SRC}/el_seed.c"   -o "${TMP_DIR}/el_seed.o"
cc -std=c11 -O2 -I"${RUNTIME_SRC}" -c "${RUNTIME_SRC}/el_runtime.c" -o "${TMP_DIR}/el_runtime.o"
ar rcs "${LIB_DIR}/libel.a" "${TMP_DIR}/el_seed.o" "${TMP_DIR}/el_runtime.o"
echo "    installed: ${LIB_DIR}/libel.a"

# 5. Generate stdlib.el with absolute paths to installed runtime files
STDLIB_OUT="${PREFIX}/stdlib.el"
{
    echo "// stdlib.el — El standard library (installed at ${PREFIX})"
    echo "// Generated by tools/install.sh — do not edit by hand."
    echo "// Import this file to get the full El runtime:"
    echo "//   import \"${PREFIX}/stdlib.el\""
    echo
    for f in "${STDLIB_SRC}"/*.el; do
        [[ -f "$f" ]] || continue
        base="$(basename "$f")"
        # Skip stdlib.el itself to avoid circular import
        [[ "${base}" == "stdlib.el" ]] && continue
        # Skip test.el — dev-only
        [[ "${base}" == "test.el" ]] && continue
        echo "import \"${RUNTIME_DIR}/${base}\""
    done
} > "${STDLIB_OUT}"
echo "    installed: ${STDLIB_OUT}"

echo
echo "==> El installed to ${PREFIX}"
echo
echo "Add ${BIN_DIR} to your PATH:"
echo "    export PATH=\"${BIN_DIR}:\$PATH\""
echo
echo "To use the stdlib in your El programs:"
echo "    import \"${PREFIX}/stdlib.el\""
echo
echo "To link El programs against the installed runtime:"
echo "    elc src/main.el > dist/main.c"
echo "    cc -std=c11 -O2 -I${INCLUDE_DIR} -o dist/main dist/main.c ${LIB_DIR}/libel.a -lcurl -lpthread"
echo
