#!/usr/bin/make -f
#
# @package      php-stem
# @file         rules
# @copyright    Copyright (c) 2010-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
#
# Copyright (c) 2010-2020 The Regents of the University of California.
#
# Redistribution and use in source and binary forms, with or without modification
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list
#    of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
#    of conditions and the following disclaimer in the documentation and/or other materials
#    provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

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

$(info ########## Goals: $(MAKECMDGOALS) ##########)

CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

export CFLAGS
export CPPFLAGS
export CXXFLAGS
export LDFLAGS

DEB_MAJOR:=$(shell lsb_release -r -s | cut -d . -f 1)

configure:
	@echo "########## configure ##########"
	dh_testdir
	(cd stem; /usr/bin/phpize)
	(cd stem; ./configure --with-php-config=/usr/bin/php-config --prefix=/usr)
	>$@

build: configure
	@echo "########## build ##########"
	dh_testdir
	$(MAKE) -C stem
	>$@

clean:
	@echo "########## clean ##########"
	dh_testdir
	dh_testroot
	rm -fr build configure
	-$(MAKE) -C stem distclean
	(cd stem; /usr/bin/phpize --clean)
	dh_clean
	(cd stem; rm -fr .deps Makefile.global acinclude.m4 aclocal.m4 autom4te.cache build)
	(cd stem; rm -fr config.guess config.h config.h.in config.nice config.sub configure)
	(cd stem; rm -fr configure.in install-sh ltmain.sh miising mkinstalldirs)
	(cd stem; rm -fr run-tests.php missing)

install: build
	@echo "########## install ##########"
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) -C stem INSTALL_ROOT=$(CURDIR)/debian/php-stem install
	install --mode 0644 stem/ChangeLog -D $(CURDIR)/debian/php-stem/usr/share/doc/php-stem/changelog
	if [ $(DEB_MAJOR) -lt 7 ]; then \
		install --mode 0644 debian/stem.ini -D $(CURDIR)/debian/php-stem/etc/php5/conf.d/stem.ini; \
	elif [ $(DEB_MAJOR) -lt 8 ]; then  \
		install --mode 0644 debian/stem.ini -D $(CURDIR)/debian/php-stem/etc/php5/mods-available/stem.ini; \
	else \
		install --mode 0644 debian/stem.ini -D $(CURDIR)/debian/php-stem/etc/php/7.0/mods-available/stem.ini; \
	fi

binary-indep: install
	@echo "########## binary-indep ##########"
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_install -i
	dh_installlogrotate -i
	dh_installinit -i
	dh_installman -i
	#dh_python2 -i
	dh_link -i
	dh_strip -i
	dh_lintian -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install
	@echo "########## binary-arch ##########"
	dh_testdir -a
	dh_testroot -a
	echo "php:Depends=phpapi-`php-config --phpapi`" >> debian/php-stem.substvars
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_install -a
	dh_installlogrotate -a
	dh_installinit -a
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_lintian -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

#binary: binary-indep 
binary: binary-arch
	@echo "########## binary ##########"

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