#!/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, ~/.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)
#
# 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 (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)

usage() {
  echo "usage: ./install.sh [--system | --prefix DIR] [--uninstall]" >&2
  exit 2
}
# 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/man1"
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/man1"
  writable "$prefix" || SUDO=sudo
fi

if [ -n "$uninstall" ]; then
  if [ -n "$prefix" ]; then
    set -- "$dest/tenkana" "$mandir/tenkana.1"
  else
    set -- "$HOME/.local/bin/tenkana" /usr/local/bin/tenkana \
           "$HOME/.local/share/man/man1/tenkana.1" \
           /usr/local/share/man/man1/tenkana.1
  fi
  for f; 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
fi

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
}

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

$SUDO mkdir -p "$dest" "$mandir"
$SUDO install -m 755 "$here/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
