#!/bin/sh
# pokecli installer, https://pokecli.xyz
#
# Installs the game into ~/.pokecli/app and drops a launcher in ~/.local/bin.
# Never uses sudo, never touches system Python, never writes outside $HOME.
#
#   curl -fsSL https://pokecli.xyz/install.sh | sh
#
set -eu

SITE="${POKECLI_SITE:-https://pokecli.xyz}"
PREFIX="${POKECLI_PREFIX:-$HOME/.pokecli}"
APP="$PREFIX/app"
BINDIR="${POKECLI_BIN:-$HOME/.local/bin}"
LAUNCHER="$BINDIR/pokecli"
TARBALL="$SITE/pokecli.tar.gz"

# ---------------------------------------------------------------- presentation

if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
  R='\033[0m'; B='\033[1m'; DIM='\033[2m'
  GOLD='\033[38;5;221m'; SKY='\033[38;5;117m'; GREEN='\033[38;5;114m'; RED='\033[38;5;203m'
else
  R=''; B=''; DIM=''; GOLD=''; SKY=''; GREEN=''; RED=''
fi

say()  { printf '%b\n' "$1"; }
step() { printf '  %b*%b %s\n' "$SKY" "$R" "$1"; }
ok()   { printf '  %b+%b %s\n' "$GREEN" "$R" "$1"; }
warn() { printf '  %b!%b %s\n' "$GOLD" "$R" "$1"; }
die()  { printf '\n  %bx%b %s\n\n' "$RED" "$R" "$1" >&2; exit 1; }

banner() {
  say ""
  say "  ${GOLD}██████${R}  ${SKY}█████${R}  ${GOLD}█   █${R} ${SKY}█████${R}  ${GOLD}█████${R} ${SKY}█     ${R} ${GOLD}█${R}"
  say "  ${GOLD}█    █${R} ${SKY}█   █${R}  ${GOLD}█  █ ${R} ${SKY}█     ${R} ${GOLD}█    ${R} ${SKY}█     ${R} ${GOLD}█${R}"
  say "  ${GOLD}██████${R} ${SKY}█   █${R}  ${GOLD}███  ${R} ${SKY}████  ${R} ${GOLD}█    ${R} ${SKY}█     ${R} ${GOLD}█${R}"
  say "  ${GOLD}█     ${R} ${SKY}█   █${R}  ${GOLD}█  █ ${R} ${SKY}█     ${R} ${GOLD}█    ${R} ${SKY}█     ${R} ${GOLD}█${R}"
  say "  ${GOLD}█     ${R}  ${SKY}█████${R} ${GOLD}█   █${R} ${SKY}█████${R}  ${GOLD}█████${R} ${SKY}█████${R} ${GOLD}█${R}"
  say ""
  say "  ${DIM}catch them all, in your terminal${R}"
  say ""
}

# -------------------------------------------------------------------- checks

banner

step "Checking Python"
PY=""
for cand in python3 python3.13 python3.12 python3.11 python3.10 python3.9 python; do
  if command -v "$cand" >/dev/null 2>&1; then
    if "$cand" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 9) else 1)' 2>/dev/null; then
      PY="$cand"; break
    fi
  fi
done
[ -n "$PY" ] || die "Python 3.9 or newer is required. Install it and try again."
ok "$($PY --version 2>&1)  $(command -v "$PY")"

step "Checking Pillow"
if "$PY" -c 'import PIL' >/dev/null 2>&1; then
  ok "Pillow already installed"
else
  warn "Pillow missing, installing into your user site"
  if "$PY" -m pip install --user --quiet Pillow >/dev/null 2>&1; then
    ok "Pillow installed"
  else
    die "Could not install Pillow. Try:  $PY -m pip install --user Pillow"
  fi
fi

step "Checking your terminal"
case "${COLORTERM:-}" in
  truecolor|24bit) ok "truecolor detected" ;;
  *)
    if [ "${TERM_PROGRAM:-}" = "Apple_Terminal" ]; then
      warn "Apple Terminal: 256 colours only"
      warn "iTerm2, Ghostty or WezTerm look far better"
    else
      warn "no truecolor detected - the game will fall back to 256 colours"
    fi
    ;;
esac

# ---------------------------------------------------------------- download

step "Downloading pokecli"
TMP="$(mktemp -d 2>/dev/null || mktemp -d -t pokecli)"
trap 'rm -rf "$TMP"' EXIT INT TERM

if command -v curl >/dev/null 2>&1; then
  curl -fsSL "$TARBALL" -o "$TMP/pokecli.tar.gz" || die "Download failed from $TARBALL"
elif command -v wget >/dev/null 2>&1; then
  wget -qO "$TMP/pokecli.tar.gz" "$TARBALL" || die "Download failed from $TARBALL"
else
  die "curl or wget is required."
fi
[ -s "$TMP/pokecli.tar.gz" ] || die "Empty archive."
ok "archive fetched ($(wc -c < "$TMP/pokecli.tar.gz" | tr -d ' ') bytes)"

step "Installing into $APP"
rm -rf "$APP"
mkdir -p "$APP"
tar -xzf "$TMP/pokecli.tar.gz" -C "$APP" || die "Could not read the archive."
# tarball may contain a single top-level directory; flatten it if so
if [ ! -f "$APP/pokecli.py" ]; then
  inner="$(find "$APP" -maxdepth 2 -name pokecli.py -print -quit 2>/dev/null || true)"
  [ -n "$inner" ] || die "Unexpected archive: pokecli.py not found."
  dir="$(dirname "$inner")"
  if [ "$dir" != "$APP" ]; then
    (cd "$dir" && tar cf - .) | (cd "$APP" && tar xf -)
    rm -rf "$dir"
  fi
fi
ok "files in place"

# ---------------------------------------------------------------- launcher

step "Creating the launcher"
mkdir -p "$BINDIR"
cat > "$LAUNCHER" <<LAUNCHER_EOF
#!/bin/sh
# pokecli launcher, generated by install.sh
exec "$PY" "$APP/pokecli.py" "\$@"
LAUNCHER_EOF
chmod +x "$LAUNCHER"
ok "$LAUNCHER"

# ---------------------------------------------------------------- PATH check

case ":${PATH}:" in
  *":$BINDIR:"*) PATH_OK=1 ;;
  *) PATH_OK=0 ;;
esac

say ""
say "  ${GREEN}${B}Installation complete.${R}"
say ""

if [ "$PATH_OK" = "1" ]; then
  say "  Start the game:"
  say "    ${GOLD}pokecli${R}"
else
  say "  ${GOLD}$BINDIR is not on your PATH.${R}"
  say "  Add this line to your ~/.zshrc or ~/.bashrc:"
  say ""
  say "    ${SKY}export PATH=\"$BINDIR:\$PATH\"${R}"
  say ""
  say "  Or run it directly:"
  say "    ${GOLD}$LAUNCHER${R}"
fi

say ""
say "  ${DIM}First launch downloads the Pokedex (~10s).${R}"
say "  ${DIM}Link a wallet:  pokecli --wallet 0xYourAddress${R}"
say "  ${DIM}Uninstall:      rm -rf $PREFIX $LAUNCHER${R}"
say ""
