# Template file for 'brother-brscan4'
pkgname=brother-brscan4
version=0.4.5
revision=1
archs="i686 x86_64"
depends="sane"
short_desc="SANE scanner driver for brscan4-compatible printers"
maintainer="Martijn van Buul <martijn.van.buul@gmail.com>"
license="custom"
homepage="http://support.brother.com/"
repository="nonfree"
nopie=yes
_mylibrary="libsane-brother4.so.1.0.7"
conf_files="/etc/opt/brother/scanner/brscan4/Brsane4.ini /etc/opt/brother/scanner/brscan4/brsanenetdevice4.cfg /etc/opt/brother/scanner/brscan4/models4/*.ini"

# license
distfiles="http://www.brother.com/agreement/English_sane/agree.html>LICENSE.html"
checksum="3434bca1936d6a5fd6afd810cde7e1876dd4d1496722b09af180278480f464f2"

if [ "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
	_debpkgid="1.amd64"
	distfiles+=" http://download.brother.com/welcome/dlf006645/brscan4-${version}-${_debpkgid}.deb"
	checksum+=" 9ad23cb4a9e82ef8ea19fbadf40058232aada3150f816ddda61048d650d1eee7"
	_deblibdir="usr/lib64"
else
	_debpkgid="1.i386"
	distfiles+=" http://download.brother.com/welcome/dlf006646/brscan4-${version}-${_debpkgid}.deb"
	checksum+=" 44f9b2e8068b4b8817e62c00a1c62e710e9c0931984a18c28799e3ec1eaff32d"
	_deblibdir="usr/lib"
fi

do_extract() {
	ar x ${XBPS_SRCDISTDIR}/${pkgname}-${version}/brscan4-${version}-${_debpkgid}.deb
	tar xzpf data.tar.gz
}

do_install() {
	# binary package makes a fine mess of things, and installs stuff in very
	# unwieldly locations. Some of this cannot be avoided.

	# deb package installs 64-bit libraries in /usr/lib64; fix this
	vinstall ./${_deblibdir}/sane/${_mylibrary} 755 usr/lib/sane
	ln -sf /usr/lib/sane/${_mylibrary} ${DESTDIR}/usr/lib/sane/$(echo ${_mylibrary} | sed -e 's/\.[0-9]\.[0-9]$//')
	ln -sf /usr/lib/sane/${_mylibrary} ${DESTDIR}/usr/lib/sane/$(echo ${_mylibrary} | sed -e 's/\.[0-9]\.[0-9]\.[0-9]$//')

	# The binary library has hard-coded paths to
	#
	# /etc/opt/brother/scanner/brscan4
	#
	# This is very unfortunate, but cannot be avoided without the source to
	# recompile the binary - which is not available.
	#
	# The deb package then goes ahead and installs symlinks to the actual
	# files in /opt/brother/scanner/brscan4. Limit the mess by actually
	# installing these files to /etc/opt/brother/scanner/brscan4 so there
	# are no configuration files in /opt

	vmkdir etc/opt/brother/scanner/brscan4 755
	vinstall opt/brother/scanner/brscan4/Brsane4.ini 644 etc/opt/brother/scanner/brscan4
	vinstall opt/brother/scanner/brscan4/brsanenetdevice4.cfg 644 etc/opt/brother/scanner/brscan4
	vcopy opt/brother/scanner/brscan4/models4 etc/opt/brother/scanner/brscan4

	# Install the licenses.
	vlicense "${XBPS_SRCDISTDIR}/${pkgname}-${version}/LICENSE.html"
	vlicense opt/brother/scanner/brscan4/doc/brscan4/readme.txt # Independent JPEG blurb.

	# config helper - installed by the deb package as a symlink.
	vbin opt/brother/scanner/brscan4/brsaneconfig4

	# The only file in the deb package that has *not* been installed is a
	# setup script to modify the SANE configuration. This will be performed
	# by INSTALL and REMOVE instead.
}
