#!/bin/sh -e

# contains and eq from dylanaraps (MIT): 
#  https://github.com/kisslinux/kiss/blob/master/LICENSE
cntn() {
    # Check if a "string list" contains a word.
    case "$1" in *"$2"*) return 0; esac; return 1
}

eq() {
    # Check if a string is equal to enother.
    # This replaces '[ "$var" = str ]' and '[ "$var" != str ]'.
    case $1 in "$2") return 0 ;; *) return 1; esac
}

log() {
	echo "$@" >&2
}

html_head() {
	cat <<-EOHTML
	<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">
	<title>${1:?}</title>
	</head>
	EOHTML
}

html_nav() {
	echo '<nav><!--'

	href() {
		echo "--><a href=\"/$1\"${class:+ class=\"$class\"}>${2:-$root_md_name}</a><!--"
	}

	# generate <nav> based on all root markdown files
	# that will be generated, and attempt to prefer the 
	# home as the first file by removing it from the glob
	# and including it as the first member in the loop
	for root_md in index.md [!index]*.md; do
		class=''
		root_md_name="${root_md%%.md}"

		# if the iterated entry is currently the one being generated
		# (and is currently being viewed) or is the name of the directory
		# of the currently generated file
		case "$root_md_name" in
			"$md_name"|"${md_name%/*}") class=visiting ;;
		esac

		href "${root_md_name}.html"

		# cut the iterated file name to use as a directory to figure out if the 
		# current generated file is associated with the iterated file
		[ "${md_name#$root_md_name/}" = "$md_name" ] ||
			 class=blog-post href "$md_name.html" "${md_name##*/}"
	done

	echo '--></nav>'
}

gen_html() {
	echo '<!DOCTYPE html>'
	echo '<html lang="en">'
	html_head "sewn/$md_name"

	echo '<body>'

	html_nav "$md_name"
	/home/sewn/src/md4c/build/md2html/md2html "$md_file"

	printf '%s' "$md_file" >&2
	case "$(type -- page_"$md_name" 2>/dev/null ||:)" in
		*function*) page_"$md_name"; echo " [+]" >&2 ;;
		*) echo >&2 ;;
	esac

	echo '</body>'
	echo '</html>'
}

page_blog() {
	echo '<ul class="blog-posts">'
	# iterate in reverse order
	printf '%s\n' blog/*.md | tail -r | while read -r post; do
		read -r title < "$post" # first header serves as title
		title="${title##*#}" # first line is markdown header
		date="${post#blog/}" # filename of post serves as date
		date="${date%.md}"
		echo "<li>$date - <a href=\"${post%.md}.html\">$title</a></li>"
	done
	echo '</ul>'
}

page_photos() {
	echo '<div class="photos">'

	mkdir -p assets/thumbnails

	# delete old thumbnails
	for t in assets/thumbnails/*; do
		orig="${t%%_thumbnail*}.${t#*.}"
		orig="assets/photos/${orig##*/}"
		[ -f "$orig" ] || rm -rf "$t"
	done

	# sort files in order of checksum numbers instead of date
	while read -r md5 file; do
		name="${file##*/}"
		thumb="assets/thumbnails/${name%.*}_thumbnail.${name#*.}"

		[ -f "$thumb" ] ||
		ffmpeg -hide_banner -loglevel error -i "$file" \
			-vf "scale=iw*0.1:ih*0.1" -update true -frames:v 1 "$thumb"

		echo "<a href=\"$file\"><img src=\"$thumb\" alt=\"$md5\"></a>"
	done <<-EOSUMS
		$(md5sum assets/photos/*.jpg | sort -n)
	EOSUMS
	echo '</div>'

	wait
}

rm -f *.html blog/*.html
for md_file in blog/*.md *.md; do
	[ -f "$md_file" ] || continue
	md_name="${md_file%%.*}"
	gen_html "$md_file" > "$md_name".html
done
