# Copyright 1999-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

# ebuild generated by hackport 0.8.2.0.9999

# We should strive to keep the version as four components on Gentoo,
# e.g. 2.6.2.0 instead of 2.6.2
# This will keep the version of sci-mathematics/agda-stdlib stable.
# Otherwise, portage may detect a version bump as a version downgrade.
CABAL_PN="Agda"
CABAL_PV="2.6.3" # the version of Agda as it appears on Hackage
CABAL_P="${CABAL_PN}-${CABAL_PV}"

CABAL_FEATURES="lib profile haddock hoogle hscolour"
inherit ghc-package haskell-cabal elisp-common

## shared with sci-mathematics/agda-stdlib
# upstream does not maintain version ordering:
#  https://github.com/agda/agda-stdlib/releases
# 0.11 -> 2.5.0.20160213 -> 2.5.0.20160412 -> 0.12
# As Agda-stdlib is tied to Agda version we encode
# both versions in gentoo version.
##
MY_UPSTREAM_AGDA_STDLIB_V="1.7.2"
MY_GENTOO_AGDA_STDLIB_V="${PV}.${MY_UPSTREAM_AGDA_STDLIB_V}"
S="${WORKDIR}/${CABAL_P}"

DESCRIPTION="A dependently typed functional programming language and proof assistant"
HOMEPAGE="https://wiki.portal.chalmers.se/agda/"

LICENSE="MIT"
SLOT="0/${PV}"
KEYWORDS="~amd64"
IUSE="cpphs debug emacs enable-cluster-counting optimise-heavily +stdlib"

RDEPEND="
	>=dev-haskell/aeson-1.1.2.0:=[profile?] <dev-haskell/aeson-2.2:=[profile?]
	>=dev-haskell/async-2.2:=[profile?] <dev-haskell/async-2.3:=[profile?]
	>=dev-haskell/blaze-html-0.8:=[profile?] <dev-haskell/blaze-html-0.10:=[profile?]
	>=dev-haskell/boxes-0.1.3:=[profile?] <dev-haskell/boxes-0.2:=[profile?]
	>=dev-haskell/case-insensitive-1.2.0.4:=[profile?] <dev-haskell/case-insensitive-1.3:=[profile?]
	>=dev-haskell/data-hash-0.2.0.0:=[profile?] <dev-haskell/data-hash-0.3:=[profile?]
	>=dev-haskell/dlist-0.8:=[profile?] <dev-haskell/dlist-1.1:=[profile?]
	>=dev-haskell/edit-distance-0.2.1.2:=[profile?] <dev-haskell/edit-distance-0.3:=[profile?]
	>=dev-haskell/equivalence-0.3.2:=[profile?] <dev-haskell/equivalence-0.5:=[profile?]
	>=dev-haskell/gitrev-1.3.1:=[profile?] <dev-haskell/gitrev-2.0:=[profile?]
	>=dev-haskell/hashable-1.2.1.0:=[profile?] <dev-haskell/hashable-1.5:=[profile?]
	>=dev-haskell/haskeline-0.7.2.3:=[profile?] <dev-haskell/haskeline-0.9:=[profile?]
	>=dev-haskell/monad-control-1.0.1.0:=[profile?] <dev-haskell/monad-control-1.1:=[profile?]
	>=dev-haskell/murmur-hash-0.1:=[profile?] <dev-haskell/murmur-hash-0.2:=[profile?]
	>=dev-haskell/parallel-3.2.2.0:=[profile?] <dev-haskell/parallel-3.3:=[profile?]
	>=dev-haskell/regex-tdfa-1.3.1.0:=[profile?] <dev-haskell/regex-tdfa-1.4:=[profile?]
	>=dev-haskell/split-0.2.0.0:=[profile?] <dev-haskell/split-0.2.4:=[profile?]
	>=dev-haskell/stmonadtrans-0.4.3:=[profile?] <dev-haskell/stmonadtrans-0.5:=[profile?]
	>=dev-haskell/strict-0.4.0.1:=[profile?] <dev-haskell/strict-0.5:=[profile?]
	>=dev-haskell/text-1.2.3.1:=[profile?] <dev-haskell/text-2.1:=[profile?]
	>=dev-haskell/time-compat-1.9.2:=[profile?] <dev-haskell/time-compat-1.10:=[profile?]
	>=dev-haskell/unordered-containers-0.2.5.0:=[profile?] <dev-haskell/unordered-containers-0.3:=[profile?]
	>=dev-haskell/uri-encode-1.5.0.4:=[profile?] <dev-haskell/uri-encode-1.6:=[profile?]
	>=dev-haskell/vector-0.12:=[profile?] <dev-haskell/vector-0.14:=[profile?]
	>=dev-haskell/vector-hashtables-0.1:=[profile?] <dev-haskell/vector-hashtables-0.2:=[profile?]
	>=dev-haskell/zlib-0.6:=[profile?] <dev-haskell/zlib-0.7:=[profile?]
	>=dev-lang/ghc-8.10.1:=
	enable-cluster-counting? (
		>=dev-haskell/text-icu-0.7.1.0:=[profile?]
	)
