wanderinghorse.net website

Makefile at tip
Login

File Makefile from the latest check-in


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
   145
   146
   147
   148
   149
   150
   151
   152
   153
   154
   155
   156
   157
   158
   159
   160
   161
   162
   163
   164
   165
   166
   167
   168
   169
   170
   171
   172
   173
   174
   175
   176
   177
   178
   179
   180
   181
   182
   183
   184
   185
   186
   187
   188
   189
   190
   191
   192
   193
   194
   195
   196
   197
   198
   199
   200
   201
   202
   203
   204
   205
   206
   207
   208
   209
   210
   211
   212
   213
   214
   215
   216
   217
   218
   219
   220
   221
   222
   223
   224
   225
   226
   227
   228
   229
   230
   231
   232
   233
   234
   235
   236
   237
   238
   239
   240
   241
   242
   243
   244
   245
   246
   247
   248
   249
   250
   251
   252
   253
   254
   255
   256
   257
   258
   259
   260
   261
   262
   263
   264
   265
   266
   267
   268
   269
   270
   271
   272
   273
   274
   275
   276
   277
   278
   279
   280
   281
   282
   283
   284
   285
   286
   287
   288
   289
   290
   291
   292
   293
   294
   295
   296
   297
   298
   299
   300
   301
   302
   303
   304
   305
   306
   307
   308
   309
   310
   311
   312
   313
   314
   315
   316
   317
   318
   319
   320
   321
   322
   323
   324
   325
   326
   327
   328
   329
   330
   331
   332
   333
   334
   335
   336
   337
   338
   339
   340
   341
   342
   343
   344
   345
   346
   347
   348
   349
   350
   351
   352
   353
   354
   355
   356
   357
   358
   359
   360
   361
   362
   363
   364
   365
   366
   367
   368
   369
   370
   371
   372
   373
   374
   375
   376
   377
   378
   379
   380
   381
   382
   383
   384
   385
   386
   387
   388
   389
   390
   391
   392
   393
   394
   395
   396
   397
   398
   399
   400
   401
   402
   403
   404
   405
   406
   407
   408
   409
   410
   411
   412
   413
   414
   415
   416
   417
   418
   419
   420
   421
   422
   423
   424
   425
   426
   427
   428
   429
   430
   431
   432
   433
   434
   435
   436
   437
   438
   439
   440
   441
   442
   443
   444
   445
   446
   447
   448
   449
   450
   451
   452
   453
   454
   455
   456
   457
   458
   459
   460
   461
   462
   463
   464
   465
   466
   467
   468
   469
   470
   471
   472
   473
   474
   475
   476
   477
   478
   479
   480
   481
   482
   483
   484
   485
   486
   487
   488
   489
   490
   491
   492
   493
   494
   495
   496
   497
   498
   499
   500
   501
   502
   503
   504
   505
   506
   507
   508
   509
   510
   511
   512
   513
   514
   515
   516
   517
   518
   519
   520
   521
   522
   523
   524
   525
   526
   527
   528
   529
   530
   531
   532
   533
   534
   535
   536
   537
   538
   539
   540
   541
   542
   543
   544
   545
   546
#!/do/not/make
########################################################################
# This makefile is responsible for all sorts of content generation for
# the 2018 www.wanderinghorse.net relaunch. One goal of that relaunch
# is avoiding PHP, which means generating pages which used to be
# constructed at visit-time by PHP.
#
# Though this tree has many subdirectories with Makefiles, this one
# file manages the build process, with subdir makesfiles mostly just
# launching this one (only non-content subdirs, e.g. site-tools, do
# otherwise). i opted against a conventional recursive make for this
# tree, though i'm still uncertain about how i feel about that.
########################################################################
dir-.:
include config.make
.PHONY: dir-.

########################################################################
# INDEX.IN.HTML is the site's main HTML template. It contains all of
# the HTML scaffolding plus a handful placemarkers which get replaced
# with content by the build process. Each directory which has its own
# content must put that in a file named $(DIR-CONTENT.HTML), which
# this makefile will see and wrap up in a generated index.html. The
# generated HTML files are marked as read-only to help prevent
# accidental hand-editing.
INDEX.HTML := index.html
INDEX.IN.HTML := $(TOP_SRCDIR)/index.s2.html
$(INDEX.IN.HTML):

