#!/usr/bin/make -f

# Handle group assignment: libvirt (Debian) and libvirtd (Ubuntu)
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
	LIB_VIRT_GROUP = libvirtd
	DISTRO = Ubuntu
else
	LIB_VIRT_GROUP = libvirt
	DISTRO = Debian
endif

dpkg_buildflags = DEB_LDFLAGS_MAINT_APPEND="-Wl,--as-needed" dpkg-buildflags
# injecting CPPFLAGS to CFLAGS and CXXFLAGS
export CPPFLAGS:=$(shell $(dpkg_buildflags) --get CPPFLAGS)
export CFLAGS:=$(shell $(dpkg_buildflags) --get CFLAGS) $(CPPFLAGS)
export CXXFLAGS:=$(shell $(dpkg_buildflags) --get CXXFLAGS) $(CPPFLAGS)
export LDFLAGS:=$(shell $(dpkg_buildflags) --get LDFLAGS)

%:
	dh $@ --with javahelper --with bash-completion

override_dh_auto_build:
	#scons  mysql=yes new_xmlrpc=yes
	tar xzvf build_opennebula.tar.gz
	#XMLRPC_DIR="debian/" debian/build_opennebula.sh
	./build_opennebula.sh
	(cd src/oca/java/; \
	ln -s /usr/share/java/xmlrpc-common-3.1.jar lib/; \
	ln -s /usr/share/java/xmlrpc-client-3.1.jar lib/; \
	ln -s /usr/share/java/ws-commons-util.jar lib/; \
	./build.sh -d)

override_dh_auto_install:
	DESTDIR=dist ./install.sh
	dh_auto_install
ifneq (,$(filter opennebula-common, $(shell dh_listpackages)))
	dh_install -p opennebula-common share/pkgs/${DISTRO}/opennebula.sudoers \
		/etc/sudoers.d
	mv debian/opennebula-common/etc/sudoers.d/opennebula.sudoers \
		debian/opennebula-common/etc/sudoers.d/opennebula
endif
ifneq (,$(filter opennebula, $(shell dh_listpackages)))
	dh_install -p opennebula share/pkgs/${DISTRO}-systemd/opennebula.service \
		/lib/systemd/system/
	dh_install -p opennebula share/pkgs/${DISTRO}-systemd/opennebula-scheduler.service \
		/lib/systemd/system/
	dh_installinit -p opennebula --name=opennebula --onlyscripts --no-start
	dh_installinit -p opennebula --name=opennebula-scheduler --onlyscripts --no-start
	dh_install -p opennebula share/pkgs/logrotate/opennebula /etc/logrotate.d/
	dh_install -p opennebula share/pkgs/${DISTRO}-systemd/opennebula.conf /usr/lib/tmpfiles.d/
endif
ifneq (,$(filter opennebula-sunstone, $(shell dh_listpackages)))
	dh_install -p opennebula-sunstone share/pkgs/${DISTRO}-systemd/opennebula-sunstone.service \
		/lib/systemd/system/
	dh_installinit -p opennebula-sunstone --name=opennebula-sunstone --onlyscripts --no-start

	dh_install -p opennebula-sunstone share/pkgs/${DISTRO}-systemd/opennebula-econe.service \
		/lib/systemd/system/
	dh_installinit -p opennebula-sunstone --name=opennebula-econe --onlyscripts --no-start

	dh_install -p opennebula-sunstone share/pkgs/${DISTRO}-systemd/opennebula-novnc.service \
		/lib/systemd/system/
	dh_installinit -p opennebula-sunstone --name=opennebula-novnc --onlyscripts --no-start

	cp share/pkgs/${DISTRO}-systemd/opennebula.conf \
		share/pkgs/${DISTRO}-systemd/opennebula-sunstone.conf
	dh_install -p opennebula-sunstone share/pkgs/${DISTRO}-systemd/opennebula-sunstone.conf \
		/usr/lib/tmpfiles.d/
endif
ifneq (,$(filter opennebula-gate, $(shell dh_listpackages)))
	dh_install -p opennebula-gate share/pkgs/${DISTRO}-systemd/opennebula-gate.service \
		/lib/systemd/system/
	dh_installinit -p opennebula-gate --name=opennebula-gate --onlyscripts --no-start

	cp share/pkgs/${DISTRO}-systemd/opennebula.conf \
		share/pkgs/${DISTRO}-systemd/opennebula-gate.conf
	dh_install -p opennebula-gate share/pkgs/${DISTRO}-systemd/opennebula-gate.conf \
		/usr/lib/tmpfiles.d/
