#!/usr/bin/env bash
# AuraOffice skills for Claude Code, Codex and Grok.
#
#   curl -sSL https://auraoffice.org/skills/install.sh | bash                    every skill
#   curl -sSL https://auraoffice.org/skills/install.sh | bash -s -- aurapage     just the ones named
#
# Names: aurapage auradeck auratrack
#
# A skill is a folder, and each assistant reads the same layout from its own
# folder. This installs each skill into every assistant found in your home
# folder: ~/.claude/skills, ~/.codex/skills and ~/.grok/skills. An existing copy
# of the same skill is replaced. To install somewhere else, set
# AURAOFFICE_SKILLS_DIRS to a colon-separated list of skills folders.
#
# The AuraTrack skill works through auratrack-cli, so installing it also puts
# auratrack-cli 0.2.0 in ~/.local/bin, checked against the hash below,
# unless that version or a newer one is already on your PATH. The skill holds no
# credentials: you sign in once with `auratrack-cli login`.
set -euo pipefail

BASE="https://auraoffice.org"
CLI_VERSION="0.2.0"
CLI_LINUX="auratrack-cli-0.2.0-linux-x86_64"
CLI_LINUX_SHA256="4e8b9f4feff7d47e869b58ae495fe3a027b5818218cd8126b35214852fb1818e"

say() { printf '%s\n' "$*"; }
die() { printf 'auraoffice skills: %s\n' "$*" >&2; exit 1; }

folder_of() {
    case "$1" in
        aurapage|aurapage-skill) echo aurapage-skill ;;
        auradeck|auradeck-skill) echo auradeck-skill ;;
        auratrack|auratrack-tasks) echo auratrack-tasks ;;
        *) return 1 ;;
    esac
}
slug_of() {
    case "$1" in
        aurapage-skill) echo aurapage ;;
        auradeck-skill) echo auradeck ;;
        auratrack-tasks) echo auratrack ;;
    esac
}

wanted=()
if [ "$#" -eq 0 ]; then
    wanted=(aurapage-skill auradeck-skill auratrack-tasks)
else
    for name in "$@"; do
        folder="$(folder_of "$name")" || die "no skill called '$name'; the skills are aurapage auradeck auratrack"
        wanted+=("$folder")
    done
fi

# Where to install: the assistants that are here.
targets=()
if [ -n "${AURAOFFICE_SKILLS_DIRS:-}" ]; then
    IFS=: read -r -a targets <<< "$AURAOFFICE_SKILLS_DIRS"
else
    for home in "$HOME/.claude" "$HOME/.codex" "$HOME/.grok"; do
        [ -d "$home" ] && targets+=("$home/skills")
    done
fi
[ "${#targets[@]}" -gt 0 ] || die "found no Claude Code (~/.claude), Codex (~/.codex) or Grok (~/.grok). Install one first, or set AURAOFFICE_SKILLS_DIRS."

command -v curl >/dev/null || die "curl is needed"
unpack() {
    if command -v unzip >/dev/null; then unzip -q -o "$1" -d "$2"
    elif command -v python3 >/dev/null; then python3 -m zipfile -e "$1" "$2"
    else die "unzip or python3 is needed to unpack the skills"; fi
}

work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT

for folder in "${wanted[@]}"; do
    slug="$(slug_of "$folder")"
    curl -fsSL "$BASE/skills/$slug.zip" -o "$work/$slug.zip" || die "could not download $BASE/skills/$slug.zip"
    mkdir -p "$work/$slug"
    unpack "$work/$slug.zip" "$work/$slug"
    [ -f "$work/$slug/$folder/SKILL.md" ] || die "$slug.zip did not hold $folder/SKILL.md"
    for dir in "${targets[@]}"; do
        mkdir -p "$dir"
        rm -rf "${dir:?}/$folder"
        cp -R "$work/$slug/$folder" "$dir/$folder"
    done
    say "installed $folder"
done
say "  into: ${targets[*]}"

# auratrack-cli, for the AuraTrack skill.
version_at_least() {
    # version_at_least HAVE WANT: true when HAVE >= WANT, both dotted numbers.
    [ "$(printf '%s\n%s\n' "$2" "$1" | sort -V | head -n1)" = "$2" ]
}
need_cli=0
for folder in "${wanted[@]}"; do [ "$folder" = auratrack-tasks ] && need_cli=1; done
if [ "$need_cli" -eq 1 ]; then
    have=""
    if command -v auratrack-cli >/dev/null; then
        have="$(auratrack-cli --version 2>/dev/null | awk '{print $2}')"
    fi
    if [ -n "$have" ] && version_at_least "$have" "$CLI_VERSION"; then
        say "auratrack-cli $have is already installed"
    elif [ "$(uname -s)" = Linux ] && [ "$(uname -m)" = x86_64 ]; then
        curl -fsSL "$BASE/releases/$CLI_LINUX" -o "$work/auratrack-cli" || die "could not download auratrack-cli"
        got="$( (sha256sum "$work/auratrack-cli" 2>/dev/null || shasum -a 256 "$work/auratrack-cli") | awk '{print $1}')"
        [ "$got" = "$CLI_LINUX_SHA256" ] || die "auratrack-cli did not match its published hash; nothing was installed"
        mkdir -p "$HOME/.local/bin"
        install -m 755 "$work/auratrack-cli" "$HOME/.local/bin/auratrack-cli"
        say "installed auratrack-cli $CLI_VERSION in ~/.local/bin"
        case ":$PATH:" in *":$HOME/.local/bin:"*) ;; *)
            say "  ~/.local/bin is not on your PATH; add it so your assistant can find auratrack-cli." ;;
        esac
    else
        say "There is no auratrack-cli build for $(uname -s) $(uname -m) yet, so the AuraTrack skill cannot work here until there is."
    fi
    say "Sign in to AuraTrack once, in your own terminal: auratrack-cli login"
fi

say "Start a new session in your assistant to pick up the skills."