########################################################################
# If a dir contains a file named $(DIR-CONTENT.HTML) then the contents
# of that file are imported into the generated index.html's main body.
DIR-CONTENT.HTML := dir-content.html

########################################################################
# If a dir contains a file named $(DIR-CONTENT.S2.HTML) then it is
# assumed to be an s2.tmpl()-formatted file and gets filtered by
# $(S2TMPL.S2) to generate a file named $(DIR-CONTENT.HTML) in
# that directory.
DIR-CONTENT.S2.HTML := dir-content.s2.html

########################################################################
# If a directory contains a file named $(DIR-CONTENT-HEAD.HTML), that
# file gets injected into the <head> tag of the generated
# index.html. The intent is that the file contain any <script> or
# <link> (CSS) tags it needs to implement page-specific behaviour.
# See also: $(DIR-CONTENT.CSS) for a simpler way to add dir-specific
# CSS.
DIR-CONTENT-HEAD.HTML := dir-content-head.html
DIR-CONTENT-HEAD.S2.HTML := dir-content-head.s2.html

########################################################################
# If a directory contains a file named $(DIR-SCRIPTS.HTML),
# that file gets injected into the bottom of the BODY tag of the
# generated index.html. The intent is that the file contain any
# <script> tags it needs to implement page-specific behaviour.
DIR-SCRIPTS.HTML := dir-scripts.html
DIR-SCRIPTS.S2.HTML := dir-scripts.s2.html

########################################################################
# Site menu: the build process checks for the file $(DIR-MENU.S2.HTML)
# in each directory, working recursively up the directory tree, and
# uses the first one it finds as the menu/navigation section of all
# pages for that directory and all subdirectories which don't
# themselves override that file. The contents of that file get
# imported as the page's navigation menu. The top-most directory of
# the build tree is required to have a file with that name which acts
# as the default menu. For simplicity, the build process assumes that
# $(DIR-MENU.HTML) is always generated from $(DIR-MENU.S2.HTML), and
# it only checks for $(DIR-MENU.S2.HTML) instead of also looking for a
# static $(DIR-MENU.HTML).
#
# This means, for example, that if /foo/$(DIR-MENU.S2.HTML) exists
# then /foo/bar/baz will also use that menu unless it or /foo/bar
# contain a replacement.
DIR-MENU.HTML := dir-menu.html
DIR-MENU.S2.HTML := dir-menu.s2.html

########################################################################
# If a dir has a file named $(DIR-CONTENT.CSS) then a <link.../> line
# importing that file gets injected into the <head> of the generated
# index.html, after the main <head> contents but before
# $(DIR-CONTENT-HEAD.HTML).
#
# Bug(ish): when we want to override CSS from a 3rd-party CSS file
# loaded in $(DIR-SCRIPTS.HTML), we can't do it in this CSS file
# because the load order is backwards.
DIR-CONTENT.CSS := dir-content.css

########################################################################
# $(S2TMPL-TO-X) $(call)'able uses $(S2TMPL.S2.RUN) to transform
# $2 into $3. Parameters:
#
# $1 = rule descriptive name (cosmetic only)
# $2 = input file (X.s2.Y)
# $3 = output file (X.Y) (gets added to $(CLEAN_FILES))
# $4 = optional script-level flags for s2sh
define S2TMPL-TO-X
ifeq ($(2),$(3))
    $$(error S2TMPL-TO-X: input/output files have the same name: $(2))
endif
$(3): $(2) $$(S2TMPL.S2) $$(MAKEFILE_LIST) $$(S2TMPL.S2.PLUGINS)
	@echo "S2TMPL-TO-X $(1): $$@"; \
	$$(S2TMPL.S2.RUN.PRECLEAN) "$$@"; \
	rm -f "$$@"; \
	$$(S2TMPL.S2.RUN) -f="$$<" -o="$$@" -chdir $(4) \
	&& chmod -w "$$@"
