#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# GNU copyright 1998, 1999 Marcelo Magallon <mmagallo@debian.org>
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Cristoph Lameter.

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

DEB_BUILD_ARCH_OS := $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
ifeq ($(DEB_BUILD_ARCH_OS),kfreebsd)
DEB_BUILD_ARCH_OS_kfreebsd = 1
endif

WDM_PREFIX             := /usr
WDM_SYSCONF_DIR        := /etc/X11
WDM_CONF_DIR           := $(WDM_SYSCONF_DIR)/wdm
WDM_CONFIG_FILE        := wdm-config
WDM_CONF_FILE          := debian/config/$(WDM_CONFIG_FILE)
WDM_BIN_DIR            := $(WDM_PREFIX)/bin

DEF_WDM_CONFIG_PATH    := $(WDM_CONF_DIR)/$(WDM_CONFIG_FILE)
DEF_DEBIAN_SYSTEM_PATH := $(shell grep systemPath: $(WDM_CONF_FILE) | cut -d : -f 2- | tr -d [:blank:])
DEF_DEBIAN_USER_PATH   := $(shell grep userPath: $(WDM_CONF_FILE) | cut -d : -f 2- | tr -d [:blank:])
DEF_DEBIAN_CHOOSER     := /usr/X11R6/lib/X11/xdm/chooser
DEF_DEBIAN_AUTH_DIR    := /var/lib/wdm
DEF_DEBIAN_PAM_PATH    := /etc/pam.d

WDM_BUILD_DIR          := $(shell pwd)/debian/wdm

INSTALL_PROGRAM        := install -m 0755
INSTALL_REGFILE        := install -m 0644

# Use dpkg-buildflags to get default compile/link flags
CFLAGS             := $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS           := $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS            := $(shell dpkg-buildflags --get LDFLAGS)

# wdm build structure is ancient and links against unneeded libs.
# Make sure this does not happen.
LDFLAGS            += -Wl,--as-needed

export CFLAGS CPPFLAGS LDFLAGS

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: config-stamp
	dh_testdir
	make
	touch build-stamp

config-stamp:
	dh_testdir
	./configure \
	  --prefix=$(WDM_PREFIX) \
	  --datadir=/usr/share \
	  --sysconfdir=$(WDM_SYSCONF_DIR) \
	  --localstatedir=/var/lib/wdm \
	  --with-gfxdir=/usr/share/pixmaps \
	  --enable-shadow \
	  --enable-pam \
	  --enable-aafont \
	  --with-pamdir=$(DEF_DEBIAN_PAM_PATH) \
	  --enable-selinux \
	  --with-wdmdir=$(WDM_CONF_DIR) \
	  --with-defuserpath=$(DEF_DEBIAN_USER_PATH) \
	  --with-defsystempath=$(DEF_DEBIAN_SYSTEM_PATH) \
	  --with-nlsdir=/usr/share/locale \
	  --enable-exitlogin
	sed \
	  -e '/DEF_CHOOSER/     s|".*"|"$(DEF_DEBIAN_CHOOSER)"|' \
	  -e '/DEF_AUTH_DIR/    s|".*"|"$(DEF_DEBIAN_AUTH_DIR)"|' \
	  -e '/DEF_XDM_CONFIG/  s|".*"|"$(DEF_WDM_CONFIG_PATH)"|' \
	  < include/config.h > include/config.h.debian
	mv include/config.h.debian include/config.h
	touch config-stamp

clean:
	dh_testdir
	dh_testroot
	-rm -f *-stamp debian/conffiles
	rm -f po/*.mo
	[ ! -f Makefile ] || $(MAKE) distclean
	debconf-updatepo

	dh_clean -Xconfigs/Xsession.orig

install-stamp: build-stamp
	dh_testdir
	dh_prep -Xconfigs/Xsession.orig
	dh_installdirs
	$(INSTALL_PROGRAM) bin/wdm                   \
	  $(WDM_BUILD_DIR)/$(WDM_BIN_DIR)
	$(INSTALL_PROGRAM) bin/wdmLogin              \
	  $(WDM_BUILD_DIR)/$(WDM_BIN_DIR)

	dh_installpam
# if I use debian/config/* I cannot build this on a CVS checked-out tree
	for config_file in `find debian/config -type f | grep -v /CVS/` ; do \
	  $(INSTALL_REGFILE) $$config_file $(WDM_BUILD_DIR)/$(WDM_CONF_DIR) ; \
	done
	chmod a+rx,u+w \
	  $(WDM_BUILD_DIR)/$(WDM_CONF_DIR)/Xreset \
	  $(WDM_BUILD_DIR)/$(WDM_CONF_DIR)/Xreset_0 \
	  $(WDM_BUILD_DIR)/$(WDM_CONF_DIR)/Xsession \
	  $(WDM_BUILD_DIR)/$(WDM_CONF_DIR)/Xsetup \
	  $(WDM_BUILD_DIR)/$(WDM_CONF_DIR)/Xsetup_0 \
	  $(WDM_BUILD_DIR)/$(WDM_CONF_DIR)/Xstartup \
	  $(WDM_BUILD_DIR)/$(WDM_CONF_DIR)/Xstartup_0
	dh_installinit --noscripts
	dh_installlogrotate
	$(INSTALL_PROGRAM) debian/update_wdm_wmlist \
	  $(WDM_BUILD_DIR)/usr/sbin/update_wdm_wmlist
	$(INSTALL_REGFILE) pixmaps/*.xpm pixmaps/*.jpg \
	  $(WDM_BUILD_DIR)/usr/share/pixmaps/
	$(INSTALL_REGFILE) debian/*.xpm $(WDM_BUILD_DIR)/usr/share/pixmaps/
        # Provide virtual facility x-display-manager for dependency based boot system
	$(INSTALL_REGFILE) debian/wdm.insservd $(WDM_BUILD_DIR)/etc/insserv.conf.d/wdm
	dh_installdocs
	dh_installdebconf
	cp debian/lintian.overrides $(WDM_BUILD_DIR)/usr/share/lintian/overrides/wdm
	cp doc/wdm.man debian/man/wdm.1x
	cp doc/wdmLogin.man debian/man/wdmLogin.1x
	dh_installman
	$(MAKE) install-nls-yes DESTDIR=$(WDM_BUILD_DIR)
	dh_installchangelogs ChangeLog
	touch install-stamp

binary-indep: build
# nothing to do here

binary-arch: build install-stamp
	dh_testdir
	dh_testroot
	dh_strip
	dh_compress
	dh_fixperms
	chmod 600 \
	  $(WDM_BUILD_DIR)/$(WDM_CONF_DIR)/wdm-config
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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