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

EAPI="2"

inherit eutils flag-o-matic multilib mount-boot

MY_P="${P/_/~}"

DESCRIPTION="GNU GRUB 2 boot loader"
HOMEPAGE="http://www.gnu.org/software/grub/"
SRC_URI="ftp://alpha.gnu.org/gnu/${PN}/${MY_P}.tar.gz"
LICENSE="GPL-3"
SLOT="2"
KEYWORDS="~amd64 ~x86"
IUSE="amd64 custom-cflags +debug efiemu multilib static truetype usb"

RDEPEND=">=sys-libs/ncurses-5.2-r5
    dev-libs/lzo
	truetype? ( media-libs/freetype:2 >=media-fonts/unifont-5 )
	usb? ( virtual/libusb:0 )"
DEPEND="${RDEPEND}
	>=sys-devel/autogen-5.10
	>=dev-lang/python-2.5.2"

PROVIDE="virtual/bootloader"

export STRIP_MASK="*/grub/*/*.mod"
QA_EXECSTACK="sbin/grub-probe sbin/grub-setup sbin/grub-mkdevicemap bin/grub-script-check bin/grub-fstest"

S="${WORKDIR}/${MY_P}"

src_prepare() {
	epatch \
		"${FILESDIR}/0001-recognize-genkernel-images.patch"
	# don't want to have to run autoconf here but
	# patching configure screws the patch for every version bump
#	sed -i \
#		-e "s|PACKAGE_TARNAME='grub'|PACKAGE_TARNAME='grub2'|" \
#		configure || die "sed failed"
}

src_configure() {
	use custom-cflags || unset CFLAGS CPPFLAGS LDFLAGS
	use static && append-ldflags -static

	local libdir="/$(get_libdir)"
	# on multilib'ed x86_64 put them in the right place
	use amd64 && use multilib && libdir="/lib32"

	local myconf=""

	if use efiemu ; then
		if use amd64 ; then
			myconf="${myconf} --enable-efiemu"
		else
			myconf="${myconf} --disable-efiemu"
			ewarn "efiemu is not supported on this platform. Please open a bug if you know otherwise."
		fi
	else
		myconf="${myconf} --disable-efiemu"
	fi

	if use usb ; then
		if use debug ; then
			myconf="${myconf} --enable-grub-emu-usb"
		else
			myconf="${myconf} --disable-grub-emu-usb"
			ewarn "there is only debug USB support but you have the debug USE flag disabled."
		fi
	else
		myconf="${myconf} --disable-grub-emu-usb"
	fi

	econf \
		--disable-werror \
		--sbindir=/sbin \
		--bindir=/bin \
		--libdir="${libdir}" \
		--program-transform-name="s|grub|grub2|" \
		$(use_enable truetype grub-mkfont) \
		$(use_enable debug mm-debug) \
		$(use_enable debug grub-emu) \
		$(use_enable debug grub-fstest) \
		--enable-grub-pe2elf \
		${myconf}
}

src_install() {
	emake DESTDIR="${D}" install || die "emake install failed"
	dodoc AUTHORS ChangeLog NEWS README THANKS TODO
	cat <<-EOF >> "${D}"/lib*/grub/grub-mkconfig_lib
	GRUB_DISTRIBUTOR="Gentoo"
	EOF
#	local b=""
#	for f in "${D}"/sbin/* "${D}"/bin/* "${D}"/usr/share/man/man1/* "${D}"/usr/share/man/man8/* ; do
#		b="$(basename "${f}")"
#		mv "${f}" "$(dirname "${f}")/${b/grub/grub2}" || die "renaming \"${f}\" failed"
#	done

	mv "${D}"/usr/share/info/grub{,2}.info
}

setup_boot_dir() {
	local boot_dir=$1
	local dir=${boot_dir}/grub

	if [[ ! -e ${dir}/grub.cfg ]] ; then
		einfo "Running: grub-mkconfig -o '${dir}/grub.cfg'"
		grub-mkconfig -o "${dir}/grub.cfg"
	fi

	#local install=grub-install
	#use multislot && install="grub2-install --grub-setup=/bin/true"
	#einfo "Running: ${install} "
	#${install}
}

pkg_postinst() {
	mount-boot_mount_boot_partition

	if use multislot ; then
		elog "You have installed grub2 with USE=multislot, so to coexist"
		elog "with grub1, the grub2 install binary is named grub2-install."
	fi
	setup_boot_dir "${ROOT}"boot

	# needs to be after we call setup_boot_dir
	mount-boot_pkg_postinst
}