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

EAPI="2"

inherit eutils toolchain-funcs multilib linux-mod

DESCRIPTION="OpenCBM is a package to control and use serial devices as used by most Commodore (CBM) 8-bit mach"
HOMEPAGE="http://opencbm.trikaliotis.net/"
SRC_URI="http://www.fbriere.net/debian/dists/unstable/opencbm/src/opencbm_${PV/_/~}.orig.tar.gz
	https://launchpad.net/~mdeslaur/+archive/ppa/+files/opencbm_${PV/_/~}.orig.tar.gz"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~ppc ~sparc ~x86"
IUSE="doc"

DEPEND="virtual/linux-sources
	dev-embedded/cc65
	app-text/linuxdoc-tools"

S="${WORKDIR}"/${PN}-${PV/_/}

BUILD_PARAMS="KDIR=$KERNEL_DIR"
MODULE_NAMES="cbm(misc:${S}/sys/linux:${S}/sys/linux)"
BUILD_TARGETS="all"

src_prepare() {
	# convert linefeed
	edos2unix `find . -name '*' -type f`
	# 6502 sources ar always up to date
	touch `find . -name '*.?65' -type f`
	# add debian patches no demo & doc & no kernel
	EPATCH_SOURCE="${FILESDIR}/${PV}" EPATCH_SUFFIX="diff" \
	EPATCH_FORCE="yes" epatch
	# sed correct path
	sed -e 's,^PREFIX.*,PREFIX = /usr,' \
                -e 's,/man/man1,/share/man/man1,g' \
                -e 's,/info,/share/info,g' -i ${S}/LINUX/config.make
	# copy module makefile that don't intefer with gentoo kernel build system
	cp ${FILESDIR}/Makefile ${S}/sys/linux/Makefile
}

src_compile() {
	emake -f LINUX/Makefile CC="$(tc-getCC)" AR="$(tc-getAR)" showvars || die "showvars fail"
	emake -f LINUX/Makefile CC="$(tc-getCC)" AR="$(tc-getAR)" || die "make fail"
	linux-mod_src_compile
}

src_install() {
	emake -f LINUX/Makefile PREFIX="${D}/usr" install
	linux-mod_src_install
        generate_modulesd

}

pkg_postinst() {
        linux-mod_pkg_postinst
}

pkg_postrm() {
        linux-mod_pkg_postrm
}