#!/bin/sh
# ANSIpants16 installer — macOS 10.14.4 Mojave or newer (universal: Intel + Apple silicon).
# On macOS 10.13 High Sierra use the -legacy tarball, which bundles the Swift runtime.
# Usage:
#   ./install.sh              # from an unpacked release tarball (binary alongside)
#   ./install.sh <binary>     # point at a specific binary
# Installs to ~/.local/bin (no root) or /usr/local/bin if run as root.

set -e

ARCH=$(uname -m)
case "$ARCH" in
    arm64) ARCH=aarch64 ;;
    amd64) ARCH=x86_64 ;;
esac

DIR=$(dirname "$0")
BIN="${1:-}"
if [ -z "$BIN" ]; then
    for candidate in "$DIR/ansipants16-linux-$ARCH" "$DIR/ansipants16" "$DIR/ansipants" "$DIR/ansipants-linux-$ARCH"; do
        [ -f "$candidate" ] && BIN="$candidate" && break
    done
fi
if [ -z "$BIN" ] || [ ! -f "$BIN" ]; then
    echo "ansipants16 binary for $ARCH not found next to install.sh" >&2
    echo "usage: ./install.sh [path-to-binary]" >&2
    exit 1
fi

if [ "$(id -u)" = 0 ]; then
    DEST=/usr/local/bin
else
    DEST="$HOME/.local/bin"
    mkdir -p "$DEST"
fi

install -m 755 "$BIN" "$DEST/ansipants16"
echo "installed: $DEST/ansipants16"

# anscat: tiny CP437 .ans viewer for modern terminals (python3)
if [ -f "$DIR/anscat" ]; then
    install -m 755 "$DIR/anscat" "$DEST/anscat"
    echo "installed: $DEST/anscat  (view .ans files: anscat file.ans)"
fi

case ":$PATH:" in
    *":$DEST:"*) ;;
    *) echo "note: $DEST is not on your PATH — add: export PATH=\"$DEST:\$PATH\"" ;;
esac

echo "run 'ansipants16' in iTerm2 (Terminal.app works too, with fewer colors)."
