ROUTE_ASSET=src/routes/bindata.go
DOCS_ASSET=src/docs/bindata.go

SOURCE=$(wildcard cmd/paste/*.go) $(filter-out src/routes/bindata.go, $(wildcard src/routes/*.go))
BINARY=paste

VERSION:=$(shell debian/inc_version.sh -p $(shell git describe --tags `git rev-list --tags --max-count=1`))
DATE:=$(shell date -u +%FT%TZ)

define DUMMY_BINDATA
package docs
import "net/http"
func assetFS() (fs http.FileSystem) { return }
endef
export DUMMY_BINDATA

all: $(BINARY)
clean:
	rm -rf $(BINARY)

setup:
	go mod download
	go mod vendor
fmt:
	go fmt ./...
test: $(ROUTE_ASSET) $(DOCS_ASSET)
	go test ./...
	go vet ./...
run: $(BINARY)
	./$(BINARY) -vv serve

$(BINARY): $(SOURCE) $(ROUTE_ASSET) $(DOCS_ASSET)
	go build \
        -ldflags "-X main.AppVersion=$(VERSION) -X main.AppBuild=$(DATE)" \
      "sour.is/x/paste/cmd/paste"

clean-ui:
	rm -rf $(ROUTE_ASSET) $(DOCS_ASSET)
build-ui: clean-ui $(ROUTE_ASSET) $(DOCS_ASSET)
$(ROUTE_ASSET):
	cd assets; \
	rm -rf build ../public; \
		npm i; npm run build; \
	cp -r build ../public
	go generate "sour.is/x/paste/src/routes"

$(DOCS_ASSET):
	echo "$$DUMMY_BINDATA" > src/docs/bindata.go
	go generate "sour.is/x/paste/cmd/paste"
	go generate "sour.is/x/paste/src/docs"
deploy: $(SOURCE) $(ROUTE_ASSET)
	cd debian && make

.PHONEY: all clean build deploy run setup
# DO NOT DELETE