#! /usr/bin/make -f
# -*- mode: makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
DH_VERSION = 2
export DH_COMPAT=2

# REMEMBER: arch-indep packaes are: gstep-base-examples

#export verbose=no

PWD	:= $(shell pwd)
ARCH	= $(shell dpkg --print-gnu-build-architecture)
CC	= gcc
v_src	:= $(shell dpkg-parsechangelog | awk '/^Version:/ {print $$2}')

# build debug packages?
debug	= yes
# the default backend
backend	= xgps

GS			= usr/lib/GNUstep
GNUSTEP_SYSTEM_ROOT	= /$(GS)
GNUSTEP_BUILD_ROOT	= $(PWD)
LIBRARY_COMBO	= gnu-gnu-gnu-$(backend)
GS_COMBO	= $(LIBRARY_COMBO)
GS_HOST		:= $(shell ./make/config.guess)
GS_CPU		:= $(shell ./make/clean_cpu.sh `./make/cpu.sh $(GS_HOST)`)
GS_OS		:= $(shell ./make/clean_os.sh `./make/os.sh $(GS_HOST)`)
GS_VENDOR	:= $(shell ./make/clean_vendor.sh `./make/vendor.sh $(GS_HOST)`)
lib_dir		= $(GS_CPU)/$(GS_OS)
combo_dir	= $(GS_CPU)/$(GS_OS)/$(GS_COMBO)
export GNUSTEP_SYSTEM_ROOT
export GNUSTEP_BUILD_ROOT

v_base	:= $(shell . base/Version; echo $$VERSION)
v_gui	:= $(shell . gui/Version;  echo $$VERSION)
v_xdps	:= $(shell . xdps/Version; echo $$VERSION)
v_xgps	:= $(shell . xgps/Version; echo $$VERSION)

# the default version value ...
#v_default := $(shell sed -n -e 's/^VERSION *= *\(.*\)$$/\1/p' make/common.make)
v_model = $(v_gui)
ifeq ($(backend),xgps)
  v_back	= $(v_xgps)
endif
ifeq ($(backend),xdps)
  v_back	= $(v_xdps)
endif

sov_base	:= $(shell echo $(v_base)  | awk -F. '{print $$1}')
sov_gui		:= $(shell echo $(v_gui)   | awk -F. '{print $$1}')
sov_model	:= $(shell echo $(v_model) | awk -F. '{print $$1}')
sov_xdps	:= $(shell echo $(v_xdps)  | awk -F. '{print $$1}')
sov_xgps	:= $(shell echo $(v_xgps)  | awk -F. '{print $$1}')
sov_back	:= $(shell echo $(v_back)  | awk -F. '{print $$1}')

p_make		= gstep-make
p_base		= gstep-base$(sov_base)
p_base_dev	= $(p_base)-dev
p_base_dbg	= $(p_base)-dbg
p_base_exam	= gstep-base-examples
p_gui		= gstep-gui$(sov_gui)
p_gui_dev	= $(p_gui)-dev
p_gui_dbg	= $(p_gui)-dbg
p_xdps		= gstep-xdps$(sov_back)
p_xdps_dev	= $(p_xdps)-dev
p_xdps_exam	= gstep-xdps-examples
p_xgps		= gstep-xgps$(sov_xgps)
p_xgps_dev	= $(p_xgps)-dev
p_xgps_dbg	= $(p_xgps)-dbg
p_xgps_exam	= gstep-xgps-examples

d		= debian/tmp
d_make		= debian/$(p_make)
d_base		= debian/$(p_base)
d_base_dev	= debian/$(p_base_dev)
d_base_dbg	= debian/$(p_base_dbg)
d_base_exam	= debian/$(p_base_exam)
d_gui		= debian/$(p_gui)
d_gui_dev	= debian/$(p_gui_dev)
d_gui_dbg	= debian/$(p_gui_dbg)
d_xraw		= debian/$(p_xraw)
d_xraw_dev	= debian/$(p_xraw_dev)
d_xraw_dbg	= debian/$(p_xraw_dbg)
d_xraw_exam	= debian/$(p_xraw_exam)
d_xdps		= debian/$(p_xdps)
d_xdps_dev	= debian/$(p_xdps_dev)
d_xdps_exam	= debian/$(p_xdps_exam)
d_xgps		= debian/$(p_xgps)
d_xgps_dev	= debian/$(p_xgps_dev)
d_xgps_dbg	= debian/$(p_xgps_dbg)
d_xgps_exam	= debian/$(p_xgps_exam)
ifeq ($(backend),xraw)
  p_back	= $(p_xraw)
  p_back_dev	= $(p_xraw_dev)
  p_back_dbg	= $(p_xraw_dbg)
  p_back_exam	= $(p_xraw_exam)
  d_back	= $(d_xraw)
  d_back_dev	= $(d_xraw_dev)
  d_back_dbg	= $(d_xraw_dbg)
  d_back_exam	= $(d_xraw_exam)
