#!/usr/bin/make -f

export DH_GOLANG_EXCLUDES := scripts/fork_go_templates tpl/tplimpl/embedded/generate

# Prevent TestImageResize8BitPNG from failing due to pkgbinarymangler
# recompressing our test PNG image on Ubuntu
export NO_PNG_PKG_MANGLE := 1

HUGO := _build/bin/hugo

DATE_FMT = %Y-%m-%dT%H:%M:%S%z
ifdef SOURCE_DATE_EPOCH
	BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)" 2>/dev/null || date -u "+$(DATE_FMT)")
else
	BUILD_DATE ?= $(shell date "+$(DATE_FMT)")
endif
BUILD_DATE := $(BUILD_DATE:+0000=Z)

%:
	dh $@ --builddirectory=_build --buildsystem=golang --with bash-completion,golang

execute_after_dh_auto_configure:
	# github.com/gohugoio/testmodBuilder is for testing only
	# and will not be packaged separately for Debian
	cp -a debian/go/src/github.com/gohugoio/testmodBuilder \
	    _build/src/github.com/gohugoio/

	# Temporary local copies, pending packaging for Debian
	cp -a debian/go/src/github.com/getkin _build/src/github.com/
	cp -a debian/go/src/gocloud.dev _build/src/

	# Convert symlinks to real directories to prepare for further tweaking
	cd _build/src/github.com && \
	    for i in tdewolff; do \
	        if [ -L "$$i" ]; then \
	            $(RM) $$i && \
	            mkdir $$i && \
	            for j in /usr/share/gocode/src/github.com/$$i/*; do \
	                cp -a $$j $$i/; \
	            done; \
	        fi; \
	    done

	# Downgrade github.com/tdewolff/minify to v2.6.2
	cd _build/src/github.com && \
	    $(RM) -r tdewolff/minify && \
	    cp -a ../../../debian/go/src/github.com/tdewolff/minify tdewolff/

override_dh_auto_build:
	dh_auto_build -O--buildsystem=golang -- -ldflags '-X github.com/gohugoio/hugo/common/hugo.buildDate=$(BUILD_DATE)' -tags 'dev extended'
	$(HUGO) version
	mkdir -p debian/completion/bash
	$(HUGO) gen autocomplete --completionfile=debian/completion/bash/hugo
	$(HUGO) gen man --dir=debian/man
	# TODO: Add "hugo-docs" package in the future?
	#$(HUGO) -s docs -d public/html --uglyURLs

# Disable network access during testing by using -test.short flag
override_dh_auto_test:
	dh_auto_test -- -test.short -timeout 120m

override_dh_auto_install:
	dh_auto_install -- --no-source
