#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS  = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

%:
	dh $@ --with=golang

MODES = address blast citmatch contact filter link notify post proxy search \
        spell
STD_WRAPPERS = $(MODES:%=bin/e%)
WRAPPERS = $(STD_WRAPPERS) bin/esummary
AS_IS_SCRIPTS = amino-acid-composition archive-pubmed between-two-genes \
                entrez-phrase-search fetch-pubmed filter-stop-words \
                index-pubmed intersect-uid-lists join-into-groups-of \
                pm-* protein-neighbors reorder-columns \
                sort-uniq-count* stream-pubmed word-at-a-time xy-plot
DL_SCRIPTS = download-pubmed download-sequence
BIN_DL_SCRIPTS = $(DL_SCRIPTS:%=bin/%)
PERL_SCRIPTS = edirutil ftp-cp ftp-ls gbf2xml nquire run-ncbi-converter \
               transmute
BIN_PERL_SCRIPTS = $(PERL_SCRIPTS:%=bin/%)
FIX_PERL_SHEBANG = 1s,^\#!/usr/bin/env perl$$,\#!/usr/bin/perl,

GOCODE    = /usr/share/gocode
GOLIBSRC_ = github.com/fatih/color github.com/klauspost/cpuid \
            github.com/mattn/go-colorable github.com/mattn/go-isatty \
            github.com/pbnjay/memory github.com/rainycape/unidecode \
            github.com/shirou/gopsutil github.com/surgebase/porter2 \
            golang.org/x/sys/unix golang.org/x/text
GOLIBSRC  = $(GOLIBSRC_:%=$(GOCODE)/src/%)

export GOPATH = $(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)
GOLIBS = $(GOLIBSRC:$(GOCODE)/%=$(GOPATH)/%)

GOVERSION := $(word 3,$(shell go version)) # go version **goX.Y.Z** OS/CPU
GO_IS_OLD := $(shell dpkg --compare-versions 0$(GOVERSION) ge 0go1.10 || echo $$?)
ifneq (,$(GO_IS_OLD))
  export PATH := /usr/lib/go-1.10/bin:$(PATH)
endif

$(GOPATH)/%: /usr/share/gocode/%
	mkdir -p $(dir $@)
	ln -s $< $@

$(GOPATH)/src/%: debian/vendor/%
	mkdir -p $(dir $@)
	ln -s $(CURDIR)/$< $@

bin/edirect: edirect.pl
	mkdir -p bin
	sed -e '1s,^#!/usr/bin/env perl$$,#!/usr/bin/perl,' $< > $@
	chmod +x $@

$(STD_WRAPPERS): bin/e%: bin/edirect
	echo '#!/bin/sh' > $@
	echo 'exec /usr/bin/edirect -$* "$$@"' >> $@
	chmod +x $@

bin/esummary: bin/edirect
	echo '#!/bin/sh' > $@
	echo 'exec /usr/bin/edirect -fetch -format docsum "$$@"' >> $@
	chmod +x $@

$(BIN_PERL_SCRIPTS): bin/%: %
	mkdir -p bin
	sed -e '$(FIX_PERL_SHEBANG)' $< > $@
	chmod +x $@

$(BIN_DL_SCRIPTS): bin/%: %
	mkdir -p bin
	sed -e 's/`has-asp`/false # &/' $< > $@
	chmod +x $@

override_dh_auto_build: $(WRAPPERS) $(BIN_DL_SCRIPTS) $(BIN_PERL_SCRIPTS) \
                        $(GOLIBS)
	dh_auto_build
	install $(AS_IS_SCRIPTS) debian/efetch debian/einfo bin/
	mkdir -p $(GOPATH)/src/github.com/fiam
	ln -s ../rainycape $(GOPATH)/src/github.com/fiam/gounidecode
# Always use gccgo?  The resulting binary would be much smaller, but
# pull in a large shared library with few other reverse dependencies.
	go build -v -gccgoflags '$(CFLAGS) $(LDFLAGS)' -o bin/xtract \
	    xtract.go common.go
	go build -v -gccgoflags '$(CFLAGS) $(LDFLAGS)' -o bin/rchive \
	    rchive.go common.go

override_dh_installexamples:
	dh_installexamples
	sed -i '$(FIX_PERL_SHEBANG)' debian/*/usr/share/doc/*/examples/*

override_dh_auto_clean:
	dh_auto_clean
	rm -rf bin $(GOPATH)
