# libsyd: syd /dev/syd interface implementation
# Makefile: Makefile for libsyd
#
# Copyright (c) 2023, 2024, 2025, 2026 Ali Polatel <alip@chesswob.org>
#
# SPDX-License-Identifier: GPL-3.0

# Package name and version
PACKAGE= syd
VERSION= 3.0.0-alpha.6

# Build profile
PROFILE?= release

# Installation directories
PREFIX = /usr/local
LIB_DIR = $(PREFIX)/lib
MAN_DIR = $(PREFIX)/share/man/man3
INCLUDE_DIR = $(PREFIX)/include

# Cargo and Installer
CARGO?= cargo
INSTALL?= install

# Cargo flags
CARGOFLAGS?= -j$(shell nproc)

# Rust flags
RUSTFLAGS := -g -Ctarget-feature=-crt-static
export RUSTFLAGS

# Perl
PERL?= perl
PLSITE?= $(shell $(PERL) -MConfig -e 'print $$Config{installsitelib}')
MANDOC?= mandoc

# Python
PYTHON?= python3
PYSITE?= $(shell $(PYTHON) -c "import site; print(site.getsitepackages()[-1])")

# Source files
SRC=\
    src/lib.rs \
    Cargo.toml

# Library Names
ifeq ($(PROFILE), debug)
	LIB_STATIC= ./target/debug/deps/lib$(PACKAGE).a
	LIB_SHARED= ./target/debug/deps/lib$(PACKAGE).so
else
	LIB_STATIC= ./target/release/deps/lib$(PACKAGE).a
	LIB_SHARED= ./target/release/deps/lib$(PACKAGE).so
endif

# Default target
all: $(LIB_SHARED) $(LIB_STATIC) syd.h

# Publish targets
PUTFLAGS= -c $(HOME)/.s3cfg.site
PUTFLAGS_HTML= $(PUTFLAGS) \
	       --no-guess-mime-type \
	       --default-mime-type=text/html

publish: upload
	$(CARGO) publish --no-verify
upload: doc godoc pldoc pydoc rbdoc
	s3cmd put $(PUTFLAGS_HTML) -P doc/syd/*.html s3://libsyd.exherbo.org/
	s3cmd put $(PUTFLAGS_HTML) -P doc/auto/pl.html s3://plsyd.exherbo.org/index.html
	s3cmd put $(PUTFLAGS_HTML) -P doc/auto/syd.html s3://pysyd.exherbo.org/index.html
	s3cmd put $(PUTFLAGS) -P \
		--no-guess-mime-type \
		--default-mime-type=text/css \
		doc/auto/go/_/css/main.css s3://gosyd.exherbo.org/main.css
	s3cmd put $(PUTFLAGS) -P \
		--no-guess-mime-type \
		--default-mime-type=text/javascript \
		doc/auto/go/_/js/permalink.js s3://gosyd.exherbo.org/permalink.jss
	sed \
		-e 's|../../../../../_/css/main.css|./main.css|' \
		-e 's|../../../../../_/js/permalink.js|./permalink.js|' \
		< 'doc/auto/go/git.sr.ht/~alip/syd/lib/src/index.html' \
		> 'doc/auto/go.html'
	s3cmd put $(PUTFLAGS_HTML) -P doc/auto/go.html s3://gosyd.exherbo.org/index.html
	s3cmd put $(PUTFLAGS_HTML) -P doc/auto/ruby/index.html s3://rbsyd.exherbo.org/index.html

# Documentation targets
doc: syd.h
	gyosu -o doc/syd syd.h
godoc: src/syd.go
	mkdir -m700 -p doc/auto
	rm -rf doc/auto/go
	bash -c "pushd src; exec doc2go ./..."
	mv src/_site doc/auto/go
pldoc: src/syd.pm
	mkdir -m700 -p doc/auto
	bash -c "pushd src && pod2man -u --errors=none syd.pm > ../doc/auto/syd.pm.3"
	$(MANDOC) -Thtml < doc/auto/syd.pm.3 > doc/auto/pl.html
pydoc: $(LIB_SHARED) src/syd.py
	env LD_LIBRARY_PATH=./target/release pdoc -d markdown --output-directory doc/auto src/syd.py
rbdoc: src/syd.rb
	mkdir -m700 -p doc/auto
	rm -rf doc/auto/ruby
	bash -c 'pushd src && yard doc --title "rbsyd: Ruby FFI bindings of libsyd, the syd API C Library" --hide-api private --one-file ./syd.rb'
	mv src/doc doc/auto/ruby

# QA targets
fmt:
	$(CARGO) fmt || true
	black src/syd.py || true
	perltidy -b src/syd.pm src/syd.pl || true
	astyle examples/*.c examples/c/*.c || true
	sh -c "cd src; exec go fmt -x ." || true
	sh -c "cd examples/go; exec go fmt -x ." || true
	rubocop --config ./.rubocop.yml --autocorrect-all src/*.rb examples/ruby/*.rb || true
lint:
	$(CARGO) deny check
	$(CARGO) acl -n || true
	$(CARGO) clippy $(CARGOFLAGS)
	pylint src/syd.py || true
	rubocop --config ./.rubocop.yml --autocorrect-all src/syd.rb || true

# Install and Uninstall Targets
install:
	$(INSTALL) -d $(LIB_DIR)
	$(INSTALL) -m 644 $(LIB_STATIC) $(LIB_DIR)
	$(INSTALL) -m 755 $(LIB_SHARED) $(LIB_DIR)
	$(INSTALL) -d $(INCLUDE_DIR)
	$(INSTALL) -m 644 syd.h $(INCLUDE_DIR)
	$(INSTALL) -d $(MAN_DIR)
	$(INSTALL) -m 644 doc/man/man3/syd.h.3 $(MAN_DIR)
	$(INSTALL) -d $(PLSITE)
	$(INSTALL) -m 755 src/syd.pm $(PLSITE)
	$(INSTALL) -d $(PYSITE)
	$(INSTALL) -m 755 src/syd.py $(PYSITE)
uninstall:
	rm -f $(LIB_DIR)/libsyd.a
	rm -f $(LIB_DIR)/libsyd.so
	rm -f $(INCLUDE_DIR)/syd.h
	rm -f $(MAN_DIR)/syd.h.3
	rm -f $(PLSITE)/syd.pm
	rm -f $(PYSITE)/syd.py

# Check target
check: test

test: $(LIB_SHARED)
	$(CARGO) test --profile=$(PROFILE) $(CARGOFLAGS)

# Clean Target
clean:
	$(CARGO) clean

syd.h: $(SRC) cbindgen.toml
	cbindgen -c cbindgen.toml -o syd.h .
$(LIB_SHARED): $(SRC)
	$(CARGO) build --profile=$(PROFILE) $(CARGOFLAGS)
$(LIB_STATIC): $(SRC)
	$(CARGO) build --profile=$(PROFILE) $(CARGOFLAGS)

# Phony Targets
.PHONY: all clean check doc godoc pldoc pydoc rbdoc test install uninstall publish fmt lint