"

RDEPEND+="
	emacs? (
		>=app-editors/emacs-23.1:*
		app-emacs/haskell-mode
	)
"
PDEPEND="
	stdlib? (
		~sci-mathematics/agda-stdlib-${MY_GENTOO_AGDA_STDLIB_V}
	)
"

DEPEND="${RDEPEND}
	dev-haskell/alex
	>=dev-haskell/cabal-3.2.0.0 <dev-haskell/cabal-3.9
	dev-haskell/happy
	cpphs? (
		dev-haskell/cpphs
	)
"
BDEPEND="
	emacs? (
		>=app-editors/emacs-23.1:*
	)
"

SITEFILE="50${PN}2-gentoo.el"

src_prepare() {
	haskell-cabal_src_prepare

	# This currently breaks the .cabal file. Using patch instead
	#if ! use emacs; then
	#	sed -e '/.*emacs-mode.*$/d' \
	#		-i "${S}/${CABAL_PN}.cabal" \
	#		|| die "Could not remove agda-mode from ${CABAL_PN}.cabal"
	#fi
	use emacs || eapply "${FILESDIR}/${PN}-2.6.2.1-remove-emacs.patch"
}

src_configure() {
	haskell-cabal_src_configure \
		$(cabal_flag cpphs cpphs) \
		$(cabal_flag debug debug) \
		$(cabal_flag enable-cluster-counting enable-cluster-counting) \
		$(cabal_flag optimise-heavily optimise-heavily)
}

src_compile() {
	if use emacs; then
		BYTECOMPFLAGS="-L ./src/data/emacs-mode"
		elisp-compile src/data/emacs-mode/*.el \
			|| die "Failed to compile emacs mode"
	fi
	export LD_LIBRARY_PATH="${S}/dist/build${LD_LIBRARY_PATH+:}${LD_LIBRARY_PATH}"
	haskell-cabal_src_compile
}

src_install() {
	local add="${ED}"/usr/share/"${P}/ghc-$(ghc-version)"

	haskell-cabal_src_install

	export LD_LIBRARY_PATH="${S}/dist/build${LD_LIBRARY_PATH+:}${LD_LIBRARY_PATH}"
	# compile Agda.Primitive and Agda.Builtin modules, emulate Setup.hs postinst phase
	Agda_datadir="${add}" \
		"${ED}"/usr/bin/agda "${add}"/lib/prim/Agda/Primitive.agda \
		|| die "Failed to build 'Primitive.agdai'"
	for file in "${add}"/lib/prim/Agda/Builtin/*.agda; do
		Agda_datadir="${add}" \
			"${ED}"/usr/bin/agda "${file}" \
			|| die "Failed to build '${file}'"
	done

	if use emacs; then
		elisp-install ${PN} src/data/emacs-mode/*.el \
			|| die "Failed to install emacs mode"
		elisp-site-file-install "${FILESDIR}/${SITEFILE}" \
			|| die "Failed to install elisp site file"
	fi
}

pkg_postinst() {
	haskell-cabal_pkg_postinst
	if use emacs; then
		elisp-site-regen
	fi
}

pkg_postrm() {
	haskell-cabal_pkg_postrm
	if use emacs; then
		elisp-site-regen
	fi
}