# Copyright (c) 2023–2024 Emma Tebibyte <emma@tebibyte.media>
# Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
# Copyright (c) 2023 Sasha Koshka <sashakoshka@tebibyte.media>
# Copyright (c) 2024 Aaditya Aryal <aryalaadi123@gmail.com>
# SPDX-License-Identifier: FSFAP
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and this
# notice are preserved.  This file is offered as-is, without any warranty.

# The octal escape \043 is utilized twice in this file as make(1p) will
# interpret a hash in a rule as an inline comment.

.POSIX:

DESTDIR ?= dist
PREFIX ?= /usr/local

# for conditionally compiling OS features
OS != uname
OS_INCLUDE != test -e include/$(OS).mk && printf 'include/$(OS).mk\n' \
	|| printf '/dev/null\n'

# normalized prefix
PREFIX_N != dirname $(PREFIX)/.
MANDIR != test $(PREFIX_N) = / && printf '/usr/share/man\n' \
	|| printf '/share/man\n'
SYSEXITS != printf '\043include <sysexits.h>\n' | cpp -M - | tr ' ' '\n' \
	| sed -n 's/sysexits\.h//p' || printf 'include\n'

CC ?= cc
RUSTC ?= rustc
RUSTFLAGS += --extern getopt=build/o/libgetopt.rlib \
	--extern strerror=build/o/libstrerror.rlib \
	--extern sysexits=build/o/libsysexits.rlib
CFLAGS += -I$(SYSEXITS)

# testing requires the absolute path to the bin directory set
BIN = build/bin

.PHONY: default
default: all test

.PHONY: all
all: dj false fop hru intcmp mm npc rpn scrut str strcmp swab true

# keep build/include until bindgen(1) has stdin support
# https://github.com/rust-lang/rust-bindgen/issues/2703
build:
	mkdir -p build/bin build/docs build/include build/lib build/o build/test

.PHONY: clean
clean:
	rm -rf build dist

dist: all docs
	mkdir -p $(DESTDIR)/$(PREFIX)/bin $(DESTDIR)/$(PREFIX)/$(MANDIR)/man1
	cp build/bin/* $(DESTDIR)/$(PREFIX)/bin
	cp build/docs/*.1 $(DESTDIR)/$(PREFIX)/$(MANDIR)/man1

.PHONY: install
install: dist
	cp -r $(DESTDIR)/* /

include tests/tests.mk

.PHONY: test
test: all $(TESTS) /tmp/getopt
	@echo $(TESTS)
	/tmp/getopt

/tmp/getopt: src/libgetopt.rs
	$(RUSTC) --test -o /tmp/getopt src/libgetopt.rs

.PHONY: docs
docs: docs/ build
	for file in docs/*; do original="$$(sed -n '/^\.TH/p' <"$$file")"; \
		title="$$(printf '%s\n' "$$original" | sed \
		"s/X\.X\.X/$$(git describe --tags --long | cut -d'-' -f1)/g")"; \
		sed "s/$$original/$$title/g" <"$$file" >"build/$$file"; done

# include OS feature libraries for compilation
include $(OS_INCLUDE)

.PHONY: rustlibs
rustlibs: build/o/libgetopt.rlib build/o/libstrerror.rlib \
	 build/o/libsysexits.rlib $(OSLIB)

build/o/libgetopt.rlib: build src/libgetopt.rs
	$(RUSTC) $(RUSTFLAGS) --crate-type=lib --crate-name=getopt \
		-o $@ src/libgetopt.rs

build/o/libstrerror.rlib: build src/libstrerror.rs
	$(RUSTC) $(RUSTFLAGS) --crate-type=lib -o $@ \
		src/libstrerror.rs

build/o/libsysexits.rlib: build/include/sysexits.h
	bindgen --fit-macro-constant-types --default-macro-constant-type unsigned --use-core --formatter=none \
		build/include/sysexits.h | $(RUSTC) $(RUSTFLAGS) --crate-type lib -o $@ -

# bandage solution until bindgen(1) gets stdin support
build/include/sysexits.h: build $(SYSEXITS)sysexits.h
	printf '\043define EXIT_FAILURE 1\n' | cat - $(SYSEXITS)sysexits.h > $@

.PHONY: dj
dj: build/bin/dj
build/bin/dj: src/dj.c build
	$(CC) $(CFLAGS) -o $@ src/dj.c

.PHONY: false
false: build/bin/false
build/bin/false: src/false.c build
	$(CC) $(CFLAGS) -o $@ src/false.c

.PHONY: fop
fop: build/bin/fop
build/bin/fop: src/fop.rs build rustlibs
	$(RUSTC) $(RUSTFLAGS) -o $@ src/fop.rs

.PHONY: hru
hru: build/bin/hru
build/bin/hru: src/hru.rs build rustlibs 
	$(RUSTC) $(RUSTFLAGS) -o $@ src/hru.rs

.PHONY: intcmp
intcmp: build/bin/intcmp
build/bin/intcmp: src/intcmp.rs build rustlibs
	$(RUSTC) $(RUSTFLAGS) -o $@ src/intcmp.rs

.PHONY: mm
mm: build/bin/mm
build/bin/mm: src/mm.rs build rustlibs 
	$(RUSTC) $(RUSTFLAGS) -o $@ src/mm.rs

.PHONY: npc
npc: build/bin/npc
build/bin/npc: src/npc.c build
	$(CC) $(CFLAGS) -o $@ src/npc.c

.PHONY: rpn
rpn: build/bin/rpn
build/bin/rpn: src/rpn.rs build rustlibs
	$(RUSTC) $(RUSTFLAGS) -o $@ src/rpn.rs

.PHONY: scrut
scrut: build/bin/scrut
build/bin/scrut: src/scrut.c build
	$(CC) $(CFLAGS) -o $@ src/scrut.c

.PHONY: str
str: build/bin/str
build/bin/str: src/str.c build
	$(CC) $(CFLAGS) -o $@ src/str.c

.PHONY: strcmp
strcmp: build/bin/strcmp
build/bin/strcmp: src/strcmp.c build
	$(CC) $(CFLAGS) -o $@ src/strcmp.c

.PHONY: swab
swab: build/bin/swab
build/bin/swab: src/swab.rs build rustlibs
	$(RUSTC) $(RUSTFLAGS) -o $@ src/swab.rs

.PHONY: true
true: build/bin/true
build/bin/true: src/true.c build
	$(CC) $(CFLAGS) -o $@ src/true.c