# Makefile to retrieve and convert pmwiki pages
#

DIRECTIVES := ':cell:\|:cellnr:\|:head:\|:redirect\|:source:\|:source\|:sourceend:\|:tableend:\|:toc:'
WIKI_PAGES := $(shell ls wiki.d/*)
SAFE_PAGES := $(shell grep -vl "$(DIRECTIVES)" $(WIKI_PAGES))
UNGROUPED_PAGES := $(patsubst wiki.d/%,%,$(SAFE_PAGES))
GROUPED_PAGES := $(subst .,/,$(UNGROUPED_PAGES))
RAW_PAGES := $(patsubst %,%.raw,$(GROUPED_PAGES))
MD_PAGES := $(patsubst %.raw,%.md,$(RAW_PAGES))
.SECONDARY: $(RAW_PAGES)  # don't delete .md files

#GROUPS=$(shell find [A-Z]* -type d)
GROUPS=Car

WIKI := http://markferry.net/pmwiki/pmwiki.php
PMDOWN := ruby ~/src/pmdown/rb/convert_pm2md.rb

# Construct a curl argument list to retrieve all URLs over a single connection
# and output each to a .raw file
CURL_ALL := $(shell echo $(NAMES) | sed -e "s%\S\+%\'$(WIKI)/&?action=markdown\' -o &.md%g")

default: all
help:
	echo grep -vl "$(DIRECTIVES)" $(NON_NETWORK_PAGES)

%.md: %.raw  FORCE
	cat $< \
	| sed -f convert.sed \
	| sed -f fix-code.sed \
	| sed -e "s%/$(@D)/%%g" \
	> $@

%.raw:
	mkdir -p $(dir $@)
	curl "$(WIKI)?n=$(subst /,.,$(basename $@))&action=markdown" -o $@

fetch: $(RAW_PAGES)
all: $(MD_PAGES)

.PHONY: FORCE help
FORCE: ;
