#!/bin/sh

date=$(date '+%D')
doc=$1
title=$2

echo "<!DOCTYPE html>" > ledger.html

echo "<html lang='en'>" >> ledger.html
echo "<head>" >> ledger.html
echo "<title>${title}</title>" >> ledger.html
echo "<meta charset=\"utf-8\"/>" >> ledger.html
echo "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" >> ledger.html
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"ledger.css\" />" >> ledger.html
echo "</head>" >> ledger.html
echo "<body>" >> ledger.html
echo "<h1>" >> ledger.html
echo "${title} <span style=\"font-size:60%;\">powered by <a href=\"https://github.com/beancount/beancount\">Beancount</a></span>" >> ledger.html
echo "</h1>" >> ledger.html
echo "<hr>" >> ledger.html
echo "<p>Go straight to the <a href=\"#your-owing\">Balances</a> section for: <ul><li> the amount of money you are owed or owing;</li><li>your personal ledger.</li></ul></p>" >> ledger.html

#==========================================
# our ledger
echo "<h2>" >> ledger.html
echo "Our ledger" >> ledger.html
echo "</h2>" >> ledger.html

#------------------------------------------
## stuff shared by all
echo "<h3>" >> ledger.html
echo "Stuff shared by all" >> ledger.html
echo "</h3>" >> ledger.html

echo "<pre>" >> ledger.html
echo "<label for=\"unfold1\">Read unfolded ledger by checking here -> </label><input id=\"unfold1\" type=\"checkbox\">" >> ledger.html
echo "<div class=\"pre-box\">" >> ledger.html
echo "<div>" >> ledger.html
echo "$ bean-query ${doc} 'SELECT date,account,payee,position WHERE narration ~ \"for all\"'" >> ledger.html
echo "</div>" >> ledger.html
echo "<div class=\"ledger\" id=\"full1\">" >> ledger.html
bean-query ${doc} 'SELECT date,account,payee,position WHERE narration ~ "for all"' >> ledger.html
echo "</div>" >> ledger.html
echo "<div class=\"ledger\" id=\"brief1\">" >> ledger.html
bean-query ${doc} 'SELECT date,account,payee,position WHERE narration ~ "for all"' | awk -f fold.awk >> ledger.html
echo "</div>" >> ledger.html
echo "</div>" >> ledger.html
echo "</pre>" >> ledger.html

#------------------------------------------
## stuff shared by some
echo "<h3>" >> ledger.html
echo "Stuff shared by some" >> ledger.html
echo "</h3>" >> ledger.html

echo "<pre>" >> ledger.html
echo "<label for=\"unfold2\">Read unfolded ledger by checking here -> </label><input id=\"unfold2\" type=\"checkbox\">" >> ledger.html
echo "<div class=\"pre-box\">" >> ledger.html
echo "<div>" >> ledger.html
echo "$ bean-query ${doc} 'SELECT date,account,payee,position WHERE narration ~ \"&\"'" >> ledger.html
echo "</div>" >> ledger.html
echo "<div class=\"ledger\" id=\"full2\">" >> ledger.html
bean-query ${doc} 'SELECT date,account,payee,position WHERE narration ~ "&"' >> ledger.html
echo "</div>" >> ledger.html
echo "<div class=\"ledger\" id=\"brief2\">" >> ledger.html
bean-query ${doc} 'SELECT date,account,payee,position WHERE narration ~ "&"' | awk -f fold.awk >> ledger.html
echo "</div>" >> ledger.html
echo "</div>" >> ledger.html
echo "</pre>" >> ledger.html

#==========================================
# balances
echo "<h2 id="your-owing">" >> ledger.html
echo "Balances" >> ledger.html
echo "</h2>" >> ledger.html
echo "<p>A negative number means you are being owed!</p>" >> ledger.html

bean-query ${doc} 'SELECT DISTINCT account' | tail -n +3 | rev | cut -d':' -f 1 | sed 's/^[[:space:]]*//g' | rev | sort | uniq -d | {
	while read p; do
		echo "<h3>" >> ledger.html
		echo $p | sed 's/\([a-zA-Z]\)\([1-9]\)/\1 \2/g' >> ledger.html
		echo "</h3>" >> ledger.html

		echo "<pre>" >> ledger.html
		echo "<div class=\"pre-box\">" >> ledger.html
		echo "<div>" >> ledger.html
		echo "$ bean-query ${doc} 'SELECT sum(position) WHERE account ~ \":${p}$\"'" >> ledger.html
		echo "</div>" >> ledger.html
		echo "<div class=\"ledger\">" >> ledger.html
		bean-query ${doc} "SELECT date,account,payee,position WHERE account ~ \":${p}$\"" >> ledger.html
		echo "</div>" >> ledger.html
		echo "<div>" >> ledger.html
		bean-query ${doc} "SELECT sum(position) WHERE account ~ \":${p}$\"" | sed 's/sum.*/ Balance/g' >> ledger.html
		echo "</div>" >> ledger.html
		echo "</div>" >> ledger.html
		echo "</pre>" >> ledger.html
	done
}

#==========================================
# Beancount sourcefile

echo "<h2>" >> ledger.html
echo "Beancount source file" >> ledger.html
echo "</h2>" >> ledger.html
echo "<pre>" >> ledger.html
echo "<label for=\"source\">Read full source file by checking here -> </label><input id=\"source\" type=\"checkbox\">" >> ledger.html
echo "<div class=\"pre-box\">" >> ledger.html
echo "<div class=\"ledger\" id=\"verbatim\">" >> ledger.html
cat $doc >> ledger.html
echo "</div>" >> ledger.html
echo "<div class=\"ledger\" id=\"dotdotdot\">" >> ledger.html
echo "..." >> ledger.html
echo "</div>" >> ledger.html
echo "</div>" >> ledger.html
echo "</pre>" >> ledger.html

#==========================================
# Last updated

echo "<hr>" >> ledger.html
echo "Last updated: ${date}" >> ledger.html
echo "</body>" >> ledger.html
