#!/bin/sh
# Installs omm and the Orbito modules skill (SKILL.md) for Claude Code, Codex and OpenCode.
#
#   curl -fsSL https://orbito.pl/docs/skill/install.sh | sh
#   curl -fsSL https://orbito.pl/docs/skill/install.sh | sh -s -- --project
#
# A thin wrapper for Linux and macOS: maps the @orbito npm scope to the Orbito registry,
# installs @orbito/omm globally and runs `omm skill install` with the same options.
# A root-owned global npm directory (EACCES) stops it before `npm i -g` with two fixes; it never runs sudo.
# On Windows (or anywhere without sh) run the two steps by hand:
#
#   npm i -g @orbito/omm --@orbito:registry=https://git.kozminski.net.pl/api/packages/orbito/npm/
#   omm skill install
#
# ORBITO_SKILL_URL overrides the SKILL.md source (passed to omm as OMM_SKILL_URL).
# ORBITO_NPM_REGISTRY overrides the npm registry of the @orbito scope (passed to omm as
# OMM_PACKAGES_REGISTRY, so `omm skill install` writes the same registry).

set -eu

ORBITO_NPM_REGISTRY="${ORBITO_NPM_REGISTRY:-https://git.kozminski.net.pl/api/packages/orbito/npm/}"

usage() {
    cat <<USAGE
Install omm and the Orbito modules skill for your AI coding agents.

Usage: install.sh [--project] [--all] [--agent claude|codex|opencode] [--no-npm]

A wrapper around \`omm skill install\`: it maps the @orbito npm scope to the
Orbito registry in your ~/.npmrc (skipped with --no-npm), installs @orbito/omm
globally and passes every option to \`omm skill install\` unchanged.
Run \`omm skill install --help\` for the meaning of the options.
USAGE
}

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

# True when npm can write to $1 (it may not exist yet): checks the first existing directory up the path.
global_dir_writable() {
    dir="$1"
    while [ ! -e "$dir" ]; do
        dir=$(dirname "$dir")
    done
    [ -w "$dir" ]
}

# Explains the EACCES case: $1 is the global directory, the rest are the original options.
die_global_dir() {
    target="$1"
    shift
    printf 'Error: the global npm directory %s is not writable by the current user\n' "$target" >&2
    printf '(typical for Node.js installed from the macOS .pkg or a Linux package). Fix it in one of two ways:\n\n' >&2
    printf 'Fix 1: install omm with sudo, then install the skill as yourself (no sudo):\n' >&2
    printf '  sudo npm i -g @orbito/omm --@orbito:registry=%s && omm skill install%s\n\n' \
        "$ORBITO_NPM_REGISTRY" "${*:+ $*}" >&2
    printf 'Fix 2: point npm to a user-owned directory (or use nvm or Homebrew node), then rerun this installer:\n' >&2
    printf '  https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally\n' >&2
    exit 1
}

main() {
    configure_npm=1
    for arg in "$@"; do
        case "$arg" in
            -h | --help)
                usage
                exit 0
                ;;
            --no-npm) configure_npm=0 ;;
        esac
    done

    command -v node >/dev/null 2>&1 && command -v npm >/dev/null 2>&1 \
        || die 'Node.js 18.13+ is required — https://nodejs.org'

    if [ "$configure_npm" -eq 1 ]; then
        npm config set @orbito:registry "$ORBITO_NPM_REGISTRY" \
            || die 'npm config set @orbito:registry failed'
    fi
    prefix=$(npm prefix -g) || die 'npm prefix -g failed'
    global_dir_writable "$prefix/lib/node_modules" || die_global_dir "$prefix/lib/node_modules" "$@"
    # The flag lets npm find @orbito/omm even with --no-npm (no scope line in ~/.npmrc).
    npm i -g @orbito/omm "--@orbito:registry=$ORBITO_NPM_REGISTRY" || die 'npm i -g @orbito/omm failed'

    OMM_PACKAGES_REGISTRY="$ORBITO_NPM_REGISTRY"
    export OMM_PACKAGES_REGISTRY
    if [ -n "${ORBITO_SKILL_URL:-}" ]; then
        OMM_SKILL_URL="$ORBITO_SKILL_URL"
        export OMM_SKILL_URL
    fi
    exec omm skill install "$@"
}

main "$@"
