#!/usr/bin/make -f

# Name of the source packages
psource:=virtualbox-source
gsource:=virtualbox-guest-source-hwe
pdkms:=virtualbox-dkms
gdkms:=virtualbox-guest-dkms-hwe

# The short upstream name
sname:=virtualbox
sxname:=virtualbox-qt
gname:=virtualbox-guest
uname:=virtualbox-guest-utils-hwe
uxname:=virtualbox-guest-x11-hwe

INSTALL_PATH:=/usr/lib/virtualbox
MAKE:=kmk

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NJOBS := -j $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

include /usr/share/dpkg/default.mk
VERSION ?= $(shell echo $(DEB_VERSION_UPSTREAM) |cut -f 1 -d "-")

DIST_NAME := $(shell lsb_release -si)
DIST_RELEASE := $(DIST_NAME)/$(shell lsb_release -sc)

SERVER_DEPENDS = $(shell cat /usr/share/xserver-xorg/videodrvdep 2>/dev/null)

export DEB_BUILD_HARDENING=1
export DEB_BUILD_HARDENING_PIE=0
export DEB_BUILD_HARDENING_FORMAT=0
export DEB_BUILD_HARDENING_BINDNOW=0

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --with python3,dkms

override_dh_auto_configure:
	@echo Building for $(DIST_RELEASE)

ifeq ($(strip $(SERVER_DEPENDS)),)
	@echo 'error: SERVER_DEPENDS is empty'
	@exit 1
endif

	mkdir -p out
	./configure \
	    --ose \
	    --odir=$(abspath out) \
	    --with-linux="/usr" \
	    --disable-kmods \
	    --enable-webservice \
	    --enable-vde \
	    --enable-vnc

override_dh_python3:
	dh_python3 --no-ext-rename

override_dh_auto_build:
	# Building package
	. out/env.sh && $(MAKE) $(NJOBS) \
	    AUTOCFG=$(abspath out/AutoConfig.kmk) \
	    LOCALCFG=$(abspath debian/LocalConfig.kmk) \
	    PATH_OUT=$(abspath out) \
	    TOOL_YASM_AS=yasm \
	    VBOX_USE_SYSTEM_XORG_HEADERS=1 \
	    VBOX_USE_SYSTEM_GL_HEADERS=1 \
	    VBOX_NO_LEGACY_XORG_X11=1 \
	    VBOX_BUILD_PUBLISHER=_$(DIST_NAME) \
	    VBOX_WITH_REGISTRATION_REQUEST= \
	    VBOX_WITH_UPDATE_REQUEST= \
	    KBUILD_VERBOSE=2 \
	    VBOX_WITH_EXTPACK_VBOXDTRACE=

	xsltproc --nonet --stringparam section.autolabel 1 \
	    -o debian/README.Debian.html \
	    /usr/share/xml/docbook/stylesheet/nwalsh/html/docbook.xsl \
	    debian/virtualbox.files/README.Debian.xml

	chmod 755 debian/lynx-dump-postprocess
	export LC_ALL=C; lynx -force_html -dump debian/README.Debian.html | \
	    ./debian/lynx-dump-postprocess > debian/virtualbox.README.Debian.tmp
	mv debian/virtualbox.README.Debian.tmp debian/virtualbox.README.Debian

override_dh_auto_install:
	dh_auto_install

ifneq (,$(filter $(sname), $(shell dh_listpackages)))
	export VBOX_INSTALL_PATH=$(INSTALL_PATH) && \
	    cd out/bin/sdk/installer && \
	    python3 ./vboxapisetup.py install --root $(CURDIR)/debian/$(sname)
endif

	sed -e 's/CVERSION/$(VERSION)/g' < debian/$(pdkms).links.in > debian/$(pdkms).links

	# Build install files for the dkms'ified sources
	sed -e 's/CVERSION/$(VERSION)/g' < debian/$(pdkms).install.in > debian/$(pdkms).install
	sed -e 's/CVERSION/$(VERSION)/g' < debian/$(gdkms).install.in > debian/$(gdkms).install

	echo "xserver:Depends=$(SERVER_DEPENDS)" >> debian/$(uxname).substvars

ifeq ($(DIST_NAME),Ubuntu)
	echo "modaliases=vboxguest(pci:v000080EEd0000CAFEsv00000000sd00000000bc*sc*i*)" >> debian/$(gdkms).substvars
	echo "modaliases=vboxx11(pci:v000080EEd0000BEEFsv00000000sd00000000bc*sc*i*)" >> debian/$(uxname).substvars
endif

override_dh_install:
ifneq (,$(filter $(sname), $(shell dh_listpackages)))
	dh_install -p$(sname) -X out/bin/VBoxDbg.so
endif
	dh_install --remaining-packages
#ifneq (,$(filter $(uxname), $(shell dh_listpackages)))
#	install -m 644 -p -D out/bin/additions/vboxvideo_drv_system.so \
#	    debian/$(uxname)/usr/lib/xorg/modules/drivers/vboxvideo_drv.so
#endif
# check if arch-any packages are being built
ifneq (,$(filter $(sxname), $(shell dh_listpackages)))
	convert debian/$(sxname)/usr/share/icons/hicolor/32x32/apps/virtualbox.png \
	    debian/$(sxname)/usr/share/pixmaps/virtualbox.xpm
