#!/usr/bin/make -f

#export DH_VERBOSE=1

# Set architecture variables for cross-compiling and for saving the configure
# script from having to guess our platform (since we know it already)
# Also sets multiarch lib vars 
include /usr/share/dpkg/architecture.mk

# Get default flags 
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)
# These additional compiler flags should bring up warnings about common
# sources of errors.
export CFLAGS += -Wall -Wformat=2 -Wunused -Wundef -Wextra -Wswitch-enum \
          -Wpointer-arith -Wnested-externs -Wbad-function-cast -Wcast-qual \
	  -Wcast-align -Wshadow

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

configure_flags =  --prefix=/usr/share --exec-prefix=/usr --with-x \
  --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE)

ifeq ($(DEB_HOST_ARCH_OS),linux)
  configure_flags += --enable-gpm
else
# libgpm is not supported by non-linux architectures: #345268 #95843
  configure_flags += --disable-gpm
endif
ifneq ($(DEB_HOST_ARCH_OS),hurd)
  configure_flags += --enable-xft
else
# The xrender extension is not supported on the hurd: #95843
  configure_flags += --disable-xft
endif


#to successfully build with a multiarch slang we need to explicitly specify
#include and library directories.
configure_flags += --with-slanglib=/usr/lib/$(DEB_HOST_MULTIARCH) --with-slanginc=/usr/include

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

jed=$(CURDIR)/debian/jed
xjed=$(CURDIR)/debian/xjed
jed-common=$(CURDIR)/debian/jed-common

clean:
	dh_testdir
	dh_autotools-dev_restoreconfig
	dh_clean
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f build-stamp
	-test -f configure.backup && mv configure.backup configure

config.status:
	dh_testdir
	dh_autotools-dev_updateconfig
	mv configure configure.backup
	(cd autoconf; $(MAKE) ../configure)
	./configure $(configure_flags) || \
	  { rc=$$?; cat config.log; exit $$rc; }

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

	#
	# --- MAKE ---
	#
        # These settings of DL_LIB, OTHERLIBS and XRENDERFONTLIBS are to get
        # right of unnecessary dependencies on libraries.
        # http://rerun.lefant.net/checklib/
        #
        # DL_LIB introduces a dependency on libdl that is already solved by
        # libc. OTHERLIBS introduces a dependency on libm that is also solved
        # by libc. XRENDERFONTLIBS fixes a problem with libxft-dev that defines
        # libfontconfig.
        #
        # Upstream refused an attempt to fix these bugs. On other systems these
        # dependencies are needed and the autoconf-make system of jed is to
        # complex to fix it there.
        # http://jupiter.jtan.com/~wingnut/jed-users/msg02560.html
	$(MAKE) DL_LIB="" OTHERLIBS=-lutil XRENDERFONTLIBS=-lXft jed # getmail
	$(MAKE) DL_LIB="" OTHERLIBS=-lutil XRENDERFONTLIBS=-lXft MOUSELIB= xjed

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -C src runtests
endif

	#unfortunately passing the multiarch dir for slang results in the
	#upstream build system adding a rpath. Remove this with chrpath
	chrpath -d src/objs/jed
	chrpath -d src/objs/xjed
	touch build-stamp

binary-indep: build-indep
	dh_testdir
	dh_testroot
	# keep dh_install informations (debian/files) and debian/tmp
	dh_prep
	dh_installdirs -i

	$(MAKE) -C src DESTDIR=$(jed-common) JED_MAN_FILES= install_data
	rm -r $(jed-common)/usr/share/share

	#
	# --- JED-COMMON ---
	#
	# fill up /usr/share/doc/jed-common
	dh_installchangelogs -pjed-common changes.txt

	mv $(jed-common)/usr/share/jed/doc/README \
	    $(jed-common)/usr/share/jed/doc/manual \
	    $(jed-common)/usr/share/doc/jed-common
	dh_link -pjed-common usr/share/jed/doc/txt usr/share/doc/jed-common/txt
	dh_installdocs -pjed-common debian/README.Debian \
	    debian/init.d/README.Debian-startup \
	    debian/Debian-Jed-Policy.txt

	dh_installinfo -pjed-common info/jed.*
	cd $(jed-common)/usr/share/info && \
	  for i in 1 2 3; do mv jed.$${i}in jed.info-$$i; done && \
	  sed -i '/jed\..in/s/jed\.\([123]\)in/jed.info-\1/' jed.info

	mv $(jed-common)/usr/share/doc/jed-common/manual \
	    $(jed-common)/usr/share/doc/jed-common/html

	# generate html files from jed.tex
	hevea -fix -o $(jed-common)/usr/share/doc/jed-common/html/jed.hevea \
	    $(jed-common)/usr/share/doc/jed-common/html/jed.tex
	cd $(jed-common)/usr/share/doc/jed-common/html/; hacha jed.hevea
	cd $(jed-common)/usr/share/doc/jed-common/html/; \
	    rm jed.hevea jed.haux jed.htoc jed.tex

	# install some extra docs from the source
	dh_installexamples -pjed-common lib/jed.conf \
	    $(jed-common)/usr/share/jed/lib/jed.rc

	# install *.sl files
	dh_install -pjed-common debian/defaults.sl usr/share/jed/lib/
	rm $(jed-common)/usr/share/jed/lib/jed.rc # it's moved to examples above

	# install compile script to build *.slc files after installation
	dh_install -pjed-common debian/compile usr/share/jed/compile
	mv $(jed-common)/usr/share/jed/compile/compile \
	    $(jed-common)/usr/share/jed/compile/jed-common
	chmod +x $(jed-common)/usr/share/jed/compile/jed-common

	# fill up /etc
	dh_install -pjed-common debian/init.d/*.sl etc/jed.d/
	dh_link -i usr/share/doc/jed-common/README.Debian-startup etc/jed.d/README

	dh_installdebconf -i

	#
	# --- INSTALL-ARCH-INDEP ---
	#
	dh_compress -i --exclude=libfuns.hlp --exclude=jedfuns.hlp

	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build-arch
	dh_testdir
	dh_testroot
	# keep dh_install informations (debian/files) and debian/tmp
	dh_clean -a
	dh_installdirs -a

	#
	# --- JED ---
	#
	dh_install -pjed src/objs/jed usr/bin/
	dh_installman -pjed doc/manual/jed.1
	dh_link -pjed usr/share/doc/jed-common usr/share/doc/jed
	#
	# --- XJED ---
	#
	dh_install -pxjed src/objs/xjed usr/bin/
	dh_installman -pxjed doc/manual/jed.1
	mv $(xjed)/usr/share/man/man1/jed.1 $(xjed)/usr/share/man/man1/xjed.1
	dh_link -pxjed usr/share/doc/jed-common usr/share/doc/xjed

	#
	# --- INSTALL-ARCH-DEP ---
	#
	dh_installmenu -a
	dh_fixperms -a
	dh_strip -a
	dh_compress -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep

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