#!/bin/sh

ctime() {
	local f="$1"; shift
	local fmt="$1"; shift
	local t="$(git log --diff-filter=A --follow --format='%at' -1 -- "$f")"
	local tstat="$(stat -c '%Z' "$f")"
	[ -n "$t" ] && [ "$tstat" -gt "$t" ] \
	|| t="$tstat" #|| git >/dev/null 2>&1 ls-files --error-unmatch -- "$f"
	[ -n "$t" -a -n "$fmt" ] && date +"$fmt" -d @"$t" || echo "$t"
}

mtime() {
	local f="$1"; shift
	local fmt="$1"; shift
	local t="$(git log --follow --format='%at' -1 -- "$f")"
	local tstat="$(stat -c '%Y' "$f")"
	[ -n "$t" ] && [ "$tstat" -lt "$t" ] || git diff-index --quiet HEAD -- "$f" || t="$tstat"
	[ -n "$t" -a -n "$fmt" ] && date +"$fmt" -d @"$t" || echo "$t"
}

sortbytc() {
	local f
	[ -e "$1" ] || return
	for f in "$@"; do
		echo "$(ctime "$f") $f"
	done | sort -rn -k2 | while IFS=' ' read -r c f; do echo "$f"; done
}

posts="$(sortbytc $(globtracked '*.md'))"

(
	hdr_title_append() {
		echo " - notchoc's blog"
	}

	hdr_title() {
		md2title "$@"
	}

	nav_contents() {
		echo ../blog/
	}

	contents_gen="$contents_gen | hdrlink"
	md2html *.md
)

(
	contents() {
		cat <<-. | ramble
		<font size="-1">
		<details>
			<summary>
				posts in progress (||i swear ill get around to finishing them eventually (||it's just gonna keep growing isn't it||)||)
			</summary>
			<ul>
		.
		sortbytc $(globtracked -o --exclude-standard -- '*.md') \
		| while IFS= read -r f; do
			local title="$(md2title "$f")"
			echo "<li>${title:-${f%.md}} &nbsp;<i>since $(ctime "$f" "%b %d, %Y")</i></li>"
		done
		cat <<-.
			</ul>
		</details>
		</font>
		<br />
		.
		local f
		for f in "$@"; do
			t="${f%.md}"
			cat <<-.
			<div>
				<i>$(ctime "$f" "%b %d, %Y")</i>
				<h2 style="margin:0;">
					<a href="./$t.html">$(md2title "$f")</a>
				</h2>
				<p>$(awk -v RS= 'NR==2{print; exit}' "$f")</p>
			</div>
			<br />
			.
			shift
		done

		echo '</nav>'
	}

	hdr_link() {
		cat <<-.
		<link href="./atom.xml" type="application/atom+xml" rel="alternate" title="notchoc's atom blog feed thing" />
		<link href="./rss.xml" type="application/rss+xml" rel="alternate" title="notchoc's rss blog feed thing" />
		.
	}

	nav_contents() {
		printf '%s\0' ../index.md
	}

	gen 'blog/' $posts > .html
)

summarize() {
	cat "$1" | tr -c 'a-zA-Z' '\n' | sed '/^$/d' | tr 'A-Z' 'a-z' \
	| grep '....' | grep -vw -e this -e that -e with -e from -e http -e https -e 'does.*' \
	| sort | uniq -c | sort -nr \
	| (a=;while read -r n w; do [ -z $a ] && a=$n || [ $n -gt $((a/3+1)) ] || break; echo "$w"; done) \
	| tr '\n' ',' | sed 's/,$//'
	echo
}

genatom() {
	local f
	DATEFORMAT='%Y-%m-%dT%H:%M:%SZ'
	cat <<-.
	<?xml version="1.0" encoding="utf-8"?>
	<feed xmlns="http://www.w3.org/2005/Atom">
		<title>notchoc blogs posts</title>
		<subtitle>in which notchoc uses software and complains about it</subtitle>
	.

	for f in $posts; do
		local published="$(ctime "$f" "$DATEFORMAT")"
		local updated="$(mtime "$f" "$DATEFORMAT")"
		: "${updated:="$published"}"

		cat <<-.
		<entry>
			<title>$(sed '1s/^##* //;q' "$f")</title>
			<link href="${f%.md}.html" ref="alternate" type="text/html" />
			<published>$published</published>
			<updated>$updated</updated>
			<content>
				$(sed '1{/^##*/d}' "$f" | contents)
			</content>
		</entry>
		.
	done

	cat <<-.
	</feed>
	.
}

genrss() {
	local f
	DATEFORMAT='%a, %d %b %Y %H:%M:%S +0000'
	cat <<-.
	<rss version="2.0">
	<channel>
		<title>notchoc blogs posts</title>
		<link>$PATHNAME/.html</link>
		<description>in which notchoc uses software and complains about it</description>
	.

	for f in $posts; do
		local published="$(ctime "$f" "$DATEFORMAT")"

		cat <<-.
		<item>
			<title>$(sed '1s/^##* //;q' "$f")</title>
			<link>$PATHNAME/${f%.md}.html</link>
			<guid>$PATHNAME/${f%.md}.html</guid>
			<pubDate>$published</pubDate>
			<description>
				<![CDATA[
				$(sed '1{/^##*/d}' "$f" | contents)
				]]>
			</description>
		</item>
		.
	done

	cat <<-.
	</channel>
	</rss>
	.
}


{
	contents() {
		cat "$@" | cmark
	}
	genrss > rss.xml
	genatom > atom.xml
}
