#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# 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)

CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS) -Wall
CFLAGS   := $(shell dpkg-buildflags --get CFLAGS) -Wall
CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) -Wall
LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CXXFLAGS += -O0
else
	CXXFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# Backup files that get overwriten so that the clean target can restore
# them.
BACKUP_FILES = \
    README.FAQ \
    README.configure \
    doc/rxvt.1.man.in \
    doc/rxvt.1.pod \
    doc/rxvt.7.man.in \
    doc/rxvt.7.pod \
    doc/rxvt.1.man.in \
    doc/rxvtc.1.pod \
    src/Makefile.in \
    src/perl/selection-popup \
    src/perl/tabbed \
    src/rxvtfont.C \
    src/rxvttoolkit.C
BACKED_UP_FILES = $(BACKUP_FILES:%=%.debian-backup)

cfgcommon = \
    --host=$(DEB_HOST_GNU_TYPE) \
    --build=$(DEB_BUILD_GNU_TYPE) \
    --prefix=/usr \
    --mandir=\$${prefix}/share/man \
    --infodir=\$${prefix}/share/info \
    --enable-keepscrolling \
    --enable-selectionscrolling \
    --enable-pointer-blank \
    --enable-utmp \
    --enable-wtmp \
    --enable-warnings \
    --enable-lastlog

flagscommon = \
    CPPFLAGS="$(CPPFLAGS)" \
    CFLAGS="$(CFLAGS)" \
    CXXFLAGS="$(CXXFLAGS)" \
    LDFLAGS="$(LDFLAGS)"

config.status: | $(BACKUP_FILES) configure
	dh_autotools-dev_updateconfig

conf-unicode:
	$(flagscommon) ./configure $(cfgcommon) \
	    --enable-unicode3 \
	    --enable-combining \
	    --enable-xft \
	    --enable-font-styles \
	    --enable-pixbuf \
	    --enable-transparency \
	    --enable-fading \
	    --enable-rxvt-scroll \
	    --enable-next-scroll \
	    --enable-xterm-scroll \
	    --enable-perl \
	    --enable-xim \
	    --enable-iso14755 \
	    --enable-mousewheel \
	    --enable-slipwheeling \
	    --enable-smart-resize \
	    --enable-startup-notification \
	    --with-term=rxvt-unicode

conf-unicode-256color:
	$(flagscommon) ./configure $(cfgcommon) \
	    --enable-unicode3 \
	    --enable-combining \
	    --enable-xft \
	    --enable-font-styles \
	    --enable-256-color \
	    --enable-pixbuf \
	    --enable-transparency \
	    --enable-fading \
	    --enable-rxvt-scroll \
	    --enable-next-scroll \
	    --enable-xterm-scroll \
	    --enable-perl \
	    --enable-xim \
	    --enable-iso14755 \
	    --enable-mousewheel \
	    --enable-slipwheeling \
	    --enable-smart-resize \
	    --enable-startup-notification \
	    --with-term=rxvt-unicode-256color

