#!/usr/bin/env make

# Copyright (C) 2025  Patrick "tyil" Spek <p.spek@tyil.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

DIST_VERSION = 1.0.0
MAKEFILE := $(lastword $(MAKEFILE_LIST))
SOURCE_DATE_EPOCH := $(shell git show -s --format=%ct)
GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)

build:
	mkdir -p -- dist
	go build -o dist/dashlab-$(DIST_VERSION)-$(GOOS)-$(GOARCH) *.go

dist:
	# Create source distribution
	mkdir -p -- dist
	tar \
		--exclude dist \
		--group=0 \
		--mtime="@$(SOURCE_DATE_EPOCH)" \
		--numeric-owner \
		--owner=0 \
		--pax-option="exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime" \
		--sort=name \
		--transform "s@^./@dashlab-$(DIST_VERSION)/@" \
		--exclude-vcs \
		--exclude-vcs-ignores \
		-cvzf "dist/dashlab-$(DIST_VERSION).tar.gz" .

	# Do some common builds -- go tool dist list
	@$(MAKE) GOOS=freebsd  GOARCH=386   -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=freebsd  GOARCH=amd64 -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=freebsd  GOARCH=arm64 -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=linux    GOARCH=386   -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=linux    GOARCH=amd64 -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=linux    GOARCH=arm64 -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=netbsd   GOARCH=386   -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=netbsd   GOARCH=amd64 -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=netbsd   GOARCH=arm64 -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=openbsd  GOARCH=386   -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=openbsd  GOARCH=amd64 -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=openbsd  GOARCH=arm64 -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=plan9    GOARCH=386   -f "$(MAKEFILE)" build
	@$(MAKE) GOOS=plan9    GOARCH=amd64   -f "$(MAKEFILE)" build

install: build
	install --mode=755 --owner=root --group=root dist/dashlab /usr/bin/dashlab

uninstall:
	rm -fr -- /usr/bin/dashlab

clean:
	rm -fr -- dist

.PHONY: build install uninstall clean dist
