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

# Generate ch01.tex .. ch12.tex for the test book (book.tex): ~220 pages of
# lipsum text, theorems, align blocks, siunitx tables, pgfplots and TikZ
# figures, with \label/\cref/\pageref cross-references.  Run it in this
# folder: sh make_chapters.sh
set -e
c=1
while [ $c -le 12 ]; do
  f=$(printf 'ch%02d.tex' $c)
  {
    printf '\\chapter{Chapter %d}\\label{ch:%d}\n' $c $c
    s=1
    while [ $s -le 6 ]; do
      printf '\\section{Section %d.%d}\\label{sec:%d-%d}\n' $c $s $c $s
      printf '\\lipsum[%d-%d]\n' $s $((s + 3))
      printf '%s' '\begin{theorem}'
      printf '\\label{thm:%d-%d} For all $x>0$, ' $c $s
      printf '%s' '$\int_0^x e^{-t^2}\,dt < \frac{\sqrt\pi}{2}$ and '
      printf '\\SI{%d.%d}' $c $s
      printf '%s\n' '{\kilo\gram\metre\per\second\squared}.\end{theorem}'
      printf '%s\n' '\begin{align}' \
        ' f(x) &= \sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!}(x-a)^n \\' \
        ' g(x) &= \prod_{k=1}^{N} \left(1+\frac{x}{k}\right)' \
        '\end{align}'
      printf 'See \\cref{sec:%d-%d} and \\cref{thm:%d-%d} ' $c $s $c $s
      printf 'on page~\\pageref{thm:%d-%d}.\n' $c $s
      if [ $((s % 2)) = 0 ]; then
        printf '%s\n' '\begin{figure}[htbp]\centering' '\begin{tikzpicture}' \
          '\begin{axis}[width=8cm,height=5cm,xlabel=$x$,ylabel=$y$]'
        printf '\\addplot[blue,domain=0:%d,samples=80]' $((c + s))
        printf '{sin(deg(x))*exp(-x/%d)};\n' $((c + 2))
        printf '\\addplot[red,domain=0:%d,samples=80]' $((c + s))
        printf '%s\n' '{cos(deg(x))};' '\end{axis}\end{tikzpicture}'
        printf '\\caption{Plot %d.%d}\\label{fig:%d-%d}' $c $s $c $s
        printf '%s\n' '\end{figure}'
      else
        printf '%s\n' '\begin{figure}[htbp]\centering' \
          '\begin{tikzpicture}[node distance=1.6cm,>=Stealth]' \
          '\node[draw,rounded corners] (a) {Input};'
        printf '\\node[draw,circle,right=of a] (b) {$f_{%d}$};\n' $c
        printf '%s\n' '\node[draw,diamond,aspect=2,right=of b] (c) {Test};' \
          '\node[draw,right=of c] (d) {Output};'
        printf '%s' '\draw[->] (a)--(b); \draw[->] (b)--(c); '
        printf '%s\n' '\draw[->,decorate,decoration=snake] (c)--(d);'
        printf '%s' '\draw[->] (c.south) .. controls +(0,-1) and +(0,-1) .. '
        printf '%s\n' '(b.south);'
        printf '\\end{tikzpicture}\\caption{Diagram %d.%d}' $c $s
        printf '\\label{fig:%d-%d}\\end{figure}\n' $c $s
      fi
      printf '%s' '\begin{table}[htbp]\centering\begin{tabular}{lSS}'
      printf '%s\n' '\toprule Item & {Value} & {Error}\\\midrule'
      r=0
      while [ $r -lt 6 ]; do  # values in thousandths: c*1.5+r, 0.01*(r+1)
        v=$((c * 1500 + r * 1000)); e=$(((r + 1) * 10))
        printf 'R%d & %d.%03d & %d.%03d\\\\\n' $r $((v / 1000)) $((v % 1000)) \
          $((e / 1000)) $((e % 1000))
        r=$((r + 1))
      done
      printf '%s\n' '\bottomrule\end{tabular}\caption{Data}\end{table}'
      printf '\\lipsum[%d-%d]\n' $((s + 10)) $((s + 14))
      s=$((s + 1))
    done
  } > "$f"
  c=$((c + 1))
done
echo "wrote ch01.tex .. ch12.tex"
