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

EAPI="6"

inherit desktop xdg-utils

DESCRIPTION="Cisco Packet Tracer"
HOMEPAGE="https://www.netacad.com"
SRC_URI="amd64? ( ${P}x64.tar )
		x86? ( ${P}x32.tar )"

RESTRICT="fetch mirror strip"
LICENSE="Cisco_EULA"
SLOT="0"
KEYWORDS="-* ~amd64 ~x86"
DEPEND="dev-qt/qtwebkit:5[multimedia]"

RDEPEND="${DEPEND}"
S="${WORKDIR}"

src_unpack() {
	# Tarball has bad permissions, this is a work around
	tar --delay-directory-restore -xf "${DISTDIR}/${A}" || die
}

pkg_nofetch () {
	ewarn "Packet Tracer @ Cisco Networking Academy (www.netacad.net)"
	ewarn "Get it for free by enrolling in PT traning course and"
	einfo "reviewing course materials."
	einfo ""
	einfo ""
	einfo "Download \"Packet Tracer 7.1.1 for Linux 32/64 bit.tar\" and"
	einfo "rename it to \"${P}x32/x64.tar\" in DISTDIR."
	einfo ""
	einfo ""
}

src_install () {
	local PKT_HOME="opt/pt"
	# Setup launcher
	sed "s|III|/${PKT_HOME}\nPT7HOME=/${PKT_HOME}|" tpl.packettracer > packettracer
	sed "s|III|/${PKT_HOME}\nPT7HOME=/${PKT_HOME}|" tpl.linguist > linguist
	# Go icons!
	doicon "art/"{pka,pkt,pkz}.png
	newicon "art/app.png" "pktapp.png"
	make_desktop_entry "/opt/pt/packettracer %F" "Cisco Packet Tracer ${PV}" "pktapp" "Education;Emulator;System" "MimeType=application/x-pkz;application/x-pkt;application/x-pka;"
	make_desktop_entry "/opt/pt/linguist" "Cisco Linguist ${PV}" "pktapp" "Education;Emulator;System"
	insinto /usr/share/mime/packages
	doins bin/Cisco-pkz.xml
	doins bin/Cisco-pkt.xml
	doins bin/Cisco-pka.xml
	# Remove bad static libs
	rm lib/* || die
	# Cleanup garbage
	rm tpl.* set_* install bin/*.xml || die
	# Install folder
	insinto /${PKT_HOME}
	doins -r .
	# Setup binaries
	exeinto /${PKT_HOME}
	doexe packettracer
	doexe linguist
	exeinto /${PKT_HOME}/bin
	doexe bin/PacketTracer7
	doexe bin/linguist
	doexe bin/meta
	exeinto /${PKT_HOME}/extensions
	doexe extensions/meta
	exeinto /${PKT_HOME}/extensions/upnp
	doexe extensions/upnp/upnp
	exeinto /${PKT_HOME}/bin/Linux
	doexe bin/Linux/zip
	doexe bin/Linux/unzip
	# Add binary to path
	dosym "/${PKT_HOME}"/packettracer /usr/bin/packettracer || die
	dosym "/${PKT_HOME}"/linguist /usr/bin/ptlinguist || die
}

pkg_postinst(){

	einfo "There is a bug in this version where Packet Tracer won't show"
	einfo "selectable files in the browse dialogue but it still works."
	einfo ""
	einfo ""
	xdg_mimeinfo_database_update
	xdg_desktop_database_update
}