#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
#export DH_OPTIONS

include /usr/share/GNUstep/debian/config.mk
GNUSTEP_MAKEFILES=$(GS_MAKE_DIR)

MAKE=gs_make
GNUSTEP_MAKEFILES=/usr/share/GNUstep/Makefiles

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

GNUSTEP_MAKE_SERVICES = true
export GNUSTEP_MAKE_SERVICES

#ADDITIONAL_INCLUDE_DIRS=" -I$(CURDIR)/FSNode -I$(CURDIR)/GWLib -I$(CURDIR)/Finder/Modules -I$(CURDIR)/Finder/SearchResults -I$(CURDIR)/Desktop/Dock"
#ADDITIONAL_LIB_DIRS=" -L$(CURDIR)/FSNode/FSNode.framework/Versions/Current  -L$(CURDIR)/GWLib/shared_obj"

#---------------------------------------------------------------------------

# libGWorkspace SONAME version :
#sov_base := 1

# packages names
p_gwork	= gworkspace.app
p_wrap	= gworkspace-apps-wrappers
#p_lib	= lib$(p_gwork)$(sov_base)
#p_dev	= lib$(p_gwork)-dev
#p_clip	= clipbook.app

# packages temp dirs
d_gwork	= debian/$(p_gwork)
d_wrap	= debian/$(p_wrap)
#d_lib	= debian/$(p_lib)
#d_dev	= debian/$(p_dev)
d_clip	= debian/$(p_clip)
d_gwr	= debian/$(p_gwr)

#---------------------------------------------------------------------------

config.status:
	dh_testdir
	. ${GNUSTEP_MAKEFILES}/GNUstep.sh ; \
	./configure \
	    --build=$(DEB_BUILD_GNU_TYPE) \
	    --host=$(DEB_HOST_GNU_TYPE) \
	    --target=$(DEB_HOST_GNU_TYPE) \
	    --prefix=/usr \
	    --bindir=\$${prefix}/bin

build: build-stamp
build-stamp: config.status
	dh_testdir
# Build Gworkspace
	$(MAKE) messages=yes
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	# Clean Gworkspace
	[ ! -f GNUmakefile ] || $(MAKE) clean distclean
	rm -f $(CURDIR)/ContentViewers/PdfViewer/config.h
	find . -name config.log -exec rm {} \;
	find . -name config.status -exec rm {} \;
	dh_clean

install: install-arch install-indep
install-indep:
	dh_testdir
	dh_testroot
	dh_clean -k -i
	dh_installdirs -i
# Copy the contents of Apps_wrappers dir in a place GNUstep can find.
	dh_install -p$(p_wrap) -XCVS -X.xvpics \
	    Apps_wrappers/*.app lib/GNUstep/Applications
	rm -rf $(CURDIR)/debian/gworkspace-apps-wrappers/lib/GNUstep/Applications/ggv.app
	# don't support non-free applications (they can't be installed from main)
	rm -rf $(CURDIR)/debian/gworkspace-apps-wrappers/lib/GNUstep/Applications/netscape.app
	rm -rf $(CURDIR)/debian/gworkspace-apps-wrappers/lib/GNUstep/Applications/StarOffice.app
	rm -rf $(CURDIR)/debian/gworkspace-apps-wrappers/lib/GNUstep/Applications/Acrobat.app
	rm -rf $(CURDIR)/debian/gworkspace-apps-wrappers/lib/GNUstep/Applications/realplay.app
	rm -rf $(CURDIR)/debian/gworkspace-apps-wrappers/lib/GNUstep/Applications/xv.app
	# don't support non-gnustep apps that we have native apps for (textedit, poe)
	rm -rf $(CURDIR)/debian/gworkspace-apps-wrappers/lib/GNUstep/Applications/gedit.app
	rm -rf $(CURDIR)/debian/gworkspace-apps-wrappers/lib/GNUstep/Applications/oggedit.app

install-arch: build
	dh_testdir
	dh_testroot
	dh_clean -k -a
	dh_installdirs -a
# Install GWorkspace
	$(MAKE) install \
	    GNUSTEP_INSTALLATION_DOMAIN=SYSTEM DESTDIR=$(CURDIR)/debian/gworkspace.app
	dh_install debian/*.desktop usr/share/applications/
	rm $(CURDIR)/debian/*.app/usr/lib/GNUstep/Applications/*.app/Resources/*.desktop
	dh_install -a

# Build architecture independant packages
binary-indep: install-indep
	dh_testdir
	dh_testroot
# Fix .tiff files permissions
	for file in `find debian/gworkspace-apps-wrappers -name "*.tiff"`; do \
	    chmod -v 644 $$file ; done
	gsdh_gnustep -i
# Overrides lintian warnings    
	dh_installdirs -p$(p_wrap) usr/share/lintian/overrides
	cp  $(CURDIR)/debian/$(p_wrap).lintian-overrides \
	    $(d_wrap)/usr/share/lintian/overrides/$(p_wrap)
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_link -i
	dh_strip -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture dependant packages.
binary-arch: build install-arch
	dh_testdir
	dh_testroot
# Install XPM Icons
	dh_install -p$(p_gwork) debian/GWorkspace.xpm usr/share/pixmaps
#	dh_install -p$(p_clip) debian/ClipBook.xpm usr/share/pixmaps
# Remove CVS dirs
	for pkg in $(p_gwork) $(p_clip) $(p_gwr) ; do \
	    for dir in `find debian/$$pkg -name "CVS" -type d` ; \
		do rm -rf $$dir ; done ; \
	    done
# Remove .xvpics dirs
	for dir in `find $(d_gwork) -name ".xvpics" -type d`; do \
	    rm -rf $$dir ; done
# Fix .tiff files permissions
	for file in `find $(d_gwork) -name "*.tiff"`; do \
	    chmod -v 644 $$file ; done
# Overrides lintian warnings
	for pack in $(p_gwork) $(p_clip) $(p_gwr); do \
	    dh_installdirs -p$$pack usr/share/lintian/overrides ; \
	    cp  $(CURDIR)/debian/$$pack.lintian-overrides \
		$(CURDIR)/debian/$$pack/usr/share/lintian/overrides/$$pack ; \
	    done
	gsdh_gnustep -a
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_installmenu -a
	dh_installman -p$(p_gwork) debian/GWorkspace.1
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -V
	dh_installdeb -a
	dh_shlibdeps -a -l$(CURDIR)/$(d_gwork)/$(GS_LIBS_DIR)
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch configure
