#!/usr/bin/make -f

DESTDIR := $(CURDIR)/debian/xmr-stak-cpu
DEB_BUILD_GNU_ARCH := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# Get version from changelog as well as lsb_release
# to compute unique build version (useful for repo mirroring systems doing file pooling)
PKG_VERSION  := $(shell dpkg-parsechangelog | grep -E '^Version:' | cut -d' ' -f2)
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)
PKG_VERSION_TAG := $(PKG_VERSION)+$(DIST_NAME).$(DIST_RELEASE).$(DIST_VERSION)

%:
	dh $@ --parallel --with systemd

override_dh_auto_configure:
	dh_auto_configure -- -DMICROHTTPD_ENABLE=ON \
	                     -DOpenSSL_ENABLE=ON \
	                     -DHWLOC_ENABLE=ON \
	                     -DCMAKE_LINK_STATIC=ON \
	                     -DCMAKE_BUILD_TYPE=Release
	# Fix default config file to match debian packaging
	sed -i 's|config.txt|/etc/xmr-stak-cpu/config.txt|' cli-miner.cpp
	# Disable donation
	sed -i 's|fDevDonationLevel\s*=\s*.*;|fDevDonationLevel = 0.0;|' donate-level.h

override_dh_auto_install:
	install -D -o root -g root -m 0644 config.txt \
	                                   $(DESTDIR)/etc/xmr-stak-cpu/config.txt.template
	install -D -o root -g root -m 0755 obj-$(DEB_BUILD_GNU_ARCH)/bin/xmr-stak-cpu \
	                                   $(DESTDIR)/usr/bin/xmr-stak-cpu
	install -D -o root -g root -m 0755 debian/create-xmr-stak-cpu-config \
	                                   $(DESTDIR)/usr/bin/create-xmr-stak-cpu-config

override_dh_auto_clean:
	dh_auto_clean
	# Restore default config.txt path and donation level
	sed -i 's|/etc/xmr-stak-cpu/config.txt|config.txt|' cli-miner.cpp
	sed -i 's|fDevDonationLevel\s*=\s*.*;|fDevDonationLevel = 2.0 / 100.0;|' donate-level.h

override_dh_systemd_enable:
	dh_systemd_enable --no-enable

override_dh_systemd_start:
	dh_systemd_start --no-start

override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)"
