#!/bin/sh
# Muse installer — https://tinymuse.app/install.sh
#
# Installs Muse (a Winamp-style music player for macOS), its "muse" command-line tool, and the
# skin-making skill for Claude Code and Codex. No sudo; nothing outside the paths listed below.
#
#   curl -fsSL https://tinymuse.app/install.sh | sh
#   curl -fsSL https://tinymuse.app/install.sh | sh -s -- --no-open      # don't open Muse afterwards
#   curl -fsSL https://tinymuse.app/install.sh | sh -s -- --no-skill     # skip the agent skill
#   curl -fsSL https://tinymuse.app/install.sh | sh -s -- --uninstall    # remove Muse, the CLI link and the skill
#
# Installs:  /Applications/Muse.app (or ~/Applications if /Applications isn't writable)
#            ~/.local/bin/muse -> Muse.app/Contents/Helpers/muse
#            ~/.claude/skills/muse-skins/SKILL.md   (if you use Claude Code)
#            ~/.codex/skills/muse-skins/SKILL.md    (if you use Codex)
set -eu

SITE="${MUSE_SITE:-https://tinymuse.app}"
PREFIX="${MUSE_PREFIX:-}"                 # install the app here instead (used for testing)
BIN_DIR="${MUSE_BIN_DIR:-$HOME/.local/bin}"
BUNDLE_ID="com.ionprodan.Muse"

OPEN=1
SKILL=1
UNINSTALL=0
for arg in "$@"; do
  case "$arg" in
    --no-open) OPEN=0 ;;
    --no-skill) SKILL=0 ;;
    --uninstall) UNINSTALL=1 ;;
    -h|--help) sed -n '2,17p' "$0" 2>/dev/null || echo "See https://tinymuse.app/install.md"; exit 0 ;;
    *) echo "muse-install: unknown option $arg" >&2; exit 2 ;;
  esac
done

say() { printf '%s\n' "$*"; }
fail() { printf 'muse-install: %s\n' "$*" >&2; exit 1; }

app_dir() {
  if [ -n "$PREFIX" ]; then echo "$PREFIX"
  elif [ -w /Applications ]; then echo /Applications
  else echo "$HOME/Applications"; fi
}

skill_dirs() {
  [ -d "$HOME/.claude" ] && echo "$HOME/.claude/skills/muse-skins"
  [ -d "$HOME/.codex" ] && echo "$HOME/.codex/skills/muse-skins"
  return 0
}

quit_running_muse() {
  # Only when replacing the real install, never during a test install into MUSE_PREFIX.
  [ -z "$PREFIX" ] || return 0
  if pgrep -xq Muse; then
    osascript -e "tell application id \"$BUNDLE_ID\" to quit" >/dev/null 2>&1 || true
    i=0
    while pgrep -xq Muse && [ $i -lt 20 ]; do sleep 0.25; i=$((i + 1)); done
  fi
}

if [ "$UNINSTALL" = 1 ]; then
  quit_running_muse
  for dir in "$(app_dir)" /Applications "$HOME/Applications"; do
    [ -d "$dir/Muse.app" ] && rm -rf "$dir/Muse.app" && say "Removed $dir/Muse.app"
  done
  if [ -L "$BIN_DIR/muse" ]; then rm -f "$BIN_DIR/muse" && say "Removed $BIN_DIR/muse"; fi
  for dir in $(skill_dirs); do
    [ -d "$dir" ] && rm -rf "$dir" && say "Removed $dir"
  done
  say "Muse is uninstalled. Your skins and settings stay in ~/Library/Application Support/Muse."
  exit 0
fi

# --- Requirements ---
[ "$(uname -s)" = "Darwin" ] || fail "Muse is a macOS app."
major=$(sw_vers -productVersion | cut -d. -f1)
[ "$major" -ge 14 ] || fail "Muse needs macOS 14 (Sonoma) or later; this Mac has $(sw_vers -productVersion)."
[ "$(uname -m)" = "arm64" ] || fail "Muse currently runs on Apple silicon Macs only."

# --- Download and check ---
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
say "Downloading Muse…"
curl -fsSL "$SITE/download/Muse.zip" -o "$tmp/Muse.zip" || fail "couldn't download $SITE/download/Muse.zip"
curl -fsSL "$SITE/download/Muse.zip.sha256" -o "$tmp/Muse.zip.sha256" || fail "couldn't download the checksum"
expected=$(tr -d ' \n' < "$tmp/Muse.zip.sha256")
actual=$(shasum -a 256 "$tmp/Muse.zip" | cut -d' ' -f1)
[ "$expected" = "$actual" ] || fail "the download is damaged (checksum mismatch); please try again."
say "Checked the SHA-256"
ditto -x -k "$tmp/Muse.zip" "$tmp/unzipped"
[ -d "$tmp/unzipped/Muse.app" ] || fail "the download doesn't contain Muse.app."

# --- Install the app ---
dest=$(app_dir)
mkdir -p "$dest"
quit_running_muse
rm -rf "$dest/Muse.app"
ditto "$tmp/unzipped/Muse.app" "$dest/Muse.app"
xattr -dr com.apple.quarantine "$dest/Muse.app" 2>/dev/null || true
say "Installed $dest/Muse.app"

# --- The command-line tool ---
mkdir -p "$BIN_DIR"
ln -sf "$dest/Muse.app/Contents/Helpers/muse" "$BIN_DIR/muse"
say "Linked $BIN_DIR/muse"

# --- The skill for AI agents ---
if [ "$SKILL" = 1 ]; then
  for dir in $(skill_dirs); do
    mkdir -p "$dir"
    curl -fsSL "$SITE/skills/muse-skins/SKILL.md" -o "$dir/SKILL.md" && say "Added the muse-skins skill to $dir"
  done
fi

# --- Open it ---
if [ "$OPEN" = 1 ]; then
  open -g "$dest/Muse.app" && say "Opened Muse"
fi

say ""
case ":$PATH:" in
  *":$BIN_DIR:"*) ;;
  *) say "Add the muse command to your PATH:  export PATH=\"$BIN_DIR:\$PATH\"  (add it to ~/.zshrc)"; say "" ;;
esac
say "Next:"
say "  muse add ~/Music --play     # play your music"
say "  muse skin list              # see the skins"
say "  $SITE/install.md   # more, including how to make skins"
say "To uninstall:  curl -fsSL https://tinymuse.app/install.sh | sh -s -- --uninstall"
