#!/bin/sh
# tenkana, a spider that jumps from the checkpoints of your TeX document.
# Copyright © 2026 निरंजन (Niranjan)
#
# tenkana is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# tenkana is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with tenkana.  If not, see <https://www.gnu.org/licenses/>.
#
# tenkana was written by Claude, a large language model (LLM) made by
# Anthropic.  Courts are divided on whether material generated by an LLM can be
# protected by copyright.  Should it be found that the copyright claimant
# cannot hold copyright in any part of tenkana, that part is in the public
# domain.  Wherever copyright does hold, tenkana is distributed under the terms
# of the GNU General Public License, version 3 or (at your option) any later
# version, as stated above.

# Install tenkana: the `tenkana' command and its man page.  No root needed.
#
#   ./install.sh               ~/.local/bin/tenkana and ~/.local/share/man
#   ./install.sh --system      /usr/local (uses sudo)
#   ./install.sh --uninstall   remove tenkana again
#
# `tenkana' is a single executable file (a Python zip application).  It needs
# Python 3.9 or newer.  To build documents it calls a TeX engine, so a
# TeX distribution such as TeX Live is needed for that, but not to install
# tenkana.  For snapshots it also needs a C compiler: tenkana compiles its
# snapshot library on first use.
set -e
here=$(cd "$(dirname "$0")" && pwd)
SUDO=""
case "$1" in
  ""|--user)
    dest="$HOME/.local/bin"
    mandir="$HOME/.local/share/man/man1" ;;
  --system)
    dest=/usr/local/bin
    mandir=/usr/local/share/man/man1
    [ "$(id -u)" -eq 0 ] || SUDO=sudo ;;
  --uninstall)
    for f in "$HOME/.local/bin/tenkana" /usr/local/bin/tenkana \
             "$HOME/.local/share/man/man1/tenkana.1" \
             /usr/local/share/man/man1/tenkana.1; do
      [ -e "$f" ] || continue
      if [ -w "$(dirname "$f")" ]; then rm -f "$f"; else sudo rm -f "$f"; fi
      echo "install.sh: removed $f"
    done
    rm -rf "${XDG_CACHE_HOME:-$HOME/.cache}/tenkana"
    exit 0 ;;
  *)
    echo "usage: ./install.sh [--user | --system | --uninstall]" >&2
    exit 2 ;;
esac

python3 -c 'import sys; sys.exit(sys.version_info < (3, 9))' \
  2>/dev/null || {
  echo "install.sh: needs Python 3.9 or newer (python3 on your PATH)."
  exit 1
}

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
cp -r "$here/src/tenkana" "$tmp/tenkana"
find "$tmp" -name __pycache__ -prune -exec rm -rf {} +
python3 -m zipapp "$tmp" -m "tenkana.cli:entry" -p "/usr/bin/env python3" \
  -o "$tmp/tenkana.pyz"

$SUDO mkdir -p "$dest" "$mandir"
$SUDO install -m 755 "$tmp/tenkana.pyz" "$dest/tenkana"
$SUDO install -m 644 "$here/tenkana.1" "$mandir/tenkana.1"
"$dest/tenkana" --version >/dev/null  # it runs (set -e stops here if not)
python3 -B -c 'import sys; sys.path.insert(0, sys.argv[1])
from tenkana import __version__
from tenkana.common import banner
print(banner(__version__) + "\n")' "$here/src"
echo "Installed successfully."

if ! command -v lualatex >/dev/null; then
  echo "install.sh: lualatex was not found; to build documents, install a"
  echo "install.sh: TeX distribution such as TeX Live."
fi
command -v cc >/dev/null || command -v gcc >/dev/null ||
  command -v clang >/dev/null ||
  echo "install.sh: no C compiler found; tenkana will build without snapshots."
case ":$PATH:" in
  *":$dest:"*)
    # another tenkana earlier on the PATH would run instead of this one
    found=$(command -v tenkana || true)
    if [ "$found" != "$dest/tenkana" ]; then
      echo "install.sh: but $found comes first on your PATH and runs instead;"
      echo "install.sh: remove it, or put $dest before it in PATH."
    fi ;;
  *) echo "install.sh: $dest is not on your PATH; add this line to ~/.bashrc"
     echo "install.sh: export PATH=\"$dest:\$PATH\"" ;;
esac
