#!/bin/sh

export SITE="https://$(git config --get remote.origin.url | sed 's,.*:\([^/]*\).*,\1,').codeberg.page"
export ROOT="${0%/*}/"
export BASENAME=""

fmt() { local f; [ $# -gt 1 ] && f="$1" && shift || f='%s'; printf "$f\n" "$@"; }

alias info='fmt >&2'

try() { command >/dev/null -v "$1" && "$@"; }

isfn() { [ "$(command -v "$1")" = "$1" ]; }

hdr_title() {
	echo "${1%.md}"
}

cmark() {
#	cmark -t html --unsafe
	pulldown-cmark -T -S -G -m
}

hdr() {
	cat <<-.
	<!DOCTYPE html>
	<html lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<link href="/style.css" rel="stylesheet" type="text/css" media="all">
		<link href="/favicon.ico" rel="icon" type="image/x-icon" sizes="256x256 128x128 48x48 32x32 16x16">
		<link href="/favicon.svg" rel="icon" type="image/svg+xml">
		$(try hdr_link "$@")
		<title>$(try hdr_title "$1" || echo "$1")$(try hdr_title_append)</title>
	</head>
	<body>
	.
}

ftr() {
	cat <<-.
	<br />
	<font size="-2">
		<a href="/.gen.sh" style="text-decoration: none">
			<code>made with <3 and /.gen.sh</code>
		</a>
	</font>
	</body>
	</html>
	.
}

globtracked() {
	git --glob-pathspecs ls-files "${@:-'*.md'}"
}

nav_contents() {
	[ $# -gt 0 ] && printf '%s\n' "$@" && return
	echo index.md
	globtracked '*.md' | grep -vF index.md
	echo */
}

nav() {
	local t
	echo '<nav style="text-align: center">'

	set -- $(nav_contents "$@")
	while [ $# -gt 0 ]; do
		t="${1%.md}"
		if [ x"$1" = x'index.md' ]; then
			printf "<a href=\"/%s.html\">%s</a>" "$t" "$t"
		else
			printf "<a href=\"./%s.html\">%s</a>" "$t" "$t"
		fi
		shift
		[ $# -gt 0 ] && printf '%s' ' | '
	done

	echo '</nav>'
	echo '<hr>'
}

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

md2title() {
	echo "$(sed -n '/^#/{s/^##* //p;q}' "$@")"
}

chatquote() {
	awk -v prefix="' " '
	BEGIN { chat = 0 }
	$0 ~ "^"prefix"[^ ]" {
		if (!chat) {
			chat = 1
			print "<table>"
		}
		sub("^"prefix, "", $0)
		i =  index($0, ": ")
		print "<tr>"
		print "<td style=\"vertical-align: top; text-align: end;\">" (i == 0 ? $0 : "<b>" substr($0, 0, i - 1) "</b></td>" "<td>" substr($0, i + 2)) "</td>"
		print "</tr>"
		next
	}
	chat == 1 {
		print "</table>"
		chat = 0
	}
	1
	'
}

linebr() {
	sed -e 's,^ \([a-zA-Z]\),<br />\n\1,'
}

ramble() {
	sed	-e 's,\(^\|[^>]\|/>\)(||\($\|[^<]\),\1 <span class="ramble" tabindex="0"><label><input type="checkbox" /></label><span> \2,g' \
		-e 's,\(^\|[^>]\|/>\)(||\($\|[^<]\),\1 <span class="ramble" tabindex="0"><label><input type="checkbox" /></label><span> \2,g' \
		-e 's,\(^\|[^>]\|/>\)||)\($\|[^<]\),\1 </span></span> \2,g' \
		-e 's,\(^\|[^>]\|/>\)||)\($\|[^<]\),\1 </span></span> \2,g' \
		-e 's,\(^\|[^>]\|/>\)||)</p>$,\1 </span></span>,'
#	| sed 's,(||,<div class="ramble"><details><summary></summary>,g;s,||),</details></div>,g'
}

hdrlink() {
	sed -e 's,<\(h[2-6]\)>\([^<]*\),<\1 id="\2"><a href="#\2">\2</a>,'
}

contents_gen="chatquote | linebr | cmark | ramble"

contents() {
	cat "$@" | eval $contents_gen
}

gen() {
	hdr "$1"
	[ "$#" -gt 1 ] && shift
	nav
	contents "$@"
	ftr
}

md2html() {
	local f
	[ "$#" -eq 0 ] && set -- $(globtracked '*.md')
	for f in "$@"; do
		[ -e "$f" ] || continue
		info "generating $f"
		gen "$f" > "${f%.md}".html
	done
}

md2html

for d in */ .assets/; do
	[ -e "$d" ] || continue
	info "entering $d"
	(cd -- "$d";
		d="${d%/}"
		BASENAME="${BASENAME:+$BASENAME/}$d";
		PATHNAME="$SITE${BASENAME:+/$BASENAME}"
		. .gen.sh
	)
done

info "generating redirects"

[ -e _redirects ] || cat > _redirects <<-.
# put manual redirects here

# AUTOGENERATED CONTENT BELOW, DO NOT EDIT
.

sed >/dev/null 2>&1 -i '/^# AUTOGENERATED CONTENT BELOW, DO NOT EDIT$/q' _redirects

find -type d -path '*/.*' -prune -o ! -name '.*' -type d -print0 \
| while IFS= read -r -d $'\0' d; do
	d="${d#.}"
	echo "$d $d/.html"
	echo "$d/index.html $d/.html"
done >> _redirects

info done!
