#!/bin/bash
set -euo pipefail

REPO="usenwep/nwep"

# Detect OS — NWEP_TARGET_OS is set by build.rs from CARGO_CFG_TARGET_OS when
# cross-compiling (e.g. targeting Android from a Linux host). Fall back to uname
# for native builds.
if [ -n "${NWEP_TARGET_OS:-}" ]; then
    case "${NWEP_TARGET_OS}" in
        macos) OS="darwin";;
        *)     OS="${NWEP_TARGET_OS}";;
    esac
else
    case "$(uname -s)" in
        Linux*)              OS="linux";;
        Darwin*)             OS="darwin";;
        MINGW*|MSYS*|CYGWIN*) OS="windows";;
        *) OS="$(uname -s | tr '[:upper:]' '[:lower:]')";;
    esac
fi

# Detect architecture — NWEP_TARGET_ARCH is set by build.rs from CARGO_CFG_TARGET_ARCH.
# Cargo arch names match artifact names directly (aarch64, arm, x86_64, x86).
if [ -n "${NWEP_TARGET_ARCH:-}" ]; then
    case "${NWEP_TARGET_ARCH}" in
        aarch64) ARCH="aarch64";;
        arm)     ARCH="arm";;
        x86_64)  ARCH="x86_64";;
        x86)     ARCH="x86";;
        *)       ARCH="${NWEP_TARGET_ARCH}";;
    esac
else
    case "$(uname -m)" in
        x86_64|amd64)  ARCH="x86_64";;
        aarch64|arm64) ARCH="aarch64";;
        armv7*)        ARCH="arm";;
        i686|i386)     ARCH="x86";;
        *) ARCH="$(uname -m)";;
    esac
fi

# Fetch the latest release and find a matching asset
echo "Fetching latest release from ${REPO}..."
CURL_AUTH=()
if [ -n "${GITHUB_TOKEN:-}" ]; then
    CURL_AUTH=(-H "Authorization: Bearer ${GITHUB_TOKEN}")
fi
RELEASE_JSON="$(curl -fsSL "${CURL_AUTH[@]}" "https://api.github.com/repos/${REPO}/releases/latest")"
TAG="$(echo "${RELEASE_JSON}" | grep -m1 '"tag_name"' | sed 's/.*: *"\(.*\)".*/\1/')"

# Build a pattern to match: must contain our OS and ARCH
# For linux x86_64, prefer gcc variant; for linux aarch64, prefer gcc over cross
ASSET_URL=""
ASSET_NAME=""

# Get all asset download URLs and names
ASSETS="$(echo "${RELEASE_JSON}" | grep -E '"browser_download_url"' | sed 's/.*: *"\(.*\)".*/\1/')"

for url in ${ASSETS}; do
    name="$(basename "${url}")"
    # Must match OS and ARCH
    if echo "${name}" | grep -qi "${OS}" && echo "${name}" | grep -qi "${ARCH}"; then
        # Prefer gcc variant on linux if available, otherwise take first match
        if [ -z "${ASSET_URL}" ]; then
            ASSET_URL="${url}"
            ASSET_NAME="${name}"
        fi
        if echo "${name}" | grep -qi "gcc"; then
            ASSET_URL="${url}"
            ASSET_NAME="${name}"
            break
        fi
    fi
done

if [ -z "${ASSET_URL}" ]; then
    echo "Error: no matching asset found for ${OS}-${ARCH} in release ${TAG}" >&2
    echo "Available assets:" >&2
    for url in ${ASSETS}; do echo "  $(basename "${url}")" >&2; done
    exit 1
fi

# NWEP_INSTALL_DIR can be set by build.rs to redirect the download target (e.g. OUT_DIR).
# If unset, defaults to third_party/nwep relative to the working directory.
INSTALL_DIR="${NWEP_INSTALL_DIR:-third_party/nwep}"

mkdir -p "${INSTALL_DIR}"
cd "${INSTALL_DIR}"

# Record existing directories before extraction
BEFORE="$(ls -d nwep-* 2>/dev/null || true)"

echo "Downloading ${ASSET_NAME} (${TAG})..."
curl -fsSL -o "${ASSET_NAME}" "${ASSET_URL}"
if echo "${ASSET_NAME}" | grep -q '\.zip$'; then
    if command -v unzip >/dev/null 2>&1; then
        unzip -oq "${ASSET_NAME}"
    else
        tar xf "${ASSET_NAME}"
    fi
else
    tar xzf "${ASSET_NAME}"
fi
rm "${ASSET_NAME}"

# Find the newly extracted directory
AFTER="$(ls -d nwep-* 2>/dev/null || true)"
TARGET=""
for d in ${AFTER}; do
    if ! echo "${BEFORE}" | grep -qx "${d}"; then
        TARGET="${d}"
        break
    fi
done

# Fallback: if no new directory appeared (already existed), pick by name
if [ -z "${TARGET}" ]; then
    TARGET="$(ls -d nwep-*"${OS}"*"${ARCH}"* 2>/dev/null | head -1 || true)"
fi
if [ -z "${TARGET}" ]; then
    TARGET="$(ls -d nwep-* 2>/dev/null | head -1 || true)"
fi
if [ -z "${TARGET}" ]; then
    echo "Error: no nwep build found in ${INSTALL_DIR}/" >&2
    exit 1
fi

rm -f current
# Prefer a symlink; fall back to a copy on systems that don't support them
# (e.g. Windows without Developer Mode / symlink privilege).
if ln -s "${TARGET}" current 2>/dev/null; then
    :
else
    cp -r "${TARGET}" current
fi
echo "nwep ready (${TARGET})"
