#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independent
# package.
#
# $Id: rules,v 1.64 2006/08/05 15:49:18 grendel Exp $
#

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

ifdef CCVER
CC=gcc-$(CCVER)
else
CC=gcc
endif

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFARGSEXTRA := --with-cdebug

ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
export CC=$(DEB_HOST_GNU_TYPE)-gcc
CFARGSEXTRA += --host=$(DEB_HOST_GNU_TYPE)
endif

DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
CFARGSEXTRA += --without-copt
else
  ifneq (,$(filter $(DEB_HOST_ARCH), i386))
    CFARGSEXTRA += --without-copt
    export DEB_CFLAGS_MAINT_STRIP := -O2 -O3
    export DEB_CFLAGS_MAINT_PREPEND := -O1
  else
    CFARGSEXTRA += --with-copt
  endif
endif

ifneq (,$(filter debug,$(DEB_BUILD_OPTIONS)))
CFARGSEXTRA += --with-rtldebug
else
CFARGSEXTRA += --without-rtldebug
endif

# Temporary workaround for hppa linker issues
ifeq ($(DEB_BUILD_ARCH),hppa)
export DEB_CFLAGS_MAINT_APPEND += -ffunction-sections
endif

EXTRA_ARGS=

export DEB_CPPFLAGS_MAINT_APPEND := -DDEBIAN
export DEB_LDFLAGS_MAINT_APPEND := -Wl,--as-needed

DO_MACHINE_CODE=no
ifeq ($(DEB_HOST_ARCH),i386)
DO_MACHINE_CODE=yes
endif
ifeq ($(DEB_HOST_ARCH),powerpc)
DO_MACHINE_CODE=yes
endif
ifeq ($(DEB_BUILD_ARCH),sparc)
DO_MACHINE_CODE=no
endif

ifeq ($(DO_MACHINE_CODE),yes)
MACHINE_OPTS=--with-machine-code
else
MACHINE_OPTS=--without-machine-code
endif

#DEBVERSION:=$(shell dpkg-parsechangelog | sed -ne 's/Version: *\(.*\)-.*$$/\1/p')
DEBVERSION:=$(shell dpkg-parsechangelog | grep "Version:" | cut -d" " -f2 | cut -d"-" -f1)
MAJOR:=$(shell sed -ne 's/major://p' buildid.txt)
MINOR:=$(shell sed -ne 's/minor://p' buildid.txt)
BUILD:=$(shell sed -ne 's/build://p' buildid.txt)
PIKEV:=$(MAJOR).$(MINOR)
VERSION:=$(MAJOR).$(MINOR).$(BUILD)
PIKE:=pike$(PIKEV)

ifneq ($(VERSION),$(DEBVERSION))
$(error Version mismatch; buildid.txt: $(VERSION), debian/changelog: $(DEBVERSION))
endif

# --with-lib-path is just to stop configure from adding all the
# standard library directories with -L as well as -R to LDFLAGS
CFARGS=$(CFARGSEXTRA) \
       --with-cflags='`dpkg-buildflags --get CFLAGS`' \
       --with-cppflags='`dpkg-buildflags --get CPPFLAGS`' \
       --with-ldflags='`dpkg-buildflags --get LDFLAGS`' \
       --prefix=/usr \
       --with-bignums \
       --with-gmp \
       --with-poll \
       --with-zlib \
       --with-freetype \
       --without-ttflib \
       --with-libnettle \
       --without-sybase \
       --without-java \
       --with-odbc \
       --with-sane \
       --with-postgres \
       --with-postgres-include-dir=`pg_config --includedir` \
       --with-libpq-dir=/usr/lib \
       --with-sass \
       --with-sqlite \
       --without-com \
       --without-ffmpeg \
       --without-fftw \
       --without-libpdf \
       --without-libpanda \
       --without-GTK \
       --without-GTK2 \
       --without-bundles \
       --without-oracle \
       --without-dvb \
       --disable-rpath \
       --without-vcdiff \
       --without-zxid \
       --disable-smartlink_binary \
       $(MACHINE_OPTS) $(EXTRA_ARGS)

include_prefix=/usr/include/pike$(PIKEV)/pike
lib_prefix=/usr/lib/pike$(PIKEV)

MODULE_PACKAGES=$(shell dh_listpackages -a -N $(PIKE)-core)
#{ADT,Array,Audio,Cache,Calendar,Calendar_I,COM,Colors,CommonLog,Crypto,Debug,Filesystem,Error,Float,Function,Geography,Getopt,Gettext,Gmp,Graphics,Gz,HTTPAccept,Int,Kerberos,Languages,Local,Locale,Mapping,Math,MIME,Mird,Multiset,Nettle,Parser,Pike,Pipe,Process,Program,Protocols,Regexp,Remote,SSL,Shuffler,Standards,Stdio,String,System,Thread,Tools,Unicode,Web,Yabu,Yp,_ADT,_Charset,_Roxen,___Gz,___MIME,___Math,___Mird,___Regexp,___Yp,____Charset,__builtin*,spider}.
#usr/lib/pike/*/modules/Sql.pmod/{module,Sql,rsql,sql_*}.*

