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

inherit eutils linux-mod cvs

DESCRIPTION="An entirely re-designed and re-implemented Unionfs."
HOMEPAGE="http://aufs.sourceforge.net/"
ECVS_SERVER="aufs.cvs.sourceforge.net:/cvsroot/aufs"
ECVS_MODULE="aufs"
ECVS_CO_OPTS=""
ECVS_UP_OPTS="-dP"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="ksize nfs"

MODULE_NAMES="aufs(addon/fs/${PN}:)"
BUILD_TARGETS="all"

S=${WORKDIR}/${ECVS_MODULE}

pkg_setup() {
	get_version
	BUILD_PARAMS="KDIR=${KV_DIR} -f local.mk"
	# kernel version check
	if kernel_is lt 2 6 16 ; then
		eerror "${PN} is being developed and tested on linux-2.6.16 and later."
		eerror "Make sure you have a proper kernel version!"
		die "Wrong kernel version"
	fi

	linux-mod_pkg_setup
}

src_install() {
	exeinto /sbin
	exeopts -m0500
	doexe mount.aufs umount.aufs auplink aulchown
	doman aufs.5
	linux-mod_src_install
}

pkg_postinst() {
	elog "To be able to use aufs, you have to load the kernel module by typing:"
	elog "modprobe aufs"
	elog "For further information refer to the aufs man page"

	# Tell the user to recompile his kernel
	if [[ ${APPLY_KSIZE_PATCH} == "y" ]] || [[ ${APPLY_LHASH_PATCH} == "y" ]] ; then
		echo
		ewarn "Remember to re-compile your kernel to make the patch(es) work"
		ewarn
	fi

	linux-mod_pkg_postinst
}

pkg_prerm() {
	built_with_use -o =${CATEGORY}/${PF} ksize nfs && DO_CHECK="y"
}

pkg_postrm() {
	# Tell the user that his kernel has already been patched
	if [[ DO_CHECK == "y" ]] ; then
		check_patch
		if [[ ${APPLY_KSIZE_PATCH} == "n" ]] || [[ ${APPLY_LHASH_PATCH} == "n" ]] ; then
			ewarn "Your kernel has been patched previously by this ebuild."
			ewarn "You can undo the patches by executing the following:"
			echo
			ewarn "cd ${KV_DIR}; make mrproper, re-emerge and re-compile your kernel - ${KV_FULL}"
		fi
	fi

	linux-mod_pkg_postrm
}

src_compile() {
	ARCH=$(tc-arch-kernel)
	emake -j1 ${BUILD_PARAMS} ${BUILD_TARGETS} || die "emake failed"
	ARCH=$(tc-arch)
}