#!/usr/bin/make -f

export DEB_VERSION=$(shell dpkg-parsechangelog | grep -E '^Version:' | cut -f 2 -d ' ')
export BUILD_VERSION=v${DEB_VERSION}-debian-pragmatic

%:
	dh $@

override_dh_systemd_start:
	echo "Not running dh_systemd_start"
override_dh_auto_clean:
override_dh_auto_test:
override_dh_auto_build:
override_dh_auto_install:
	@make build

	@BOUNCER=crowdsec-firewall-bouncer; \
	for BACKEND in iptables nftables; do \
		PKG="$$BOUNCER-$$BACKEND"; \
		install -D $$BOUNCER -t "debian/$$PKG/usr/bin/"; \
		install -D scripts/_bouncer.sh -t "debian/$$PKG/usr/lib/$$PKG/"; \
		BACKEND=$$BACKEND envsubst '$$BACKEND' < config/$$BOUNCER.yaml | install -D /dev/stdin "debian/$$PKG/etc/crowdsec/bouncers/$$BOUNCER.yaml"; \
		BIN="/usr/bin/$$BOUNCER" CFG="/etc/crowdsec/bouncers" envsubst '$$BIN $$CFG' < "config/$$BOUNCER.service" | install -D /dev/stdin "debian/$$PKG/etc/systemd/system/$$BOUNCER.service"; \
	done

execute_after_dh_fixperms:
	@BOUNCER=crowdsec-firewall-bouncer; \
	for BACKEND in iptables nftables; do \
		PKG="$$BOUNCER-$$BACKEND"; \
		chmod 0755 "debian/$$PKG/usr/bin/$$BOUNCER"; \
		chmod 0600 "debian/$$PKG/usr/lib/$$PKG/_bouncer.sh"; \
		chmod 0600 "debian/$$PKG/etc/crowdsec/bouncers/$$BOUNCER.yaml"; \
		chmod 0644 "debian/$$PKG/etc/systemd/system/$$BOUNCER.service"; \
	done
