#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules for icewm

%:
	dh $@ --parallel

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk
-include /usr/share/quilt/quilt.make

CXXFLAGS += $(CPPFLAGS) -DXTERMCMD=x-terminal-emulator -Wall -Wextra
ifeq (linux,$(DEB_TARGET_ARCH_OS))
	 CXXFLAGS += -DUSE_SIGNALFD
endif

ICEWM_COMMON=$(CURDIR)/debian/icewm-common
ICEWM=$(CURDIR)/debian/icewm
ICEWM_LITE=$(CURDIR)/debian/icewm-lite
ICEWM_EXP=$(CURDIR)/debian/icewm-experimental
DOCDIR=/usr/share/doc/icewm-common
BDIR=builddir
ICONS=$(CURDIR)/debian/icons
FDO=$(CURDIR)/debian/fdo
CUSTOM=$(CURDIR)/debian/custom

# DEBVERSION=$(shell dpkg-parsechangelog -SVersion)
# that one also works with wheezy
DEBVERSION := $(shell dpkg-parsechangelog |grep ^Version | sed -e s,Version:.,,)
ORIGVERSION := $(shell echo $(DEBVERSION) | sed -e 's,-[^-]*$$,,' )

cmakecmd = cmake $(CURDIR) -DICEHELPIDX=/usr/share/doc/icewm-common/html/icewm.html -DCFGDIR=/etc/X11/icewm -DCMAKE_INSTALL_PREFIX=/usr -DVERSION=$(ORIGVERSION) -DDOCDIR=$(DOCDIR) -DEXTRA_LIBS="-lsupc++" -DCMAKE_VERBOSE_MAKEFILE=ON

config_lite:
	mkdir -p $(BDIR)-lite
	cd $(BDIR)-lite && $(cmakecmd) -DLITE=on -DEXEEXT=-lite -DCONFIG_XFREETYPE=off -DCONFIG_COREFONTS=on -DXINERAMA=on -DCONFIG_XRANDR=off -DCONFIG_TASKBAR=off -DCONFIG_FDO_MENUS=off -DSKIP_TRANSLATIONS=on

config_normal:
	mkdir -p $(BDIR)-normal
	cd $(BDIR)-normal && $(cmakecmd) -DCONFIG_XRANDR=on -DCONFIG_GUIEVENTS=on

override_dh_auto_configure:
	$(MAKE) -f debian/rules -j2 config_normal config_lite

override_dh_auto_build:
	dh_auto_build -- -C $(BDIR)-lite
	dh_auto_build -- -C $(BDIR)-normal
	cd debian && for x in -lite -experimental ; do cp -lf icewm.postinst icewm$$x.postinst ; cp -lf icewm.prerm icewm$$x.prerm ; done

override_dh_auto_install:
	$(MAKE) -C $(BDIR)-lite install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C $(BDIR)-normal install DESTDIR=$(CURDIR)/debian/tmp

