#!/usr/bin/env bash

# This file is generated by scripts/generate-global-skills-installer.sh.
# Do not edit manually; regenerate it after changing skills-lock.json.
# shellcheck shell=bash

set -Eeuo pipefail

SKILL_COUNT=119
DRY_RUN=0
COPY_MODE=0
SHARED_SKILLS_DIR="${SKILLS_GLOBAL_DIR:-${HOME:?HOME must be set}/.agents/skills}"
CODEX_HOME_DIR="${CODEX_HOME:-${HOME:?HOME must be set}/.codex}"
CODEX_SKILLS_DIR="${CODEX_HOME_DIR}/skills"

usage() {
    cat <<'USAGE'
Usage: scripts/install-global-skills.sh [options]

Install the repository skill baseline globally and bridge it into Codex.

Options:
  --dry-run  Show commands and filesystem changes without applying them.
  --copy     Copy skills into Codex instead of creating symlinks.
  --help     Show this help text.

Environment:
  SKILLS_GLOBAL_DIR  Shared global skills directory (default: ~/.agents/skills).
  CODEX_HOME         Codex home directory (default: ~/.codex).
USAGE
}

die() {
    printf 'Error: %s\n' "$1" >&2
    exit 1
}

print_command() {
    printf '+'
    printf ' %q' "$@"
    printf '\n'
}

run_command() {
    if (( DRY_RUN )); then
        print_command "$@"
        return 0
    fi
    "$@"
}

require_command() {
    command -v "$1" >/dev/null 2>&1 || die "Required command not found: $1"
}

while (( $# > 0 )); do
    case "$1" in
        --dry-run)
            DRY_RUN=1
            ;;
        --copy)
            COPY_MODE=1
            ;;
        --help|-h)
            usage
            exit 0
            ;;
        *)
            die "Unknown option: $1 (use --help for usage)"
            ;;
    esac
    shift
done

require_command bash
require_command npx
require_command mkdir
require_command ln
require_command readlink
if (( COPY_MODE )); then
    require_command cp
    require_command diff
fi

printf 'Installing %d skills globally...\n' "$SKILL_COUNT"

run_command npx --yes skills add asyrafhussin/agent-skills --global --yes --skill api-design-patterns --skill clean-code-principles --skill code-slop --skill e2e-playwright-testing --skill git-workflow --skill laravel-ai-sdk --skill laravel-best-practices --skill laravel-database-optimization --skill laravel-inertia-react --skill laravel-mcp --skill laravel-owasp-security --skill laravel-queues --skill laravel-testing --skill php-best-practices --skill prd-writing --skill project-docs --skill react-vite-best-practices --skill seo-best-practices --skill state-management --skill tailwind-best-practices --skill technical-debt --skill testing-best-practices --skill typescript-react-patterns --skill web-design-guidelines

run_command npx --yes skills add iserter/laravel-claude-agents --global --yes --skill eloquent-best-practices --skill laravel-api-resource-patterns --skill laravel-authorization-patterns --skill laravel-blade-component-patterns --skill laravel-brainstorming --skill laravel-caching-strategies --skill laravel-event-driven-architecture --skill laravel-feature-flags --skill laravel-middleware-patterns --skill laravel-notification-patterns --skill laravel-queue-patterns --skill laravel-systematic-debugging --skill laravel-task-scheduling --skill laravel-tdd --skill laravel-validation-patterns

run_command npx --yes skills add jeffallan/claude-skills --global --yes --skill angular-architect --skill api-designer --skill architecture-designer --skill atlassian-mcp --skill chaos-engineer --skill cli-developer --skill cloud-architect --skill code-documenter --skill code-reviewer --skill cpp-pro --skill csharp-developer --skill database-optimizer --skill debugging-wizard --skill devops-engineer --skill django-expert --skill dotnet-core-expert --skill embedded-systems --skill fastapi-expert --skill feature-forge --skill fine-tuning-expert --skill flutter-expert --skill fullstack-guardian --skill game-developer --skill golang-pro --skill graphql-architect --skill java-architect --skill javascript-pro --skill kotlin-specialist --skill kubernetes-specialist --skill laravel-specialist --skill legacy-modernizer --skill mcp-developer --skill microservices-architect --skill ml-pipeline --skill monitoring-expert --skill nestjs-expert --skill nextjs-developer --skill pandas-pro --skill php-pro --skill playwright-expert --skill postgres-pro --skill prompt-engineer --skill python-pro --skill rag-architect --skill rails-expert --skill react-expert --skill react-native-expert --skill rust-engineer --skill salesforce-developer --skill secure-code-guardian --skill security-reviewer --skill shopify-expert --skill spark-engineer --skill spec-miner --skill spring-boot-engineer --skill sql-pro --skill sre-engineer --skill swift-expert --skill terraform-engineer --skill test-master --skill the-fool --skill typescript-pro --skill vue-expert --skill vue-expert-js --skill websocket-engineer --skill wordpress-pro

run_command npx --yes skills add leeovery/claude-laravel --global --yes --skill laravel-quality

