#!/usr/bin/make -f

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

# 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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# minimise needless linking
export DEB_LDFLAGS_MAINT_APPEND += -Wl,--as-needed

export CXXFLAGS+= $(CPPFLAGS)

%:
	dh $@ --parallel --with javahelper,systemd,linktree

override_dh_auto_clean:
	dh_auto_clean
	if [ -s "SConstruct" ]; then \
          scons --clean .  ;\
          cd src/oca/java && ./build.sh -c ;\
        fi

override_dh_clean:
	dh_clean
	-$(RM) -v -R \
          src/nebula/.xmlrpc_test/xmlrpc_test.* \
          src/oca/java/jar \
          src/oca/java/share/doc \
          src/scheduler/.xmlrpc_test/xmlrpc_test.* \
          src/scheduler/src/sched/.xmlrpc_test \
          .xmlrpc_test .scons_temp

override_dh_auto_build:
	scons --no-cache mysql=yes #parsers=yes
	(cd src/oca/java/; \
         ln -s /usr/share/java/xmlrpc-common.jar lib/; \
         ln -s /usr/share/java/xmlrpc-client.jar lib/; \
         ln -s /usr/share/java/ws-commons-util.jar lib/; \
         ./build.sh -d)

src/sunstone/public/css/app.css:
	sass -t expanded --no-cache --default-encoding utf-8 -I src/sunstone/public/bower_components/foundation/scss src/sunstone/public/scss/app.scss >$@

override_dh_auto_install: src/sunstone/public/css/app.css
	DESTDIR=debian/tmp ./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
	## Un-minify:
	cp -vf src/sunstone/public/bower_components/datatables/media/js/jquery.dataTables.js \
               debian/tmp/usr/lib/one/sunstone/public/vendor/4.0/datatables/jquery.dataTables.min.js
	cp -vf src/sunstone/public/bower_components/foundation/js/foundation.js \
               debian/tmp/usr/lib/one/sunstone/public/vendor/4.0/foundation/foundation.min.js
	cp -vf src/sunstone/public/bower_components/jgrowl/jquery.jgrowl.js \
               debian/tmp/usr/lib/one/sunstone/public/vendor/4.0/jgrowl/jquery.jgrowl.min.js
	cp -vf src/sunstone/public/bower_components/jgrowl/jquery.jgrowl.css \
               debian/tmp/usr/lib/one/sunstone/public/vendor/4.0/jgrowl/jquery.jgrowl.min.css
	cp -vf src/sunstone/public/bower_components/jquery-migrate/jquery-migrate.js \
               debian/tmp/usr/lib/one/sunstone/public/vendor/4.0/jquery-migrate.min.js

debian/opennebula-node.postinst:
	sed "s/@LIBVIRTGRP@/${LIB_VIRT_GROUP}/g" $@.in > $@

override_dh_install: debian/opennebula-node.postinst
	dh_install -X/LICENSE -X/LICENSE.txt -X/BSD-LICENSE.txt -X/NEW-BSD-LICENSE.txt
	## Wipe empty dirs if any:
	-find $(CURDIR)/debian/opennebula-sunstone -type d -empty -delete -printf 'removed %p\n'

override_dh_installinit:
	dh_installinit -p opennebula-sunstone --name=sunstone --name=opennebula-novnc
	dh_installinit -p opennebula --name=opennebula --name=opennebula-scheduler
	dh_installinit

override_dh_fixperms:
	dh_fixperms
	## executable-not-elf-or-script:
	-chmod -R -c a-x+X \
            debian/opennebula-sunstone/usr/share/opennebula-sunstone/public
