#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# build.sh -- build The Makronix Guide.
#
# The guide has one source, docs/guide/book.xml with one file per chapter in
# docs/guide/chapters. This script derives the five forms from it:
#
# out/makronix-guide.html one self-contained HTML file (CSS and logo in it)
# out/makronix-guide.pdf the same page printed by Chrome
# out/man/*.7 one manual page per chapter and the index
# out/infolib/makronix.dti the MCDE Information Manager bookcase
#
# Usage: docs/guide/build.sh [all|html|pdf|man|infolib|check|install]
#
# The MCDE infolib step needs a host build of MCDE (make mcde-host); it uses
# build/mcde-root/dt/bin/dtdocbook2infolib.
set -eu
TOP=$(cd "$(dirname "$0")/../.." && pwd)
GUIDE=$TOP/docs/guide
OUT=$GUIDE/out
MCDE=$TOP/ext/src/mcde
ARCH=${ARCH:-amd64}
DESTDIR=${DESTDIR:-$TOP/build/obj/destdir.$ARCH}
DOCDIR=$TOP/src/share/doc/makronix
MANDIR=$TOP/src/share/man/man7
ETCDIR=$TOP/src/etc
WWW=$TOP/www
TMP=
cleanup()
{
[ -n "$TMP" ] && rm -rf "$TMP"
}
trap cleanup EXIT INT TERM
die()
{
echo "guide: $*" >&2
exit 1
}
find_tool()
{
for t in "$@"; do
if command -v "$t" >/dev/null 2>&1; then
command -v "$t"
return 0
fi
done
return 1
}
PANDOC=$(find_tool pandoc) || die "pandoc is not installed"
XMLLINT=$(find_tool xmllint) || die "xmllint is not installed"
MANDOC=$(find_tool mandoc) || die "mandoc is not installed"
PDFINFO=$(find_tool pdfinfo) || true
BASE64=$(find_tool base64) || die "base64 is not installed"
if [ -z "${CHROME:-}" ]; then
CHROME=$(find_tool google-chrome google-chrome-stable) || CHROME=
fi
GS=$(find_tool gs) || GS=
QPDF=$(find_tool qpdf) || QPDF=
MUTOOL=$(find_tool mutool) || MUTOOL=
PDFTOTEXT=$(find_tool pdftotext) || PDFTOTEXT=
find_mcde_bin()
{
for d in "$TOP/build/mcde-root/dt/bin" \
"$TOP/build/mcde-host-stage/usr/mdt/bin"; do
if [ -x "$d/dtdocbook2infolib" ] && [ -x "$d/dtsrcreate" ]; then
echo "$d"
return 0
fi
done
return 1
}
resolve_xml()
{
$XMLLINT --xinclude --noout "$GUIDE/book.xml" ||
die "book.xml does not resolve (XInclude or XML error)"
$XMLLINT --xinclude "$GUIDE/book.xml" |
sed "s|@WWWIMG@|$TOP/www/img|g" > "$TMP/guide.xml"
}
write_template()
{
# The CSS is a plain file, not a pandoc variable: a stylesheet inside a
# template variable would be escaped. The logo goes in as a data URI so
# that the installed HTML is one file.
awk -v css="$GUIDE/style.css" '
/^@CSS@$/ {
while ((getline line < css) > 0) print line
close(css)
next
}
{ print }
' "$GUIDE/template.html5" > "$TMP/template.html5"
logo=$($BASE64 < "$TOP/docs/logo/makronix.png" | tr -d '\n')
awk -v logo="data:image/png;base64,$logo" '
{ gsub(/@LOGO@/, logo); print }
' "$TMP/template.html5" > "$TMP/template-html5"
}
build_html()
{
write_template
$PANDOC -f docbook -t html5 --standalone \
--toc --toc-depth=2 --syntax-highlighting=none \
--template="$TMP/template-html5" \
--metadata title="The Makronix Guide" \
--metadata subtitle="A guide to the Makronix operating system and the MCDE desktop" \
--metadata edition="Makronix 0.99.10 (Camouflage) -- November 2026" \
"$TMP/guide.xml" -o "$OUT/makronix-guide.html" ||
die "pandoc could not write the HTML"
[ -s "$OUT/makronix-guide.html" ] || die "empty HTML"
embed_images "$OUT/makronix-guide.html"
}
embed_images()
{
# The system HTML is one file: the screenshots become data URIs. The
# website form keeps www/img/ paths (build_web rewrites them).
f=$1
for img in "$TOP"/www/img/*.png; do
[ -f "$img" ] || continue
grep -q "src=\"$img\"" "$f" || continue
$BASE64 < "$img" | tr -d '\n' > "$TMP/image.b64"
awk -v needle="src=\"$img\"" \
-v datafile="$TMP/image.b64" '
BEGIN {
data = ""
while ((getline line < datafile) > 0)
data = data line
close(datafile)
}
{
out = ""; s = $0
while ((p = index(s, needle)) > 0) {
out = out substr(s, 1, p - 1) \
"src=\"data:image/png;base64," data "\""
s = substr(s, p + length(needle))
}
print out s
}
' "$f" > "$f.tmp" || die "cannot embed $img"
mv "$f.tmp" "$f"
done
}
build_web()
{
$PANDOC -f docbook -t html4 --standalone \
--toc --toc-depth=1 --syntax-highlighting=none \
--template="$GUIDE/template-web.html" \
--metadata title="The Makronix Guide" \
--metadata subtitle="A guide to the Makronix operating system and the MCDE desktop" \
--metadata edition="Makronix 0.99.10 (Camouflage) -- November 2026" \
"$TMP/guide.xml" -o "$OUT/guide.html" ||
die "pandoc could not write the web page"
# The website uses the images next to guide.html, and HTML 4.01 Strict
# has no self-closing void elements.
sed -e "s|src=\"$TOP/www/img/|src=\"img/|g" \
-e 's|
]*\) />|
|g' \
-e 's|
|
|g' -e 's|
|
|g' \
"$OUT/guide.html" > "$OUT/guide.html.tmp" &&
mv "$OUT/guide.html.tmp" "$OUT/guide.html"
[ -s "$OUT/guide.html" ] || die "empty web page"
}
pdf_pages()
{
$PDFINFO "$OUT/makronix-guide.pdf" 2>/dev/null |
awk '/^Pages:/ { print $2; exit }'
}
add_pdf_numbers()
{
# A transparent PostScript overlay, one page per PDF page: the cover
# stays without a number, the rest says "Page N" in the bottom margin.
pages=$(pdf_pages)
[ -n "$pages" ] || die "cannot read the PDF page count"
{
printf '%%!PS\n'
printf '<< /PageSize [595 842] >> setpagedevice\n'
printf '/Helvetica findfont 9 scalefont setfont\n'
printf '1 1 %s {\n' "$pages"
printf ' /n exch def\n'
printf ' n 1 gt {\n'
printf ' gsave\n'
printf ' 0 0 0 setrgbcolor\n'
printf ' /s (Page ) def\n'
printf ' /t n 20 string cvs def\n'
printf ' s stringwidth pop t stringwidth pop add 2 div\n'
printf ' 297 exch sub 28 moveto\n'
printf ' s show t show\n'
printf ' grestore\n'
printf ' } if\n'
printf ' showpage\n'
printf '} for\n'
} > "$TMP/numbers.ps"
"$GS" -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
-sOutputFile="$TMP/numbers.pdf" "$TMP/numbers.ps" ||
die "gs could not make the page-number overlay"
"$QPDF" --overlay --file="$TMP/numbers.pdf" -- \
"$OUT/makronix-guide.pdf" "$TMP/numbered.pdf" ||
die "qpdf could not overlay the page numbers"
mv "$TMP/numbered.pdf" "$OUT/makronix-guide.pdf"
}
add_pdf_outline()
{
# The chapter headings are the first line of their page (the CSS starts
# every h1 on a new page). Read the first two non-empty lines of every
# page once, then find each chapter title in order.
"$PDFTOTEXT" -layout "$OUT/makronix-guide.pdf" "$TMP/pages.txt" ||
die "pdftotext could not read the PDF"
awk -v RS='\f' '
{
n = split($0, line, "\n")
first = ""; second = ""
for (i = 1; i <= n; i++) {
gsub(/^[ \t]+|[ \t]+$/, "", line[i])
if (line[i] == "") continue
if (first == "") first = line[i]
else if (second == "") second = line[i]
else break
}
printf "%d\t%s\t%s\n", NR, first, second
}
' "$TMP/pages.txt" > "$TMP/pagefirst.txt"
tab=$(printf '\t')
last=0
: > "$TMP/outline.map"
while IFS='|' read -r id title suffix file; do
case "$id" in
''|\#*) continue ;;
esac
found=
while IFS="$tab" read -r p first second; do
[ "$p" -le "$last" ] && continue
if [ "$first" = "$title" ] ||
[ "$first $second" = "$title" ]; then
found=$p
break
fi
done < "$TMP/pagefirst.txt"
[ -n "$found" ] ||
die "PDF outline: no page starts with '$title'"
printf '%s\t%s\n' "$title" "$found" >> "$TMP/outline.map"
last=$found
done < "$GUIDE/chapters.list"
{
printf 'var doc = Document.openDocument("%s");\n' \
"$OUT/makronix-guide.pdf"
printf 'var it = doc.outlineIterator();\n'
while IFS="$tab" read -r title p; do
t=$(printf '%s' "$title" | sed 's/\\/\\\\/g; s/"/\\"/g')
printf 'it.insert({title: "%s", uri: "#page=%s"});\n' \
"$t" "$p"
printf 'it.next();\n'
done < "$TMP/outline.map"
printf 'doc.save("%s");\n' "$TMP/outlined.pdf"
} > "$TMP/outline.js"
"$MUTOOL" run "$TMP/outline.js" ||
die "mutool could not write the PDF outline"
# mutool saves the outline in an incremental update that mutool show,
# qpdf and some readers do not see until the file is rewritten.
"$MUTOOL" clean -gggg "$TMP/outlined.pdf" "$TMP/clean.pdf" ||
die "mutool could not normalize the PDF outline"
mv "$TMP/clean.pdf" "$OUT/makronix-guide.pdf"
}
build_pdf()
{
[ -n "$CHROME" ] || die "Chrome (google-chrome or chromium) is required for the PDF"
[ -s "$OUT/makronix-guide.html" ] || build_html
rm -f "$OUT/makronix-guide.pdf"
"$CHROME" --headless=new --no-sandbox --disable-gpu \
--disable-dev-shm-usage --user-data-dir="$TMP/chrome" \
--no-pdf-header-footer \
--print-to-pdf="$OUT/makronix-guide.pdf" \
"file://$OUT/makronix-guide.html" >"$TMP/chrome.log" 2>&1 ||
{ cat "$TMP/chrome.log" >&2; die "Chrome could not print the PDF"; }
[ -s "$OUT/makronix-guide.pdf" ] || die "empty PDF"
if [ -n "$GS" ] && [ -n "$QPDF" ] && [ -n "$MUTOOL" ] &&
[ -n "$PDFTOTEXT" ] && [ -n "$PDFINFO" ]; then
add_pdf_numbers
add_pdf_outline
else
echo "guide: gs/qpdf/mutool/pdftotext missing; PDF without page numbers and outline" >&2
fi
}
upper()
{
printf '%s' "$1" | tr '[:lower:]' '[:upper:]'
}
build_man()
{
mkdir -p "$OUT/man"
: > "$TMP/man.index"
while IFS='|' read -r id title suffix file; do
case "$id" in ''|\#*) continue ;; esac
$XMLLINT --xinclude --xpath "//*[@id='$id']" "$TMP/guide.xml" \
> "$TMP/chapter.xml" ||
die "chapter $id not found in book.xml"
$PANDOC -f docbook -t man "$TMP/chapter.xml" > "$TMP/chapter.man" ||
die "pandoc could not write the man page for $id"
{
printf '.TH "%s" "7" "November 2026" "Makronix 0.99.10" "Makronix Guide"\n' \
"MAKRONIX-GUIDE-$(upper "$suffix")"
cat "$TMP/chapter.man"
} > "$OUT/man/makronix-guide-$suffix.7"
printf '%s|%s\n' "$suffix" "$title" >> "$TMP/man.index"
done < "$GUIDE/chapters.list"
{
printf '.TH "MAKRONIX-GUIDE" "7" "November 2026" "Makronix 0.99.10" "Makronix Guide"\n'
printf '.SH NAME\n'
printf 'makronix-guide \\- the Makronix Guide\n'
printf '.SH DESCRIPTION\n'
printf 'This is the guide to Makronix, a desktop operating system built on\n'
printf 'NetBSD, and to MCDE, its desktop. The guide has one chapter per\n'
printf 'manual page, and the same text is installed as a PDF and as an HTML\n'
printf 'file in\n'
printf '.I /usr/share/doc/makronix/\n'
printf '.PP\n'
printf 'The MCDE Information Manager\n'
printf '.Xr dtinfo 1\n'
printf 'opens the same text from its information library.\n'
printf '.SH CHAPTERS\n'
while IFS='|' read -r suffix title; do
printf '.TP\n'
printf '.B makronix-guide-%s(7)\n' "$suffix"
printf '%s\n' "$title"
done < "$TMP/man.index"
printf '.SH FILES\n'
printf '.TP\n'
printf '.I /usr/share/doc/makronix/makronix-guide.pdf\n'
printf 'The guide as a PDF.\n'
printf '.TP\n'
printf '.I /usr/share/doc/makronix/makronix-guide.html\n'
printf 'The guide as one HTML file.\n'
printf '.TP\n'
printf '.I /etc/dt/infolib/en_US.UTF-8/makronix.dti\n'
printf 'The guide as an MCDE information library.\n'
printf '.SH SEE ALSO\n'
printf '.BR dtinfo (1),\n'
printf '.BR man (1),\n'
printf '.BR afterboot (8)\n'
} > "$OUT/man/makronix-guide.7"
}
build_infolib()
{
MCDE_BIN=$(find_mcde_bin) ||
die "no host MCDE build (build/mcde-root/dt/bin); run make mcde-host first"
mkdir -p "$TMP/cde"
$PANDOC -f docbook -t "$GUIDE/tools/docbook-to-cde.lua" "$TMP/guide.xml" \
> "$TMP/cde/book.sgm" ||
die "the DocBook to CDE SGML writer failed"
grep -q '' "$TMP/cde/book.sgm" ||
die "the CDE writer produced no Book"
# A nested is invalid in the CDE DTD; catch it here, not in dtinfo.
if grep -q '[^<]*' "$TMP/cde/book.sgm"; then
die "the CDE writer produced a nested para"
fi
cat > "$TMP/cde/bookcase.bc" <
]>
MAKRONIX>
The Makronix Guide>
&docsty;
The Makronix Guide>
Makronix Guide>
&guideTOC;>
&guide;>
EOF
COMMON=$MCDE/doc/en_US.UTF-8/guides/common
PATH=$MCDE_BIN:$PATH
SGML_SEARCH_PATH=$TMP/cde:$MCDE/doc/en_US.UTF-8/guides:$MCDE/programs/dtdocbook/sgml
export PATH SGML_SEARCH_PATH
$MCDE_BIN/dtdocbook2infolib tocgen -L en_US.UTF-8 -T "$TMP/cde/toc" \
-m "$COMMON/catalog" -f "$TMP/cde/TOC.sgm" \
-id makronix.guide.toc -title "The Makronix Guide" \
"$TMP/cde/book.sgm" > "$TMP/cde/toc.log" 2>&1 ||
{ cat "$TMP/cde/toc.log" >&2; die "tocgen failed"; }
$MCDE_BIN/dtdocbook2infolib build -v -L en_US.UTF-8 \
-T "$TMP/cde/infolib-build" -m "$COMMON/catalog" \
-l "$TMP/cde/infolib" -n makronix -d "The Makronix Guide" \
"$TMP/cde/bookcase.bc" > "$TMP/cde/build.log" 2>&1 ||
{ cat "$TMP/cde/build.log" >&2; die "dtdocbook2infolib failed"; }
rm -rf "$OUT/infolib"
mkdir -p "$OUT/infolib"
cp -R "$TMP/cde/infolib" "$OUT/infolib/makronix.dti"
# Keep the generated SGML and bookcase beside the compiled library: they
# are the readable record of what dtinfo holds (and are not installed).
cp "$TMP/cde/book.sgm" "$OUT/infolib/book.sgm"
cp "$TMP/cde/TOC.sgm" "$OUT/infolib/TOC.sgm"
cp "$TMP/cde/bookcase.bc" "$OUT/infolib/bookcase.bc"
[ -f "$OUT/infolib/makronix.dti/bookcase.map" ] ||
die "the information library has no bookcase.map"
}
verify_all()
{
# Rebuild the two deterministic forms and compare them with the copies
# in src/. The PDF and the information library are not byte-reproducible
# (Chrome and dtdocbook2infolib write timestamps), so they are checked by
# check.sh, not here.
v=$TMP/verify
mkdir -p "$v"
saved=$OUT
OUT=$v
build_html
build_man
OUT=$saved
cmp -s "$v/makronix-guide.html" "$DOCDIR/makronix-guide.html" ||
die "the HTML in src/ is stale; run build.sh install"
for f in "$v"/man/*.7; do
b=${f##*/}
cmp -s "$f" "$MANDIR/$b" ||
die "the manual page $b in src/ is stale; run build.sh install"
done
echo "guide verify: the HTML and the manual pages in src/ match the source"
}
check_all()
{
[ -s "$OUT/makronix-guide.html" ] || die "missing HTML"
[ -s "$OUT/makronix-guide.pdf" ] || die "missing PDF"
[ -s "$OUT/guide.html" ] || die "missing web page"
grep -q 'class="content guide"' "$OUT/guide.html" ||
die "the web page has no site window"
grep -q 'href="guide.html" class="here"' "$OUT/guide.html" ||
die "the web page does not mark the Guide menu"
if $XMLLINT --html --noout "$OUT/guide.html" 2>"$TMP/web.err"; then
:
else
cat "$TMP/web.err" >&2
die "the web page does not parse as HTML"
fi
grep -q 'class="cover"' "$OUT/makronix-guide.html" ||
die "the HTML has no cover"
grep -q 'The Makronix Guide' "$OUT/makronix-guide.html" ||
die "the HTML does not name the guide"
[ -f "$OUT/man/makronix-guide.7" ] || die "missing man index"
n=0
for f in "$OUT"/man/*.7; do
[ -f "$f" ] || continue
head -1 "$f" | grep -q '^\.TH ' || die "$f has no .TH line"
$MANDOC -Tutf8 "$f" >/dev/null 2>"$TMP/mandoc.err" ||
{ cat "$TMP/mandoc.err" >&2; die "$f does not render"; }
n=$((n + 1))
done
[ "$n" -ge 2 ] || die "no man pages were generated"
[ -f "$OUT/infolib/makronix.dti/bookcase.map" ] ||
die "missing information library"
[ -f "$OUT/infolib/makronix.dti/MAKRONIX/MAKRONIX.dbd" ] ||
die "the information library has no book database"
if [ -n "$PDFINFO" ]; then
pages=$($PDFINFO "$OUT/makronix-guide.pdf" 2>/dev/null |
awk '/^Pages:/ { print $2 }')
[ -n "$pages" ] && [ "$pages" -gt 1 ] ||
die "the PDF has no pages"
echo "guide: $n manual pages, $pages PDF pages"
else
echo "guide: $n manual pages (pdfinfo not installed)"
fi
if [ -n "$MUTOOL" ]; then
$MUTOOL show "$OUT/makronix-guide.pdf" outline 2>/dev/null |
grep -q 'MCDE: The Desktop' ||
die "the PDF has no chapter outline"
fi
if [ -n "$PDFTOTEXT" ]; then
$PDFTOTEXT "$OUT/makronix-guide.pdf" - 2>/dev/null |
grep -q 'Page ' ||
die "the PDF has no page numbers"
fi
}
install_all()
{
[ -s "$OUT/makronix-guide.html" ] || die "build the guide first"
[ -s "$OUT/makronix-guide.pdf" ] || die "build the guide first"
[ -s "$OUT/guide.html" ] || die "build the web page first"
mkdir -p "$DOCDIR" "$MANDIR" "$ETCDIR" "$DOCDIR/infolib"
cp "$OUT/guide.html" "$WWW/guide.html"
cp "$OUT/makronix-guide.html" "$OUT/makronix-guide.pdf" "$DOCDIR/"
cp "$OUT"/man/*.7 "$MANDIR/"
rm -rf "$DOCDIR/infolib/makronix.dti"
cp -R "$OUT/infolib/makronix.dti" "$DOCDIR/infolib/"
# The Information Manager uses the default library name; the session
# script makes the Makronix Guide the default. The library data is
# installed under /usr/share/doc/makronix/infolib; src/etc/Makefile
# makes the symlink under /etc/dt/infolib.
cat > "$ETCDIR/makronix-xsession" <<'EOF'
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
# The Makronix Guide is the default library of the Information Manager.
# See docs/guide and makronix-guide(7).
DTINFOLIBDEFAULT=makronix
export DTINFOLIBDEFAULT
EOF
chmod 755 "$ETCDIR/makronix-xsession"
echo "guide: installed into src/share/doc/makronix, src/share/man/man7, src/etc and www"
}
mkdir -p "$OUT"
TMP=$(mktemp -d "${TMPDIR:-/tmp}/makronix-guide.XXXXXX") ||
die "cannot create a temporary directory"
case ${1:-all} in
all)
resolve_xml
build_html
build_pdf
build_web
build_man
build_infolib
check_all
;;
html) resolve_xml; build_html ;;
pdf) resolve_xml; build_html; build_pdf ;;
web) resolve_xml; build_web ;;
man) resolve_xml; build_man ;;
infolib) resolve_xml; build_infolib ;;
check) check_all ;;
verify) resolve_xml; verify_all ;;
install) install_all ;;
*) echo "usage: $0 [all|html|pdf|web|man|infolib|check|verify|install]" >&2; exit 1 ;;
esac