# NEW Makefile by pierre-antoine@w3.org 2025-05-07
#
# Generate ns.* from
# - vcard.rdf (legacy vocabulary)
# - ns-ext.ttl (terms added by the Solid CG)
# - context.jsonld (used to compact ns.jsonld)
# - cleanup-ns-jsonld.py (used to, well, clean up ns.jsonld)
#
# NB: I failed to run the XSLT transformation from vcard.xml to ns.rdf,
#     so I moved the old ns.rdf to vcard.rdf,
#     and used it as a source of truth for generating ns.*
#
# Dependencies:
#   - https://github.com/pchampin/sophia-cli
#     (cargo install --git https://github.com/pchampin/sophia-cli)
#   - https://github.com/digitalbazaar/jsonld-cli
#     (npm install -g jsonld-cli)
#   - python3
SOP=sop
JSONLD=jsonld
PY3=python3
BASE=http://www.w3.org/2006/vcard/ns
PREFIXES=:$(BASE)\#,rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns\#,rdfs:http://www.w3.org/2000/01/rdf-schema\#,owl:http://www.w3.org/2002/07/owl\#,xsd:http://www.w3.org/2001/XMLSchema\#

.PHONY: all
all: ns.nt ns.rdf ns.ttl ns.jsonld

ns.nt: vcard.rdf ns-ext.ttl
	echo "# generated from vcard.rdf and ns-ext.ttl" > "$@"
	$(SOP) parse -m vcard.rdf ns-ext.ttl m- ! serialize -f nt >> "$@"

ns.rdf: ns.nt
	$(SOP) parse "$<" ! relativize $(BASE) ! serialize -p "$(PREFIXES)" -o "$@"
	sed -i 's|<rdf:RDF |<rdf:RDF xml:base="$(BASE)" |' "$@"

ns.ttl: ns.nt
	echo "# generated from vcard.rdf and ns-ext.ttl" > "$@"
	echo "@base <$(BASE)>." >>"$@"
	$(SOP) parse "$<" ! relativize $(BASE) ! serialize -f ttl -p "$(PREFIXES)" >> "$@"

ns.jsonld: ns.nt ns-context.jsonld
	$(SOP) parse "$<" ! serialize -f jsonld | $(JSONLD) compact -c ns-context.jsonld > "$@"
	$(PY3) cleanup-ns-jsonld.py "$@"

.PHONY: check
check: all
	rdfdiff ns.rdf ns.nt
	rdfdiff ns.ttl ns.nt
	rdfdiff ns.jsonld ns.nt

.PHONY: clean
clean:
	rm -f ns.nt ns.rdf ns.ttl ns.jsonld

# BELOW is the old Makefile,
# which seems to have been effectively deprecated overtime
# (for example, ns.ttl, which is now served by default since 2010, is not covered)

# XSLT=saxon
# TIDY=tidy
# S=/devel/WWW/2000/10/swap
# CWM=python $S/cwm.py

# all: ns.html ns.rdf ns.n3 links.rdf

# links.rdf: links.n3
# 	$(CWM) --n3 $< --rdf > $@

# ns.rdf: vcard.xml vcard2rdf.xsl
# 	$(XSLT) $^ $@

# ns.n3: ns.rdf
# 	$(CWM) --rdf $< --n3 > $@

# ns.html: vcard.xml vcard.xsl
# 	$(XSLT) $^ $@
# 	$(TIDY) $@
