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

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
cmakeargs = -DDOCDIR=$(DOCDIR) -DHTMLDIR=$(DOCDIR)/html -DICEHELPIDX=/usr/share/doc/icewm-common/html/icewm.html -DCFGDIR=/etc/X11/icewm -DCMAKE_INSTALL_PREFIX=/usr -DVERSION=$(ORIGVERSION) -DCMAKE_VERBOSE_MAKEFILE=ON
#-DEXTRA_LIBS="-lsupc++" 
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

%:
	dh $@ --parallel -Scmake

# 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,-[^-]*$$,,' )
ORIGTAG := upstream/$(shell echo $(ORIGVERSION) | sed -e 's,~,,g' )

# dh_auto_build is no help here...
PAROPT=$(shell echo $$DEB_BUILD_OPTIONS | sed -e 's,.*parallel=\([0-9]\).*,-j\1,')

#build_lite:
#	$(MAKE) $(PAROPT) -C $(BDIR)-lite
#
#build_normal:
#	$(MAKE) $(PAROPT) -C $(BDIR)-normal

#config_lite:
#	mkdir -p $(BDIR)-lite
#	# this fails on prefering cmake :-( dh_auto_configure --builddirectory=$(BDIR)-lite -- 
#	cd $(BDIR)-lite && cmake .. -DCMAKE_BUILD_TYPE=None $(cmakeargs) -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 -DICESOUND=off
#
#config_normal:
#	mkdir -p $(BDIR)-normal
#	cd $(BDIR)-normal && cmake .. -DCMAKE_BUILD_TYPE=None $(cmakeargs) -DCONFIG_XRANDR=on -DCONFIG_GUIEVENTS=on

override_dh_auto_configure:
	dh_auto_configure -- $(cmakeargs)

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

#override_dh_auto_build:
#	dh_auto_build
#	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
	cd debian && for x in -lite -experimental ; do cp -lf icewm.postinst icewm$$x.postinst ; cp -lf icewm.prerm icewm$$x.prerm ; done
	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 icewmbg icesh icesound ; do \
					for y in lite experimental ; do \
					ln -s $$x debian/icewm-$$y/usr/bin/$$x-$$y ; done ; done
	# Install undocumented manpages
	for v in "" -experimental ; do \
		mkdir -p debian/icewm$$v/usr/share/man/man1 ; \
			for f in icewm icehelp icewmbg icewmhint icesh 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
	mkdir -p debian/icewm-lite/usr/share/man/man1
	cp -a debian/icewm-lite.1 debian/icewm-lite/usr/share/man/man1
	for f in icewm icehelp icewmbg icewmhint icesh icewm-session sound; do \
					ln -sf icewm-lite.1.gz debian/icewm-lite/usr/share/man/man1/$$f-lite.1.gz ; \
					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	
	sed -e 's,^.link.*fonts.googleapis.*,,' -i "$(ICEWM_COMMON)/usr/share/doc/icewm-common/html/"*.html

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-4-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-4-BRANCH | head -n1 | cut -f1 -d" "` && echo "Expected upstream ref: $$lc (on failure run update-debian-mod target)" && grep -m1 ^commit ChangeLog | grep $$lc
	git tag $(ORIGTAG) icewm-1-4-BRANCH
	git archive $(ORIGTAG) --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-4-BRANCH

update-debian-mod:
	grep debian .git/HEAD
	oldstate=$$(head ChangeLog | grep ^commit | head -1 | cut -f2 -d ' ') && git log $$oldstate..icewm-1-4-BRANCH --stat=76 -M -C |fmt -sct -w80 > ChangeLog.debian
	mv ChangeLog ChangeLog.upstream
	cat ChangeLog.debian ChangeLog.upstream > ChangeLog
	rm ChangeLog.debian ChangeLog.upstream
	git commit ChangeLog -m "Updating ChangeLog for a new Debian snapshot"
