#!/bin/sh
# agents — laravel guidelines installer
# https://agents.c11.dev/laravel
#
# Installs the laravel coding guidelines, plus their php prerequisite, into
# .ai/guidelines/ in the current project. Run from your project root:
#
#     curl https://agents.c11.dev/laravel | sh
#
# Optional files (e.g. Filament) are asked about interactively. To drive it
# non-interactively:
#
#     curl https://agents.c11.dev/laravel | sh -s -- --with filament  # add a specific optional
#     curl https://agents.c11.dev/laravel | sh -s -- --all            # add every optional
#     curl https://agents.c11.dev/laravel | sh -s -- --no-optional    # skip the prompt
#     curl https://agents.c11.dev/laravel | sh -s -- --force          # skip the Laravel-project check
#
set -eu

# Rewritten to the requested host at download time (so a script curled from
# localhost fetches from localhost). Falls back to prod if served as a raw file.
BASE_URL="https://agents.c11.dev"

GUIDELINES_DIR=".ai/guidelines"

# Required files, in install order — php first as the laravel prerequisite.
# Installed flat with a c11-<topic>- prefix: tools like Laravel Boost key user
# guidelines by filename alone, so the prefix keeps ours unique and namespaced.
REQUIRED="php/core.md laravel/core.md laravel/testing.md"

# Optional files as name=path pairs.
OPTIONAL="filament=laravel/filament.md"

# --- options (non-interactive override for the prompt) --------------------
ALL=0
NO_OPTIONAL=0
FORCE=0
INCLUDE=""
while [ $# -gt 0 ]; do
    case "$1" in
        --all | -y) ALL=1 ;;
        --no-optional) NO_OPTIONAL=1 ;;
        --force) FORCE=1 ;;
        --with)
            shift
            INCLUDE="$INCLUDE ${1:-}"
            ;;
        --with=*) INCLUDE="$INCLUDE ${1#--with=}" ;;
        *)
            echo "agents: unknown option: $1" >&2
            exit 1
            ;;
    esac
    shift
done

if ! command -v curl >/dev/null 2>&1; then
    echo "agents: curl is required but was not found." >&2
    exit 1
fi

# Make sure we're actually in a Laravel project before installing its
# guidelines. `artisan` is the canonical marker; fall back to composer.json.
is_laravel() {
    [ -f artisan ] && return 0
    [ -f composer.json ] && grep -q '"laravel/framework"' composer.json 2>/dev/null && return 0
    return 1
}

if [ "$FORCE" -eq 0 ] && ! is_laravel; then
    echo "agents: this doesn't look like a Laravel project — no ./artisan or laravel/framework in composer.json." >&2
    if { : >/dev/tty; } 2>/dev/null; then
        printf 'Install the laravel guidelines here anyway? [y/N] ' >/dev/tty
        read ans </dev/tty || ans=""
        case "$ans" in
            [Yy]*) ;;
            *)
                echo "agents: aborted — run from your Laravel project root." >&2
                exit 1
                ;;
        esac
    else
        echo "agents: run from your Laravel project root, or re-run with --force." >&2
        exit 1
    fi
fi

# Decide whether a given optional name should be installed.
wants_optional() {
    name="$1"
    [ "$ALL" -eq 1 ] && return 0
    case " $INCLUDE " in
        *" $name "*) return 0 ;;
    esac
    return 1
}

# Ask interactively about any optional file not already settled by a flag.
# Probe the controlling terminal by actually opening it: [ -r /dev/tty ] can
# pass with no tty (then the open fails), so test for real.
asked_any=0
if [ "$ALL" -eq 0 ] && [ "$NO_OPTIONAL" -eq 0 ] && { : >/dev/tty; } 2>/dev/null; then
    for entry in $OPTIONAL; do
        name="${entry%%=*}"
        wants_optional "$name" && continue
        printf 'Include optional %s guidelines? [y/N] ' "$name" >/dev/tty
        read ans </dev/tty || ans=""
        case "$ans" in
            [Yy]*) INCLUDE="$INCLUDE $name" ;;
        esac
        asked_any=1
    done
fi

# Assemble the final list: required + chosen optional paths.
FILES="$REQUIRED"
skipped=""
for entry in $OPTIONAL; do
    name="${entry%%=*}"
    path="${entry#*=}"
    if wants_optional "$name"; then
        FILES="$FILES $path"
    else
        skipped="$skipped $name"
    fi
done

mkdir -p "$GUIDELINES_DIR"
echo "agents: installing laravel guidelines into ${GUIDELINES_DIR}/"

for f in $FILES; do
    topic="${f%%/*}"                              # php
    rest=$(printf '%s' "${f#*/}" | tr '/' '-')    # core.md
    name="c11-${topic}-${rest}"                   # c11-php-core.md
    echo "  + ${name}"
    curl -fsSL "${BASE_URL}/guidelines/${f}" -o "${GUIDELINES_DIR}/${name}"
done

echo "agents: done."

# If optional files were skipped without being asked (no tty / non-interactive),
# tell the user how to get them.
if [ -n "$skipped" ] && [ "$asked_any" -eq 0 ] && [ "$NO_OPTIONAL" -eq 0 ]; then
    echo "agents: optional not installed:${skipped}. Add one with --with <name>, or --all for every optional."
fi
