#!/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.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk


.NOTPARALLEL:

SHELL           = /bin/bash

package         = freeradius
freeradius_dir  = $(CURDIR)/debian/tmp

mandir          = /usr/share/man
libdir          = /usr/lib/$(package)
logdir          = /var/log/$(package)
pkgdocdir       = /usr/share/doc/$(package)
raddbdir        = /etc/$(package)

modulelist=krb5 ldap sql_mysql sql_iodbc sql_postgresql dhcp redis rest yubikey
pkgs=$(shell dh_listpackages)

# This has to be exported to make some magic below work.
export DH_OPTIONS

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

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

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# Add dependency on distribution specific version of openssl that fixes Heartbleed (CVE-2014-0160).
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
       SUBSTVARS = -Vdist:Depends="libssl1.0.0 (>= 1.0.1f-1ubuntu2)"
else
       SUBSTVARS = -Vdist:Depends="libssl1.0.0 (>= 1.0.1e-2+deb7u5)"
endif

include /usr/share/quilt/quilt.make

config.status: configure
	dh_testdir

ifeq (config.sub.dist,$(wildcard config.sub.dist))
	rm config.sub
else
	mv config.sub config.sub.dist
endif
ifeq (config.guess.dist,$(wildcard config.guess.dist))
	rm config.guess
else
	mv config.guess config.guess.dist
endif
	ln -s /usr/share/misc/config.sub config.sub
	ln -s /usr/share/misc/config.guess config.guess


	./configure $(confflags) \
		--config-cache \
		--disable-developer \
		--disable-openssl-version-check \
		--prefix=/usr \
		--exec-prefix=/usr \
		--mandir=$(mandir) \
		--sysconfdir=/etc \
		--libdir=$(libdir) \
		--datadir=/usr/share \
		--localstatedir=/var \
		--with-raddbdir=$(raddbdir) \
		--with-logdir=/var/log/$(package) \
		--with-large-files \
		--with-udpfromto \
		--without-rlm_eap_tnc \
		--with-rlm_sql_postgresql_lib_dir=`pg_config --libdir` \
		--with-rlm_sql_postgresql_include_dir=`pg_config --includedir` \
		--with-iodbc-include-dir='/usr/include/iodbc' \
		--without-rlm_eap_ikev2 \
		--without-rlm_sql_oracle \
		--without-rlm_sql_unixodbc

	rm config.guess
	mv config.guess.dist config.guess
	rm config.sub
	mv config.sub.dist config.sub

#Architecture
build: build-arch build-indep

build-arch: patch build-arch-stamp
build-arch-stamp: config.status
	$(MAKE)
	touch $@

build-indep: patch build-indep-stamp
build-indep-stamp: config.status
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp
	rm -f config.cache config.log
	rm -f src/freeradius-devel

	[ ! -d src/modules/lib ] || rm -fr src/modules/lib || true
	[ ! -d src/binary ] || rm -fr src/binary || true

	# Add here commands to clean up after the build process.
ifeq (Make.inc,$(wildcard Make.inc))
	$(MAKE) distclean
endif
ifeq (config.sub.dist,$(wildcard config.sub.dist))
	rm -f config.sub
	mv config.sub.dist config.sub
endif
ifeq (config.guess.dist,$(wildcard config.guess.dist))
	rm -f config.guess
	mv config.guess.dist config.guess
endif
	dh_clean

install: install-arch

install-arch: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_installdirs
	test -d $(freeradius_dir)/usr/lib/freeradius || mkdir -p $(freeradius_dir)/usr/lib/freeradius
	ln -fs rlm_sql.so $(freeradius_dir)/usr/lib/freeradius/librlm_sql.so
	$(MAKE) install R=$(freeradius_dir) PACKAGE='debian'

	# rename radius binary to play nicely with others
	mv $(freeradius_dir)/usr/sbin/radiusd $(freeradius_dir)/usr/sbin/$(package)
	mv $(freeradius_dir)/$(mandir)/man8/radiusd.8 $(freeradius_dir)/$(mandir)/man8/$(package).8

	dh_install -i --sourcedir=$(freeradius_dir)
	dh_install --sourcedir=$(freeradius_dir) -p libfreeradius3
	dh_install --sourcedir=$(freeradius_dir) -p libfreeradius-dev
	dh_install --sourcedir=$(freeradius_dir) -p freeradius-config

	for mod in ${modulelist}; do \
	  pkg=$${mod##sql_} ; \
	  dh_install --sourcedir=$(freeradius_dir) -p freeradius-$$pkg ; \
	  rm -f $(freeradius_dir)/usr/lib/freeradius/rlm_$$mod*.so ; \
	done

	dh_install --sourcedir=$(freeradius_dir) -p freeradius-memcached
	rm -f $(freeradius_dir)/usr/lib/freeradius/rlm_cache_memcached.so

	dh_install --sourcedir=$(freeradius_dir) -p freeradius-utils
	dh_install --sourcedir=$(freeradius_dir) -p freeradius

	dh_strip -a --dbg-package=freeradius-dbg

	dh_makeshlibs -a -n
	dh_shlibdeps -l$(freeradius_dir)/usr/lib/freeradius

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs doc/ChangeLog
	dh_installdocs -Xdebian/tmp/usr/share/doc/freeradius/ChangeLog
	dh_installexamples
	dh_installlogrotate
	dh_installpam --name=radiusd
	dh_installinit --noscripts
	dh_installman
	dh_lintian
	dh_link
	dh_compress -Xexamples
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- $(SUBSTVARS)
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

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

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