code / chum / chums

1#!/bin/sh
2trampoline=; define()(:) # -*- makefile -*-
3###########################################
4## C H U M S : lots of CHUMs at once ... ##
5## Make a website out of code repoes, ez ##
6## Quickstart: cd project; vi .chum ---> ##
7## [change variables given below].  see          ##
8## README for details #####################
9###########################################
10define trampoline
11if command -v gmake >/dev/null 2>&1
12then exec gmake -kr -f "$0" "$@"
13else exec make -kr -f "$0" "$@"
14fi
15endef
16####################### code starts here ##
17PROJECTS =
18OUTDIR = $(PWD)/chums.d
19VDUMPF = $(OUTDIR)/.chump.txt
20
21CHUM = $(HOME)/code/chum/chum
22SERVER = @printf 'Generated files are in %s/\n'
23
24REMOTE =
25ROOT =
26
27define TEMPLATE =
28<html><title>chums</title>
29{{CONTENT}}
30</html>
31endef
32
33### INTERNAL
34
35define _CHUMS
36BEGIN { FS = "\t"; }
37{ if (URLS[$$1]) next; URLS[$$1] = 1
38  thisline = sprintf("<dt><a href=\"%s\">%s</a></dt>\n<dd>%s</dd>\n",
39                     $$2, $$1, $$3)
40  OUTSTR = OUTSTR (OUTSTR?"\n":"") thisline
41}
42END { print(template_replace(template)) }
43
44function template_replace (str) {
45  for (ts in TMPLV) gsub("{{"ts"}}", TMPLV[ts], str)
46  gsub(/&/, "\\&", OUTSTR)
47  sub("{{CONTENT}}", OUTSTR, str)
48  return str
49}
50endef
51
52-include .chums
53
54$(OUTDIR): export TEMPL:=-vtemplate=$(TEMPLATE)
55$(OUTDIR): export PROG:=$(_CHUMS)
56$(OUTDIR): $(PROJECTS)
57	for p in $(PROJECTS); do \
58	$(CHUM) VDUMPF="$(VDUMPF)" \
59		URL_ROOT="$(ROOT)/$$p/" \
60		OUTDIR="$(OUTDIR)/$$p" \
61		-C "$$p" build; done
62	awk "$$TEMPL" "$$PROG" "$(VDUMPF)" > $(OUTDIR)/index.html
63
64.PHONY: serve publish clean
65
66serve: $(OUTDIR)
67	$(SERVER) $(OUTDIR)
68
69publish: $(OUTDIR)
70	rsync -zaP $(OUTDIR)/ $(REMOTE)/
71
72clean:
73	rm -rf $(OUTDIR) $(VDUMPF)