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

# Build tenkana: make the single-file `tenkana' command.
#
#   ./build.sh          writes tenkana next to this script
#   ./build.sh PATH     writes it to PATH instead (a file, or a folder to
#                       put tenkana in; missing folders are made)
#   ./build.sh clean    removes what building made: build/ and tenkana
#
# Needs Rust (cargo, 1.70 or newer), a C compiler and the static C library
# (on Debian and Ubuntu: libc6-dev); nothing else is downloaded.  The work
# is done in build/, next to this script, which is emptied first; the
# finished file is checked (it must run and report this version) before it
# is copied out, and build/ is then removed.  If the check fails, build/ is
# kept, to look at.  Like build/, tenkana is generated and is not kept in
# git or in the CTAN package.
#
# tenkana is one statically linked program for GNU/Linux, with its
# LD_PRELOAD libraries (c/) inside: it runs on any GNU/Linux system with the
# processor it was built on, whatever its C library, and needs nothing
# installed besides TeX.  It is built for the processor of this machine:
# x86-64, with snapshots, or another one, such as aarch64, without them.
# The same sources built with the same Rust and C compilers give the same
# file, byte for byte, wherever they are built.
set -e
here=$(cd "$(dirname "$0")" && pwd)
build="$here/build"

case "$1" in
  clean)
    rm -rf "$build" "$here/tenkana"
    exit 0 ;;
  "") out="$here/tenkana" ;;
  */) out="${1}tenkana" ;;
  *) out=$1
     if [ -d "$out" ]; then out="$out/tenkana"; fi ;;
esac
if [ $# -gt 1 ]; then
  echo "usage: ./build.sh [PATH | clean]" >&2
  exit 2
fi

command -v cargo >/dev/null || {
  echo "build.sh: needs Rust's cargo (see https://rustup.rs)." >&2
  exit 1
}

rm -rf "$build"
mkdir "$build"
# this machine's processor, as Rust names it (x86_64-unknown-linux-gnu, ...)
target=$(rustc -vV | sed -n 's/^host: //p')
# static, and without the paths of this machine in the file
RUSTFLAGS="-C target-feature=+crt-static --remap-path-prefix=$here=tenkana \
--remap-path-prefix=${CARGO_HOME:-$HOME/.cargo}=cargo" \
  cargo build --quiet --release --locked --offline --target $target \
    --manifest-path "$here/Cargo.toml" --target-dir "$build/target"
bin="$build/target/$target/release/tenkana"

# the file must run and be this version
version=$(sed -n 's/^version = "\(.*\)"$/\1/p' "$here/Cargo.toml")
first=$(XDG_CACHE_HOME="$build/cache" "$bin" --version | sed -n 1p)
[ "$first" = "tenkana $version" ] || {
  echo "build.sh: the built file does not run correctly; see above." >&2
  exit 1
}

mkdir -p "$(dirname "$out")"
cp "$bin" "$out.tmp"
mv -f "$out.tmp" "$out"
rm -rf "$build"
echo "build.sh: wrote $out (tenkana $version)"