override_dh_install:
	dh_install
	for v in "" -experimental -lite ; do \
		rm -rf debian/icewm$$v/usr/share/doc/icewm$$v ; \
		ln -sf icewm-common debian/icewm$$v/usr/share/doc/icewm$$v ; done
	sed -e 's,icewm-session,icewm-session-lite,g;s,^Name\(.*\),Name\1 [lite],' < lib/icewm-session.desktop > $(ICEWM_LITE)/usr/share/xsessions/IceWM-Lite.desktop
	# XXX: find out why some settings are not gettint defaults if the file is not present, like WorkspaceNames
	#cp $(ICEWM_COMMON)/usr/share/doc/icewm-common/preferences $(ICEWM_COMMON)/usr/share/icewm/
	# Install Debian custom files
	install -m 644 $(ICONS)/mutt*.xpm $(ICEWM_COMMON)/usr/share/icewm/icons
	install -m 644 $(ICONS)/mozilla*.xpm $(ICEWM_COMMON)/usr/share/icewm/icons
	install -m 644 $(ICONS)/icewm*.xpm $(ICEWM_COMMON)/usr/share/icewm/icons
	-mv $(ICEWM_COMMON)/usr/share/icewm/taskbar/icewm.xpm  $(ICEWM_COMMON)/usr/share/icewm/taskbar/icewm.orig.xpm
	install -m 644 $(ICONS)/debian.xpm $(ICEWM_COMMON)/usr/share/icewm/taskbar/icewm.xpm
	mkdir -p $(ICEWM_COMMON)/usr/bin $(ICEWM_COMMON)/etc/X11/icewm
	install -m 644 $(CUSTOM)/toolbar $(CUSTOM)/menu $(ICEWM_COMMON)/usr/share/icewm
	if apt-cache show firefox | grep -q pool.main.f ; then sed -e "s,Iceweasel,Firefox,g;s,iceweasel,firefox,g" -i $(ICEWM_COMMON)/usr/share/icewm/menu $(ICEWM_COMMON)/usr/share/icewm/toolbar ; fi
	install -m 644 debian/README.cfg $(ICEWM_COMMON)/usr/share/icewm/README
	# Extra docs
	mkdir -p "$(ICEWM_COMMON)/usr/share/doc/icewm-common/FAQ"
	cd $(ICEWM_COMMON)/usr/share/doc/icewm-common/FAQ && sgml2html $(CURDIR)/debian/FAQ/source/*.sgml
	sgml2txt $(CURDIR)/debian/FAQ/source/*.sgml && col -bx < IceWM-FAQ.txt > $(ICEWM_COMMON)/usr/share/doc/icewm-common/FAQ/IceWM-FAQ.txt
	# making -experimental compat symlinks
	for x in icewm-session icewm icewmhint icehelp icewmtray icewmbg icesh icesound ; do ln -s $$x $(ICEWM_EXP)/usr/bin/$$x-experimental ; done
	# Install undocumented manpages
	for v in "" -experimental -lite ; do \
		mkdir -p debian/icewm$$v/usr/share/man/man1 ; \
			for f in icewm icehelp icewmbg icewmhint icesh icewmtray icewm-session ; do \
				test "$$f$$v.1.gz" = icewm.1.gz || ln -s icewm.1.gz debian/icewm$$v/usr/share/man/man1/$$f$$v.1.gz ; \
		done ; \
	done
	# only where needed
	ln -sf icewm.1.gz debian/icewm/usr/share/man/man1/icesound.1.gz
	ln -sf icewm.1.gz debian/icewm-experimental/usr/share/man/man1/icesound-experimental.1.gz
	ln -sf icewm.1.gz debian/icewm-common/usr/share/man/man1/icewm-menu-fdo.1.gz
	cd debian ; for x in icewm icewm-lite icewm-experimental ; do mkdir -p $$x/usr/share/lintian/overrides ; cp $$x.lintian $$x/usr/share/lintian/overrides/$$x ; done	

override_dh_auto_clean:
	rm -rf $(BDIR)-lite $(BDIR)-normal IceWM-FAQ.txt configure.in
	# the variants are just symlinks
	rm -f debian/icewm-*.postinst debian/icewm-*.prerm

override_dh_installchangelogs:
	dh_installchangelogs -picewm-common CHANGES

override_dh_fixperms:
	dh_fixperms
	chmod a+rx,u+w "$(ICEWM_COMMON)/usr/share/doc/icewm-common/examples/startup"

override_dh_compress:
	dh_compress

cleancruft:
	git clean -fdx -e .cproject -e .project -e .settings
	git checkout HEAD -- README.md configure.ac configure.in ChangeLog

set-orig-snapshot:
	lc=$$(git reflog icewm-1-3-BRANCH | head -n1 | cut -f1 -d" ") && dd=$$(date +\%Y\%m\%d) && ov=$$(dpkg-parsechangelog -S Version) && pfx=$$(echo $$ov | sed -e 's,mod.*,mod,') && echo $$lc $$dd $$ov $$pfx && dch --newversion $$pfx+$$dd+$$lc-1

# builds a snapshot of the upstream branch after some plausibility checks
get-orig-source:
	! test -e ../icewm_$(ORIGVERSION).orig.tar.xz || (echo Target already exists ; exit 1)
	lc=`git reflog icewm-1-3-BRANCH | head -n1 | cut -f1 -d" "` && echo "Expected upstream ref: $$lc" && grep -m1 ^commit ChangeLog | grep $$lc
	git tag upstream/$(ORIGVERSION) icewm-1-3-BRANCH
	git archive upstream/$(ORIGVERSION) --prefix icewm-$(ORIGVERSION)/ | xz -9 > ../icewm_$(ORIGVERSION).orig.tar.xz
	test -d ../tarballs && ln ../icewm_$(ORIGVERSION).orig.tar.xz ../tarballs || true
	pristine-tar commit ../icewm_$(ORIGVERSION).orig.tar.xz icewm-1-3-BRANCH

update-debian-mod:
	grep debian .git/HEAD
	git log icewm-1-3-BRANCH -n 200 --decorate --stat=76 -M -C|fmt -sct -w80 > ChangeLog
	git commit ChangeLog -m "Updating ChangeLog for a new Debian snapshot"
