# Makefile for loccount
#
# You must have the Go compiler and tools installed to build this software.
# You will also need Python installed to build it, though not to run it.

# 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

VERSION=$(shell sed -n <NEWS.adoc '/^[0-9]/s/:.*//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 test check check-mal-missing buildchecks spellcheck reflow fixme
.PHONY: install uninstall version dist release refresh

# Build

all: loccount loccount.1

GOFLAGS=-ldflags='-X main.version=$(VERSION)'
loccount: loccount.go languages.yaml tablegen.py
	-test -f go.mod || go mod init loccount
	go generate
	go build $(GOFLAGS)

languages.html: loccount webify.py languages.yaml 
	@./loccount -W | ./webify.py -o languages.html

clean:
	go clean
	rm -f *.html *.1 *~ tests/*~ traits.go

test: loccount
	@NO_COLOR=1 uvx ruff check --output-format=concise
	@go test .
	@./loccount -s >/dev/null
	@(./loccount -N -i tests; ./loccount -u tests) | LC_ALL=C sort | diff -u check.good -
	@echo "No check output (other than this line) is good news"

check: test spellcheck

buildchecks: loccount
	@rm -f tests/*~; (./loccount -N -i tests; ./loccount -u tests) | LC_ALL=C sort >check.good


reflow:
	gofmt -s -d loccount.go | patch -p0

# Validate

spellcheck:
	@spellcheck local.dic loccount.adoc README.adoc languages.yaml hacking.adoc

# You can profile this code with, e.g.
#
# loccount -cpuprofile /tmp/loc.prof .
# go tool pprof loccount /tmp/loc.prof
#
# "top20" is a useful command to start with in viewing results.
#
# Also, remember to check the report card occasionally:
# https://goreportcard.com/report/gitlab.com/esr/loccount#L1

# Report which languages lack lloc support
sloc: loccount
	./loccount -i -s  | sort >/tmp/sloc$$; ./loccount -i -l | sort >/tmp/lloc$$
	comm -23 /tmp/sloc$$ /tmp/lloc$$
	rm -f /tmp/sloc$$ /tmp/lloc$$

fixme:
	@if command -v rg; then \
		rg --no-heading FIX''ME; \
	else \
		find . -type f -exec grep -n FIX''ME {} /dev/null \; | grep -v "[.]git"; \
	fi

# Install/uninstall

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

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

# Export

version:
	@echo $(VERSION)

# In case the logo ever needs to be scaled, the SVG original can be found here:
# https://commons.wikimedia.org/wiki/File:Tally_marks-Five-bar_Gate.svg
SOURCES = local.dic README.adoc COPYING NEWS.adoc control loccount.go loccount_test.go languages.yaml tablegen.py webify.py \
		loccount.adoc hacking.adoc design-notes.adoc \
		Makefile TODO loccount-logo.png check.good tests/

loccount-$(VERSION).tar.gz: $(SOURCES)
	mkdir loccount-$(VERSION)
	cp -r $(SOURCES) loccount-$(VERSION)
	tar -czf loccount-$(VERSION).tar.gz loccount-$(VERSION)
	rm -fr loccount-$(VERSION)
	ls -l loccount-$(VERSION).tar.gz

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

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

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

# end