CLEAN_FILES += $(3)
endef

########################################################################
# $(S2TMPL-TO-HTML) $(call)'able: Helper to generate X.html from
# X.s2.html.
#
# $1 = rule descriptive name (cosmetic only)
# $2 = input file (X.s2.html)
#
# These rules generate X.html and add it to the list of CLEAN_FILES.
# X.html is created as read-only to avoid accidental manual editing.
define S2TMPL-TO-HTML
$(call S2TMPL-TO-X,$(1),$(2),$$(subst .s2.html,.html,$(2)))
# Reminder: we use a delayed-expansion $$(subst), rather than
# $(2:%.s2.html=%.html) because it allows callers to rely on delayed
# expansion of the passed-on $(3). Without this, callers have to fully
# expand the $2 passed to this function, e.g. by passing $(foo)
# instead of $$(foo).
endef

########################################################################
# $(S2TMPL-TO-CSS) $(call)'able: Helper to generate X.css from
# X.s2.css.
#
# $1 = rule descriptive name (cosmetic only)
# $2 = input file (X.s2.css)
define S2TMPL-TO-CSS
$(call S2TMPL-TO-X,$(1),$(2),$$(subst .s2.css,.css,$(2)))
# See notes above about why we use $$(subst) instead of a substitution
# reference.
$(2).out := $(subst .s2.css,.css,$(2))
$(2).mini := $(subst .s2.css,.min.css,$(2))
$$($(2).mini): $$($(2).out) $$(BIN.CSSMINC)
	$(BIN.CSSMINC) -m -b0 < $$< > $$@
all: $$($(2).mini)
endef

BIN.CSSMINC := $(TOP_SRCDIR)/site-tools/cssminc
$(BIN.CSSMINC):
	$(MAKE) -C $(TOP_SRCDIR)

########################################################################
# Site-wide primary CSS file. We need to define this way up here
# so that index.html can use it as an order-only prerequisite.
MAIN.S2.CSS := assets/css/2022.s2.css
MAIN.CSS := $(subst .s2.css,.css,$(MAIN.S2.CSS))
$(eval $(call S2TMPL-TO-CSS,MAKE.MAIN.CSS,$(MAIN.S2.CSS)))

########################################################################
# Generates $(DIR-CONTENT.HTML) from $(DIR-CONTENT.S2.HTML) (an
# s2.tmpl()-format HTML file).
#
# Usage:
# $(eval $(call MAKE.DIR-CONTENT.HTML,$(1)))
# $(1) = directory containing $(DIR-CONTENT.S2.HTML), to which
# $(DIR-CONTENT.HTML) will be written.
define MAKE.DIR-CONTENT.HTML
$(call S2TMPL-TO-HTML,MAKE.DIR-CONTENT.HTML,$(1)/$$(DIR-CONTENT.S2.HTML))
endef

########################################################################
# Generates $(DIR-SCRIPTS.HTML) from $(DIR-SCRIPTS.S2.HTML) (an
# s2.tmpl()-format HTML file).
#
# Usage:
# $(eval $(call MAKE.DIR-SCRIPTS.HTML,$(1)))
# $(1) = directory containing $(DIR-SCRIPTS.S2.HTML), to which
# $(DIR-SCRIPTS.HTML) will be written.
define MAKE.DIR-SCRIPTS.HTML
$(call S2TMPL-TO-HTML,MAKE.DIR-SCRIPTS.HTML,$(1)/$$(DIR-SCRIPTS.S2.HTML))
$(1)/$$(DIR-CONTENT.HTML): $(1)/$$(DIR-SCRIPTS.HTML)
endef

