# Makefile for gif2png
#
# SPDX-FileCopyrightText: (C) Eric S. Raymond <esr@thyrsus.com>
# SPDX-License-Identifier: BSD-2-Clause

PREFIX      ?= /usr/local
BINDIR      ?= $(PREFIX)/bin
DATADIR     ?= $(PREFIX)/share
MANDIR      ?= $(DATADIR)/man

TAR = tar

VERSION = $(shell sed -n <NEWS.adoc '/::/s/^\([^:]*\).*/\1/p' | head -1)

# Rules

# Note: to suppress the footers with timestamps being generated in HTML,
# we use "-a nofooter".
# To debug asciidoc problems, you may need to run "xmllint --nonet --noout --valid"
# on the intermediate XML that throws an error.
.SUFFIXES: .html .adoc .1

.adoc.1:
	asciidoctor -D. -a nofooter -b manpage $<
.adoc.html:
	asciidoctor -D. -a nofooter -a webfonts! $<

.PHONY: all clean check spellcheck pylint reflow
.PHONY: install uninstall version dist release refresh

# Build

all: gif2png gif2png.1 web2png.1

GOBUILD_FLAGS = -ldflags '-X main.version=$(VERSION)'
gif2png: gif2png.go go.mod go.sum
	go build $(GOBUILD_FLAGS) -o gif2png gif2png.go

# Validate

check: gif2png
	@go test ./...
	@$(MAKE) --quiet -C tests test

clean:
	rm -f gif2png gif2png.1 web2png.1 index.html

# Validate

pylint:
	@pylint --score=n web2png

spellcheck:
	@spellcheck local.dic gif2png.adoc web2png.adoc INSTALL.adoc README.adoc

# Install/uninstall

install: gif2png gif2png.1 web2png.1
	install -d $(DESTDIR)$(BINDIR)
	install -m 755 gif2png $(DESTDIR)$(BINDIR)/gif2png
	install -m 755 web2png $(DESTDIR)$(BINDIR)/web2png
	install -d $(DESTDIR)$(MANDIR)
	install -d $(DESTDIR)$(MANDIR)/man1
	install -m 644 gif2png.1 $(DESTDIR)$(MANDIR)/man1/gif2png.1
	install -m 644 web2png.1 $(DESTDIR)$(MANDIR)/man1/web2png.1

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/gif2png
	rm -f $(DESTDIR)$(MANDIR)/man1/gif2png.1
	rm -f $(DESTDIR)$(BINDIR)/web2png
	rm -f $(DESTDIR)$(MANDIR)/man1/web2png.1

# Export

TEST_DIST = tests/.gitignore tests/Makefile tests/README \
	tests/666666.gif tests/666666.png-check \
	tests/ducks.gif tests/ducks.png-check \
	tests/gtcd.gif tests/gtcd.png-check \
	tests/ten-wire.gif tests/ten-wire.png-check \
	tests/snapshot01.gif tests/snapshot01.png \
	tests/test.html tests/test.html-gif \
	tests/links2.js tests/links2.js-gif \
	tests/style.css tests/style.css-gif \
	tests/test_gif2png.py tests/test_web2png.py tests/pngcmp.go
EXTRA_DIST = README.adoc NEWS.adoc INSTALL.adoc control Makefile web2png \
	gif2png.adoc web2png.adoc gif2png.1 web2png.1 \
	gif2png-logo.xcf gif2png-logo.png local.dic $(TEST_DIST)
SOURCES = gif2png.go gif2png_test.go go.mod go.sum $(EXTRA_DIST)

version:
	@echo $(VERSION)

gif2png-$(VERSION).tar.gz: $(SOURCES)
	$(TAR) --transform='s:^:gif2png-$(VERSION)/:' -czf $@ $(SOURCES)
	ls -l gif2png-$(VERSION).tar.gz

dist: gif2png-$(VERSION).tar.gz

release: gif2png-$(VERSION).tar.gz gif2png.html web2png.html
	shipper version=$(VERSION) | sh -e -x

refresh: gif2png.html
	shipper -N -w version=$(VERSION) | sh -e -x

# end
