# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/media-sound/spotify/spotify-0.9.11.27-r2.ebuild,v 1.1 2014/12/12 16:19:50 prometheanfire Exp $

EAPI=5
inherit eutils unpacker versionator

DESCRIPTION="ecoDMS PDF/A printer"
HOMEPAGE="http://www.ecodms.de/"
SRC_BASE="http://www.ecodms.de/"

MY_PV=$(replace_version_separator 2 '-' )
MY_P="${PN}-${MY_PV}"
EDITION=$(delete_all_version_separators $(get_version_component_range 1-2 ${MY_PV} ))
SRC_URI="
        amd64? ( ${SRC_BASE}ecodms_${EDITION}64/wheezy/${MY_P}_amd64.deb )
        x86?   ( ${SRC_BASE}ecodms_${EDITION}32/wheezy/${MY_P}_i386.deb )
        "
SLOT="0"
KEYWORDS="~amd64 ~x86"
RESTRICT="mirror strip"
IUSE="bundled-qt5"

QA_PREBUILT="*"

DEPEND=""
RDEPEND="${DEPEND}
        !bundled-qt5? ( dev-qt/qtcore:5[icu] )
        !bundled-qt5? ( dev-qt/qtdbus:5 )
        !bundled-qt5? ( dev-qt/qtxml:5 )

        dev-libs/icu
        net-print/cups
        app-text/poppler"

S=${WORKDIR}

src_prepare() {
    rm ${S}/_gpgbuilder ${S}/_gpgbuilder0

    sed -i -e 's/\/usr\/lib\/cups\/ecodms\//\/usr\/libexec\/cups\/ecodms\//g' usr/lib/cups/backend/ecodms
}

src_install() {
	exeinto /usr/libexec/cups/backend
    # use my changed backend-file instead of upstream version
    doexe ${FILESDIR}/ecodms
	#doexe usr/lib/cups/backend/ecodms

    if use bundled-qt5; then
      insinto /usr/libexec/cups/ecodms
      doexe usr/lib/cups/ecodms/lib*
    fi

    exeinto /usr/libexec/cups/ecodms
	doexe usr/lib/cups/ecodms/ecodmsprinter

	insinto /usr/share/cups/model
	doins usr/share/cups/model/*.ppd

	dodir /var/spool/ecodms
    fperms 1777 /var/spool/ecodms
}

#pkg_preinst() {
#}

pkg_postinst() {
    chmod g+w /var/spool/ecodms
    chgrp lp /var/spool/ecodms
    elog "Restarting CUPS..."
    service cupsd restart
	elog "Removing ecodmsprinter (may print an error if it does not exist) from CUPS..."
    lpadmin -x ecodmsprinter
	elog "Adding ecodmsprinter to CUPS..."
    lpadmin -p ecodmsprinter -v ecodms:/var/spool/ecodms/ -D "ecoDMS" -E -P /usr/share/cups/model/ecodmspdf.ppd
}

pkg_postrm() {
    elog "Removing ecodmsprinter from CUPS..."
    lpadmin -x ecodmsprinter
	elog "Restarting CUPS..."
    service cupsd restart
}