########################################################################
# Generates $(DIR-MENU.HTML) from $(DIR-MENU.S2.HTML) (an
# s2.tmpl()-format HTML file).
#
# Usage:
# $(eval $(call MAKE.DIR-MENU.HTML,$(1)))
# $(1) = directory containing $(DIR-MENU.S2.HTML), to which
# $(DIR-MENU.HTML) will be written.
define MAKE.DIR-MENU.HTML
$(call S2TMPL-TO-HTML,MAKE.DIR-MENU.HTML,$(1)/$$(DIR-MENU.S2.HTML))
#$(1)/$$(DIR-CONTENT.HTML): $(1)/$$(DIR-MENU.HTML)
$(1)/$$(INDEX.HTML): $(1)/$$(DIR-MENU.HTML)
CLEAN_FILES += $(1)/$$(DIR-MENU.HTML)
endef

########################################################################
# Generates $(DIR-CONTENT-HEAD.HTML) from $(DIR-CONTENT-HEAD.S2.HTML) (an
# s2.tmpl()-format HTML file).
#
# Usage:
# $(eval $(call MAKE.DIR-CONTENT-HEAD.HTML,$(1)))
# $(1) = directory containing $(DIR-CONTENT-HEAD.S2.HTML), to which
# $(DIR-CONTENT-HEAD.HTML) will be written.
define MAKE.DIR-CONTENT-HEAD.HTML
$(call S2TMPL-TO-HTML,MAKE.DIR-CONTENT-HEAD.HTML,$(1)/$$(DIR-CONTENT-HEAD.S2.HTML))
$(1)/$$(DIR-CONTENT.HTML): $(1)/$$(DIR-CONTENT-HEAD.HTML)
endef

########################################################################
# These rules create $(1)/index.html from $(INDEX.IN.HTML),
# $(1)/$(DIR-CONTENT.HTML), and optionally a few other files.  The
# generated index.html is made read-only to help prevent accidental
# editing.
#
# Usage:
# $(eval $(call MAKE.INDEX,$(1)))
# $(1) = target directory
#
# Reminders to self:
#
#  - Template flags which refer to a file name which is inteded to get
#  imported into the template start with '@'.
#
define MAKE.INDEX
# $$(info queuing MAKE.INDEX: $1)
$(1)/$$(DIR-SCRIPTS.HTML):
$(1)/$$(DIR-CONTENT-HEAD.HTML):
$(1)/$$(DIR-CONTENT.CSS):
$(1)/$$(DIR-MENU.HTML):
$(1)/$$(INDEX.HTML): $$(INDEX.IN.HTML) $(1)/$$(DIR-CONTENT.HTML) \
	$$(MAKEFILE_LIST) \
	$$(wildcard $(1)/$$(DIR-SCRIPTS.HTML)) \
	$$(wildcard $(1)/$$(DIR-CONTENT-HEAD.HTML)) \
	$$(wildcard $$($(1)/$$(INDEX.HTML).menu)) \
	| $$(MAIN.CSS)
	@echo -e "MAKE.INDEX:\t$$@"; \
	rm -f $$@; \
	timestamp=$$$$(date -r "$$(word 1,$$(wildcard $(1)/$$(DIR-CONTENT.S2.HTML) $(1)/$$(DIR-CONTENT.HTML)))"); \
	dirName="/$(1)"; \
	[ '/.' = $$$$dirName ] && dirName=/; \
	scripts="$$(wildcard $(1)/$$(DIR-SCRIPTS.HTML))"; \
	[ -f "$$$$scripts" ] && echo -e "\t\tscripts: $$$$scripts"; \
	menu="$$(wildcard $$($(1)/$$(INDEX.HTML).menu))" ; \
	if [ -f "$$$$menu" ]; then echo -e "\t\tmenu: $$$$menu"; fi ; \
	dircss="$$(wildcard $(1)/$$(DIR-CONTENT.CSS))"; \
	if [ -f "$$$$dircss" ]; then \
		echo -e "\t\tcss: $$$$dircss"; \
		dircss=true; \
	else dircss=false; fi; \
	head="$$(wildcard $(1)/$$(DIR-CONTENT-HEAD.HTML))"; \
	[ -f "$$$$head" ] && echo -e "\t\thead: $$$$head"; \
	$$(S2TMPL.S2.RUN.PRECLEAN) "$$@"; \
	$$(MAKE.INDEX.VERBOSE) $$(S2TMPL.S2.RUN) \
		-f=$$< -o=$$@ +chdir \
		--dir-name=$$$$dirName \
		--timestamp="$$$$timestamp" \
		--dir-css=$$$$dircss \
		--@dir-content="$(1)/$$(DIR-CONTENT.HTML)" \
		--@dir-scripts="$$$$scripts" \
		--@menu="$$$$menu" \
		--@dir-head="$$$$head"
	@chmod -w $$@