endif

ifeq ($(DIST_NAME),Ubuntu)
	install -m 644 -p -D debian/apport-hook.py \
	    debian/$(sname)/usr/share/apport/package-hooks/source_$(sname).py
	install -m 644 -p -D debian/apport-hook.py \
	    debian/$(gdkms)/usr/share/apport/package-hooks/$(gdkms).py
endif

override_dh_fixperms:
	dh_fixperms

# check if arch-any packages are being built
ifneq (,$(filter $(sname), $(shell dh_listpackages)))
	chmod +s debian/$(sname)$(INSTALL_PATH)/VBoxSDL;
	chmod +s debian/$(sname)$(INSTALL_PATH)/VBoxHeadless;
	chmod +s debian/$(sname)$(INSTALL_PATH)/VBoxNetNAT;
	chmod +s debian/$(sname)$(INSTALL_PATH)/VBoxNetDHCP;
	chmod +s debian/$(sname)$(INSTALL_PATH)/VBoxNetAdpCtl;
	chmod 644 debian/$(sname)$(INSTALL_PATH)/*.r0;

	chmod 644 debian/$(sname)$(INSTALL_PATH)/sdk/bindings/xpcom/python/xpcom/*.py;
	chmod 644 debian/$(sname)$(INSTALL_PATH)/sdk/bindings/xpcom/python/xpcom/server/*.py;
	chmod 644 debian/$(sname)$(INSTALL_PATH)/sdk/bindings/xpcom/python/xpcom/client/*.py;
	chmod 644 debian/$(sname)$(INSTALL_PATH)/sdk/bindings/xpcom/java/vboxjxpcom.jar;

	chmod 755 debian/$(sname)/usr/share/virtualbox/VBox.sh;
endif

ifneq (,$(filter $(sxname), $(shell dh_listpackages)))
	chmod +s debian/$(sxname)$(INSTALL_PATH)/VirtualBoxVM;
endif

ifneq (,$(filter $(uxname), $(shell dh_listpackages)))
	chmod 755 debian/$(uxname)/usr/share/virtualbox/x11config.pl;
endif

# check if arch-all packages are being built
ifneq (,$(filter $(psource), $(shell dh_listpackages)))
	# These files have incorrect permissions, fixing it.
	chmod 755 debian/$(psource)/usr/src/modules/$(sname)/debian/rules

	# this needs all prior work to be finished
	cd debian/$(psource)/usr/src && \
	    tar -cjf $(sname).tar.bz2 modules && \
	    rm -rf modules

	chmod 644 debian/$(psource)/usr/src/$(sname).tar.bz2
endif

# check if arch-all packages are being built
ifneq (,$(filter $(gsource), $(shell dh_listpackages)))
	# These files have incorrect permissions, fixing it.
	chmod 755 debian/$(gsource)/usr/src/modules/$(gname)/debian/rules

	# this needs all prior work to be finished
	cd debian/$(gsource)/usr/src && \
	    tar -cjf $(gname).tar.bz2 modules && \
	    rm -rf modules

	chmod 644 debian/$(gsource)/usr/src/$(gname).tar.bz2
endif

override_dh_installinit:
	dh_installinit -p$(uxname) --no-start --update-rcd-params="start 45 S ."
	dh_installinit --remaining-packages --no-start

override_dh_systemd_enable:
ifneq (,$(filter $(sname), $(shell dh_listpackages)))
	dh_systemd_enable -p$(sname) --name vboxweb --no-enable debian/vboxweb.service
endif
	dh_systemd_enable -p$(uname) debian/$(uname).service

override_dh_dkms:
	dh_dkms -V $(VERSION)

override_dh_strip:
ifneq (,$(filter $(sname), $(shell dh_listpackages)))
	dh_strip --dbgsym-migration='$(sname)-dbg'

	# strip files not handled by dh_strip
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	set -e && for file in debian/$(sname)/usr/lib/virtualbox/*.r0; do \
	    strip --strip-debug --remove-section=.comment --remove-section=.note $${file}; \
	done
endif
	# I don't see a reason to have these. If any user complains, we can try ship it.
	rm -f debian/$(sname)/usr/lib/virtualbox/VMMRC.debug
	rm -f debian/$(sname)/usr/lib/virtualbox/VMMR0.debug
endif

override_dh_shlibdeps:
ifneq (,$(filter $(sname), $(shell dh_listpackages)))
	dh_shlibdeps -p$(sname) -X debian/$(sname)$(INSTALL_PATH)/VBoxTestOGL -- \
	    -dRecommends debian/$(sname)$(INSTALL_PATH)/VBoxTestOGL -dDepends
endif
	dh_shlibdeps --remaining-packages

override_dh_compress:
	dh_compress -X.pdf

override_dh_auto_clean:
	dh_auto_clean

	# Cleaning package
	rm -rf out
	rm -f build-stamp
	rm -f debian/virtualbox.README.Debian debian/README.Debian.html
	rm -f debian/$(pdkms).install
	rm -f debian/$(gdkms).install
	rm -f debian/$(pdkms).links
	rm -f debian/$(gdkms).links

	find . -name "*.pyc" -exec rm -f {} \;

