#!/bin/bash
# Copyright 2019 makepost <makepost@firemail.cc>

[ -f testing.sh ] && . testing.sh

#testing "name" "command" "result" "infile" "stdin"

mkdir -p banana/man{1,3,5}

# Can't/shouldn't write to /usr/share/man so use -M to change path
MAN="man -M banana"
echo one | bzip2 >banana/man1/numbers.1.bz2
echo three | gzip >banana/man3/numbers.3.gz
echo five > banana/man5/numbers.5

testing "man" "$MAN numbers" " one\n\n" "" ""
testing "man.section" "$MAN numbers.3" " three\n\n" "" ""
testing "section man" "$MAN 5 numbers" " five\n\n" "" ""
testing "/" "$MAN /" "" "" "" # Regression guard for !suf in zopen

cat >banana/man1/toybox.1 <<EOF
.TP
.PD 0
.B \\-F
.TP
.PD
.B \\-\\^\\-foo\\-bar
Does something.
.TP
.PD 0
.B \\-\\-no\\-alias
Has no alias.
EOF

testing "gawk alias" "$MAN toybox" " -F\n --foo-bar Does something.\n\n --no-alias Has no alias.\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
\\fBsh\\fR-compatible
\\fIKorn\\fP
EOF
testing "bash bold,ita" "$MAN toybox" " sh-compatible Korn\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
quoted \\(aqstring\\(aq
\\(dqserver\\(dq
Don\\(cq\\&t
EOF
testing "bash,rsync quote" "$MAN toybox" " quoted 'string' \"server\" Don't\n\n" "" ""

echo "\\*(lq\\-\\^\\-\\*(rq" >banana/man1/toybox.1
testing "gawk quote" "$MAN toybox" " \"--\"\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
.IP \\(bu
[\\fB|\\fP\\(bv\\fB|&\\fP]
EOF
testing "bash symbol" "$MAN toybox" " * [|||&]\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
\\&\\fBfflush(...)\\fR
\\f(CW$ ssh ...\\fP
EOF
testing "gawk,rsync fancy" "$MAN toybox" " fflush(...) $ ssh ...\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
"\\eC\\-x\\eC\\-r": re\\-read
must be \\(>= 1
EOF
testing "bash escape" "$MAN toybox" " \"\C-x\C-r\": re-read must be >= 1\n\n" "" ""

echo "\\*(AK language.  The \\*(PX standard" >banana/man1/toybox.1
testing "gawk var" "$MAN toybox" " #AK language.  The #PX standard\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
If set to
.BR On ,
(...)
EOF
testing "bash boldpunct" "$MAN toybox" " If set to On, (...)\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
.IP \\fB\$endif\\fP
(...)
.IP \\fB\$else\\fP
(...)
.IP \\fB\$include\\fP
EOF
testing "bash list" "$MAN toybox" " \$endif (...)\n\n \$else (...)\n\n \$include\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
asynchronously (in the
.IR background ),
it prints
EOF
testing "bash itapunct" "$MAN toybox" " asynchronously (in the background), it prints\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
(...)
.B Bash
is
EOF
testing "bash bold" "$MAN toybox" " (...) Bash is\n\n" "" ""

# TODO: Unquote.
cat >banana/man1/toybox.1 <<EOF
.PD 0
.BI \\-f " program-file"
.TP
EOF
testing "gawk boldita" "$MAN toybox" " -f \" program-file\"\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
same format as
.FN /etc/hosts
(...)
EOF
testing "bash filename" "$MAN toybox" " same format as /etc/hosts (...)\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
The
.I Internal Field Separator
that is used
EOF
testing "bash ita" "$MAN toybox" " The Internal Field Separator that is used\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
.if n Copyright (C) 2019
.if t Copyright \\(co 2019
EOF
testing "bash nroff" "$MAN toybox" " Copyright (C) 2019\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
.EX
#include <stdio.h>
#include <stdlib.h>
.EE
EOF
testing "stat example" "$MAN toybox" "#include <stdio.h>\n#include <stdlib.h>\n\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
(...)
.PP
(...)
EOF
testing "bash paragraph" "$MAN toybox" " (...)\n\n (...)\n\n" "" ""

# TODO: Find some command I saw that has a different see below.
cat >banana/man1/toybox.1 <<EOF
(see
.SM
.B INVOCATION
below)
EOF
testing "bash small" "$MAN toybox" " (see INVOCATION below)\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
(...)
.SH "SHELL GRAMMAR"
.SS Simple Commands
.PP
(...)
EOF
testing "bash section" "$MAN toybox" " (...)\n\n\"SHELL GRAMMAR\"\n\nSimple Commands\n\n (...)\n\n" "" ""

echo ".so man1/last.1" >banana/man1/toybox.1
testing "lastb" "$MAN toybox" "See last.1\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
.TH TOYBOX 1 "Apr 13 2019" "Project Organization" "Document Name"
.PP
.TH "TOYBOX" "1" "04/13/2019" "Toybox 0\\&.8\\&.0" "Toybox Manual"
EOF
testing "gawk,git head" "$MAN toybox" "TOYBOX 1  Apr 13 2019   Project Organization   Document Name \n\n TOYBOX   1   04/13/2019   Toybox 0.8.0   Toybox Manual \n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
(...)
.PP
.PD 0
.TP 10
.B \\-c
If the
.B \\-c
option is present
EOF
testing "bash table" "$MAN toybox" " (...)\n\n -c If the -c option is present\n\n" "" ""

cat >banana/man1/toybox.1 <<EOF
.de FN
.sp
.RE
'\" t
EOF
testing "bash,git garbage" "$MAN toybox" "" "" ""

# "bash,git garbage" from above has no content, -k skips it.
bzip2 >banana/man1/numbers.1.bz2 <<EOF
.TH NUMBERS 1 "2019 Apr 28" "Toybox Manual"
.SH NAME
man \- test -k
.SH ANOTHER SECTION
.so noop.1
Skip this text.
EOF
echo "No dash." | gzip >banana/man3/numbers.3.gz
echo .so man1/numbers.1 >banana/man5/numbers.5
testing "-k ." "$MAN -k ." "numbers.1.bz2 - test -k\nnumbers.3.gz - No dash.\nnumbers.5 - See numbers.1\n" "" ""
testing "-k -k" "$MAN -k -k" "numbers.1.bz2 - test -k\n" "" ""
testing "-k d.*h" "$MAN -k 'd.*h'" "numbers.3.gz - No dash.\n" "" ""
testing "-k ers.1" "$MAN -k ers.1" "numbers.1.bz2 - test -k\nnumbers.5 - See numbers.1\n" "" ""

mkdir -p walrus/man1
echo Local development override. | bzip2 >walrus/man1/numbers.1.bz2
testing "-M /dir:/dir0" "man -M walrus:banana numbers" " Local development override.\n\n" "" ""
testing "-M /dir:/dir0 -k ." "man -M walrus:banana -k ." "numbers.1.bz2 - Local development override.\nnumbers.1.bz2 - test -k\nnumbers.3.gz - No dash.\nnumbers.5 - See numbers.1\n" "" ""
testing "-M '' x" "man -M '' numbers 2>&1" "man: no numbers\n" "" ""
testing "-M '' 1 x" "man -M '' 1 numbers 2>&1" "man: section 1 no numbers\n" "" ""
testing "-M '' -k ." "man -M '' -k ." "" "" ""

# TODO: emerge section header newline
# TODO: fdm,man-pages man1p/, .nf, rare tags

rm -rf banana walrus