excluded_modules:=FSEvents Ssleay Mird Msql msql PDF Ffmpeg Oracle oracle sybase DVB Java TTF Gnome GTK GDK GTKSupport ZXID COM.so VCDiff
# This is tricky. Note that it's only possible for an exclusion to match the last
# component specified in an .install file or on the command line, and below.
DH_EXCLUDE:=$(addprefix -X,$(excluded_modules) README NEWS LICENSE CONTRIBUTING Makefile)
DH_ALREADY=`for p in $(MODULE_PACKAGES); do \
	      find debian/$$p/usr/lib/pike?.?/* -type f -printf "-X%P "; done`

TMP=$(CURDIR)/debian/tmp

#export CFLAGS CC PIKE VERSION
export CC

configure: configure-stamp
configure-stamp:
	dh_autoreconf src/run_autoconfig
	$(MAKE) force_configure CONFIGUREARGS="$(CFARGS)"
	touch configure-stamp

build-arch: build-arch-stamp
build-arch-stamp: configure
	dh_testdir
	$(MAKE) compile
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: configure
	dh_testdir
	$(MAKE) documentation
	touch build-indep-stamp

build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	$(MAKE) distclean
	rm -rf refdoc/modref refdoc/traditional_manual
	dh_autoreconf_clean
	dh_clean

install: build-arch
	dh_testdir
	dh_testroot
	dh_prep
	$(MAKE) INSTALLARGS=--traditional \
		buildroot=$(TMP) \
		prefix=/usr \
		man_prefix=/usr/share/man \
		lib_prefix="$(lib_prefix)" \
		include_prefix="$(include_prefix)" \
		pike_name=/usr/bin/$(PIKE) \
		install_nodoc

#	Some cleaning
	find "$(TMP)$(lib_prefix)" -name '.autodoc' -type f -exec rm -f '{}' ';'

	install -d -m755 $(TMP)/usr/share/pixmaps
	install -m644 debian/pike.xpm $(TMP)/usr/share/pixmaps/$(PIKE).xpm
	mv $(TMP)/usr/bin/pike $(TMP)/usr/bin/$(PIKE)
	mv $(TMP)/usr/share/man/man1/pike.1 $(TMP)/usr/share/man/man1/$(PIKE).1

#	Simply strip exec permissions and shebangs from all pike scripts.
	find $(TMP)/usr -type f '(' -name '*.p*' -o -name '*.h' ')' \
		-execdir chmod -x '{}' + \
		-execdir sed -rsi -e '1!b;/^#!/d' '{}' +

#	Install our own, simpler specs file.
	sed -e "s;@include_prefix@;$(include_prefix);g" \
	    -e "s;@CONFIGURE_ARGS@;$(CFARGS);g" \
	    debian/specs.in > $(TMP)$(include_prefix)/specs

#	Fix precompike.pike
	sed -i -e "s%\.\./lib%$(lib_prefix)%" $(TMP)$(include_prefix)/precompile.pike

# 	# pike -x module fix
#	sed -i -e 's;\$$(MODULE_BASE)/dynamic_module_makefile;\$$(PIKE_SRC_DIR)/modules/dynamic_module_makefile;g' \
#		$(TMP)$(include_prefix)/modules/dynamic_module_makefile

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build-indep
	dh_testdir -i
	dh_testroot -i
	# install the docs
	dh_install -p$(PIKE)-doc -Ximages refdoc/traditional_manual/* usr/share/doc/$(PIKE)-doc/html/manual
	dh_install -p$(PIKE)-doc refdoc/modref/* usr/share/doc/$(PIKE)-doc/html/reference
	dh_link -p$(PIKE)-doc usr/share/doc/$(PIKE)-doc/html/reference/images usr/share/doc/$(PIKE)-doc/html/manual/images
	dh_install -i
	dh_installdocs -i
	dh_installchangelogs -i -k CHANGES
	dh_lintian -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build-arch install
	dh_testdir -a
	dh_testroot -a
	dh_install -a -N$(PIKE)-core --sourcedir=debian/tmp $(DH_EXCLUDE)
	dh_install -p$(PIKE)-core --sourcedir=debian/tmp $(DH_EXCLUDE) $(DH_ALREADY)
	for p in $(MODULE_PACKAGES); do \
		sed "s/#PACKAGE#/$$p/" debian/prerm-snippet >> debian/$$p.prerm.debhelper; \
	done
	dh_installdocs -a --link-doc=$(PIKE)-core
	dh_installmenu -a
	dh_installchangelogs -a -k CHANGES
	dh_lintian -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a -X/run_autoconfig -X/precompile.sh -X/smartlink -X/fixdepends.sh -X/install_module -X/mktestsuite
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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