#!/usr/bin/env bash

set -euo pipefail

COMMON_PATHS="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export PATH="${COMMON_PATHS}:${PATH}"

repo_contents_cache_dir() {
    if [ "$(uname -s)" = "Darwin" ]; then
        printf '%s\n' "${HOME}/Library/Caches/eagle0-bazel-repo-contents"
    else
        printf '%s\n' "${XDG_CACHE_HOME:-${HOME}/.cache}/eagle0-bazel-repo-contents"
    fi
}

write_bazel_local_config() {
    local repo_contents_cache
    repo_contents_cache="$(repo_contents_cache_dir)"

    if [ "$(uname -s)" != "Darwin" ]; then
        if [ -n "${CI:-}" ]; then
            mkdir -p "$repo_contents_cache"
            {
                echo "# Generated by ci/github_actions/ensure_bazel_installed.sh; do not edit."
                printf 'common --repo_contents_cache=%s\n' "$repo_contents_cache"
            } > .bazelrc.local
        fi
        return
    fi

    local developer_dir=""
    if [ -d "/Applications/Xcode.app/Contents/Developer" ]; then
        developer_dir="/Applications/Xcode.app/Contents/Developer"
    elif [ -d "/Library/Developer/CommandLineTools" ]; then
        developer_dir="/Library/Developer/CommandLineTools"
    else
        developer_dir="$(xcode-select -p 2>/dev/null || true)"
    fi

    if [ -z "$developer_dir" ] || [ ! -d "$developer_dir" ]; then
        echo "ERROR: no usable Apple developer directory found"
        echo "Install Command Line Tools with: xcode-select --install"
        echo "Full Xcode is only required for workflows that build/sign Apple apps."
        exit 1
    fi

    export DEVELOPER_DIR="$developer_dir"
    if [ -f .bazelrc.local ] && [ -z "${CI:-}" ]; then
        echo "Keeping existing .bazelrc.local outside CI"
    else
        mkdir -p "$repo_contents_cache"
        {
            echo "# Generated by ci/github_actions/ensure_bazel_installed.sh; do not edit."
            printf 'common --repo_contents_cache=%s\n' "$repo_contents_cache"
            printf 'common:macos --repo_env=DEVELOPER_DIR=%s\n' "$developer_dir"
        } > .bazelrc.local
    fi

    if [ -n "${GITHUB_ENV:-}" ]; then
        echo "DEVELOPER_DIR=$developer_dir" >> "$GITHUB_ENV"
    fi

    echo "Using Apple developer directory at $developer_dir"
}

write_bazel_local_config

if [ -n "${GITHUB_PATH:-}" ]; then
    for path in /opt/homebrew/bin /usr/local/bin; do
        if [ -d "$path" ]; then
            echo "$path" >> "$GITHUB_PATH"
        fi
    done
fi

if command -v bazel >/dev/null 2>&1; then
    echo "Using bazel at $(command -v bazel)"
    bazel --version
    exit 0
fi

if ! command -v bazelisk >/dev/null 2>&1; then
    if ! command -v brew >/dev/null 2>&1; then
        echo "ERROR: neither bazel nor bazelisk is on PATH, and Homebrew is unavailable"
        exit 1
    fi

    echo "Installing bazelisk with Homebrew"
    brew install bazelisk
fi

BAZEL_BIN_DIR="${RUNNER_TEMP:-/tmp}/bazel-bin"
mkdir -p "$BAZEL_BIN_DIR"
ln -sf "$(command -v bazelisk)" "$BAZEL_BIN_DIR/bazel"

if [ -n "${GITHUB_PATH:-}" ]; then
    echo "$BAZEL_BIN_DIR" >> "$GITHUB_PATH"
fi

export PATH="$BAZEL_BIN_DIR:$PATH"
echo "Using bazelisk as bazel at $BAZEL_BIN_DIR/bazel"
bazel --version
