#!/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)
#
# 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 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

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

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