# Copyright 1999-2003 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2

inherit eutils versionator linux-mod

SRC_URI="XFiDrv_Linux_US-${PV}.tar.gz"
DESCRIPTION="Kernel drivers for the XI-FI Sound card series"
HOMEPAGE="http://connect.creativelabs.com/linux/default.aspx"
LICENSE="gpl-2" #TODO it looks like we need a new licence for creative setting
				#up Within /usr/portage/licences/
SLOT="0"
KEYWORDS="x86 amd64 ~ppc ~sparc"
IUSE=""
DEPEND=""
RDEPEND=""
RESTRICT="fetch"
DOWNLOAD_URL="http://asia.creative.com/support/downloads/download.asp?searchString=XFiDrv_Linux"
S="${WORKDIR}/drivers"

# TODO we need a check to make sure the main alsa driver has ether been compiled
# in as part of the main kernel, or via the alsa-driver ebuild

# TODO fix module depends in the source
# TODO look at a new udev rule


pkg_setup() {
	CONFIG_CHECK="SLAB"
	linux-mod_pkg_setup
	BUILD_PARAMS="KDIR=${KV_DIR}"
}

pkg_nofetch() {
	einfo "Please download XFiDrv_Linux_US-${PV}.tar.gz from "
	einfo "${DOWNLOAD_URL}"
	einfo "and place it in ${DISTDIR} named as ${SRC_URI}"
}

src_unpack() {
	# unpack the main source file
	unpack ${A}
	cd ${WORKDIR}
	# Unpack the secondary source file inside the main source file
	mv "XFiDrv_Linux_US-${PV}" "XFiDrv_Linux_US-${PV}.src"
	einfo "unpacking XFiDrv_Linux_US-${PV}.tar.bz2"
	tar -xjf "XFiDrv_Linux_US-${PV}.src/XFiDrv_Linux_US-${PV}.tar.bz2"
	# Fix the permissions
	#chmod -R 755 XFiDrv_Linux_US-${PV} && find XFiDrv_Linux_US-${PV}/ -exec touch -c {} \;
	# Patch the sources
	cd ${S}
		
	# Patches for 2.6.23 / 2.6.26
	epatch ${FILESDIR}/XFiDrv_Kernel-1.patch
	# Fix install path
	epatch ${FILESDIR}/XFiDrv_Gentoo-1.patch
}

src_compile() {
	local myconf
	cd ${S}
	myconf="$myconf --prefix=/usr"
	./configure || die

	# CTLOG makes the make process visible, makes things easier to debug
	# KBUILD_NOPEDANTIC gets rid of the EXTRA_CFLAGS error
	make CTLOG=0 KBUILD_NOPEDANTIC=1
}

src_install() {
	cd ${S}
	make CTLOG=0 DESTDIR=${D} installonly || die "Install Problem"
	dodoc ChangeLog README
	chmod 644 ${D}/lib/modules/${KV_FULL}/kernel/drivers/ssound/*

	# Blacklist the modules for udev or hotplug to prevent auto loading
	# so we can load manually
	insinto /etc/modprobe.d
	doins ${FILESDIR}/ctalsa_blacklist

	# Crappy init script as a temporary measure
	exeinto /etc/init.d
	doexe ${FILESDIR}/ctsound

	# Trigger the update of modprobe.conf
	#dodir /etc/modules.d
	#echo "options ctalsa" >${D}etc/modules.d/ctsound
}

pkg_postinst() {
	linux-mod_pkg_postinst
	# There seems to be a bug within linux-mod eclass at the time of writing
	# also update-modules only normally gets called when there's something
	# within /etc/modules.d
	# snce we need to make sure this is called for the blacklist
	update-modules
	ewarn ">>> Please be aware This is a Beta Driver, and may be prone to crashes"
	ewarn ">>> the module depends are a bit screwed up at the moment so each of"
	ewarn ">>> the x-fi modules needs to be loaded in the correct order"
	ewarn ">>> otherwise bad things will happen"
	ewarn ""
	ewarn "/etc/modprobe.d/ctalsa_blacklist should have been created to prevent"
	ewarn "module autoloading"
	ewarn "see the /etc/init.d/ctsound script as a temporary measure for startup"
	ewarn "this currently doesn't work with my system"
	ewarn "but is as close as I could get"
}