#!/usr/bin/make -f

CC = gcc
CFLAGS = -g -Wall

#export DH_VERBOSE=1


install: build
	dh_testdir
	dh_testroot
	
	#Remove brother printers that don't exist.
	rm -R build-tree/hl4040cnjcupswrapper*
	rm -R build-tree/mfc9640cwcupswrapper*
	
	for i in `ls build-tree/ | sed s/'cupswrapper.*'//g` ; do \
		mkdir -p debian/tmp/usr/Brother/Printer/$$i/cupswrapper ; \
		mkdir -p debian/tmp/usr/share/ppd/Brother/ ; \
		sed -e '1,/^cat/d' -e '/!ENDOFWFILTER/,$$d' -e 's/\\//g' -e 's/$${printer_model}/'$$i'/g' build-tree/$${i}cupswrapper*/cupswrapper/cupswrapperSetup_$$i > debian/tmp/usr/Brother/Printer/$$i/cupswrapper/brlpdwrapper_$$i ; \
		sed -i s,'brlpdwrapper','/usr/Brother/Printer/'$$i'/cupswrapper/brlpdwrapper_',g build-tree/$${i}cupswrapper*/PPD/*.ppd ; \
		cp build-tree/$${i}cupswrapper*/PPD/*.ppd debian/tmp/usr/share/ppd/Brother/ ; \
		cp build-tree/dcp9040cncupswrapper*/brcupsconfig/brcupsconfcl1 debian/tmp/usr/Brother/Printer/ ; \
		ln -sf /usr/Brother/Printer/brcupsconfcl1 debian/tmp/usr/Brother/Printer/$$i/cupswrapper/brcupsconfcl1 ; \
	done
	#Fix wrong folders in scripts
	sed -i s_'usr/share/cups/model'_'usr/share/ppd/Brother'_g debian/tmp/usr/Brother/Printer/*/cupswrapper/brlpd*
	sed -i s_'/usr/local'_'/usr'_g debian/tmp/usr/Brother/Printer/*/cupswrapper/brlpd*
	
	dh_install --autodest debian/tmp/usr


build:
	mkdir -p build-tree
	cd build-tree && for i in ../*.tar.gz; do tar zxvf $$i; done
	#Fix wrong folder and build
	cd build-tree/dcp9040cncupswrapper*/brcupsconfig && sed -i s_'/usr/local'_'/usr'_g brcupsconfig.c
	cd build-tree/dcp9040cncupswrapper*/brcupsconfig && gcc brcupsconfig.c -o brcupsconfcl1

binary-indep: build install

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	chmod a+x debian/brother-cups-wrapper-ac/usr/Brother/Printer/*/cupswrapper/brlpdwrapper*
	dh_installdeb -a
	dh_link -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean checkroot

clean:
	dh_testdir
	-rm -r build-tree
	dh_clean
	rm -f build
