# $Id: PKGBUILD 210089 2014-04-09 19:22:49Z andyrtr $
# Maintainer: Tom Gundersen <teg@jklm.no>
# Contributor : Rémy Oudompheng <remy@archlinux.org>
# Contributor: Morgan LEFIEUX <comete@archlinuxfr.org>

pkgname=hplip
pkgver=3.14.4
pkgrel=1
pkgdesc="Drivers for HP DeskJet, OfficeJet, Photosmart, Business Inkjet and some LaserJet"
arch=('i686' 'x86_64')
url="http://hplipopensource.com"
license=('GPL')
depends=('python2' 'ghostscript>=8.64-6' 'foomatic-db' 'foomatic-db-engine'
	 'net-snmp>=5.7.1' 'wget')
makedepends=('python2-pyqt4' 'python2-gobject2' 'sane' 'rpcbind' 'cups' 'libusb')
optdepends=('cups: for printing support'
            'python2-dbus: for dbus support'
            'sane: for scanner support'
            'python2-pillow: for commandline scanning support'
            'python2-notify: for Desktop notification support'
            'python2-reportlab: for pdf output in hp-scan'
            'rpcbind: for network support'
            'python2-pyqt4: for running hp-toolbox'
            'python2-gobject2: for running hp-toolbox'
            'hplip-plugin: binary blobs for specific devices (AUR) or run hp-setup to download the plugin'
            'libusb: for advanced usb support')
install=hplip.install
source=(http://downloads.sourceforge.net/${pkgname}/$pkgname-$pkgver.tar.gz{,.asc}
        disable_upgrade.patch)
md5sums=('6e8d53fb2284271505d2e1706c01f6c5'
         'SKIP'
         '257c8bfe675862d90f4ce07d3dff1d9f')

prepare() {
 cd $pkgname-$pkgver
 
 export PYTHON=python2
 
 find . -type f -exec sed -i 's~^#.*env python~#!/usr/bin/env python2~' {} +
 sed -i '1s|#!/usr/bin/python|#!/usr/bin/python2|' doctor.py setup.py makeuri.py logcapture.py base/magic.py
 sed -i '1s|#!/usr/bin/python|#!/usr/bin/python2|' ui/upgradeform.py uninstall.py upgrade.py config_usb_printer.py
 sed -i 's|python ./print.py|python2 ./print.py|' scan.py
 sed -i 's|python ./testpage.py|python2 ./testpage.py|' setup.py
 sed -i 's|python ./setup.py|python2 ./setup.py|' ui4/devmgr5.py ui4/nodevicesdialog.py 
 sed -i 's|python %HOME%|python2 %HOME%|' base/utils.py
 sed -i 's|python ./plugin.py|python2 ./plugin.py|' base/pkit.py
 sed -i 's|/usr/bin/python|/usr/bin/python2|' data/rules/hplip-printer@.service
 
 # disable insecure update - https://bugs.archlinux.org/task/38083
 patch -Np0 -i ${srcdir}/disable_upgrade.patch
 
 # https://bugs.archlinux.org/task/30085 - hack found in Gentoo
 # Use system foomatic-rip for hpijs driver instead of foomatic-rip-hplip
 # The hpcups driver does not use foomatic-rip
	local i
	for i in ppd/hpijs/*.ppd.gz ; do
		rm -f ${i}.temp
		gunzip -c ${i} | sed 's/foomatic-rip-hplip/foomatic-rip/g' | \
			gzip > ${i}.temp || return 1
		mv ${i}.temp ${i}
	done
   
 export AUTOMAKE='automake --foreign'
 autoreconf --force --install
}

build() {
 cd $pkgname-$pkgver
 ./configure --prefix=/usr \
             --enable-qt4 \
             --disable-foomatic-rip-hplip-install \
             --enable-foomatic-ppd-install \
             --enable-hpcups-install \
             --enable-new-hpcups \
             --enable-cups-ppd-install \
             --enable-cups-drv-install \
             --enable-hpijs-install \
             --enable-foomatic-drv-install \
             --enable-pp-build \
             --enable-udev-acl-rules #--help

 make
}

package() {
 #cd $pkgname-$pkgver
 cd $pkgname-${pkgver/.a/a}
 make rulesdir=/usr/lib/udev/rules.d DESTDIR="$pkgdir/" install
 
 # remove config provided by sane and autostart of hp-daemon
 rm -rf "$pkgdir"/etc/{sane.d,xdg}
 
 # remove HAL .fdi file because HAL is no longer used
 rm -vrf "$pkgdir"/usr/share/hal
 
 # remove rc script
 rm -vrf "$pkgdir"/etc/init.d
}
