#!/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.  Its maintainer designs tenkana and tests what it does, but does
# not understand its code; as far as he knows, no human has reviewed it.
# 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, ~/.local/share/man
#   ./install.sh --system       /usr/local/bin, /usr/local/share/man (sudo)
#   ./install.sh --prefix DIR   DIR/bin, DIR/share/man (sudo if DIR is not
#                               yours)
#   ./install.sh --uninstall    remove tenkana again (from the places above;
#                               add --prefix DIR for that place)
#
# --uninstall also removes what tenkana keeps for you: its cache (snapshots
# on disk, cached preambles, its libraries; ~/.cache/tenkana) and
# /tmp/tenkana-<uid>.  Snapshots still in memory end by themselves within
# half a minute once their cache is gone.  It never touches a tenkana that
# belongs to TeX Live: remove that one with `tlmgr remove tenkana'.
# `tenkana --uninstall' does the same for the tenkana it is run as; this
# script's needs no working tenkana.
#
# The command is built first with ./build.sh, so this folder must be one
# you can write to (a copy of the git repository or the CTAN package).
#
# `tenkana' is a single executable file.  Building it needs Rust (cargo) and
# a C compiler (see build.sh); running it needs nothing but, to build
# documents, a TeX distribution such as TeX Live.
set -e
here=$(cd "$(dirname "$0")" && pwd)

usage() {
  echo "usage: ./install.sh [--system | --prefix DIR] [--uninstall]" >&2
  exit 2
}
# true if FILE is in a TeX Live installation (a folder with
# tlpkg/texlive.tlpdb above it); with --resolve, also if FILE is a link to
# a file there
in_texlive() {
  set -- "$1" ${2:+"$(readlink -f "$1" 2>/dev/null || echo "$1")"}
  for p; do
    d=$(dirname "$p")
    while [ "$d" != / ] && [ -n "$d" ]; do
      [ -f "$d/tlpkg/texlive.tlpdb" ] && return 0
      d=$(dirname "$d")
    done
  done
  return 1
}
# true if DIR, or the nearest folder above it that exists, is writable
writable() {
  d=$1
  while [ ! -e "$d" ]; do d=$(dirname "$d"); done
  [ -w "$d" ]
}

dest="$HOME/.local/bin"
mandir="$HOME/.local/share/man/cat1"
prefix=""
uninstall=""
SUDO=""
while [ $# -gt 0 ]; do
  case "$1" in
    --user) prefix="" ;;
    --system) prefix=/usr/local ;;
    --prefix) [ $# -ge 2 ] && [ -n "$2" ] || usage
              prefix=$2; shift ;;
    --prefix=*) prefix=${1#--prefix=}; [ -n "$prefix" ] || usage ;;
    --uninstall) uninstall=yes ;;
    *) usage ;;
  esac
  shift
done
if [ -n "$prefix" ]; then
  case "$prefix" in /*) ;; *) prefix="$PWD/$prefix" ;; esac
  dest="$prefix/bin"
  mandir="$prefix/share/man/cat1"
  writable "$prefix" || SUDO=sudo
fi

if [ -n "$uninstall" ]; then
  if [ -n "$prefix" ]; then
    set -- "$dest/tenkana" "$mandir/tenkana.1" \
           "$prefix/share/man/man1/tenkana.1"
  else
    set -- "$HOME/.local/bin/tenkana" /usr/local/bin/tenkana \
           "$HOME/.local/share/man/cat1/tenkana.1" \
           /usr/local/share/man/cat1/tenkana.1 \
           "$HOME/.local/share/man/man1/tenkana.1" \
           /usr/local/share/man/man1/tenkana.1
  fi
  # (man1/tenkana.1: the man page of tenkana 1.0.0 and before)
  for f; do
    [ -e "$f" ] || [ -L "$f" ] || continue
    if in_texlive "$f"; then
      echo "install.sh: $f belongs to TeX Live; left as it is."
      continue
    fi
    if [ -w "$(dirname "$f")" ]; then rm -f "$f"; else sudo rm -f "$f"; fi
    echo "install.sh: removed $f"
  done
  # what tenkana keeps for you (yours only: never another user's)
  for d in "${XDG_CACHE_HOME:-$HOME/.cache}/tenkana" \
           "/tmp/tenkana-$(id -u)"; do
    [ -e "$d" ] && [ -O "$d" ] || continue
    rm -rf "$d"
    echo "install.sh: removed $d"
  done
  hash -r 2>/dev/null || true
  left=$(command -v tenkana || true)
  if [ -n "$left" ] && in_texlive "$left" --resolve; then
    echo "install.sh: TeX Live's tenkana ($left) is left as it is;"
    echo "install.sh: \`tlmgr remove tenkana' removes it."
  elif [ -n "$left" ]; then
    echo "install.sh: another tenkana is still on your PATH: $left"
  fi
  exit 0
fi

"$here/build.sh" >/dev/null

$SUDO mkdir -p "$dest" "$mandir"
$SUDO install -m 755 "$here/tenkana" "$dest/tenkana"
# the manual is plain text, a man page as man shows it (cat1); man would
# show an older tenkana's man1/tenkana.1 instead, so that goes
$SUDO install -m 644 "$here/tenkana.txt" "$mandir/tenkana.1"
old="${mandir%/cat1}/man1/tenkana.1"
if [ -e "$old" ] && ! in_texlive "$old"; then
  $SUDO rm -f "$old"
fi
"$dest/tenkana" --version >/dev/null  # it runs (set -e stops here if not)
"$dest/tenkana" --banner
echo
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
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
