#!/usr/bin/make -sf

r := showerthoughts CrazyIdeas oneliners
update := 600

wits := /tmp/reddit-wisdom/all.txt
age := $(shell expr `date +%s` - `date -r $(wits) +%s 2>/dev/null || echo 0`)
$(shell [ $(age) -gt $(update) ] && rm -f /tmp/reddit-wisdom/*)

all: $(wits)
	sort -R < $< | head -1 | w3m -dump -T text/html

$(wits): $(patsubst %, /tmp/reddit-wisdom/%.json, $(r))
	jq -r '.data?.children[]?.data | "\"\(.title)\" -- \(.author)" | gsub("\\n"; " ")' $^ > $@

/tmp/reddit-wisdom/%.json:
	mkdir -p $(dir $@)
	echo Fetching r/$*
	curl -sf 'https://www.reddit.com/r/$*/top.json?sort=top&t=week&limit=100' > $@

.DELETE_ON_ERROR:
