#!/usr/bin/make -f

export DEB_VERSION=$(shell dpkg-parsechangelog -SVersion)
export BUILD_VERSION=v${DEB_VERSION}-debian-pragmatic

%:
	dh $@

override_dh_auto_clean:
override_dh_auto_test:
override_dh_auto_build:
override_dh_auto_install:

	# just use the prebuilt binaries, otherwise:
	# make build BUILD_STATIC=1

	mkdir -p debian/crowdsec/usr/bin
	mkdir -p debian/crowdsec/etc/crowdsec
	mkdir -p debian/crowdsec/etc/crowdsec/acquis.d
	mkdir -p debian/crowdsec/usr/share/crowdsec
	mkdir -p debian/crowdsec/etc/crowdsec/hub/
	mkdir -p debian/crowdsec/etc/crowdsec/console/
	mkdir -p debian/crowdsec/var/lib/crowdsec/data

	mkdir -p debian/crowdsec/usr/lib/crowdsec/plugins/
	mkdir -p debian/crowdsec/etc/crowdsec/notifications/
	mkdir -p debian/crowdsec/usr/libexec/crowdsec/

	# keep the plugin dir in /usr/lib/crowdsec as it's referenced in config.yaml

	install -m 551 \
		cmd/notification-slack/notification-slack \
		cmd/notification-http/notification-http \
		cmd/notification-splunk/notification-splunk \
		cmd/notification-email/notification-email \
		cmd/notification-sentinel/notification-sentinel \
		cmd/notification-file/notification-file \
		debian/crowdsec/usr/lib/crowdsec/plugins/

	install -m 600 \
		cmd/notification-slack/slack.yaml \
		cmd/notification-http/http.yaml \
		cmd/notification-splunk/splunk.yaml \
		cmd/notification-email/email.yaml \
		cmd/notification-sentinel/sentinel.yaml \
		cmd/notification-file/file.yaml \
		debian/crowdsec/etc/crowdsec/notifications/

	cp cmd/crowdsec/crowdsec debian/crowdsec/usr/bin
	cp cmd/crowdsec-cli/cscli debian/crowdsec/usr/bin
	install -m 600 config/config.yaml debian/crowdsec/etc/crowdsec/config.yaml
	cp config/simulation.yaml debian/crowdsec/etc/crowdsec/simulation.yaml
	cp config/profiles.yaml debian/crowdsec/etc/crowdsec/profiles.yaml
	cp config/context.yaml debian/crowdsec/etc/crowdsec/console/context.yaml
	cp config/console.yaml debian/crowdsec/etc/crowdsec/console.yaml
	cp -a config/patterns debian/crowdsec/etc/crowdsec
	cp config/detect.yaml debian/crowdsec/var/lib/crowdsec/data/detect.yaml
	install -m 755 debian/hubupdate.sh debian/crowdsec/usr/libexec/crowdsec/hubupdate.sh

override_dh_fixperms:
	dh_fixperms
	chmod 600 debian/crowdsec/etc/crowdsec/notifications/*
	chmod 600 debian/crowdsec/etc/crowdsec/config.yaml

override_dh_installsystemd:
	install -D -m 644 debian/crowdsec-hubupdate.service debian/crowdsec/usr/lib/systemd/system/crowdsec-hubupdate.service
	install -D -m 644 debian/crowdsec-hubupdate.timer debian/crowdsec/usr/lib/systemd/system/crowdsec-hubupdate.timer
	dh_installsystemd
