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

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

export GOPATH=$(CURDIR)
#export GOCACHE="$(mkdir -p "${GOPATH}/cache" &>/dev/null; cd "${GOPATH}/cache" && echo "$PWD" && cd - &>/dev/null)"
export GOCACHE=${GOPATH}/cache
export PATH=${HOME}/.local/bin:/snap/bin:/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin:/usr/local/sbin

PKGDIR=debian/pvx-caddy

%:
	dh $@ 

clean:
	dh_clean
	rm -f "${GOPATH}/caddy"
	#bash -c "[[ -d '${GOPATH}/pkg' ]] && rm -rf '${GOPATH}/pkg' || true"
	#bash -c "[[ -d '${GOPATH}/src' ]] && rm -rf '${GOPATH}/src' || true"
	bash -c "[[ -d '${GOCACHE}' ]] && rm -rf '${GOCACHE}' || true"
	#cd $(GOPATH)/src && find * -name '*.go' -exec dirname {} \; | xargs -n1 go clean
	rm -f "${GOPATH}/goinstall.log"

binary-arch: clean
	dh_prep
	dh_installdirs
	mkdir -p "${GOCACHE}"
	mkdir -p "${GOPATH}/src/github.com"
	xcaddy build v2.2.1 --with github.com/mholt/caddy-webdav \
			--with github.com/abiosoft/caddy-json-parse \
			--with github.com/caddy-dns/cloudflare \
			--with github.com/gamalan/caddy-tlsredis
	cp -vr "${GOPATH}/usr" "${GOPATH}/lib" "${GOPATH}/etc" "${PKGDIR}/"
	mkdir -pv "${PKGDIR}/usr/bin"
	cp -v "${GOPATH}/caddy" "${PKGDIR}/usr/bin/"
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-arch