endif
ifneq (,$(filter opennebula-flow, $(shell dh_listpackages)))
	dh_install -p opennebula-flow share/pkgs/${DISTRO}-systemd/opennebula-flow.service \
		/lib/systemd/system/
	dh_installinit -p opennebula-flow --name=opennebula-flow --onlyscripts --no-start

	cp share/pkgs/${DISTRO}-systemd/opennebula.conf \
		share/pkgs/${DISTRO}-systemd/opennebula-flow.conf
	dh_install -p opennebula-flow share/pkgs/${DISTRO}-systemd/opennebula-flow.conf \
		/usr/lib/tmpfiles.d/
endif
ifneq (,$(filter opennebula-node, $(shell dh_listpackages)))
	dh_install -p opennebula-node share/etc/sysctl.d/bridge-nf-call.conf /etc/sysctl.d/
	dh_install -p opennebula-node share/pkgs/${DISTRO}-systemd/opennebula-node.conf \
		/usr/lib/tmpfiles.d/
endif
ifneq (,$(filter opennebula-tools, $(shell dh_listpackages)))
	cp share/install_gems/Ubuntu1604/Gemfile.lock dist/usr/share/one/
endif

override_dh_install:
	dh_install

override_dh_auto_clean:
	dh_auto_clean
	rm -rf dist/
	scons --clean .
	(cd src/oca/java/; \
	rm lib/xmlrpc-common-3.1.jar; \
	rm lib/xmlrpc-client-3.1.jar; \
	rm lib/ws-commons-util.jar; \
	rm -rf jar/; rm -rf share/doc/; \
	./build.sh -c)
	-rm -Rf .scons_temp
	-rm -Rf share/scons/lex_bison.pyc \
		src/nebula/.xmlrpc_test/xmlrpc_test.* \
		src/scheduler/.xmlrpc_test/xmlrpc_test.* \
		src/scheduler/src/sched/.xmlrpc_test \
		.xmlrpc_test
	-rm -Rf debian/opennebula-node.postinst

override_dh_installdeb:
ifneq (,$(filter opennebula-node, $(shell dh_listpackages)))
	sed "s/@LIBVIRTGRP@/${LIB_VIRT_GROUP}/g" debian/opennebula-node.postinst.in > debian/opennebula-node.postinst
endif
	dh_installdeb

override_dh_fixperms:
	dh_fixperms

# opennebula
ifneq (,$(filter opennebula, $(shell dh_listpackages)))
	find debian/opennebula/etc/one/ -type f -exec chmod 644 {} \;
	#chmod 755 debian/opennebula/usr/lib/one/remotes/im/common.d/ssh_pubkey.sh
endif

# ruby-opennebula
ifneq (,$(filter ruby-opennebula, $(shell dh_listpackages)))
	find debian/opennebula/etc/one/ -type f -exec chmod 644 {} \;
endif

# opennebula-tools
ifneq (,$(filter opennebula-tools, $(shell dh_listpackages)))
	find debian/opennebula/etc/one/ -type f -exec chmod 644 {} \;
	#chmod 755 debian/opennebula-tools/usr/lib/one/ruby/cloud/econe/EC2QueryClient.rb
endif

# opennebula-gate
ifneq (,$(filter opennebula-gate, $(shell dh_listpackages)))
	find debian/opennebula/etc/one/ -type f -exec chmod 644 {} \;
endif

# opennebula-flow
ifneq (,$(filter opennebula-flow, $(shell dh_listpackages)))
	find debian/opennebula/etc/one/ -type f -exec chmod 644 {} \;
endif

# opennebula-common
ifneq (,$(filter opennebula-common, $(shell dh_listpackages)))
	chmod 0440 debian/opennebula-common/etc/sudoers.d/opennebula
endif

# opennebula-sunstone
ifneq (,$(filter opennebula-sunstone, $(shell dh_listpackages)))
endif

get-orig-source:
	uscan --force-download --no-symlink

.PHONY: override_dh_strip
override_dh_strip:
	dh_strip --exclude=opennebula

