# Contributor: ScrumpyJack <scrumpyjack@st.ilet.to>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=w3m
pkgver=0.5.3_git20250505
_gitrev=a80300d5e7fba3e2f3b63b220b8fa431b9629741
pkgrel=0
pkgdesc="text-based web & gopher browser, as well as pager"
url="https://sr.ht/~rkta/w3m/"
arch="all"
license="MIT"
makedepends="
	gc-dev
	imlib2-dev
	linux-headers
	ncurses-dev
	openssl-dev
	"
subpackages="
	$pkgname-doc
	$pkgname-perl::noarch
	$pkgname-image
	"
source="$pkgname-$_gitrev.tar.gz::https://git.sr.ht/~rkta/w3m/archive/$_gitrev.tar.gz"
builddir="$srcdir/$pkgname-$_gitrev"

# secfixes:
#   0.5.3_git20241203-r0:
#     - CVE-2023-38252
#     - CVE-2023-38253
#     - CVE-2022-38223
#     - CVE-2018-6196
#     - CVE-2018-6197
#     - CVE-2018-6198

build() {
	PERL=/usr/bin/perl ./configure \
		--prefix=/usr \
		--libexecdir=/usr/lib \
		--enable-image=fb \
		--with-ssl \
		--disable-nntp \
		--disable-dict \
		--disable-help-cgi \
		--with-imagelib=imlib2 \
		--with-termlib=ncurses \
		--with-editor="/usr/bin/vi" \
		--with-browser="/usr/bin/xdg-open" \
		--disable-w3mmailer
	make
}

check() {
	./w3m -version
}

package() {
	make DESTDIR="$pkgdir" install
	# when ./configure has --disable-help-cgi passed, the (H)elp key
	# is hardcoded to open "w3mhelp-w3m_en.html", instead of the
	# w3mhelp.cgi script. 'make install' has install-scripts as a
	# target, but not install-helpfile, probably ./configure doesn't
	# get tested with --disable-help-cgi very often. anyway, we have
	# to make install-helpfile or else the (H)elp key won't work.
	# additionally, the _en & _ja help files have hyperlinks to each
	# other, so install both, unless we want to patch the help file
	make DESTDIR="$pkgdir" \
		HELP_ALLFILES="w3mhelp-w3m_en.html w3mhelp-w3m_ja.html" \
		install-helpfile

	# the following 3 Perl scripts have dependencies not found in Alpine
	# xface2xpm needs uncompface, w3mdict.cgi needs dictionary client
	# multipart.cgi needs NKF (Network Kanji code conversion Filter)
	rm "$pkgdir"/usr/lib/w3m/xface2xpm
	rm "$pkgdir"/usr/lib/w3m/cgi-bin/w3mdict.cgi
	rm "$pkgdir"/usr/lib/w3m/cgi-bin/multipart.cgi
	# w3mmail.cgi needs cmd:sendmail (which Alpine has pkgs for), but
	# removing it because --disable-w3mmailer causes w3m to use an
	# external program (default: /usr/bin/mail) to handle mailto: links
	rm "$pkgdir"/usr/lib/w3m/cgi-bin/w3mmail.cgi

	install -Dvm644 doc/* -t "$pkgdir"/usr/share/doc/$pkgname/
	install -Dvm644 COPYING -t "$pkgdir"/usr/share/licenses/$pkgname/
}

perl() {
	pkgdesc="$pkgdesc (perl helper scripts)"
	depends="perl cmd:man"			# w3mman needs cmd:man

	amove \
		usr/bin/w3mman \
		usr/lib/w3m/cgi-bin/*.cgi \
		usr/share/w3m/*.pl
}

image() {
	pkgdesc="$pkgdesc (inline image support)"
	install_if="$pkgname=$pkgver-r$pkgrel imlib2"

	amove usr/lib/w3m/w3mimgdisplay
}

sha512sums="
394eb7c648e9da4a211fb5fc9feb8126570948f0163a10d6f234319ceafbc02d9a85b3013cbd438d1b8f351796177e2afcc689bca1cd4e21620f0e1feeafb917  w3m-a80300d5e7fba3e2f3b63b220b8fa431b9629741.tar.gz
"
