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

SHELL                   := /bin/bash
DH_AUTO_OPTIONS         := -v -Sautoconf
DESTDIR                 := $(CURDIR)/debian/tmp
CFLAGS                  := $(shell dpkg-buildflags --get CFLAGS)
# The build system doesn't use CPPFLAGS, pass them to CFLAGS to enable the
# missing (hardening) flags.
CFLAGS                  += $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS                 := -Wl,-z,defs -Wl,--as-needed
LDFLAGS                 += $(shell dpkg-buildflags --get LDFLAGS)

BUILD_DIR               := DEB-build-tree
STAMP_DIR               := $(BUILD_DIR)/stamps
BUILD_TREE              := $(BUILD_DIR)/build-dynamic
BUILD_TREE_STATIC       := $(BUILD_DIR)/build-static

GEN_DH_FILES            := debian/packages.d/gen_debhelper_files.pl
DHINSTALL_LIBDIR	:= usr/lib/$(shell dpkg-architecture -qDEB_HOST_MULTIARCH 2>/dev/null || :)
INSTALL_PROGRAM         := install -p -m 755

pafterstep              := afterstep
pafterstep_data         := afterstep-data
pafterstep_dbg          := afterstep-dbg
plibafterstep           := libafterstep1
plibafterimage          := libafterimage0
plibafterimage_dev      := libafterimage-dev

export DEB_VERSION      := $(shell dpkg-parsechangelog | sed -ne 's/^Version: *//p')

ifneq (,$(findstring dbg,$(DEB_VERSION)))
    CONF_ADD += --enable-gdb
endif

define common-configure
    dh_auto_configure $(DH_AUTO_OPTIONS) --   \
    --with-imageloader="display -window root" \
    --with-helpcommand="x-terminal-emulator -e man" \
    --disable-different-looknfeels \
    --enable-dependency-tracking \
    --enable-savewindows \
    --enable-fixeditems \
    --with-desktops=4 \
    --enable-xinerama \
    --with-deskgeometry=2x2 \
    --disable-mmx-optimization \
    --with-gif \
    --with-svg \
    --with-dbus1 \
    --enable-i18n \
    $(CONF_ADD) $1
endef

define common-install
    $(MAKE) $1 DESTDIR=${DESTDIR} \
    LDCONFIG=/bin/true \
    CP="cp -a" \
    INSTALL_PROGRAM="$(INSTALL_PROGRAM)" \
    INSTALL_DATA="install -p -m 644" \
    INSTALL_SCRIPT="install -p -m 755" \
    INSTALL_HEADER="install -p -m 644" \
    INSTALL_LIB="install -p -m 644" \
    INSTALL="install -p" \
    AFTER_DOC_DIR=${DESTDIR}/usr/share/doc/$(pafterstep_data)
endef

build: export MISSING_BUILD_DEPS := $(shell dpkg-checkbuilddeps 2>&1)
binary binary-arch binary-indep build build-arch build-indep: $(STAMP_DIR)/prepare.stamp
	dh ${@}

clean:
	dh ${@}

ifneq ($(shell dh_listpackages),$(shell dh_listpackages -i))  # arch:any packages are built
override_dh_auto_build:       $(STAMP_DIR)/build-arch.stamp
override_dh_auto_configure:   $(STAMP_DIR)/configure-arch.stamp
override_dh_auto_install:     install-arch-files
endif

ifneq ($(shell dh_listpackages),$(shell dh_listpackages -a)) # arch:all packages are built
  ifneq (,$(MISSING_BUILD_DEPS))
    $(warning $(MISSING_BUILD_DEPS))
    $(warning Postponing build of architecture independent files until binary or binary-indep target is called)
  else
override_dh_auto_build:       $(STAMP_DIR)/build-indep.stamp
override_dh_auto_configure:   $(STAMP_DIR)/configure-indep.stamp
  endif
override_dh_auto_install:     install-indep-files
endif

override_dh_auto_clean:
	rm -rf  $(STAMP_DIR)
	rm -rf  $(BUILD_DIR)
	perl $(GEN_DH_FILES) clean $(BUILD_TREE)
	dh_autotools-dev_restoreconfig


$(STAMP_DIR)/configure-indep.stamp: $(STAMP_DIR)/configure-dynamic.stamp		
	touch "$@"

$(STAMP_DIR)/configure-arch.stamp: $(STAMP_DIR)/configure-dynamic.stamp	 $(STAMP_DIR)/configure-static.stamp
	touch "$@"

$(STAMP_DIR)/prepare.stamp:
	rm -f .pc/.*unapply # see bug#649521
	dh_testdir
	dh_autotools-dev_updateconfig
	
	@echo
	@echo "***** Creating build directory for dynamic libs build  *****"
	@echo
	mkdir -p $(STAMP_DIR) $(BUILD_TREE)
	cp -al [!dD]*  d[!e]*  $(BUILD_TREE)
	ln -s $(CURDIR)/debian $(BUILD_TREE)/debian
	
	@echo
	@echo "***** Creating build directory for static libs build  *****"
	@echo
	cp -al $(BUILD_TREE) $(BUILD_TREE_STATIC)
	
	touch "$@"


$(STAMP_DIR)/configure-static.stamp: $(STAMP_DIR)/prepare.stamp
	
	@echo
	@echo "***** Configure static  *****"
	@echo
	cd $(BUILD_TREE_STATIC) && \
	    CFLAGS="$(CFLAGS)" LDCONFIG="/bin/true" \
	    LDFLAGS="$(LDFLAGS)"  \
	    $(call common-configure, --enable-staticlibs)
	
	touch "$@"