all: $(1)/index.html
FTS.FILES += $(1)/$$(DIR-CONTENT.HTML)
DISTCLEAN_FILES += $(1)/index.html
endef
#MAKE.INDEX.VERBOSE := set -x;
MAKE.INDEX.VERBOSE :=

########################################################################
# Create indexes which emit various HTTP status codes for (re)moved
# content...
CGI.410 := $(TOP_SRCDIR)/410.cgi
$(CGI.410):
CGI.503 := $(TOP_SRCDIR)/503.cgi
$(CGI.503):

########################################################################
# Install an index.cgi file in directory $(2) which emits an HTTP $(1)
# response. Requires $(TOP_SRCDIR)/$(1).cgi as a template. The
# resulting file is made read-only to help prevent accidental
# hand-editing.
#
# Usage:
# $(eval $(call MAKE.HTTPCODE,$(1),$(2)))
# $(1) = HTTP response code
# $(2) = directory
define MAKE.HTTPCODE
$(2)/index.cgi: $$(TOP_SRCDIR)/$(1).cgi $$(MAKEFILE_LIST)
	@echo "MAKE.HTTPCODE $(1): $$@"; mkdir -p "$(2)"; cp -f $$< "$$@" && chmod +x-w "$$@"
all: $(2)/index.cgi
DISTCLEAN_FILES += $(2)/index.cgi
endef

########################################################################
# Directories to install HTTP 410's in...
#
# These are dirs from the "classic" site which won't be ported over to
# the newer one. Someday maybe we'll remove them altogether, but a 410
# response sounds reasonable for now, given how long they've been
# online, as that "should" give web spiders a chance to see this and
# remove the entries.
dirs.410 := $(patsubst %,computing/%, \
		c editline gcc js javascript javascript/jquery javascript/v8 \
		parse0x ptsigslot qemu readline shellish \
		sigslot sqlite webdesign yo5 zfstream \
	) \
	$(patsubst %,gaming/%, \
		bpi nintendo QBoard randomwords sony \
	)
$(foreach sdir,$(dirs.410),$(eval $(call MAKE.HTTPCODE,410,$(sdir))))

########################################################################
# Directories to install HTTP 503's in...
dirs.503 := \
	$(patsubst %,gaming/%, \
		ogre/byoo/ajax \
	)
$(foreach sdir,$(dirs.503),$(eval $(call MAKE.HTTPCODE,503,$(sdir))))

# TODO?: redirects (HTTP 301 and 308):
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections


########################################################################
# Put a placeholder page in directory $(1), using notyet/index.html as
# the basis...
# $(eval $(call MAKE.INDEX.NOTYET,$(1)))
# $(1) = directory
INDEX.HTML.NOTYET := $(TOP_SRCDIR)/notyet/index.html
$(INDEX.HTML.NOTYET):
define MAKE.INDEX.NOTYET
$(1)/index.html: $$(INDEX.HTML.NOTYET)
	@echo "MAKE.INDEX.NOTYET: $$@"; \
	rm -f $$@; \
	mkdir -p "$(1)"; \
	sed -e 's,notyet,$(1),' $$< > $$@
	@chmod -w $$@
all: $(1)/index.html
DISTCLEAN_FILES += $(1)/index.html
endef

#dirs.notyet := $(dirs.503)
#$(foreach sdir,$(dirs.notyet),$(eval $(call MAKE.INDEX.NOTYET,$(sdir))))

########################################################################
# Finagle the cleanup rules a bit...
clean distclean: clean-tilde
.PHONY: clean-tilde
clean-tilde:
	@echo "Cleaning up **/*~ ..."
	@find . -type f -name '*~' -exec rm -f \{} \;


