#Packager: George Vlahavas <gapan~at~salixos~dot~org>

pkgname=pandoc
pkgver=2.16.2
pkgrel=1gv
export _ghcup_arch=$(echo $arch | sed "s/i[456]86/i386/")
source=(
	"https://github.com/jgm/pandoc/archive/refs/tags/${pkgver}.tar.gz"
	"https://downloads.haskell.org/~ghcup/${_ghcup_arch}-linux-ghcup"
)
url=https://pandoc.org

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"pandoc (markup convertion tool)"
"Pandoc is a command-line tool for converting from one markup format to"
"another. It can read markdown and (subsets of) reStructuredText, HTML,"
"and LaTeX, and it can write plain text, markdown, reStructuredText,"
"HTML, LaTeX, ConTeXt, PDF, RTF, DocBook XML, OpenDocument XML, ODT,"
"GNU Texinfo, MediaWiki markup, groff man pages, EPUB ebooks, and S5"
"and Slidy HTML slide shows. PDF output (via LaTeX) is also supported"
"with the included markdown2pdf wrapper script."
)


build() {
	cd $startdir/src
	
	# for when /tmp is not big enough
	mkdir tmp
	export TMPDIR=`pwd`/tmp
	
	# hijack $HOME to not pollute it with ~/.ghcup and ~/.cabal dirs
	export HOME=`pwd`

	# so.5 needed by ghcup binaries. We're at so.6 in 15.0
	ln -s /usr/lib${LIBDIRSUFFIX}/libtinfo.so libtinfo.so.5
	export LD_LIBRARY_PATH=`pwd`

	# install ghc and cabal
	export GHCUP_INSTALL_BASE_PREFIX=$HOME/ghcup
	chmod +x ${_ghcup_arch}-linux-ghcup
	./${_ghcup_arch}-linux-ghcup install ghc || exit 1
	./${_ghcup_arch}-linux-ghcup install cabal || exit 1
	export PATH=$PATH:$HOME/ghcup/.ghcup/bin

	# not sure why this is not created by default
	(
	 cd $HOME/ghcup/.ghcup/bin
	 ln -s `ls ghc-* | head -n1` ghc
	)
	cabal update || exit 1
	
	
	# build pandoc!
	cd $startdir/src/$pkgname-$pkgver
	cabal v2-configure . --ghc-options -fdiagnostics-color=always -j1 +RTS -A8m -RTS --flags '+embed_data_files +trypandoc' --disable-debug-info
	# OK, this is so weird. It fails at random times, so we're going to
	# continue from where it left, until the build finishes
	while true; do
		cabal v2-build . -O0 --disable-debug-info && break
	done
	# install pandoc
	mkdir -p $startdir/pkg/usr/bin
	cp ./dist-newstyle/build/${_ghcup_arch}-linux/ghc-*/pandoc-2.16.2/x/pandoc/noopt/build/pandoc/pandoc $startdir/pkg/usr/bin/pandoc || exit 1
	mkdir -p $startdir/pkg/usr/man/man1
	cp man/pandoc.1 $startdir/pkg/usr/man/man1/
	mkdir -p $startdir/pkg/usr/doc/pandoc-$pkgver
	cp -r *.md COPYRIGHT MANUAL.txt BUGS doc $startdir/pkg/usr/doc/pandoc-$pkgver/
} 