$(STAMP_DIR)/configure-dynamic.stamp: $(STAMP_DIR)/prepare.stamp
	
	@echo
	@echo "***** Configure dynamic  *****"
	@echo
	cd $(BUILD_TREE) && \
	    CFLAGS="$(CFLAGS) -fPIC" LDCONFIG="/bin/true" \
	    LDFLAGS="$(LDFLAGS) -rdynamic"  \
	    $(call common-configure, --disable-staticlibs)
	
	touch "$@"


$(STAMP_DIR)/build-indep.stamp: $(STAMP_DIR)/configure-indep.stamp
	@echo
	@echo "***** Make dynamic (indep)  *****"
	@echo
	cd $(BUILD_TREE) && \
	    dh_auto_build $(DH_AUTO_OPTIONS) --parallel -- -C src/ASDocGen doc_html
	
	touch "$@"


$(STAMP_DIR)/build-arch.stamp: $(STAMP_DIR)/configure-arch.stamp
	@echo
	@echo "***** Make static (arch)  *****"
	@echo
	cd $(BUILD_TREE_STATIC) && \
	    dh_auto_build $(DH_AUTO_OPTIONS) --parallel
	
	@echo
	@echo "***** Make dynamic (arch)  *****"
	@echo
	cd $(BUILD_TREE) && \
	    dh_auto_build $(DH_AUTO_OPTIONS) --parallel
	
	touch "$@"

$(STAMP_DIR)/packages.stamp: $(patsubst %,debian/packages.d/%.in,$(shell dh_listpackages)) $(GEN_DH_FILES)
	mkdir -p $(STAMP_DIR)
	perl $(GEN_DH_FILES) generate $(BUILD_TREE) $(DHINSTALL_LIBDIR)
	touch "$@"


install-common-files: $(STAMP_DIR)/build-indep.stamp $(STAMP_DIR)/packages.stamp
	@echo
	@echo "***** Install dynamic (common) *****"
	@echo
	cd $(BUILD_TREE)/src/ASDocGen && \
	    $(call common-install,install.doc_html)
	
	mkdir -p ${DESTDIR}/usr/share/doc/$(plibafterimage_dev)
	mv ${DESTDIR}/usr/share/doc/$(pafterstep_data)/html/API \
	    ${DESTDIR}/usr/share/doc/$(plibafterimage_dev)/html


install-indep-files: install-common-files  $(STAMP_DIR)/build-indep.stamp $(STAMP_DIR)/packages.stamp
	@echo
	@echo "***** Install dynamic (indep) *****"
	@echo
	cd $(BUILD_TREE) && \
	    $(call common-install,install.share install.data install.menu)

####	rm -rf ${DESTDIR}/usr/share/afterstep/scripts
	
	mkdir -p -m755 ${DESTDIR}/usr/share/pixmaps
	convert -scale 32x32\! ${DESTDIR}/usr/share/afterstep/desktop/icons/logos/AfterStep \
	    ${DESTDIR}/usr/share/pixmaps/AfterStep.xpm

install-arch-files: install-common-files  $(STAMP_DIR)/build-arch.stamp
	@echo
	@echo "***** Install static (arch)  *****"
	@echo
	cd $(BUILD_TREE_STATIC) && \
	    $(call common-install,install.lib)
	
	@echo
	@echo "***** Install dynamic (arch) *****"
	@echo
	cd $(BUILD_TREE) && \
	    $(call common-install,install.lib install.bin install.script install.man install.desktop install.wmprops)
	
override_dh_installdirs: $(STAMP_DIR)/packages.stamp
	for i in `dh_listpackages`; do \
	    echo $$i; dh_installdirs -v -p$$i -P${DESTDIR}; \
	done
	dh_installdirs

override_dh_installdocs: $(STAMP_DIR)/packages.stamp
	dh_installdocs  --link-doc=$(pafterstep_data) -p$(pafterstep)
	dh_installdocs
	
override_dh_installchangelogs: $(STAMP_DIR)/packages.stamp
	dh_installchangelogs -p$(pafterstep_data) -k $(BUILD_TREE)/ChangeLog
	dh_installchangelogs -p$(plibafterstep)  -p$(pafterstep_dbg)    -k $(BUILD_TREE)/ChangeLog
	dh_installchangelogs -p$(plibafterimage) -p$(plibafterimage_dev)   $(BUILD_TREE)/libAfterImage/ChangeLog
	
override_dh_makeshlibs:
	dh_makeshlibs -p$(plibafterstep) -V
	dh_makeshlibs -p$(plibafterimage) -V
	
override_dh_strip:
	dh_strip --dbg-package=$(pafterstep_dbg)

override_dh_shlibdeps:
	dh_shlibdeps -L$(plibafterstep) -L$(plibafterimage) -l${DESTDIR}/$(DHINSTALL_LIBDIR)

override_dh_compress:
	sh debian/scripts/fix_examples `dh_listpackages`
	sh debian/scripts/fix_conffiles `dh_listpackages`
	rm -f debian/afterstep/etc/menu-methods/afterstep
	rm -f debian/afterstep/usr/share/doc/afterstep-data/copyright
	dh_compress

override_dh_md5sums:
	sh debian/scripts/fix_autogen `dh_listpackages`
	dh_md5sums

.PHONY: clean                                           \
        binary binary-indep binary-arch binary-common   \
        build  build-indep  build-arch                  \
        install-indep-files install-arch-files install-common-files       \
        override_dh_auto_build override_dh_auto_clean override_dh_auto_configure    \
        override_dh_auto_install override_dh_compress override_dh_installchangelogs \
        override_dh_installdirs override_dh_installdocs override_dh_makeshlibs      \
        override_dh_md5sums override_dh_shlibdeps override_dh_strip

.DEFAULT_GOAL := binary