conf-unicode-lite:
	$(flagscommon) ./configure $(cfgcommon) \
	    --disable-xft \
	    --disable-pixbuf\
	    --disable-perl \
	    --disable-iso14755 \
	    --disable-startup-notification \
	    --with-term=rxvt-unicode

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: config.status
	dh_testdir

	mkdir -p tmp
	dh_installdirs -A usr/bin etc/X11/app-defaults usr/share/pixmaps /usr/share/applications

	set -e; \
	for x in unicode unicode-256color unicode-lite; do \
		cp -f debian/rxvt-unicode-base.preinst debian/rxvt-$$x.preinst; \
		cp -f debian/rxvt-unicode-base.postinst debian/rxvt-$$x.postinst; \
		cp -f debian/rxvt-unicode-base.prerm debian/rxvt-$$x.prerm; \
		cp -f debian/rxvt-unicode-base.docs debian/rxvt-$$x.docs; \
		y=$(CURDIR)/debian/rxvt-$$x ; z=$(CURDIR)/tmp/$$x; \
		$(MAKE) $(MFLAGS) -f debian/rules conf-$$x && \
			perl -pe s,..tag.CXX,, -i src/Makefile; \
		if expr $(DEB_HOST_GNU_TYPE) : 'alpha-linux-gnu\|sparc-linux-gnu' > /dev/null; then \
			echo "#define EV_USE_EPOLL 0" >> config.h; \
		fi; \
		$(MAKE); \
		mkdir -p tmp/$$x; \
		$(MAKE) -C doc clean alldoc; \
		TERMINFO=$$y/etc/terminfo $(MAKE) DESTDIR=$$y install; \
		ln -sf urxvt $$y/usr/bin/rxvt-unicode; \
		ln -sf urxvt.1.gz $$y/usr/share/man/man1/rxvt-unicode.1.gz; \
		chrpath -d $$y/usr/bin/*; \
		install -m644 debian/*.xpm $$y/usr/share/pixmaps; \
		install -m644 debian/*.desktop $$y/usr/share/applications; \
		install -m755 debian/urxvtcd $$y/usr/bin; \
		install -m644 debian/urxvtcd.1 $$y/usr/share/man/man1; \
		gzip -9 $$y/usr/share/man/man1/urxvtcd.1; \
		ln -sf urxvt.xpm $$y/usr/share/pixmaps/urxvt_32x32.xpm; \
		cp debian/app-defaults $$y/etc/X11/app-defaults/URxvt; \
	done

	# Don't install empty directories
	rmdir debian/rxvt-unicode-ml/usr/bin
	rmdir debian/rxvt-unicode-ml/usr/share/applications
	rmdir debian/rxvt-unicode-ml/usr/share/pixmaps
	rm -fr debian/rxvt-unicode-ml/etc/

	touch build-stamp

clean: restore
	dh_testdir
	dh_testroot

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	[ ! -f Makefile ] || $(MAKE) -C doc realclean
	rm -f config.log
	dh_autotools-dev_restoreconfig
	# these will be fixed shortly
	rm -f src/rxvtlib.h
	rm -f doc/Makefile

	# Remove various extra files we create during build
	set -e; \
	for x in unicode unicode-256color unicode-lite; do \
	    rm -f debian/rxvt-$$x.preinst; \
	    rm -f debian/rxvt-$$x.postinst; \
	    rm -f debian/rxvt-$$x.prerm; \
	    rm -f debian/rxvt-$$x.docs; \
	done

	rm -f doc/*.txt.in
	rm -f doc/README.*.in
	rm -f doc/pod2*.tmp
	rm -f doc/rxvt.7.tbl
	rm -f mantmp
	rm -fr tmp

	dh_clean build-stamp

# XXX: rewrite all of this and use it; it is untouched.
install: build
#	dh_testdir
#	dh_testroot
#	dh_clean -k
#	dh_installdirs -A usr/bin etc/X11/app-defaults usr/share/pixmaps etc/terminfo/r
#	$(MAKE) install prefix=$(CURDIR)/debian/rxvt-unicode/usr
#	cd debian/rxvt-unicode/usr/share/man/man1 && mv rxvt.1 rxvt-unicode.1 && cp $(CURDIR)/debian/rxvtc.1 . && ln -sf rxvtc.1.gz rxvtd.1.gz
#	cd $(CURDIR)/debian/rxvt-unicode/usr/bin && rm -f *-1.* && mv rxvt rxvt-unicode && chrpath -d *
# and the icon
# now clone to -lite and -ml	
#	set -e ; for x in code code-lite code-ml ; do \
#		y=debian/rxvt-uni$$x ; z=tmp/$$x ;\
#			#fixme VVV
#		cp -f debian/rxvt-unicode-base.postinst debian/rxvt-uni$$x.postinst ;\
#		cp -f debian/rxvt-unicode-base.prerm debian/rxvt-uni$$x.prerm ;\
#		cp -af $$z/rxvt $$y/usr/bin/urxvt ;\
#		cp -af $$z/rxvtd $$y/usr/bin/urxvtd ;\
#		cp -af $$z/rxvtc $$y/usr/bin/urxvtc ;\
#		ln -sf urxvt $$y/usr/bin/rxvt-unicode ;\
#		chrpath -d $$y/usr/bin/* ;\
#		dh_installman -prxvt-uni$$x $$z/urxvt.1 $$z/urxvt.7 debian/urxvtc.1 ;\
#		ln -sf urxvtc.1.gz $$y/usr/share/man/man1/urxvtd.1.gz ;\
#		ln -sf urxvt.1.gz $$y/usr/share/man/man1/rxvt-unicode.1.gz ;\
#		install -m644 debian/*.xpm $$y/usr/share/pixmaps/ ;\
#		cp -lf $$y/usr/share/pixmaps/urxvt.xpm $$y/usr/share/pixmaps/urxvt_32x32.xpm ;\
#		TERMINFO=$$y/etc/terminfo tic doc/etc/rxvt-unicode.terminfo ;\
#		cp debian/app-defaults $$y/etc/X11/app-defaults/URxvt$${x#code} ;\
#	done
	# extra icons are wasted space in -lite
	# rm debian/rxvt-unicode-lite/usr/share/pixmaps/*_*

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -A Changes
	dh_installdocs
	dh_installexamples
	# fix the perms
#	chmod -x debian/*/usr/share/doc/*/examples/menu/*
	rm -rf debian/*/etc/terminfo
#	dh_install
	dh_installmenu
#	dh_installdebconf
#	dh_installinfo
#	dh_link
	dh_lintian
	dh_strip
	dh_compress
	dh_fixperms
	for x in unicode unicode-256color unicode-lite; do \
	    y=$(CURDIR)/debian/rxvt-$$x ;\
	    chgrp utmp $$y/usr/bin/urxvt $$y/usr/bin/urxvtd;\
	    chmod g+s $$y/usr/bin/urxvt $$y/usr/bin/urxvtd;\
	done
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

copyright:
	# Check to see if there's any changes in copyright
	[ -d .git ] && git grep Copyright | \
	    grep -v 'debian/' | \
	    sed -e 's/.*: \* //g;s/.*:# //g;s/(c)/(C)/g' | \
	    sort -k4,3 | uniq

%.debian-backup:
	@# Move the matching stem, foo, to foo.debian-backup.
	mv -f $* $@

restore:
	for file in $(BACKUP_FILES); do \
            [ ! -f $${file}.debian-backup ] || mv $${file}.debian-backup $${file}; \
	done
.PHONY: build clean binary-indep binary-arch binary install copyright restore
