#!/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
else
	LIB_VIRT_GROUP = libvirt
endif

LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS) 
export LDFLAGS

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

override_dh_auto_build:
	scons mysql=yes #parsers=yes
	(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-node, $(shell dh_listpackages)))
	dh_install -p opennebula-node debian/opennebula-node.sudoers /etc/sudoers.d
	mv debian/opennebula-node/etc/sudoers.d/opennebula-node.sudoers \
	   debian/opennebula-node/etc/sudoers.d/opennebula-node
endif
ifneq (,$(filter opennebula, $(shell dh_listpackages)))
	dh_install -p opennebula share/etc/init.d/one.debian /etc/init.d
	mv debian/opennebula/etc/init.d/one.debian \
	   debian/opennebula/etc/init.d/opennebula
	dh_installinit -p opennebula --name=opennebula --onlyscripts
endif

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
ifneq (,$(filter opennebula, $(shell dh_listpackages)))
	find debian/opennebula/etc/one/ -type f -exec chmod 644 {} \;
	chmod 644 debian/opennebula/usr/lib/one/ruby/cloud/occi/*.rb
	chmod 644 debian/opennebula/usr/lib/one/ruby/cloud/CloudServer.rb
	chmod 755 debian/opennebula/usr/lib/one/ruby/cloud/econe/EC2QueryClient.rb
	chmod 755 debian/opennebula/usr/lib/one/ruby/cloud/occi/OCCIClient.rb
	#chmod 755 debian/opennebula/usr/lib/one/remotes/im/common.d/ssh_pubkey.sh
endif
ifneq (,$(filter opennebula-node, $(shell dh_listpackages)))
	chmod 0440 debian/opennebula-node/etc/sudoers.d/opennebula-node
endif

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