########################################################################
# Purely static content which does not use the site's template but
# which we want included in the FTS index...
#
# Remember that the search engine delivers only *directories* (not
# files) as results and assumes that there's only one page per
# dir. Adding multiple pages/dir here will lead to each new entry
# overwriting the next.  That uniqueness can be considered a bug and
# fixing it MIGHT be as simple as removing the UNIQUE constraint on
# that FTS field, but i'm not up for testing it right now :/.
FTS.FILES += gaming/mecklemquest/index.html

########################################################################
# sqlite3 FTS indexing...
# FTS.CONTENT
BIN.SQLITE3 ?= $(call ShakeNMake.CALL.FIND_FILE,sqlite3)
ifeq (,$(BIN.SQLITE3))
$(error Cannot find sqlite3 in PATH.)
endif
FTS-PAGES.SQL := $(wildcard $(DIR.SITETOOLS)/fts-pages.sql)
FTS-PAGES.S2 := $(wildcard $(DIR.SITETOOLS)/fts-pages.s2)
ifeq (,$(FTS-PAGES.S2))
$(error cannot find fts-pages.s2)
endif
FTS-PAGES.DIR := $(S2_HOME)/cgi/search.d
$(FTS-PAGES.DIR):
	mkdir -p "$@"
FTS-PAGES.DB := $(FTS-PAGES.DIR)/fts-pages.sqlite3
$(FTS-PAGES.SQL):
$(FTS-PAGES.DB): $(FTS-PAGES.SQL)
	rm -f $@; $(BIN.SQLITE3) $@ < $<
.PHONY: fts
fts: $(FTS-PAGES.DB)
	@echo -n "FTS'ing $(words $(FTS.FILES)) files... "; \
	false && echo $(FTS.FILES); \
	$(BIN.S2SH.CALL) "$(FTS-PAGES.S2)" -- \
		--db-file="$(FTS-PAGES.DB)" \
		--s2.home="$(S2_HOME)" \
		$(sort $(FTS.FILES)); \
	echo; \
	echo "insert into reindex(x) values(1);" \
		| "$(BIN.SQLITE3)" $(FTS-PAGES.DB)
	@echo "FTS: done!"
# /FTS index
########################################################################

########################################################################
# Push our stuff to the remote server...
.PHONY: push
SSH_HOST ?= wh2
# ^^^^ SSH_HOST = host configuration name from ~/.ssh/config
push: send2site.sh fts
	SSH_HOST=$(SSH_HOST) bash send2site.sh
	scp -p "$(FTS-PAGES.DB)" $(SSH_HOST):s2/cgi/search.d/.

########################################################################
# Creates rules for cleaning up subdir $(1).
define SUBDIR.CLEANUP
# Build bug: why must these [dist]clean-$(1) rules be defined here,
# instead of picking up the [dist]clean-% rules defined in
# common.make? Those aren't triggering .
.PHONY: clean-$(1) distclean-$(1)
clean-$(1):
	@echo "clean [$(1)] ..."; $$(MAKE) --silent --no-print-directory -C $(1) clean > /dev/null
distclean-$(1):
	@echo "distclean [$(1)] ..."; $$(MAKE) --silent --no-print-directory -C $(1) distclean > /dev/null
# Clean up depth-first:
clean-.: clean-$(1)
distclean-.: distclean-$(1)
endef
# Creates rules for running the default rule and clean/distclean rules
# in subdir $(1).
define SUBDIR
$(1):
	@echo "sub-make [$$@] ..."; \
	$$(MAKE) --silent --no-print-directory -C $$@
all: $(1)
.PHONY: $(1)
$$(eval $$(call SUBDIR.CLEANUP,$(1)))
endef

