#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

#For overriding debian/compat
#export DH_COMPAT=7

#This rule file have a bashism
SHELL:=/bin/bash

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DPKG_EXPORT_BUILDFLAGS = 1
-include /usr/share/dpkg/buildflags.mk

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

# Personal Variables
PACKNAME=$(shell dh_listpackages)
PACKAGE=$(shell dh_listpackages)

# Variables used by the Debian Maintainer exclusively for package
# development.
SOURCENAME=$(shell dpkg-parsechangelog | grep "Source:" | cut -d ' ' -f 2)
DEBVER=$(shell dpkg-parsechangelog | grep "Version:" | cut -d ' ' -f 2)
VER=$(shell dpkg-parsechangelog | grep "Version:" | cut -d ' ' -f 2 | cut -f 1 -d -)


SCONS=scons
SCONSFLAGS=prefix=/usr \
		bindir=/usr/bin sbindir=/usr/sbin \
		mandir=/usr/share/man infodir=/usr/share/info \
		sysconfdir=/etc \
		with-olsr=true \
		disable_fw=true \
		enable_systemd=true \
		disable_werror=true \
		CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" \
		CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)"

CXXFLAGS += -std=gnu++98


config.status:
	dh_testdir
	# Add here commands to configure the package.


build: build-arch build-indep

build-indep: build-indep-stamp
build-indep-stamp:
	# Upstream dropped documentation out of source code
	touch $@


build-arch:  build-arch-stamp
build-arch-stamp:  config.status
	dh_testdir
	dh_update_autotools_config
	# Compile the package
	$(SCONS) $(SCONSFLAGS)
	docbook-to-man debian/xorp_rtrmgr.sgml > debian/xorp_rtrmgr.8
	docbook-to-man debian/xorpsh.sgml > debian/xorpsh.1
	touch $@


check:
	$(SCONS) $(SCONSFLAGS) check



clean: 
	dh_testdir
	dh_testroot
	# Add here commands to clean up after the build process.
	[ ! -e ./libxorp/build_info.cc ] || $(SCONS) $(SCONSFLAGS) -c
	rm -rf obj
	rm -f config.status config.log build-arch-stamp build-indep-stamp
	rm -f relpath.pyc site_scons/*/*.pyc
	rm -f xrl/scripts/Xif/*.pyc
	dh_clean 


install: install-arch


install-arch:
	dh_testdir
	dh_testroot
	#dh_clean -k -a
	dh_prep
	dh_installdirs -a
	# Install the program:
	$(SCONS) $(SCONSFLAGS) \
		DESTDIR=$(CURDIR)/debian/$(SOURCENAME) install

	# After the install, we move the files and create the symlinks : 
	for file in rib_xrl_shell_funcs.sh fea_xrl_shell_funcs.sh bgp_xrl_shell_funcs.sh ; do mv debian/$(SOURCENAME)/usr/sbin/$$file debian/$(SOURCENAME)/usr/lib/xorp/bin/   ; done
	cd debian/$(SOURCENAME)/usr/sbin/ && find -type f -exec ln -vsf /usr/sbin/{} ../lib/xorp/bin/ \; && cd $(CURDIR)
	# Config files: 
	cp debian/config.boot.small debian/$(SOURCENAME)/etc/xorp/config.boot
	cp debian/$(SOURCENAME).default debian/$(SOURCENAME)/etc/default/xorp
	cp debian/$(SOURCENAME).init.d  debian/$(SOURCENAME)/etc/init.d/xorp
	dh_install -s


# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples -pxorp rtrmgr/config/*.boot debian/config.boot.mcast  rtrmgr/config/README
	dh_installlogrotate
	dh_systemd_enable
	dh_installinit
	dh_systemd_start
	dh_installman 
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -l/usr/lib/xorp/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb


# Build architecture dependant packages using the common target.
binary-arch: build-arch-stamp install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common


binary: binary-arch

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