# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI=2

inherit eutils

DESCRIPTION="General-Use FTools for manipulating and inspecting FITS files"
HOMEPAGE="http://heasarc.gsfc.nasa.gov/docs/software/ftools/ftools_menu.html"
SRC_URI="heasoft${PV}src.tar.gz"
LICENSE="UNKOWN"
SLOT="0"
KEYWORDS="~x86"
IUSE=""
RESTRICT="test"

DEPEND="x11-base/xorg-server
		>=dev-lang/perl-5.6.0"
RDEPEND="${DEPEND}"

S="${WORKDIR}/heasoft-${PV}/BUILD_DIR"
PREFIX="/usr/heasoft-${PV}"

MSG="The HEASOFT General-Use FTools have been installed into
${PREFIX}.

To use the HEASOFT, you need to source the headas init script,
like so
	. \${HEADAS}/headas-init.sh
if you are using a bourne-shell. As a shortcut you can put an
alias in your ~/.bashrc:
	alias heainit=\". \${HEADAS}/headas-init.sh\"

C-shells should source \${HEADAS}/headas-init.csh instead.

The environment vairable \${HEADAS} has been defined for you
globally in /etc/env.d/99headas."

pkg_setup() {
	[[ -d "${PREFIX}" ]] && die "Please unmerge previous installation."
	ewarn "This ebuild works but doesn't do things as god intended..."
}

src_prepare() {
	for subdir in tcl tk; do
		cd "${S}"/../tcltk/${subdir}/unix
		epatch "${FILESDIR}"/installManPage-S-namecollision.patch \
			|| die "epatch failed"
	done
}

src_configure() {
	econf --prefix="${PREFIX}" || die "econf failed"
}

src_install() {
	# The heasoft install scripts don't allow for $DESTDIR. Thus we
	# must let headas install itself. This is very, very bad.
	addwrite "${PREFIX}"
	einfo "Installing into ${PREFIX} directly..."
	# Hell knows why we need -j1 here. But we sure need it.
	emake -j1 install || die "emake install failed"
	einfo "Moving ${PREFIX} to image directory..."
	mkdir "${D}"/usr
	mv "${PREFIX}" "${D}"/usr

	echo "${MSG}" > WTF
	dodoc WTF

	echo "HEADAS=\"`grep "HD_TOP_EXEC_PFX.*= " Makefile | sed "s#^HD_TOP_EXEC_PFX.*= ##"`\"" > 99headas
	doenvd 99headas
}

pkg_postinst() {
	elog "${MSG}"
}
