#!/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

PACKAGE = ncbi-entrez-direct
LIB_DIR = /usr/lib/$(PACKAGE)

MODES = address blast citmatch contact filter link notify post proxy search \
        spell
STD_WRAPPERS = $(MODES:%=bin/e%)
OTHER_WRAPPERS = esummary ftp-cp ftp-ls nquire transmute
WRAPPERS = $(STD_WRAPPERS) $(OTHER_WRAPPERS:%=bin/%)
AS_IS_SCRIPTS = accn-at-a-time amino-acid-composition archive-pubmed \
                between-two-genes entrez-phrase-search esample \
                exclude-uid-lists expand-current fetch-extras fetch-pubmed \
                filter-stop-words gbf2xml index-pubmed intersect-uid-lists \
                join-into-groups-of pm-* protein-neighbors reorder-columns \
                sort-uniq-count* stream-pubmed theme-aliases word-at-a-time \
                xml2tbl xy-plot
BASH_SCRIPTS = index-extras index-themes
BIN_BASH_SCRIPTS = $(BASH_SCRIPTS:%=bin/%)
DL_SCRIPTS = download-ncbi-data download-pubmed download-sequence
BIN_DL_SCRIPTS = $(DL_SCRIPTS:%=bin/%)
PERL_SCRIPTS = edirutil run-ncbi-converter
BIN_PERL_SCRIPTS = $(PERL_SCRIPTS:%=bin/%)
# Only bt-link and xplore need this treatment at present,
# but list all bt-* scripts to be safe.
BT_SCRIPTS = bt-link bt-load bt-save bt-srch xplore
BIN_BT_SCRIPTS = $(BT_SCRIPTS:%=bin/%)
OTHER_SCRIPTS = enquire
BIN_OTHER_SCRIPTS = $(OTHER_SCRIPTS:%=bin/%)
FIX_PERL_SHEBANG = 1s,^\#!/usr/bin/env perl$$,\#!/usr/bin/perl,
FIX_BASH_SHEBANG = 1s,^\#!/bin/sh,\#!/bin/bash,

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 $(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     = g2x j2x rchive t2x 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/enquire: enquire
	mkdir -p bin
	sed -e 's/ --ca.*\.pem//' $< > $@
	chmod +x $@

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

bin/ftp-cp: bin/edirect
	echo '#!/bin/sh' > $@
	echo 'exec /usr/bin/edirect -ftpcp "$$@"' >> $@
	chmod +x $@

bin/ftp-ls: bin/edirect
	echo '#!/bin/sh' > $@
	echo 'exec /usr/bin/edirect -ftpls "$$@"' >> $@
	chmod +x $@

bin/nquire: bin/edirect
	echo '#!/bin/sh' > $@
	echo 'exec /usr/bin/edirect -nquir "$$@"' >> $@
	chmod +x $@

bin/transmute: bin/edirect
	echo '#!/bin/sh' > $@
	echo 'exec /usr/bin/edirect -tmute "$$@"' >> $@
	chmod +x $@

$(BIN_BASH_SCRIPTS): bin/%: %
	mkdir -p bin
	sed -e '$(FIX_BASH_SHEBANG)' $< > $@
	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 $@

$(BIN_BT_SCRIPTS): bin/%: %
	mkdir -p bin
	sed -e 's,"*\$$dir"*\(/bt-[^ .]*\.txt\)"*,$(LIB_DIR)\1,g' $< > $@
	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/g2x: COMMON = 
bin/j2x: COMMON = 
bin/t2x: 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_configure:
	mkdir -p saved
	-mv go.mod go.sum s2p.go saved/

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

override_dh_install:
	dh_install
	mv debian/$(PACKAGE)/usr/bin/xtract debian/$(PACKAGE)/$(LIB_DIR)/
	printf '#!/bin/sh\nexec $(LIB_DIR)/xtract "$$@"\n' \
	    > debian/$(PACKAGE)/usr/bin/xtract
	chmod +x debian/$(PACKAGE)/usr/bin/xtract

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)
	if [ -d saved ]; then mv saved/* . && rmdir saved; fi