run_command npx --yes skills add thienanblog/awesome-ai-agent-skills --global --yes --skill agents-md-generator --skill ask-questions-if-underspecified --skill design-system-generator --skill docker-local-dev --skill documentation-guidelines --skill find-scene --skill laravel-11-12-app-guidelines --skill noizai-voice-workflow --skill office-web-ui-system --skill project-development-mindset --skill symcli-skill --skill ui-mockup-visualizer --skill vps-docker-traefik-deploy

SKILLS=(
    api-design-patterns
    clean-code-principles
    code-slop
    e2e-playwright-testing
    git-workflow
    laravel-ai-sdk
    laravel-best-practices
    laravel-database-optimization
    laravel-inertia-react
    laravel-mcp
    laravel-owasp-security
    laravel-queues
    laravel-testing
    php-best-practices
    prd-writing
    project-docs
    react-vite-best-practices
    seo-best-practices
    state-management
    tailwind-best-practices
    technical-debt
    testing-best-practices
    typescript-react-patterns
    web-design-guidelines
    eloquent-best-practices
    laravel-api-resource-patterns
    laravel-authorization-patterns
    laravel-blade-component-patterns
    laravel-brainstorming
    laravel-caching-strategies
    laravel-event-driven-architecture
    laravel-feature-flags
    laravel-middleware-patterns
    laravel-notification-patterns
    laravel-queue-patterns
    laravel-systematic-debugging
    laravel-task-scheduling
    laravel-tdd
    laravel-validation-patterns
    angular-architect
    api-designer
    architecture-designer
    atlassian-mcp
    chaos-engineer
    cli-developer
    cloud-architect
    code-documenter
    code-reviewer
    cpp-pro
    csharp-developer
    database-optimizer
    debugging-wizard
    devops-engineer
    django-expert
    dotnet-core-expert
    embedded-systems
    fastapi-expert
    feature-forge
    fine-tuning-expert
    flutter-expert
    fullstack-guardian
    game-developer
    golang-pro
    graphql-architect
    java-architect
    javascript-pro
    kotlin-specialist
    kubernetes-specialist
    laravel-specialist
    legacy-modernizer
    mcp-developer
    microservices-architect
    ml-pipeline
    monitoring-expert
    nestjs-expert
    nextjs-developer
    pandas-pro
    php-pro
    playwright-expert
    postgres-pro
    prompt-engineer
    python-pro
    rag-architect
    rails-expert
    react-expert
    react-native-expert
    rust-engineer
    salesforce-developer
    secure-code-guardian
    security-reviewer
    shopify-expert
    spark-engineer
    spec-miner
    spring-boot-engineer
    sql-pro
    sre-engineer
    swift-expert
    terraform-engineer
    test-master
    the-fool
    typescript-pro
    vue-expert
    vue-expert-js
    websocket-engineer
    wordpress-pro
    laravel-quality
    agents-md-generator
    ask-questions-if-underspecified
    design-system-generator
    docker-local-dev
    documentation-guidelines
    find-scene
    laravel-11-12-app-guidelines
    noizai-voice-workflow
    office-web-ui-system
    project-development-mindset
    symcli-skill
    ui-mockup-visualizer
    vps-docker-traefik-deploy
)

if (( DRY_RUN )); then
    printf 'Would bridge skills from %s to %s\n' "$SHARED_SKILLS_DIR" "$CODEX_SKILLS_DIR"
    for skill in "${SKILLS[@]}"; do
        if (( COPY_MODE )); then
            printf '+ copy %s -> %s/%s\n' "$SHARED_SKILLS_DIR/$skill" "$CODEX_SKILLS_DIR" "$skill"
        else
            printf '+ ln -s %s %s/%s\n' "$SHARED_SKILLS_DIR/$skill" "$CODEX_SKILLS_DIR" "$skill"
        fi
    done
    exit 0
fi

run_command mkdir -p "$CODEX_SKILLS_DIR"
conflict_count=0
for skill in "${SKILLS[@]}"; do
    source_path="$SHARED_SKILLS_DIR/$skill"
    target_path="$CODEX_SKILLS_DIR/$skill"
    [[ -d "$source_path" ]] || { printf 'Error: Installed skill is missing: %s\n' "$source_path" >&2; conflict_count=$((conflict_count + 1)); continue; }
    if [[ -L "$target_path" ]]; then
        if [[ "$(readlink "$target_path")" == "$source_path" ]]; then
            printf 'Already linked: %s\n' "$skill"
            continue
        fi
        printf 'Error: Refusing to replace unmanaged symlink: %s\n' "$target_path" >&2
        conflict_count=$((conflict_count + 1))
        continue
    fi
    if [[ -e "$target_path" ]]; then
        if (( COPY_MODE )) && [[ -d "$target_path" ]] && diff -qr -- "$source_path" "$target_path" >/dev/null; then
            printf 'Already copied: %s\n' "$skill"
            continue
        fi
        printf 'Error: Refusing to overwrite existing path: %s\n' "$target_path" >&2
        conflict_count=$((conflict_count + 1))
        continue
    fi
    if (( COPY_MODE )); then
        run_command cp -R -- "$source_path" "$target_path"
        printf 'Copied: %s\n' "$skill"
    else
        run_command ln -s -- "$source_path" "$target_path"
        printf 'Linked: %s\n' "$skill"
    fi
done

(( conflict_count == 0 )) || die "${conflict_count} skill bridge conflict(s) require manual resolution"
printf 'Global skills installation complete.\n'