endif
ifeq ($(backend),xgps)
  p_back	= $(p_xgps)
  p_back_dev	= $(p_xgps_dev)
  p_back_dbg	= $(p_xgps_dbg)
  p_back_exam	= $(p_xgps_exam)
  d_back	= $(d_xgps)
  d_back_dev	= $(d_xgps_dev)
  d_back_dbg	= $(d_xgps_dbg)
  d_back_exam	= $(d_xgps_exam)
endif

MAKEDFLAGS = \
  'GNUSTEP_SYSTEM_ROOT=$(GNUSTEP_SYSTEM_ROOT)' \
  'GNUSTEP_BUILD_ROOT=$(GNUSTEP_BUILD_ROOT)' \
  'GNUSTEP_MAKEFILES=$(PWD)/Makefiles' \
  verbose=no

default: build

ctrl_flags = -D_V_OBJC_=1:2.95.2 -D_V_BASE_=$(sov_base) -D_V_GUI_=$(sov_gui) \
	-D_V_XGPS_=$(sov_xgps) -D_V_XDPS_=$(sov_xdps)
ifeq ($(debug),yes)
  ctrl_flags += -DDEBUG
endif
ifeq ($(backend),xgps)
  ctrl_flags += -DBACK_XGPS
endif
ifeq ($(backend),xdps)
  ctrl_flags += -DBACK_XDPS
endif
debian/control: debian/control.in debian/rules
	cpp -A- -P $(ctrl_flags) debian/control.in \
	  | awk '/^ *$$/ && x !~ /^ / {next} /NEWLINE/ {print ""; next} {print; x=$$0}' \
	  | sed -e '/^[^ ]/s/  */ /g' -e 's/  *\([,)]\)/\1/g' -e 's/ *$$//' \
	    -e 's/_V_BASE_/$(sov_base)/g' -e 's/_V_GUI_/$(sov_gui)/g' \
	    -e 's/_V_XGPS_/$(sov_xgps)/g' -e 's/_V_XDPS_/$(sov_xdps)/g' \
	  > debian/control

config: config.status
config.status: patched-stamp
	dh_testdir
	autoconf
	rm -f config.cache
	CC=$(CC) \
	  ./configure --prefix=$(GNUSTEP_SYSTEM_ROOT) \
	    --without-system-root \
	    --with-library-combo=$(LIBRARY_COMBO) \
	    --with-jpeg-library=/usr/lib --with-jpeg-include=/usr/include \
	    --with-tiff-library=/usr/lib --with-tiff-include=/usr/include
#	    --with-dps-library=/usr/lib --with-dps-include=/usr/include \


GNUmakefile GNUmakefile.local: GNUmakefile.in GNUmakefile.local.in \
    config.status
	./config.status

build: build-stamp
build-stamp: debian/control \
    patched-stamp build-shared-stamp build-static-stamp build-debug-stamp \
    build-test-stamp warnings
	touch build-stamp

build-shared-stamp: config.status GNUmakefile GNUmakefile.local
	dh_testdir
	($(MAKE) $(MAKEDFLAGS); echo $$? >.s) 2>&1 \
	   | tee prot-core.shared; exit `cat .s`
	touch build-shared-stamp

build-debug-stamp: config.status GNUmakefile GNUmakefile.local
ifeq ($(debug),yes)
	$(MAKE) -C base/Source debug=yes $(MAKEDFLAGS)
	$(MAKE) -C gui/Source debug=yes  $(MAKEDFLAGS)
	$(MAKE) -C gui/Model debug=yes   $(MAKEDFLAGS)
	$(MAKE) -C $(backend)/Source debug=yes $(MAKEDFLAGS)
endif
	touch build-debug-stamp

build-static-stamp: config.status GNUmakefile GNUmakefile.local
	for subproj in base/Source gui/Source gui/Model $(backend)/Source; \
	do \
	  $(MAKE) -C $$subproj shared=no $(MAKEDFLAGS) || exit $$?; \
	done
#	$(MAKE) shared=no $(MAKEDFLAGS)
	touch build-static-stamp

build-test-stamp: config.status GNUmakefile GNUmakefile.local build-shared-stamp
	$(MAKE) -C Testing $(MAKEDFLAGS)
	touch build-test-stamp

