# Copyright 1999-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

# TODO: USB Blaster Support, see
# https://wiki.archlinux.org/index.php/Altera_Design_Software#USB-Blaster_Download_Cable_Driver
# For What Needs Doing
# XXX: in gentoo you might just need to be in the usb group (depending on what devices are used - /dev/bus/usb* is in the usb group, /dev/usb/hid* aren't)
# Double Check with a USB Blaster

EAPI=5

inherit eutils fdo-mime

ORIG_V="15.1.0.185"
ORIG_TAR="Quartus-lite-${ORIG_V}-linux.tar"
PATCH_FN="QuartusSetup-${PV}-linux.run"

QDIR="/opt/quartus/quartus-lite-${PV}"

DESCRIPTION="Full-featured EDA product for altera devices"
HOMEPAGE="https://www.altera.com/products/design-software/fpga-design/quartus-prime/overview.html"
SRC_URI="
	${ORIG_TAR}
	${PATCH_FN}
"
# variable to store the download URL
DOWNLOADPAGE="http://dl.altera.com/?edition=lite"

LICENSE="Quartus-prime-megacore"
SLOT="0"
KEYWORDS="~amd64 ~x86"

RDEPEND="media-libs/libpng:1.2"

RESTRICT="fetch bindist"

S=${WORKDIR}

pkg_nofetch() {
	elog "Due to licensing restrictions, you need to download the distfiles manually."
	elog "Please navigate to ${DOWNLOADPAGE}"
	elog "Sign in with your Altera account (you need to register)"
	elog "Download:"
	elog "\t${ORIG_TAR}"
	elog "\t${PATCH_FN} (you also need to chmod +x it)"
	elog "Place them into ${DISTDIR} And restart the installation."
}

pkg_pretend() {
	einfo "The checksum and unpack will take quite some time to run"
}

src_install() {
	einfo "The Quartus install runs in unattended mode"
	einfo "you wont see much and it will take a long time"

	#sed -e "s#@QUARTUS_DIR@#${QDIR}#" "${FILESDIR}/envd/90quartus" > "${S}/90quartus"
	#doenvd "${S}/90quartus"

	ln -s "${DISTDIR}/${PATCH_FN}" ./components/

	./setup.sh --mode unattended --unattendedmodeui none \
		--installdir "${D}${QDIR}"

	# No need to keep the update rollback dir around, or the uninstallers really (but the uninstallers themselves aren't using that much space)
	rm -r "${D}${QDIR}/uninstall/quartus-${PV}-rollbackdir/"

	make_desktop_entry "${QDIR}/quartus/bin/quartus" \
		"Quartus lite ${PV}" "${QDIR}/quartus/adm/quartusii.png" \
		"Engineering;Electronics;"
}

pkg_postinst() {
	fdo-mime_desktop_database_update
	fdo-mime_mime_database_update
	elog "To launch Quartus, run ${QDIR}/quartus/bin/quartus"
}

pkg_postrm() {
	fdo-mime_desktop_database_update
	fdo-mime_mime_database_update
}
