#!/usr/bin/env bash


# set default config values, load user config, export config variables
name=""
output="web"
article_template="/opt/notes2web/templates/article.html"
textarticlehead_template="/opt/notes2web/templates/textarticlehead.html"
textarticlefoot_template="/opt/notes2web/templates/textarticlefoot.html"
listitem_template="/opt/notes2web/templates/listitem.html"
index_template="/opt/notes2web/templates/index.html"
stylesheet="/opt/notes2web/styles.css"

for configpath in "$HOME/.notes2web.conf" "$HOME/.config/notes2web/config" ".notes2web.conf"
do
	[[ -f "$configpath" ]] && source "$configpath"
done

export name
export output
export article_template
export textarticlehead_template
export textarticlefoot_template
export listitem_template
export index_template
export stylesheet


[[ "$1" == "--help" ]] && echo "USAGE: $0 [NOTES_DIRECTORY_1 [NOTES_DIRECTORY_2 [...]]]" && exit 0
[[ "$1" == "--clean" ]] && {
	find -name ".2web" -exec rm {} \;
	rm -rf "$output"
	exit 0
}


mkdir -p "$output"


function _renderarticle {
	echo "rendering $1"
	mkdir -p "$(dirname "$output/${1}.html")"
	pandoc\
		--toc\
		--standalone\
		-t html\
		--template "$article_template"\
		-o "$output/${1}.html"\
		"$1"\
		--mathjax
}


function _rendertextarticle {
	[[ "$(file -b "$1")" == "ASCII text" ]] || exit
	echo "rendering text file $1"
	mkdir -p "$(dirname "$output/${1}.html")"
	sed -e "s#\\\$title\\\$#$1#" "$textarticlehead_template"\
		> "$output/${1}.html"
	cat "$1" >> "$output/${1}.html"
	cat "$textarticlefoot_template" >> "$output/${1}.html"
}


function _adddirtoindex {
	dir="$(dirname "$1")"
	echo "<h2>$(basename "$dir") notes</h2>" >> $output/index.md
	find "$dir" -name '*.md' -exec bash -c "_addarticletoindex '{}'" \;
	find "$dir" -not -path '**/.git/**' -not -name '*.md' -type f -exec bash -c "_addtextarticletoindex '{}'" \;
}


function _addtextarticletoindex {
	[[ "$(file -b "$1")" == "ASCII text" ]] || exit
	pandoc\
		-t html\
		-V "filepath=${1}.html"\
		-V "title=$1"\
		--template "$listitem_template"\
		"$1"\
		>> $output/index.md
}


function _addarticletoindex {
	echo "adding $1 to list of notes"
	pandoc\
		-t html\
		-V "filepath=${1}.html"\
		--template "$listitem_template"\
		"$1"\
		>> $output/index.md
}


export -f _renderarticle
export -f _rendertextarticle
export -f _adddirtoindex
export -f _addarticletoindex
export -f _addtextarticletoindex


#render each markdown file in every folder passed in args
for dir in "$@"
do
	find "$dir" -name '*.md' -exec bash -c "_renderarticle '{}'" \;
	find "$dir" -not -path '**/.git/**' -not -name '*.md' -type f -exec bash -c "_rendertextarticle '{}'" \;
done


# create an intermediate markdown file of links to each article
echo "---" > $output/index.md
[[ -z "$name" ]] && echo "title: notes" >> $output/index.md || echo "title: ${name}'s notes" >> $output/index.md
echo "---" >> $output/index.md


# mark folders to be included in notes2web's index
for file in "$@"
do
	[[ ! -f "$file" ]] && echo "the presence of this files tells notes2web that it should be added to the notes2web index" > "$file/.2web"
done


# add articles to index and render
find -name '.2web' -exec bash -c "_adddirtoindex '{}'" \;


echo "copying styles.css to current directory"
cp "$stylesheet" "$output/styles.css"


echo "rendering index.md"
pandoc\
	-t html\
	--template "$index_template"\
	-o "$output/index.html"\
	"$output/index.md"