#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
DEB_BUILD_ARCH_OS := $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DRIVERS= --enable-avalon \
         --enable-avalon2 \
         --enable-bab \
         --enable-bflsc \
         --enable-bitforce \
         --enable-bitfury \
         --enable-cointerra \
         --enable-drillbit \
         --enable-hashfast \
         --enable-icarus \
         --enable-klondike \
         --enable-minion \
         --enable-modminer

ifeq ($(DEB_BUILD_ARCH_OS),linux)
#           --enable-ants2 \ only one of ants1 or ants2 allowed
DRIVERS+= --enable-ants1 \
           --enable-bitmine_A1 \
           --enable-knc \
           --enable-sp10 \
           --enable-sp30
endif

%:
	dh $@ --with autoreconf

override_dh_auto_configure:
	dh_auto_configure -- --bindir=/usr/lib/cgminer $(DRIVERS) --with-system-libusb --disable-silent-rules

override_dh_auto_build:
	dh_auto_build
	$(CC) api-example.c $(shell dpkg-buildflags --get CFLAGS) \
                        $(shell dpkg-buildflags --get LDFLAGS) \
                        $(shell dpkg-buildflags --get CPPFLAGS) \
                        -I /usr/include/libusb-1.0/ -o cgminer-api


#generates manpage, done during build since each arch has different config flags
MAN_NAME=multi-threaded multi-pool GPU, FPGA and CPU bitcoin miner.
debian/cgminer.1:
	help2man --no-discard-stderr --no-info --name="$(MAN_NAME)" ./cgminer > debian/cgminer.1
	perl \
             -E 's{\s+(It was generated by help2man)}{ $$1};   # correcting help2man comment'               \
             -E 's{^cgminer\s+[\d.]+$$}{$(MAN_NAME)};          # correcting DESCRIPTION section'            \
             -E 's{\.PP\K}{\n.SH OPTIONS\n};                   # create OPTIONS section'                    \
             -E 's{^(Options\s+for)}{.SS\n$$1};                # separate Options paragraphs'               \
             -E 's{^(\\fB\\-\\-)}{.TP\n$$1};                   # prepend each option with .TP'              \
             -E 's{(?:<arg>\s+|\s\s+|\\fR\s+(?!<arg>))\K}{\n}; # separate arguments and their descriptions' \
          -pi $@

override_dh_installman: debian/cgminer.1
	dh_installman

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_builddeb:
	dh_builddeb -- -Zxz

copyright_check:
	$(MAKE) -f /usr/share/cdbs/1/rules/utils.mk debian/stamp-copyright-check
	rm debian/stamp-copyright-check

DPATH := $(abspath $(dir $(MAKEFILE_LIST)))
VER ?= $(shell dpkg-parsechangelog -l$(DPATH)/changelog | perl -ne 'print $$1 if m{Version:\s*([\d\.]+)}')
.PHONY: get-orig-source
get-orig-source:
	@echo "# Downloading..."
	uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(DPATH)