warnings: prot-core.shared
	chmod +x debian/warn_summary
	cat prot-core.shared > prot.shared
	for i in base gui $(backend); do \
	  echo -e "\n*************** $$i ***************"; \
	  debian/warn_summary -$$i prot.shared; \
	done > $@
	rm -f prot.shared

clean: debian/control
	dh_testdir
	dh_testroot
	rm -f build-*stamp install-stamp first-move-stamp
	rm -f warnings prot*.{shared,static}

	: # removing all build directories ...
	-find $(sub_projects) Testing -type d \
	  \( -name 'shared*_obj' -o -name 'static*_obj' -o -name '*.app' \) \
	  | xargs rm -rf
	rm -f `find -name 'lib*.so*' -type l` || true
	: # removing local installation directories
	rm -rf Apps Headers Libraries Library Makefiles Tools share
	rm -f GNUmakefile make/GNUmakefile
# this is terribly slow ...
#	-$(MAKE) distclean $(MAKEDFLAGS)
	-find \( -name config.cache -o -name config.log -o -name config.mak \
	    -o -name config.status -o -name config.make \
	    -o -name GNUmakefile.local \
	  \) | xargs rm -f
	-find \( -name mframe -prune ! -name mframe \) \
	    -o \( -name $(GS_CPU) -type d \) | xargs rm -rf
	: # manually cleaning `make'
	rm -f make/{config.h,GNUstep.sh,GNUstep.csh,which_lib,which_lib.pl} \
	  make/{openapp,debugapp,opentool} \
	  make/executable.template
	: # manually cleaning `base'
	rm -rf base/Source/{srcdir-include,gnustep}
	rm -f base/Source/{o_*_{bas,cbs}.m,dynamic-load.h} \
	  base/Source/Foundation \
	  base/Headers/Foundation \
	  base/Headers/gnustep/base/{preface.h,o_*_{bas,cbs}.h,mframe.h} \
	  base/Source/{NSNumber,NSValue}{[0-9],[0-9][0-9]}.m \
	  base/Source/mframe/config.{cache,log,status} \
	  base/Documentation/{version.tmpl.texi,STATUS} \
	  base/Documentation/gnustep-base.{aux,cp,cps,dvi,fn,info,ky,log,pg,ps} \
	  base/Documentation/gnustep-base.{toc,tp,vr,vrs} \
	  base/Documentation/{ANNOUNCE,README,CODING-STANDARDS,NEWS,INSTALL,TODO}
	for i in base/Documentation/*.tmpl.texi; do \
	  rm -f `basename $$i .tmpl.texi`.texi; \
	done
	: # manually cleaning `gui'
	rm -f gui/Headers/gnustep/gui/config.h gui/Headers/{AppKit,DPSClient} \
	  gui/Tools/example.service/Resources/Info-gnustep.plist
	: # manually cleaning backend
	rm -f $(backend)/config.h
	: # manually cleaning testing
	-find Testing \( -name '*.app' -o -name '*.bundle' \) -type d \
	  -exec rm -rf {} \;
	: # cleaning debian files
	rm -rf debian/gstep-make
	rm -f debian/{services.add,shlibs.local,*.shlibs}
	-for i in debian/*.in; do \
	  case "$$i" in debian/control.in) continue; esac; \
	  rm -f "`echo $$i | sed 's/\.in$$//'`"; \
	done
	: # remove Debian install files
	rm -f debian/{$(p_base),$(p_gui),$(p_back)}{,-dev,-dbg}.{preinst,postinst,prerm,postrm,conffiles,init}
	rm -f .s
	$(MAKE) -f debian/rules reverse-patches
	dh_clean

dirs_make	= \
	usr/share/doc/$(p_make) \
	$(GS)/Makefiles \
	$(GS)/Tools/$(lib_dir) \
	$(GS)/Tools/$(combo_dir) \
	$(GS)/share \
	$(GS)/Apps \
	$(GS)/Libraries/$(combo_dir) \
	$(GS)/Headers/$(lib_dir) \
	$(GS)/Library/{info,PostScript,man}

dirs_base	= \
	etc/GNUstep \
	usr/share/doc/$(p_base) \
	usr/share/doc/GNUstep \
	$(GS)/Headers/$(lib_dir) \
	$(GS)/Libraries/$(combo_dir) \
	$(GS)/Libraries/Resources/{NSCharacterSets,NSTimeZones} \
	$(GS)/Tools/$(combo_dir)
files_base	= \
	$(GS)/Libraries/$(combo_dir)/libgnustep-base*.so.* \
	$(GS)/Libraries/Resources/{NSCharacterSets,NSTimeZones} \
	$(GS)/Tools/$(lib_dir)/gdomap \
	$(GS)/Tools/$(combo_dir)/{defaults,dread,dwrite,dremove,gdnc}

dirs_base_dbg	= \
	usr/share/doc \
	$(GS)/Libraries/$(combo_dir)
files_base_dbg	= \
	$(GS)/Libraries/$(combo_dir)/libgnustep-base_d.so*

dirs_base_dev	= \
	usr/share/doc \
	$(GS)/Headers \
	$(GS)/Libraries/$(combo_dir)
files_base_dev	= \
	$(GS)/Headers \
	$(GS)/Libraries/$(combo_dir)/libgnustep-base*.{a,so}

dirs_base_exam	= \
	usr/share/doc/$(p_base)/examples
files_base_exam	= \

dirs_gui	= \
	usr/share/doc/$(p_gui) \
	usr/share/doc/GNUstep \
	$(GS)/Libraries/Resources/Images \
	$(GS)/Libraries/$(combo_dir) \
	$(GS)/Tools/$(combo_dir) \
	$(GS)/Library/Model
files_gui	= \
	$(GS)/Libraries/Resources/Images \
	$(GS)/Libraries/$(combo_dir)/{libgmodel,libgnustep-gui}*.so.* \
	$(GS)/Tools/$(combo_dir)/{make_services,gpbs} \
	$(GS)/Library/Model/*.gmodel

dirs_gui_dev	= \
	usr/share/doc \
	$(GS)/Headers/gnustep/AppKit \
	$(GS)/Libraries/$(combo_dir)
files_gui_dev	= \
	$(GS)/Headers/gnustep/AppKit/ \
	$(GS)/Libraries/$(combo_dir)/{libgmodel,libgnustep-gui}*.so \
	$(GS)/Libraries/$(combo_dir)/{libgmodel,libgnustep-gui}*.a

dirs_gui_dbg	= \
	usr/share/doc \
	$(GS)/Libraries/$(combo_dir)
files_gui_dbg	= \
	$(GS)/Libraries/$(combo_dir)/{libgmodel,libgnustep-gui}_d.so*

dirs_xraw	= \
	usr/share/doc/$(p_xraw)/examples \
	usr/share/doc/GNUstep \
	$(GS)/Libraries/$(combo_dir)
files_xraw	= \
	$(GS)/Libraries/$(combo_dir)/libgnustep-xraw*.so.*

dirs_xraw_dev	= \
	usr/share/doc \
	$(GS)/Headers/gnustep/xraw \
	$(GS)/Libraries/$(combo_dir)
files_xraw_dev	= \
	$(GS)/Headers/gnustep/xraw \
	$(GS)/Libraries/$(combo_dir)/libgnustep-xraw*.so \
	$(GS)/Libraries/$(combo_dir)/libgnustep-xraw*.a

dirs_xraw_dbg	= \
	usr/share/doc \
	$(GS)/Libraries/$(combo_dir)
files_xraw_dbg	= \
	$(GS)/Libraries/$(combo_dir)/libgnustep-xraw_d.so*

dirs_xraw_exam	= \
	usr/share/doc/$(p_xraw)/examples \
	$(GS)/Apps \
	$(GS)/Library/Services/example.service
files_xraw_exam	= \
	$(GS)/Library/Services/example.service

dirs_xgps	= \
	usr/share/doc/$(p_xgps)/examples \
	usr/share/doc/GNUstep \
	$(GS)/Libraries/$(combo_dir) \
	$(GS)/Libraries/Resources
files_xgps	= \
	$(GS)/Libraries/$(combo_dir)/libgnustep-xgps*.so.*

dirs_xgps_dev	= \
	usr/share/doc \
	$(GS)/Headers/gnustep/xgps \
	$(GS)/Libraries/$(combo_dir)
files_xgps_dev	= \
	$(GS)/Headers/gnustep/xgps \
	$(GS)/Libraries/$(combo_dir)/libgnustep-xgps*.so \
	$(GS)/Libraries/$(combo_dir)/libgnustep-xgps*.a

dirs_xgps_dbg	= \
	usr/share/doc \
	$(GS)/Libraries/$(combo_dir)
files_xgps_dbg	= \
	$(GS)/Libraries/$(combo_dir)/libgnustep-xgps_d.so*

dirs_xgps_exam	= \
	usr/share/doc \
	$(GS)/Apps \
	$(GS)/Library/Services/example.service
files_xgps_exam	= \


ifeq ($(backend),xraw)
  dirs_back	= $(dirs_xraw)
  dirs_back_dev	= $(dirs_xraw_dev)
  dirs_back_dbg	= $(dirs_xraw_dbg)
  dirs_back_exam= $(dirs_xraw_exam)
  files_back	= $(files_xraw)
  files_back_dev= $(files_xraw_dev)
  files_back_dbg= $(files_xraw_dbg)
  files_back_exam= $(files_xraw_exam)
endif
ifeq ($(backend),xgps)
  dirs_back	= $(dirs_xgps)
  dirs_back_dev	= $(dirs_xgps_dev)
  dirs_back_dbg	= $(dirs_xgps_dbg)
  dirs_back_exam= $(dirs_xgps_exam)
  files_back	= $(files_xgps)
  files_back_dev= $(files_xgps_dev)
  files_back_dbg= $(files_xgps_dbg)
  files_back_exam= $(files_xgps_exam)
endif

install: install-stamp first-move-stamp
install-stamp: build-stamp warnings
	dh_testversion $(DH_VERSION)
	dh_testdir
	dh_testroot
	rm -f first-move-stamp
	dh_clean -k
	rm -rf debian/$(p_make)
	dh_installdirs -p$(p_make) $(dirs_make)
	dh_installdirs -p$(p_base) $(dirs_base)
	dh_installdirs -p$(p_base_dev) $(dirs_base_dev)
	dh_installdirs -p$(p_base_exam) $(dirs_base_exam)
	dh_installdirs -p$(p_gui) $(dirs_gui)
	dh_installdirs -p$(p_gui_dev) $(dirs_gui_dev)
	dh_installdirs -p$(p_back) $(dirs_back)
	dh_installdirs -p$(p_back_dev) $(dirs_back_dev)
	dh_installdirs -p$(p_back_exam) $(dirs_back_exam)
ifeq ($(debug),yes)
	dh_installdirs -p$(p_base_dbg) $(dirs_base_dbg)
	dh_installdirs -p$(p_gui_dbg)  $(dirs_gui_dbg)
	dh_installdirs -p$(p_back_dbg) $(dirs_back_dbg)
endif

	: # install gstep-make
	$(MAKE) -C make install \
	  prefix=$(PWD)/$(d_make)/$(GS) \
	  GNUSTEP_INSTALLATION_DIR=$(PWD)/$(d_make)/$(GS) \
	  $(MAKEDFLAGS) INSTALL_ROOT_DIR=$(PWD)/$(d_make)
	: # install shared gstep-base version
	$(MAKE) -C base install \
	  prefix=$(PWD)/$(d_base)/$(GS) \
	  GNUSTEP_INSTALLATION_DIR=$(PWD)/$(d_base)/$(GS) \
	  $(MAKEDFLAGS) INSTALL_ROOT_DIR=$(PWD)/$(d_base)
	: # install shared gstep-gui version
	$(MAKE) -C gui install \
	  prefix=$(PWD)/$(d_gui)/$(GS) \
	  GNUSTEP_INSTALLATION_DIR=$(PWD)/$(d_gui)/$(GS) \
	  $(MAKEDFLAGS) INSTALL_ROOT_DIR=$(PWD)/$(d_gui)
	: # install shared gstep-backend version
	$(MAKE) -C $(backend) install \
	  prefix=$(PWD)/$(d_back)/$(GS) \
	  GNUSTEP_INSTALLATION_DIR=$(PWD)/$(d_back)/$(GS) \
	  $(MAKEDFLAGS) INSTALL_ROOT_DIR=$(PWD)/$(d_back)

	: # install static gstep-base version
	$(MAKE) -C base/Source install shared=no \
	  prefix=$(PWD)/$(d_base)/$(GS) \
	  GNUSTEP_INSTALLATION_DIR=$(PWD)/$(d_base)/$(GS) \
	  $(MAKEDFLAGS) INSTALL_ROOT_DIR=$(PWD)/$(d_base)
	: # install static gstep-gui version
	$(MAKE) -C gui/Source install shared=no \
	  prefix=$(PWD)/$(d_gui)/$(GS) \
	  GNUSTEP_INSTALLATION_DIR=$(PWD)/$(d_gui)/$(GS) \
	  $(MAKEDFLAGS) INSTALL_ROOT_DIR=$(PWD)/$(d_gui)
	$(MAKE) -C gui/Model install shared=no \
	  prefix=$(PWD)/$(d_gui)/$(GS) \
	  GNUSTEP_INSTALLATION_DIR=$(PWD)/$(d_gui)/$(GS) \
	  $(MAKEDFLAGS) INSTALL_ROOT_DIR=$(PWD)/$(d_gui)
	: # install static gstep-backend version
	$(MAKE) -C $(backend)/Source install shared=no \
	  prefix=$(PWD)/$(d_back)/$(GS) \
	  GNUSTEP_INSTALLATION_DIR=$(PWD)/$(d_back)/$(GS) \
	  $(MAKEDFLAGS) INSTALL_ROOT_DIR=$(PWD)/$(d_back)

ifeq ($(debug),yes)
	: # install debug versions of libraries
	cp -a base/Source/shared_debug_obj/$(combo_dir)/libgnustep-base_d.so* \
		$(d_base)/$(GS)/Libraries/$(combo_dir)/
	cp -a gui/Source/shared_debug_obj/$(combo_dir)/libgnustep-gui_d.so* \
		$(d_gui)/$(GS)/Libraries/$(combo_dir)/
	cp -a gui/Model/shared_debug_obj/$(combo_dir)/libgmodel_d.so* \
		$(d_gui)/$(GS)/Libraries/$(combo_dir)/
	cp -a $(backend)/Source/shared_debug_obj/$(combo_dir)/libgnustep-$(backend)_d.so* \
		$(d_back)/$(GS)/Libraries/$(combo_dir)/
endif
	: # install test applications
#	dh_installdirs -p$(p_back_exam) $(dirs_back_exam)
#	$(MAKE) -C Testing install \
#	  GNUSTEP_INSTALLATION_DIR=$(PWD)/$(d_back_exam)/$(GS) \
#	  $(MAKEDFLAGS) INSTALL_ROOT_DIR=$(PWD)/$(d_back_exam)
	cp -a Testing/*.app $(d_back_exam)/$(GS)/Apps/.

	touch install-stamp

first-move-stamp:
	dh_testversion $(DH_VERSION)
	dh_testdir
	dh_testroot
	rm -f install-stamp

	: # gstep-make
	: # better to modify the config.site file ...
	rm -f $(d_make)/$(GS)/share/config.site

	: # gstep-base
	: # we trust the Debian timezones ...
	rm -rf $(d_base)/$(GS)/Libraries/Resources/NSTimeZones/zones
	ln -sf ../../../../../share/zoneinfo \
	    $(d_base)/$(GS)/Libraries/Resources/NSTimeZones/zones
	: # symlink to local timezone file
	ln -sf /etc/timezone \
	    $(d_base)/$(GS)/Libraries/Resources/NSTimeZones/localtime
ifeq ($(debug),yes)
	dh_movefiles -p$(p_base_dbg) --sourcedir=$(d_base) $(files_base_dbg)
endif
	dh_movefiles -p$(p_base_dev) --sourcedir=$(d_base) $(files_base_dev)
	(cd base; tar cf - Examples Testing Version config.mak) \
		| (cd $(d_base_exam)/usr/share/doc/$(p_base)/examples; tar xfp -)
	rm -f $(d_base_exam)/usr/share/doc/$(p_base)/examples/{Examples,Testing}/GNUmakefile.local
	mv -f $(d_base)/etc/services.add debian/services.add

	: # gstep-gui
ifeq ($(debug),yes)
	dh_movefiles -p$(p_gui_dbg) --sourcedir=$(d_gui) $(files_gui_dbg)
endif
	dh_movefiles -p$(p_gui_dev) --sourcedir=$(d_gui) $(files_gui_dev)

	: # gstep- backend
ifeq ($(debug),yes)
	dh_movefiles -p$(p_back_dbg) --sourcedir=$(d_back) $(files_back_dbg)
endif
	dh_movefiles -p$(p_back_dev) --sourcedir=$(d_back) $(files_back_dev)
ifeq ($(backend),xraw)
#	### XXX: hack alert: this should be done in internal-all-install!
#	install -d $(d_xraw_exam)/$(GS)/Apps
	(cd xraw/Testing; tar cf - *.app) \
	    | (cd $(d_xraw_exam)/$(GS)/Apps; tar xpf -)
	(cd xraw/Testing; tar cfh - --exclude='*.app' --exclude='*_obj' .) \
	    | (cd $(d_xraw_exam)/usr/share/doc/$(p_xraw)/examples; tar xpf -)
endif

	: # remove empty directories, when all components are in place
	for d in `find debian -depth -type d -empty 2> /dev/null`; do \
	  case $$d in */usr/share/doc*|*/etc) continue; esac; \
	  while rmdir $$d 2> /dev/null; do d=`dirname $$d`; done; \
	done

	: # copy prerm files usr/doc -> usr/share/doc transition
	for b in $(p_base) $(p_base_dev) $(p_base_dbg) $(p_xgps) $(p_xgps_dev) $(p_xgps_dbg) \
		 $(p_gui) $(p_gui_dev) $(p_gui_dbg) $(p_make) $(p_base_exam) $(p_xgps_exam); \
	do \
	    cp -pf debian/gstep-base.prerm debian/$$b.prerm; \
	done

	touch first-move-stamp