########################################################################
# Deps generation for directories which generate HTML using the
# above-defined features...
#
# $(CONTENT-DEPS.bash) is a script which generates Makefile code for
# the most common HTML generation cases.
CONTENT-DEPS.bash := content-deps.bash
# $(CONTENT-DEPS.make) is a makefile snippet generated by
# $(CONTENT-DEPS.bash) which contains deps for most generated HTML
# files. It gets include'd into this makefile after it's generated.
#
# Note that we have to generate/import this even when the cleanup
# rules are being run because the generated rules define some of
# what needs to be cleaned up. OTOH, we possibly don't need to
# because if we're cleaning up then either this file already
# exists or the tree is already clean.
CONTENT-DEPS.make := .content-deps.make
ifeq (,$(findstring clean,$(MAKECMDGOALS)))
########################################################################
# Directories to create an index.html for (using $(DIR-CONTENT.HTML)
# and friends)...
DIRS-WITH-INDEX.HTML := \
	$(sort \
	  $(patsubst ./%,%,$(patsubst %/,%,$(dir \
		$(shell find . -type f -name $(DIR-CONTENT.HTML) -o -name $(DIR-CONTENT.S2.HTML)) \
	  ))) \
	)
# ^^^^ reminder: $(sort) is needed after $(dir)/$(patsubst) to remove
# dupes.  We should probably manage that list by hand so we can tweak
# the order (and avoid a potentially slow $(shell find...)). As of
# this writing, the order doesn't matter or is managed via other deps.
$(CONTENT-DEPS.make): $(CONTENT-DEPS.bash) $(filter-out $(CONTENT-DEPS.make),$(MAKEFILE_LIST))
	@echo "Creating $@ ..."; \
	bash $(CONTENT-DEPS.bash) $(DIRS-WITH-INDEX.HTML) > $@
else
$(info Cleanup: skipping generation of $(CONTENT-DEPS.make))
endif
-include $(CONTENT-DEPS.make)
CLEAN_FILES += $(CONTENT-DEPS.make)
# /$(CONTENT-DEPS.make)
########################################################################

########################################################################
#
#
# *For the most part*, everything above is site-independent, and could
# theoretically be used to generate any site which uses compatible
# conventions. The parts below this section are specific to this tree.
#
#
########################################################################

.PHONY: serve
serve.port ?= 8080
serve:
	@echo "Visit: http://localhost:$(serve.port)"
	~/bin/althttpd -debug 1 -root . -jail 0 -port $(serve.port)

########################################################################
# 'touch' the SCM-controlled content files to their "real" last
# modification time so that the build process can properly timestamp
# the content parts. This "should" be done, along with a rebuild,
# before any update of the live site, but isn't done by default
# because touching files currently opened for editing is annoying.
#
# Note that fossil does not actually save file timestamps: it instead
# considers the timestamp of the last checkin which modified the file
# to be that file's modification time. That's close enough for what
# we're doing.
.PHONY: touch rebuild
touch:
	fossil touch --checkin --glob '*dir-*.html,*.txt,*.tar.?z,*.zip,*.tar.bz2,*.odt,*.pdf'

rebuild:
	make clean touch
	make

# Make sure these things are built first...
dir-.: site-tools assets/js
dir-.: all
# That unfortunately triggers a build of those subdirs even whe
# using MAKE.FROM.SUBDIR, but we don't currently have a nicer
# way of integrating those.
#
# Reminder to self: We have a chicken/egg situation with assets/css
# and minification of main.s2.css.

########################################################################
# Non-HTML-content subdirs which build stuff.
subdirs := site-tools assets/js
$(foreach sub,$(subdirs),$(eval $(call SUBDIR,$(sub))))

########################################################################
# Special-case deps for certain subdirs...
define DO-DIR-DEPS
echo "[dir-deps]: [$(1)]"; \
$(MAKE) --no-print-directory -C $(1) dir-deps || exit $$?; \
echo "[/dir-deps]: [$(1)]"
endef

BYOO.DIR := gaming/ogre/byoo
$(BYOO.DIR)/dir-tab-gear.html: $(BYOO.DIR)/dir-tab-gear.s2.html
	@$(call DO-DIR-DEPS,$(BYOO.DIR))
$(BYOO.DIR)/$(DIR-CONTENT.HTML): $(addprefix $(BYOO.DIR)/, \
	dir-tab-ciscos.html \
	dir-tab-gear.html \
	dir-tab-byoo.html \
)
# /special-case subdir deps
########################################################################