#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

export DH_GOPKG := github.com/rfjakob/gocryptfs
export DH_GOLANG_EXCLUDES := example test
export DH_GOLANG_INSTALL_ALL := 1

PKD = $(word 1,$(abspath $(dir $(MAKEFILE_LIST))))
#PKG = $(shell dpkg-parsechangelog -l$(PKD)/changelog --show-field=Source)
PKG = $(DEB_SOURCE)

DEBIAN_VERSION = $(shell (echo $(DEB_VERSION_UPSTREAM) | sed 's/\+dfsg1//'))
#PRODUCT_VERSION = $(shell (grep "version=" setup.py | sed "s/.\+'\(.\+\)'.\+/\1/"))

GOFUSE_VERSION_UPSTREAM=$(shell dpkg-query -f='$${source:Upstream-Version}' -W golang-github-hanwen-go-fuse-dev)
#CHANGELOG_TIMESTAMP=$(shell date -u --rfc-3339=seconds -d@$(SOURCE_DATE_EPOCH))
CHANGELOG_TIMESTAMP=$(shell date -u --rfc-3339=date -d@$(SOURCE_DATE_EPOCH))
DEB_LDFLAGS="-X main.GitVersion=$(DEB_VERSION_UPSTREAM) -X main.GitVersionFuse=$(GOFUSE_VERSION_UPSTREAM) -X main.BuildDate=$(CHANGELOG_TIMESTAMP)"

%:
	dh $@ --buildsystem=golang --with=golang

CHDATE = $(shell grep "\-\-" debian/changelog | head -1 | sed 's/.\+, //' | sed 's/...\:.\+//' )
DT = $(shell date -d "${CHDATE}" "+%Y-%m-%d")

override_dh_auto_build:
	dh_auto_build -- -ldflags $(DEB_LDFLAGS)
	pandoc Documentation/MANPAGE.md -s -t man -o debian/gocryptfs.1
	pandoc Documentation/MANPAGE-XRAY.md -s -t man -o debian/gocryptfs-xray.1

override_dh_auto_clean:
	rm -f debian/gocryptfs.1
	rm -f debian/gocryptfs-xray.1
	dh_auto_clean

override_dh_auto_test:
	cp -r internal/* obj*/src/github.com/rfjakob/gocryptfs/internal/
	cp -r tests/* obj*/src/github.com/rfjakob/gocryptfs/tests
	cp obj*/bin/gocryptfs obj*/src/github.com/rfjakob/gocryptfs/
	dh_auto_test
	rm -f obj*/src/github.com/rfjakob/gocryptfs/gocryptfs

override_dh_auto_install:
	dh_auto_install
	rm -rf debian/gocryptfs/usr/share/gocode
