# Maintainer: Max Rees <maxcrees@me.com>
pkgname=mandoc
pkgver=1.14.5
pkgrel=0
pkgdesc="Manual page database, compiler, and viewer"
url="https://mandoc.bsd.lv/"
arch="all"
license="ISC AND BSD-2-Clause AND BSD-3-Clause"
replaces="man-db"
depends="less"
makedepends="zlib-dev"
checkdepends="perl"
subpackages="$pkgname-doc $pkgname-cgi"
install="$pkgname.post-deinstall"
triggers="$pkgname.trigger=/usr/share/man"
source="https://mandoc.bsd.lv/snapshots/mandoc-$pkgver.tar.gz
	configure.patch
	empty-tbl-null-deref.patch
	tbl-colwidth-oob.patch
	less.patch
	doc.patch
	cgi-adelie.patch
	cgi-gz.patch
	cgi-search.patch
	"

prepare() {
	default_prepare
	cat >"configure.local" <<-EOF
		PREFIX=/usr
		MANDIR=/usr/share/man
		WWWPREFIX=/var/lib/mandoc-cgi
		# remove /usr/X11R6 from the following.
		MANPATH_BASE=/usr/share/man
		MANPATH_DEFAULT=/usr/share/man:/usr/local/share/man
		# groff also provides a soelim.
		BINM_SOELIM=mandoc-soelim

		CFLAGS="$CFLAGS"
		LDFLAGS="$LDFLAGS"
		LN="ln -sf"
		OSNAME="Adelie Linux"
		UTF8_LOCALE=C.UTF-8

		BUILD_CATMAN=1
		BUILD_CGI=1
	EOF

	cat >"cgi.h" <<-EOF
		#define SCRIPT_NAME ""
		#define MAN_DIR "/var/lib/mandoc-cgi"
		#define CSS_DIR ""
		#define CUSTOMIZE_TITLE "Adelie Linux manual pages"
	EOF
}

build() {
	./configure
	make
}

check() {
	make regress
}

package() {
	make DESTDIR="$pkgdir" install
	install -Dm644 -t "$pkgdir"/usr/share/man/man8 man.cgi.8

	mkdir -p "$pkgdir"/etc
	cat >"$pkgdir"/etc/man.conf <<-EOF
		# See man.conf(5).
		#manpath /usr/share/man
		#manpath /usr/local/share/man
	EOF
}

cgi() {
	pkgdesc="$pkgdesc (web interface)"
	depends="$pkgname=$pkgver-r$pkgrel"
	mkdir -p "$subpkgdir"/var/lib
	mv "$pkgdir"/var/lib/mandoc-cgi \
		"$subpkgdir"/var/lib
}

sha512sums="848f290847f3095757f25647e6d73eb7044018b145bf6f9dc8b5db164b1421911efdc5b3b9022ec3a2c5af9687a84d5acedee810f2bd2f7caaa9242dadb2cc52  mandoc-1.14.5.tar.gz
074dc73b45c71b5c178750db6c7a4a45e97d97b5388fa5d6028faa90600358dd2b954781280ca41d17da3c577ad5a1a7a13ddc4dbea72ac5e86f1652e03ecde7  configure.patch
c4055371a92be958f0d385c83b4788dc4c18bc990b1f780137309c83e174b8d3e65cae077aba3c5286a97fcee1514d61f6b7507ca25bd5155b8d577a7e8acd3a  empty-tbl-null-deref.patch
9a8953f17e2db75bf3667e1d51c7db21ccc5e3042c59035ed6178094963eac919b7367f96708c9bcc81450ffc13842d12c23edd9bca3330be730149625db88e7  tbl-colwidth-oob.patch
4b17b144b53ee30f9aae87c7824597151e16e6f5ad795e7b03e701f2d13ca47a62c696cb837689c75d5f34d3c190255c8f0583bc91a0f83f61afde7563a6fb2b  less.patch
f35268ed507d5c6431f8cafe5a311d1ffe990083c6e1d22f5db479e19745d486cd45a699b64c4fb6757d1c0ab588c3e8d0778bfd75e98e751d1a786e89e8b925  doc.patch
0dfe9e39570a2b6764303708b98f29b2b57114e124a0f37e9c77d4d33f25d474a79d271df46babd700e4cc93e3385a02356f19caa44ebe4e9b056fa4be96b95b  cgi-adelie.patch
957bbaef888fb2ea15f9b795b15952332e65aed70d2e70f3142305a4fb9313ade455eae6dfc923af3ef008834e771b1a85d71389b86cd0ff9a1c31e84fef861c  cgi-gz.patch
37be119308814b060ed9d5517b54eb02b210d677ffb085385929b6aa0e8e8bef2c6c1ab2055713ed8c72f2bc85593bf8744d2d0f163dd1aff65ce439c474b640  cgi-search.patch"