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

# Inspired by
#	http://forums.gentoo.org/viewtopic-t-430913.html
#	the file bjfiler-common.spec (download SRC_URI)
#	The Ebuild Howto (gentoo.org)

inherit eutils rpm

_release="2"
_prefix="/usr/local"
_bindir="/usr/local/bin"
_libdir="/usr/lib"
_ppddir="/usr/share/cups/model"

DESCRIPTION="Canon Bubble Jet Printer Driver for Linux (Pixus/Pixma-Series)."
HOMEPAGE="ftp://download.canon.jp/pub/driver/bj/linux/"
RESTRICT="nomirror"

SRC_URI="ftp://download.canon.jp/pub/driver/bj/linux/bjfilter-common-${PV}-${_release}.src.rpm"
LICENSE="???" # GPL-2 source and proprietary binaries

SLOT="0"
KEYWORDS="~x86"
IUSE="	pixusip3100
	pixusip4100
	pixusip8600
	pixmaip1000
	pixmaip1500"
DEPEND=">=sys-devel/automake-1.8
	sys-devel/autoconf"
RDEPEND="x11-libs/gtk+
	dev-libs/libxml
	media-libs/libpng
	amd64? (app-emulation/emul-linux-x86-gtklibs)"

# It might be possible to compile against dev-libs/libxml2 instead of Version 1.


# Dependencies I couldn't resolve
# Requires: libtiff ???
# BuildRequires: gtk-devel cups-devel ???


# Table of supported Printers and it's IDs
# _pr		the printers name
# _prid		the printers ID	
# _prcomp	compatible printers

_pr1="pixusip3100"
_pr2="pixusip4100"
_pr3="pixusip8600"
_pr4="pixmaip1000"
_pr5="pixmaip1500"
_prid1="218"
_prid2="221"
_prid3="238"
_prid4="230"
_prid5="214"
_prcomp1="i560 i850 iP3000 MP730"
_prcomp2="i865 iP4000 iP5000 MP750 MP780"
_prcomp3="?"
_prcomp4="i250 i320 i350"
_prcomp5="iP2000"

# Changelog since last bugzilla upload
#
# fixed linking-problem


###
#   Standard Ebuild-functions
###

pkg_setup() {
        if use amd64 ; then
		append-flags -m32
#		append-ldflags -L/opt/emul/x86/lib -L/opt/emul/x86/usr/lib
        fi
	einfo ""
	einfo " http://bugs.gentoo.org/show_bug.cgi?id=128560"
	einfo ""
	einfo "Available driver models (and propably compatible printers)"
	einfo " $_pr1 ($_prcomp1)"
	einfo " $_pr2 ($_prcomp2)"
	einfo " $_pr3 ($_prcomp3)"
	einfo " $_pr4 ($_prcomp4)"
	einfo " $_pr5 ($_prcomp5)"
	einfo ""
	if ! (use $_pr1 || use $_pr2 || use $_pr3 || use $_pr4 || use $_pr5); then
		eerror "You must enable at least one driver model (set it's USE-flag)."
		die "no use-flag given"
	fi
}

src_unpack() {
	rpm_src_unpack || die
	mv bjfilter-common-${PV} ${P} || die # Correcting directory-structure
	if use "pixusip4100"; then
		epatch ${FILESDIR}/bjfilter-2.50-pixusip4100-ppd.patch || die
	fi
}

