#!/usr/bin/make -f
# GNU Copyright 1997-2005 Marcelo Magallon <mmagallo@debian.org>

package := wmaker
version := $(shell dpkg-parsechangelog | grep-dctrl -ne -sVersion -FVersion .)

# use '(>= some_version)' whenever version dependencies are needed
# libwraster_deps := (>= 0.90.0-1)

SHELL=/bin/sh

CFLAGS = -g -Wall -DGLOBAL_DEFAULTS_SUBDIR="\\\"GNUstep/Defaults\\\""

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
CFLAGS += -DDEBUG
endif

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
export DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	HOSTSPEC := --build $(DEB_HOST_GNU_TYPE)
else
	HOSTSPEC := --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif



LINGUAS           := $(patsubst po/%.po, %, $(wildcard po/*.po))

XLOCALE           := --disable-locale
MODELOCK          := --enable-modelock

XINERAMA	  := --enable-xinerama

# USERMENU	  := --enable-usermenu

# SHAPE_EXT	  := --disable-shape
# USE_SHM	  := --disable-shm

# USE_XPM	  := --disable-xpm
# USE_PNG	  := --disable-png
# USE_JPEG	  := --disable-jpeg
# USE_GIF	  := --disable-gif
# USE_TIFF	  := --disable-tiff

WMAKER_OPTIONS := $(XLOCALE) $(MODELOCK) $(XINERAMA) \
	$(USERMENU) $(SHAPE_EXT) $(USE_SHM) \
	$(USE_XPM) $(USE_PNG) $(USE_JPEG) $(USE_GIF) $(USE_TIFF) \
	$(HOSTSPEC)

TOPSRCDIR         := $(shell pwd)
DEBTMPDIR         := $(TOPSRCDIR)/debian/wmaker

WMAKER            := wmaker
WMAKER_BD         := $(TOPSRCDIR)/$(WMAKER)
WMAKER_TMP        := $(DEBTMPDIR)

# Be careful with the leading / because some of these values are going
# to be hardcoded into the executables

BASEDIR           := /usr
CONFDIR           := /etc
BINDIR            := $(BASEDIR)/bin
INCLUDEDIR        := $(BASEDIR)/include
SHAREDIR          := $(BASEDIR)/share
DOCDIR            := $(SHAREDIR)/doc
MANDIR            := $(SHAREDIR)/man
MAN1DIR           := $(MANDIR)/man1
MAN8DIR           := $(MANDIR)/man8
LIBDIR            := $(BASEDIR)/lib
PKGLIBDIR         := $(BASEDIR)/lib/WindowMaker
NLSDIR            := $(SHAREDIR)/locale
WMCONFDIR         := $(CONFDIR)/X11/WindowMaker
WMSHAREDIR        := $(SHAREDIR)/WindowMaker
WMDOCDIR          := $(DOCDIR)/wmaker
GNUSTEPDIR        := $(LIBDIR)/GNUstep/System
GNUSTEPCONFDIR    := $(CONFDIR)/GNUstep
DEFAULTSDIR       := $(GNUSTEPCONFDIR)/Defaults
PIXMAPDIR         := $(INCLUDEDIR)/X11/pixmaps
GNUSTEPAPPS       := $(GNUSTEPDIR)/Applications
WPREFSAPPDIR      := $(GNUSTEPAPPS)/WPrefs.app

WPREFSSRCDIR      := $(TOPSRCDIR)/WPrefs.app
WRASTERSRCDIR     := $(TOPSRCDIR)/wrlib
WINGSSRCDIR       := $(TOPSRCDIR)/WINGs

LIBWRASTER        := libwraster3
LIBWRASTER_TMP    := $(DEBTMPDIR)/../$(LIBWRASTER)
LIBWINGS          := libwings-dev
LIBWINGS_TMP      := $(DEBTMPDIR)/../$(LIBWINGS)

RM		  := rm -f
RMDIR		  := rmdir --ignore-fail-on-non-empty

COMMON_OPTIONS    := --prefix=$(BASEDIR)              \
                     --mandir=$(MANDIR)               \
                     --includedir=$(INCLUDEDIR)       \
                     --sysconfdir=$(CONFDIR)          \
                     --datadir=$(SHAREDIR)            \
                     --with-nlsdir=$(NLSDIR)          \
                     --with-pixmapdir=$(PIXMAPDIR)    \
                     --with-gnustepdir=$(GNUSTEPDIR)  \
                     --with-xft

install_file      := install -p    -o root -g root -m 0644
install_prog      := install -p    -o root -g root -m 0755
install_dir       := install -p -d -o root -g root -m 0755

build: build-stamp
build-stamp: build-wmaker-stamp
	touch $@

build-wmaker-stamp: config-wmaker-stamp
	$(RM) $(patsubst %.in,%,$(shell find WindowMaker -name \*.in ! -name Makefile.in))
	test -x libtool || ln -sf $(WMAKER_BD)/libtool .
	$(MAKE) -C $(WMAKER_BD)
	touch $@

config-wmaker-stamp: configure
	dh_testdir
	test -x config.guess || chmod +x config.guess
	test -x config.sub || chmod +x config.sub
	find -name \*.h.in | sed -e 's/\.in$$//' | xargs -r rm -f
	mkdir -p $(WMAKER_BD)
	cd $(WMAKER_BD) && \
	LINGUAS="$(LINGUAS)" $(TOPSRCDIR)/configure $(COMMON_OPTIONS) \
	 $(WMAKER_OPTIONS) CFLAGS="$(CFLAGS)"
	touch $@

clean:
	dh_testdir
	dh_testroot
	$(RM) -r $(WMAKER_BD)
	$(RM) *-stamp
	dh_clean libtool

binary-indep: build
#	dh_testdir -i
#	dh_testroot
#	dh_prep -i

binary-arch: build
	dh_testdir -a
	dh_testroot
	dh_prep -a
	dh_installdirs -a
	debian/rules install-wmaker-stamp
	dh_movefiles -a --sourcedir=debian/wmaker
# Remove cruft
	$(RM) $(DEBTMPDIR)/usr/bin/wkdemenu.pl
# deal with menus
	dh_installmenu -a --noscripts
	$(install_prog) debian/appearance.menu-method \
		$(DEBTMPDIR)/etc/menu-methods/wmappearance
	$(install_file) WindowMaker/appearance.menu \
		$(DEBTMPDIR)/$(WMCONFDIR)/
	$(install_file) WindowMaker/background.menu \
		$(DEBTMPDIR)/$(WMCONFDIR)/
	$(install_file) WindowMaker/wmmacros \
		$(DEBTMPDIR)/$(WMCONFDIR)/
	touch $(DEBTMPDIR)/$(WMCONFDIR)/menu.prehook
	touch $(DEBTMPDIR)/$(WMCONFDIR)/menu.posthook
	$(install_file) debian/wmaker.desktop $(DEBTMPDIR)/usr/share/xsessions
	dh_installdocs -a
	$(install_file) $(WINGSSRCDIR)/README \
		$(LIBWINGS_TMP)/$(DOCDIR)/$(LIBWINGS)/README.WINGs
	$(install_file) $(WRASTERSRCDIR)/README \
		$(LIBWRASTER_TMP)/$(DOCDIR)/$(LIBWRASTER)/README.wrlib
	$(RM) $(DEBTMPDIR)/$(MAN1DIR)/convertstyle*
	dh_installman -a
	dh_installchangelogs -a ChangeLog
	dh_link -a
	dh_strip -a --dbg-package=wmaker-dbg
	dh_lintian
	dh_compress -a
	dh_fixperms -a
	cd debian && find -name \*.la -exec chmod a-X {} \;
	#dh_makeshlibs -p$(LIBWRASTER) -V '$(LIBWRASTER) $(libwraster_deps)'
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	find debian -type d -empty -print0 | xargs -0r rm -rf
	$(RMDIR) $(DEBTMPDIR)/$(INCLUDEDIR)
	dh_builddeb -a

binary:	binary-indep binary-arch

install-wmaker-stamp: DH_OPTIONS=-pwmaker
install-wmaker-stamp: build-wmaker-stamp
	dh_testdir
	dh_installdirs
	$(MAKE) -C $(WMAKER_BD) install DESTDIR=$(DEBTMPDIR)/
# Ugly hack, FIXME
	mv $(DEBTMPDIR)/etc/WindowMaker/* $(DEBTMPDIR)/$(DEFAULTSDIR)/

# Now begin fixing stuff

# First, provide a wrapper to compensate for Window Maker's funny first
# start up requierements
	mv $(DEBTMPDIR)/$(BINDIR)/wmaker $(DEBTMPDIR)/$(PKGLIBDIR)/WindowMaker
	$(install_prog) debian/wmaker.sh $(DEBTMPDIR)/$(BINDIR)/wmaker

	-mv $(DEBTMPDIR)/$(BINDIR)/convertfonts $(DEBTMPDIR)/$(PKGLIBDIR)/

# place wm-oldmenu2new under a more appropiate directory
	-mv $(DEBTMPDIR)/$(BINDIR)/wm-oldmenu2new $(DEBTMPDIR)/$(WMDOCDIR)

# the next is stricly not necessary, because there's a symlink in place,
# but I don't want to abuse it. Some dumb program uses that path and is a
# PITA to modify it)
	perl -pi -e 's:/$(WPREFSAPPDIR)/WPrefs:/$(BINDIR)/WPrefs:' \
	   $(DEBTMPDIR)/$(DEFAULTSDIR)/WMState

# Ditch this!
	$(RM) $(DEBTMPDIR)/$(BINDIR)/wmaker.inst
	$(RM) $(DEBTMPDIR)/$(BINDIR)/wmsetup
# this copyright is located in /$(DOCDIR)/wmaker
	$(RM) $(DEBTMPDIR)/$(WMCONFDIR)/Copyright

# Fix permissions
	chmod +x $(DEBTMPDIR)/usr/share/WindowMaker/autostart.sh
	chmod +x $(DEBTMPDIR)/usr/share/WindowMaker/exitscript.sh

# Install our transition script
	$(install_prog) debian/upgrade-windowmaker-defaults \
	  $(DEBTMPDIR)/usr/sbin/upgrade-windowmaker-defaults

# This file qualifies as "unpatchable"
	cp debian/WMWindowAttributes $(DEBTMPDIR)/$(DEFAULTSDIR)/
# Need this file for start up
	echo '"menu.hook"' > $(DEBTMPDIR)/$(DEFAULTSDIR)/WMRootMenu

# the plethora of readmes
	-cp $(TOPSRCDIR)/po/README \
		$(DEBTMPDIR)/$(WMDOCDIR)/README.po
	-cp $(TOPSRCDIR)/README.definable-cursor \
		$(DEBTMPDIR)/$(WMDOCDIR)/README.definable-cursor
	-cp $(WPREFSSRCDIR)/README \
		$(DEBTMPDIR)/$(WMDOCDIR)/README.WPrefs
	-cp $(WPREFSSRCDIR)/po/README \
		$(DEBTMPDIR)/$(WMDOCDIR)/README.WPrefs.po

# Copy in the Debian theme
	cp debian/Debian.theme $(DEBTMPDIR)/$(WMSHAREDIR)/Themes/Debian
	uudecode -o $(DEBTMPDIR)/$(WMSHAREDIR)/Backgrounds/debian.tiff \
	    debian/debian.tiff.uu
# This has the new default Debian theme settings.
	cp debian/WindowMaker.default $(DEBTMPDIR)/$(DEFAULTSDIR)/WindowMaker

# libwraster is compiled with tiff support, we don't need the xpm version
	$(RM) -r $(DEBTMPDIR)/$(WPREFSAPPDIR)/xpm
	$(RM) $(patsubst %.tiff,%.xpm,$(wildcard $(DEBTMPDIR)/$(WMSHAREDIR)/Icons/*.tiff))

# Fix the get-*-flags scripts
	perl -pi -e '/^WCFLAGS/ && s:=.*:="-I/usr/X11R6/include":; /^WLFLAGS/ && s:=.*:="-L/usr/X11R6/lib":;' `find $(DEBTMPDIR) -name get-*-flags`
	perl -pi -e '/^WLIBS/ && s:=.*:="-lwraster":' `find $(DEBTMPDIR) -name get-wraster-flags`
	perl -pi -e '/^WLIBS/ && s:=.*:="-lWINGs -lwraster -lXft":' `find $(DEBTMPDIR) -name get-wings-flags`
	perl -pi -e '/^WLIBS/ && s:=.*:="-lWUtil -lX11":' `find $(DEBTMPDIR) -name get-wutil-flags`

configure:	configure.ac
	$(TOPSRCDIR)/autogen.sh

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