#!/usr/bin/make -f

package=ghfaxviewer
primary=ghfaxviewer-gtk
secondary=ghfaxviewer-gnome

build:

build-gtk: configure-gtk
	$(MAKE)
	touch build-gtk

build-gnome: configure-gnome
	$(MAKE)
	touch build-gnome
	
configure-gtk:
	$(checkdir)
	-rm -f build-gtk
	if [ -f configure-gnome ]; then \
	  rm -f configure-gnome; \
	  $(MAKE) distclean; \
	fi
	./configure --prefix=/usr --without-gnome
	touch configure-gtk

configure-gnome:
	$(checkdir)
	-rm -f build-gnome
	if [ -f configure-gtk ]; then \
	  rm -f configure-gtk; \
	  $(MAKE) distclean; \
	fi
	./configure --prefix=/usr
	touch configure-gnome

clean:
	$(checkdir)
	-rm -f build-gtk build-gnome
	-$(MAKE) distclean
	-rm -f `find . -name "*~"`
	-rm -f debian/files* core debian/*substvars
	-rm -rf debian/tmp debian/$(secondary)
	-rm -f build-gtk build-gnome configure-gtk configure-gnome
	-rm -f debian/*.debhelper
	
#can't this section be removed ?
#binary-indep: checkroot # clean
#	$(checkdir)
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.


binary-arch-gtk: checkroot build-gtk
	$(checkdir)
	-rm -rf debian/tmp
	-install -d debian/tmp
	-cd debian/tmp && install -d `cat ../dirs`
	$(MAKE) install prefix=`pwd`/debian/tmp/usr
	-strip -R .note -R .comment debian/tmp/usr/bin/ghfaxviewer
	-mv debian/tmp/usr/bin/ghfaxviewer debian/tmp/usr/bin/ghfaxviewer-gtk
	-ln -s ghfaxviewer-gtk debian/tmp/usr/bin/ghfaxviewer
	-ln -s ../man7/undocumented.7.gz debian/tmp/usr/share/man/man1/ghfaxviewer.1.gz
	-ln -s ../man7/undocumented.7.gz debian/tmp/usr/share/man/man1/ghfaxviewer-gtk.1.gz
	debstd AUTHORS ChangeLog NEWS README TODO debian/copyright
	dh_installmenu -pghfaxviewer-gtk -Pdebian/tmp
	
	# fixes the /usr/share/doc link problem...
	-cat debian/tmp/DEBIAN/prerm | sed -e 's/-gtk//g' > debian/tmp/DEBIAN/prerm.sed
	-cat debian/tmp/DEBIAN/postinst | sed -e 's/-gtk//g' > debian/tmp/DEBIAN/postinst.sed
	-rm -f debian/tmp/DEBIAN/prerm debian/tmp/DEBIAN/postinst
	-mv debian/tmp/DEBIAN/prerm.sed debian/tmp/DEBIAN/prerm
	-mv debian/tmp/DEBIAN/postinst.sed debian/tmp/DEBIAN/postinst

	cat debian/postinst.debhelper >> debian/tmp/DEBIAN/postinst

	echo "#!/bin/sh -e" > debian/tmp/DEBIAN/postrm
	cat debian/postrm.debhelper >> debian/tmp/DEBIAN/postrm

	-chmod 755 debian/tmp/DEBIAN/prerm debian/tmp/DEBIAN/postinst debian/tmp/DEBIAN/postrm
	-chown root.root debian/tmp/DEBIAN/prerm debian/tmp/DEBIAN/postinst debian/tmp/DEBIAN/postrm

	dpkg-gencontrol -isp -pghfaxviewer-gtk

	-chown -R root.root debian/tmp
	-chmod -R go=rX debian/tmp
	dpkg --build debian/tmp ..

binary-arch-gnome: checkroot build-gnome
	$(checkdir)
	-rm -rf debian/$(secondary)
	-install -d debian/$(secondary)
	-cd debian/$(secondary) && install -d `cat ../dirs`
	$(MAKE) install prefix=`pwd`/debian/$(secondary)/usr
	-strip -R .note -R .comment debian/$(secondary)/usr/bin/ghfaxviewer
	-mv debian/$(secondary)/usr/bin/ghfaxviewer debian/$(secondary)/usr/bin/ghfaxviewer-gnome
	-ln -s ghfaxviewer-gnome debian/$(secondary)/usr/bin/ghfaxviewer
	-mv debian/tmp debian/primary-tmp
	-mv debian/$(secondary) debian/tmp
	-ln -s ../man7/undocumented.7.gz debian/tmp/usr/share/man/man1/ghfaxviewer.1.gz
	-ln -s ../man7/undocumented.7.gz debian/tmp/usr/share/man/man1/ghfaxviewer-gnome.1.gz
	debstd AUTHORS ChangeLog NEWS README TODO debian/copyright
	dh_installmenu -pghfaxviewer-gnome -Pdebian/tmp
	-rm -rf debian/$(secondary)
	-mv debian/tmp debian/$(secondary)
	-mv debian/primary-tmp debian/tmp

	# fixes the /usr/share/doc link problem...
	-cat debian/$(secondary)/DEBIAN/prerm | sed -e 's/-gtk//g' > debian/$(secondary)/DEBIAN/prerm.sed
	-cat debian/$(secondary)/DEBIAN/postinst | sed -e 's/-gtk//g' > debian/$(secondary)/DEBIAN/postinst.sed
	-rm -f debian/$(secondary)/DEBIAN/prerm debian/$(secondary)/DEBIAN/postinst
	-mv debian/$(secondary)/DEBIAN/prerm.sed debian/$(secondary)/DEBIAN/prerm
	-mv debian/$(secondary)/DEBIAN/postinst.sed debian/$(secondary)/DEBIAN/postinst

	cat debian/ghfaxviewer-gnome.postinst.debhelper >> debian/$(secondary)/DEBIAN/postinst
	echo "#!/bin/sh -e" > debian/$(secondary)/DEBIAN/postrm
	cat debian/ghfaxviewer-gnome.postrm.debhelper >> debian/$(secondary)/DEBIAN/postrm

	-chmod 755 debian/$(secondary)/DEBIAN/prerm debian/$(secondary)/DEBIAN/postinst debian/$(secondary)/DEBIAN/postrm
	-chown root.root debian/$(secondary)/DEBIAN/prerm debian/$(secondary)/DEBIAN/postinst debian/$(secondary)/DEBIAN/postrm

	dpkg-gencontrol -Pdebian/$(secondary) -isp -pghfaxviewer-gnome
	-chown -R root.root debian/$(secondary)
	-chmod -R go=rX debian/$(secondary)
	dpkg --build debian/$(secondary) ..

define checkdir
	test -f debian/rules
endef

binary: binary-arch-gtk binary-arch-gnome

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: binary binary-arch-gtk binary-arch-gnome clean checkroot
