#!/usr/bin/make -f
export DH_VERBOSE = 1
export DESTDIR := $(shell pwd)/debian/opensnitch
export UIDESTDIR := $(shell pwd)/debian/python3-opensnitch-ui
export EBPFDESTDIR := $(shell pwd)/debian/opensnitch-ebpf-modules

ifeq ($(DEB_BUILD_ARCH),amd64)
  WITH_EBPF := true
else ifeq ($(DEB_BUILD_ARCH),arm64)
  WITH_EBPF := true
else ifeq ($(DEB_BUILD_ARCH),riscv64)
  WITH_EBPF := true
else ifeq ($(DEB_BUILD_ARCH),s390x)
  WITH_EBPF := true
else ifeq ($(DEB_BUILD_ARCH),loong64)
  WITH_EBPF := true
else ifeq ($(DEB_BUILD_ARCH),ppc64)
  WITH_EBPF := true
else
  WITH_EBPF := false
endif

# The Linux 7.0 headers (openKylin huanghe) declare some struct members with
# the Microsoft "anonymous struct/union member by tag" extension, e.g.
# include/linux/fs.h ("struct filename { struct __filename_head; ... }")
# and include/linux/ns/ns_common_types.h ("union { struct ns_tree; ... }")
# in order to keep the struct layout compact.  The kernel itself builds
# with that extension enabled (Makefile: KBUILD_CFLAGS += -fms-extensions),
# and clang needs the same flag when compiling the eBPF programs against
# those headers: without it the anonymous members are silently dropped and
# clang fails with "declaration does not declare anything" followed by
# static_assert("sizeof(struct filename) % 64 == 0") and "no member named
# 'ns_id' in 'struct ns_common'" errors.
# ebpf_prog/Makefile forwards EXTRA_FLAGS to clang.  Only 32 bit arm uses
# EXTRA_FLAGS for its own value (-D__LINUX_ARM_ARCH__=7) and eBPF is not
# built for armhf, so overriding it here is safe.
EBPF_EXTRA_FLAGS := -fms-extensions

override_dh_installsystemd:
	dh_installsystemd --restart-after-upgrade

override_dh_auto_build:
	$(MAKE) protocol
# Workaround for Go build problem when building in _build
	mkdir -p _build/src/github.com/evilsocket/opensnitch/daemon/ui/protocol/
	cp daemon/ui/protocol/* _build/src/github.com/evilsocket/opensnitch/daemon/ui/protocol/
	dh_auto_build
	cd ui && python3 setup.py build --force
	if $(WITH_EBPF) ; then make -C ebpf_prog EXTRA_FLAGS=$(EBPF_EXTRA_FLAGS); fi

override_dh_auto_install:
# daemon
	mkdir -p $(DESTDIR)/usr/bin
	cp _build/bin/daemon $(DESTDIR)/usr/bin/opensnitchd
# GUI
	make -C ui/i18n
	cp -r ui/i18n/locales/ ui/opensnitch/i18n/
	pyrcc5 -o ui/opensnitch/resources_rc.py ui/opensnitch/res/resources.qrc
	sed -i 's/^import ui_pb2/from . import ui_pb2/' ui/opensnitch/ui_pb2*
	cd ui && python3 setup.py install --force --root=$(UIDESTDIR) --no-compile -O0 --install-layout=deb

# ebpf modules
	if $(WITH_EBPF); then \
	  mkdir -p $(EBPFDESTDIR)/usr/lib/opensnitchd/ebpf ; \
	  make -C ebpf_prog EXTRA_FLAGS=$(EBPF_EXTRA_FLAGS) && cp ebpf_prog/opensnitch*o $(EBPFDESTDIR)/usr/lib/opensnitchd/ebpf/ ; \
	fi

# daemon
	dh_auto_install

%:
	dh $@ --builddirectory=_build --buildsystem=golang --with=golang,python3

override_dh_auto_clean:
	dh_auto_clean
	$(MAKE) clean
	$(RM) daemon/ui/protocol/ui_grpc.pb.go
	$(RM) ui/opensnitch/resources_rc.py
	$(RM) -r ui/opensnitch/i18n/
	$(RM) ui/i18n/locales/*/*.qm
	cd ui && python3 setup.py clean -a
	$(RM) -r ui/opensnitch_ui.egg-info/
	find ui -name \*.pyc -exec rm {} \;
	$(MAKE) -C ebpf_prog/ clean
