#!/bin/sh
# sshj installer — one command, stdlib-only Python, no root, no pip.
#
# Usage:  sh install.sh        (run from the sshj tree, or the extracted tarball)
#
# What it does:
#   1. Copies the package to  ~/.local/share/sshj/src  (SSHJ_INSTALL_DIR to override)
#   2. Drops launchers        ~/.local/bin/sshj  +  ~/.local/bin/sshj-relay
#   3. Registers ~/.local/bin on PATH in the rc files of EVERY shell that is
#      installed on this machine (bash, zsh, fish) — idempotent via marker.
#      This is the "all present shells" guarantee: the launcher lives in the
#      standard user-bin dir, and each shell's rc makes sure that dir is on
#      PATH, whatever the shell.
#   4. Smoke-tests the install (import + version) and prints what it did.
#
# Uninstall: rm -rf ~/.local/share/sshj ~/.local/bin/sshj ~/.local/bin/sshj-relay
#            (and the marked PATH line in your shell rc files, if you want it gone)

set -eu

SRC_ROOT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
PKG="$SRC_ROOT/src/sshj"
RELAY="$SRC_ROOT/src/sshj-relay"
[ -d "$PKG" ]  || { echo "install: $PKG not found — run from inside the sshj tree" >&2; exit 1; }
[ -f "$RELAY" ] || { echo "install: $RELAY not found" >&2; exit 1; }

PY=${PYTHON:-python3}
command -v "$PY" >/dev/null 2>&1 || { echo "install: python3 not found on PATH" >&2; exit 1; }

INSTALL_DIR=${SSHJ_INSTALL_DIR:-"$HOME/.local/share/sshj"}
BIN_DIR=${SSHJ_BIN_DIR:-"$HOME/.local/bin"}
MARKER="# sshj (install.sh)"

echo "== sshj installer =="
echo "source:    $SRC_ROOT"
echo "package:   $INSTALL_DIR/src"
echo "bin dir:   $BIN_DIR"

# --- 1. copy the package ----------------------------------------------------
mkdir -p "$INSTALL_DIR/src" "$BIN_DIR"
rm -rf "$INSTALL_DIR/src/sshj"
cp -R "$PKG" "$INSTALL_DIR/src/sshj"
cp "$RELAY" "$INSTALL_DIR/src/sshj-relay"
chmod +x "$INSTALL_DIR/src/sshj-relay"

# --- 2. launchers -----------------------------------------------------------
cat > "$BIN_DIR/sshj" <<EOF
#!/usr/bin/env python3
"""sshj launcher — package installed at $INSTALL_DIR/src by install.sh."""
import sys
sys.path.insert(0, "$INSTALL_DIR/src")
from sshj import main
if __name__ == "__main__":
    main()
EOF
chmod +x "$BIN_DIR/sshj"
# the relay is found by sshj itself (shutil.which first, then the copy next
# to the package); a bin-dir link keeps TIOCSTI-refusing setups covered too
ln -sf "$INSTALL_DIR/src/sshj-relay" "$BIN_DIR/sshj-relay" 2>/dev/null || \
  cp "$INSTALL_DIR/src/sshj-relay" "$BIN_DIR/sshj-relay"

# --- 3. PATH registration: every shell that exists --------------------------
# Marker-guarded append: re-running never duplicates. We do not try to parse
# PATH semantics out of rc files — the line we add is ours, the marker proves
# it is there. (Procedural: each shell's rc is that shell's own config file;
# we add to the ones that exist, we don't rewire the rest.)
registered=""
skipped=""

add_path_bash() {  # $1 = rc file
  if [ ! -f "$1" ] || ! grep -qF "$MARKER" "$1"; then
    printf '\n%s\nexport PATH="%s:$PATH"\n' "$MARKER" "$BIN_DIR" >> "$1"
    registered="$registered $1"
  else
    skipped="$skipped $1"
  fi
}
add_path_zsh() {   # same shape; zsh reads .zshrc for interactive shells
  add_path_bash "$1"
}
add_path_fish() {  # $1 = config.fish — fish has no export
  if [ ! -f "$1" ]; then
    mkdir -p "$(dirname "$1")"
  fi
  if ! grep -qF "$MARKER" "$1"; then
    printf '\n%s\n' "$MARKER" >> "$1"
    printf 'set -gx PATH "%s" $PATH\n' "$BIN_DIR" >> "$1"
    registered="$registered $1"
  else
    skipped="$skipped $1"
  fi
}

if command -v bash >/dev/null 2>&1; then
  add_path_bash "$HOME/.bashrc"
  add_path_bash "$HOME/.profile"
fi
if command -v zsh >/dev/null 2>&1; then
  add_path_zsh "$HOME/.zshrc"
fi
if command -v fish >/dev/null 2>&1; then
  add_path_fish "$HOME/.config/fish/config.fish"
fi

if [ -n "$registered" ]; then echo "PATH registered:$registered"; fi
if [ -n "$skipped" ]; then echo "PATH already set (skipped):$skipped"; fi

# --- 4. smoke test -----------------------------------------------------------
"$PY" -c "import sys; sys.path.insert(0, '$INSTALL_DIR/src'); import sshj; print('import OK — sshj ' + sshj.VER)"

echo
echo "installed. Open a NEW shell (or: source your rc) and run:  sshj"
echo "uninstall: rm -rf '$INSTALL_DIR' '$BIN_DIR/sshj' '$BIN_DIR/sshj-relay'"
