#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

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


# get-orig-source target
DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+).*,\1,p')

DEB_HOST_ARCH?=$(shell dpkg-architecture -qDEB_HOST_ARCH)
SUPPORTED_ARCH_UDEB = amd64 arm64 armhf i386 ia64 mips mipsel powerpc ppc64el x32

get-orig-source:
	[ ! -f $(ORIG_FILE).gz ] || rm -f $(ORIG_FILE).gz
	uscan --force-download --rename --repack --download-version=$(DEB_UPSTREAM_VERSION) --destdir=.


CFLAGS = -Wall -g
export OPTFLAGS =-DDISCOVERY_FILE=\"/var/lib/open-iscsi/discovery\" -DNODE_FILE=\"/var/lib/open-iscsi/node\"

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

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	dh_autotools-dev_updateconfig
	touch configure-stamp


#Architecture 
build: build-arch

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp 

	# Add here commands to compile the arch part of the package.
	$(MAKE) -C utils/fwparam_ibft
	$(MAKE) user
	$(MAKE) -C utils

	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp 

	# Add here commands to compile the indep part of the package.
	#$(MAKE) doc
	touch build-indep-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp

	# Add here commands to clean up after the build process.
	$(MAKE) -C utils/fwparam_ibft clean
	$(MAKE) -C usr clean
	$(MAKE) -C utils clean
	rm -rf modules

	dh_autotools-dev_restoreconfig
	dh_clean 

install: install-arch
# disabled kernel module package for now because source is already in debian stock kernelsy
install-indep:

install-arch:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -s

	# Add here commands to install the arch part of the package into 
	# debian/open-iscsi.
	install -m 755 usr/iscsiadm $(CURDIR)/debian/open-iscsi/usr/bin
	install -m 755 usr/iscsid $(CURDIR)/debian/open-iscsi/usr/sbin
	install -m 755 usr/iscsistart $(CURDIR)/debian/open-iscsi/usr/sbin
	install -m 755 utils/iscsi_discovery $(CURDIR)/debian/open-iscsi/usr/sbin
	install -m 755 utils/iscsi-iname $(CURDIR)/debian/open-iscsi/usr/sbin
	install -m 600 etc/iscsid.conf $(CURDIR)/debian/open-iscsi/etc/iscsi
	install -m 644 debian/open-iscsi.default $(CURDIR)/debian/open-iscsi/etc/default/open-iscsi

ifneq (,$(findstring $(DEB_HOST_ARCH),$(SUPPORTED_ARCH_UDEB)))
	# udeb stuff
	install -m 755 usr/iscsiadm $(CURDIR)/debian/open-iscsi-udeb/bin
	install -m 755 utils/iscsi-iname $(CURDIR)/debian/open-iscsi-udeb/sbin
	install -m 755 utils/iscsi_discovery $(CURDIR)/debian/open-iscsi-udeb/sbin
	install -m 755 usr/iscsid $(CURDIR)/debian/open-iscsi-udeb/sbin
	install -m 755 usr/iscsistart $(CURDIR)/debian/open-iscsi-udeb/sbin
	install -m 644 etc/iscsid.conf $(CURDIR)/debian/open-iscsi-udeb/etc/iscsi
	install -m 644 debian/open-iscsi-udeb.start $(CURDIR)/debian/open-iscsi-udeb/sbin/iscsi-start
	install -m 755 debian/open-iscsi-udeb.finish-install $(CURDIR)/debian/open-iscsi-udeb/usr/lib/finish-install.d/10open-iscsi
endif

	# initramfs stuff
	install -m 755 debian/extra/initramfs.hook \
		$(CURDIR)/debian/open-iscsi/usr/share/initramfs-tools/hooks/iscsi
	install -m 755 debian/extra/initramfs.local-top \
		$(CURDIR)/debian/open-iscsi/usr/share/initramfs-tools/scripts/local-top/iscsi

	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
	dh_installinit -u 'start 45 S . stop 81 0 1 6 .' --no-start
	dh_installinit -u 'stop 80 0 1 6 .' --no-start --name=umountiscsi.sh
	dh_installinit --name=iscsi-network-interface -n --upstart-only
	dh_installman
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	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=-a binary-common

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch configure get-orig-source