# Build architecture-independent files here.
binary-indep: build install
	dh_testversion $(DH_VERSION)
	dh_testdir -i
	dh_testroot -i
	ln -sf $(p_base_dev) $(d_base_exam)/usr/share/doc/$(p_base_exam)
###	dh_installdocs -i
#	dh_installexamples -i
#	dh_installmenu -i
#	dh_installemacsen -i
#	dh_installinit -i
#	dh_installcron -i
#	dh_installmanpages -i
#	dh_undocumented
###	dh_installchangelogs -i
	dh_compress -i -X.m
	dh_fixperms -i
	dh_suidregister -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testversion $(DH_VERSION)
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -p$(p_make) \
		ANNOUNCE ChangeLog FAQ GNUstep-HOWTO NEWS README Version
	rm -rf $(d_make)/usr/share/doc/GNUstep
	mv $(d_make)/usr/share/doc/$(p_make) $(d_make)/usr/share/doc/GNUstep
	dh_installdocs -p$(p_make) make/{DESIGN,README}
	ln -sf ../$(p_make) $(d_make)/usr/share/doc/GNUstep/$(p_make)
	dh_installdocs -p$(p_base) \
	    base/{ANNOUNCE,AUTHORS,NEWS,README,Version}
	ln -sf ../$(p_base) $(d_base)/usr/share/doc/GNUstep/$(p_base)
	ln -sf $(p_base) $(d_base_dev)/usr/share/doc/$(p_base_dev)
	dh_installdocs -p$(p_gui) \
	    gui/{ANNOUNCE,FAQ,NEWS,README,SUPPORT,Version}
	ln -sf ../$(p_gui) $(d_gui)/usr/share/doc/GNUstep/$(p_gui)
	ln -sf $(p_gui) $(d_gui_dev)/usr/share/doc/$(p_gui_dev)
