#!/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 esample exclude-uid-lists fetch-pubmed \
                filter-stop-words index-bioc index-pubmed intersect-uid-lists \
                join-into-groups-of pm-* protein-neighbors reorder-columns \
                sort-uniq-count* stream-pubmed word-at-a-time xml2tbl xy-plot
DL_SCRIPTS = download-ncbi-data 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
GH        = github.com
GL        = golang.org
GOLIBSRC_ = $(GH)/fatih/color $(GH)/gedex/inflector $(GH)/klauspost/cpuid \
            $(GH)/mattn/go-colorable $(GH)/mattn/go-isatty \
            $(GH)/pbnjay/memory $(GH)/rainycape/unidecode \
            $(GH)/shirou/gopsutil $(GH)/surgebase/porter2 \
            $(GL)/x/sys/unix $(GL)/x/text
GOLIBSRC  = $(GOLIBSRC_:%=$(GOCODE)/src/%)

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

GO_APPS     = j2x rchive xtract
BIN_GO_APPS = $(GO_APPS:%=bin/%)

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 '$(FIX_PERL_SHEBANG)' $< > $@
	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 $@

$(GOPATH)/src/$(GH)/fiam/gounidecode: $(GOLIBS)
	mkdir -p $(GOPATH)/src/$(GH)/fiam
	ln -s ../rainycape $(GOPATH)/src/$(GH)/fiam/gounidecode

COMMON = common.go
bin/j2x: COMMON = 
$(BIN_GO_APPS): bin/%: %.go $(GOPATH)/src/$(GH)/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 $@ $*.go $(COMMON)

override_dh_auto_build: $(WRAPPERS) $(BIN_DL_SCRIPTS) $(BIN_PERL_SCRIPTS) \
                        $(BIN_GO_APPS)
	dh_auto_build
	install $(AS_IS_SCRIPTS) debian/efetch debian/einfo bin/

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 $(GOCACHE) $(GOPATH)