src_compile() {
	cd libs || die
	./autogen.sh --prefix=${_prefix} || die "Error: libs/autoconf.sh failed"
	make || die "Couldn't make libs"

	cd ../bjcups || die
	./autogen.sh --prefix=${_prefix} --enable-progpath=${_bindir} || die "Error: bjcups/autoconf.sh failed"
	make || die "Couldn't make bjcups"

	cd ../pstocanonbj || die
	./autogen.sh --prefix=/usr --enable-progpath=${_bindir} || die "Error: pstocanonbj/autoconf.sh failed"
	make || die "Couldn't make pstocanonbj"

	cd ../bjcupsmon || die # It's neccessary to cd into bjcupsmon for the next script to work
	./autogen.sh --prefix=${_prefix} || die "Error: bjcupsmon/autoconf.sh failed"
	make || die "Couldn't make bjcupsmon"

	cd ..

	_pr=$_pr1 _prid=$_prid1
	if use $_pr; then
		src_compile_pr;
	fi

	_pr=$_pr2 _prid=$_prid2
	if use $_pr; then
		src_compile_pr;
	fi

	_pr=$_pr3 _prid=$_prid3
	if use $_pr; then
		src_compile_pr;
	fi

	_pr=$_pr4 _prid=$_prid4
	if use $_pr; then
		src_compile_pr;
	fi

	_pr=$_pr5 _prid=$_prid5
	if use $_pr; then
		src_compile_pr;
	fi
}

src_install() {
	mkdir -p ${D}${_bindir} || die
	mkdir -p ${D}${_libdir}/cups/filter || die
	mkdir -p ${D}${_ppddir} || die
	mkdir -p ${D}${_libdir}/bjlib || die

	cd libs || die
	make DESTDIR=${D} install || die "Couldn't make install libs"

	cd ../bjcups || die
	make DESTDIR=${D} install || die "Couldn't make install bjcups"

	cd ../pstocanonbj || die
	make DESTDIR=${D} install || die "Couldn't make install pstocanonbj"

	cd ../bjcupsmon || die
	make DESTDIR=${D} install || die "Couldn't make install bjcupsmon"

	cd ..

	_pr=$_pr1 _prid=$_prid1
	if use $_pr; then
		src_install_pr;
	fi

	_pr=$_pr2 _prid=$_prid2
	if use $_pr; then
		src_install_pr;
	fi

	_pr=$_pr3 _prid=$_prid3
	if use $_pr; then
		src_install_pr;
	fi

	_pr=$_pr4 _prid=$_prid4
	if use $_pr; then
		src_install_pr;
	fi

	_pr=$_pr5 _prid=$_prid5
	if use $_pr; then
		src_install_pr;
	fi
}

pkg_postinst() {
	einfo ""
	einfo "For installing a printer:"
	einfo " * Restart CUPS: /etc/init.d/cupsd restart"
	einfo " * Go to http://127.0.0.1:631/"
	einfo "   -> Printers -> Add Printer"
	einfo ""
	einfo "If you experience any problems visit:"
	einfo " http://forums.gentoo.org/viewtopic-p-3217721.html"
	einfo ""
}


###
#	Custom Helper Functions
###

src_compile_pr()
{
	mkdir ${_pr}
	cp -a ${_prid} ${_pr} || die
	cp -a bjfilter ${_pr} || die
	cp -a printui ${_pr} || die
	cp -a stsmon ${_pr} || die

	cd ${_pr}/bjfilter || die
	./autogen.sh --prefix=${_prefix} --program-suffix=${_pr} --enable-libpath=${_libdir}/bjlib --enable-binpath=${_bindir} || die
	make || die "Couldn't make ${_pr}/bjfilter"
	
	cd ../printui || die
	./autogen.sh --prefix=${_prefix} --program-suffix=${_pr} || die
	make || die "Couldn't make ${_pr}/printui"
	
	cd ../stsmon || die
	./autogen.sh --prefix=${_prefix} --program-suffix=${_pr} --enable-progpath=${_bindir} || die
	make || die "Couldn't make ${_pr}/stsmon"
	
	cd ../..
}

src_install_pr()
{
	cd ${_pr}/bjfilter || die
	make DESTDIR=${D} install || die "Couldn't make install ${_pr}/bjfilter"
	
	cd ../printui || die
	make DESTDIR=${D} install || die "Couldn't make install ${_pr}/printui"
	
	cd ../stsmon || die
	make DESTDIR=${D} install || die "Couldn't make install ${_pr}/stsmon"

	cd ../..
	cp ${_prid}/libs_bin/* ${D}${_libdir} || die
	cp ${_prid}/database/* ${D}${_libdir}/bjlib || die
	cp ppd/canon${_pr}.ppd ${D}${_ppddir} || die
}