ifeq ($(backend),xgps)
	dh_installdocs -p$(p_xgps) xgps/Version
endif
ifeq ($(backend),xraw)
	dh_installdocs -p$(p_xraw) \
	    xraw/{FAQ,README,SUPPORT,TODO,Version}
endif
	ln -sf ../$(p_back) $(d_back)/usr/share/doc/GNUstep/$(p_back)
	ln -sf $(p_back) $(d_back_dev)/usr/share/doc/$(p_back_dev)
ifeq ($(backend),xraw)
	ln -sf $(p_xraw) $(d_xraw_exam)/usr/share/doc/$(p_xraw_exam)
endif
ifeq ($(debug),yes)
	ln -sf $(p_base_dev) $(d_base_dbg)/usr/share/doc/$(p_base_dbg)
	ln -sf $(p_gui_dev)  $(d_gui_dbg)/usr/share/doc/$(p_gui_dbg)
	ln -sf $(p_back_dev) $(d_back_dbg)/usr/share/doc/$(p_back_dbg)
endif
#	dh_installexamples -a
#	dh_installmenu -a
#	dh_installemacsen -a
#	dh_installmanpages -a

	: # copy Debian scripts
	b=gstep-base; v=$(sov_base); \
	for ext in preinst postinst prerm postrm init conffiles; do \
	  for t in '' -dev -dbg; do \
	    if [ -f debian/$$b$$t.$$ext ]; then \
	      cp -pf debian/$$b$$t.$$ext debian/$$b$$v$$t.$$ext; \
	    fi; \
	  done; \
	done
	sed 's,GSLIBDIR,/$(GS)/Libraries/$(combo_dir),' \
	    debian/gstep-base.postinst.in > debian/$(p_base).postinst
	b=gstep-gui; v=$(sov_gui); \
	for ext in preinst postinst prerm postrm init conffiles; do \
	  for t in '' -dev -dbg; do \
	    if [ -f debian/$$b$$t.$$ext ]; then \
	      cp -pf debian/$$b$$t.$$ext debian/$$b$$v$$t.$$ext; \
	    fi; \
	  done; \
	done
	set +e; b=gstep-xgps; v=$(sov_xgps); \
	for ext in preinst postinst prerm postrm init conffiles; do \
	  for t in '' -dev -dbg; do \
	    if [ -f debian/$$b$$t.$$ext ]; then \
	      cp -pf debian/$$b$$t.$$ext debian/$$b$$v$$t.$$ext; \
	    fi; \
	  done; \
	done
	sed -e 's,ARCHDIR,$(combo_dir),' -e 's,PACKAGE,$(p_xgps),' \
	    debian/gstep-xgps.preinst.in > debian/$(p_xgps).preinst
	sed -e 's,ARCHDIR,$(combo_dir),' -e 's,PACKAGE,$(p_xgps),' \
	    debian/gstep-xgps.postrm.in > debian/$(p_xgps).postrm

# move the upstream changelogs in the dev packages?
	dh_installchangelogs -p$(p_make) make/ChangeLog
	dh_installchangelogs -p$(p_base) base/ChangeLog
	dh_installchangelogs -p$(p_gui) gui/ChangeLog
	dh_installchangelogs -p$(p_back) $(backend)/ChangeLog
	dh_strip -a -X_d.so -X_d.a
	dh_compress -a -Xusr/share/doc/$(p_xraw)/examples

	: # generate gstep-base.init
	sed 's,ARCHDIR,$(lib_dir),' \
	    debian/gstep-base.init.in > debian/$(p_base).init
	dh_installinit -p$(p_base) --init-script=GNUstep

#	install -m 644 debian/gdomap_addresses $(d_base)/etc/
#	dh_installcron -a

	dh_fixperms -a
	chmod 4755 $(d_base)/$(GS)/Tools/$(lib_dir)/gdomap
	dh_suidregister -a
#	dh_makeshlibs -a

	: # generate shlibs files
	echo 'libgnustep-base $(sov_base) $(p_base) (>= $(v_src))' \
		> debian/$(p_base).shlibs
	echo 'libgnustep-gui $(sov_gui) $(p_gui) (>= $(v_src))' \
		> debian/$(p_gui).shlibs
	echo 'libgmodel $(sov_model) $(p_gui) (>= $(v_src))' \
		>> debian/$(p_gui).shlibs
	echo 'libgnustep-$(backend) $(sov_back) $(p_back) (>= $(v_src))' \
		> debian/$(p_back).shlibs
ifeq ($(debug),yes)
	echo 'libgnustep-base_d $(sov_base) $(p_base_dbg) (>= $(v_src))' \
		> debian/$(p_base_dbg).shlibs
	echo 'libgnustep-gui_d $(sov_gui) $(p_gui_dbg) (>= $(v_src))' \
		> debian/$(p_gui_dbg).shlibs
	echo 'libgmodel_d $(sov_model) $(p_gui_dbg) (>= $(v_src))' \
		>> debian/$(p_gui_dbg).shlibs
	echo 'libgnustep-$(backend)_d $(sov_back) $(p_back_dbg) (>= $(v_src))' \
		> debian/$(p_back_dbg).shlibs
endif

	awk '{print $$1, $$2}' debian/*.shlibs > debian/shlibs.local
	LD_LIBRARY_PATH=`echo '$(foreach pkg, $(d_base) $(d_gui) $(d_xraw),$(PWD)/$(pkg)/$(GS)/Libraries/$(combo_dir))' | tr ' ' ':'`; \
	    export LD_LIBRARY_PATH; \
	    dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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

patch_dir = debian/patches
debian_patches = make-verbose

apply-patches: patched-stamp
reverse-patches:
	for stamp in none `ls -1t patched-*`; do \
	  case "$$stamp" in none|patched-stamp) continue; esac; \
	  patch=`echo $$stamp | sed -e 's/patched-//'`; \
	  echo "trying to revert patch $$patch ..."; \
	  if [ -x $(patch_dir)/$$patch.dpatch ]; then true; else \
	    chmod +x $(patch_dir)/$$patch.dpatch; fi; \
	  if $(patch_dir)/$$patch.dpatch -unpatch; then \
	    echo "reverted $$patch patch."; \
	    rm -f $$stamp; \
	  else \
	    echo "error in reverting $$patch patch."; \
	    exit 1; \
	  fi; \
	done
	rm -f patched-stamp

patched-%: $(patch_dir)/%.dpatch
	if [ -x $< ]; then true; else chmod +x $<; fi
	$< -patch
	echo "$* patches applied." > $@

patched-stamp: $(foreach p,$(debian_patches),patched-$(p))
	echo -e "Patches applied in the Debian version of GNUstep:" > pxxx
	for i in $(debian_patches) none; do \
	  case "$$i" in none) continue; esac; \
	  echo -e "\n$$i:" >> pxxx; \
	  sed -n 's/^# *DP: */  /p' debian/patches/$$i.dpatch >> pxxx; \
	done
	mv -f pxxx patched-stamp
