#!/bin/bash

if [ "$#" != "2" ]; then
	echo "usage: $0 file.md generated_file.extension"
	echo -e "extensions: html, pdf, odt, 1-8 (man page), md (embed images)"
	echo "example: $0 file.md file.pdf"
	exit 1
fi

src="$1"
dst="$2"
src_ext=$(echo "$src" | rev | cut -f1 -d. | rev)
dst_ext=$(echo "$dst" | rev | cut -f1 -d. | rev)

#~ if [ -f "$dst" ]; then
	#~ echo "error: $dst already exists."
	#~ exit 2
#~ fi
if [ "$src" == "$dst" ]; then
	echo "error: source and destination must be different ($src)"
	exit 2
fi

title=$(cat "$src" | grep -B1 "^=\+" | head -n1)
title_cnt=$(cat "$src" | grep "^=\+" | wc -l)
if [ -z "$title" ]; then
	title=$(cat "$src" | grep "^# " | cut -c3- | head -n1)
	title_cnt=$(cat "$src" | grep "^# " | wc -l)
fi


if [ "$src_ext" == "md" ]; then
	if [ "$dst_ext" == "html" ]; then
		lowdown -m title="$title" --html-no-skiphtml --html-no-escapehtml -sthtml -o "$dst" "$src"
		table_css="table, td, th { border: 1px solid; border-collapse: collapse; padding: 0.25em; }"
		title_css="h1 { text-align: center; }"
		css="<style>\n$table_css\n$title_css\n<\/style>"
		sed "s/\(<\/head>\)/$css\n\1/" -i "$dst"
		
	elif [ "$dst_ext" == "md" ]; then #md -> md
		cp "$src" "$dst"
		cat "$dst" | grep "\!\[" | sed 's/\!\[.*\](\([^ ]*\)\( .*\)\?)\({.*}\)\?/\1/' | while read url; do #for each inline image: ![alt text](URL ...){...}
			img="$(basename "$url")"
			download="false"; if [ ! -f "$img" ]; then wget "$url"; download="true"; fi #download if needed
			mime=$(file --mime-type "$img" | cut -f2 -d' ')
			if [ "$mime" == "image/svg+xml" ]; then
				cnt=$(cat "$img"); cnt=$(echo $cnt)
				sed "s@\!\[.*\]($url\( .*\)\?)\({.*}\)\?@$cnt@" -i "$dst"
			else
				b64=$(base64 "$img"); b64=$(echo $b64 | sed 's/ //g')
				sed "s@\!\[\(.*\)\]($url\( =\([0-9]\+\)x\([0-9]\+\)\)\?\( ['\"]\(.*\)['\"]\)\?)\({.*}\)\?@<img alt='\1' title='\6' width='\3' width='\4' src='data:$mime;base64,$b64'/>@" -i "$dst"
			fi
			if [ "$download" == "true" ]; then rm -f "$img"; fi
		done
		
	else
		if [ "$title_cnt" == 1 ]; then
			backshift="-m shiftheadinglevelby=-1"
			cat "$src" | sed -e "/^$title$/d" -e '/^=\+/d' -e '/^# /d' > "$src.tmp"
		else
			cp "$src" "$src.tmp"
		fi
		if [ "$dst_ext" == "pdf" ]; then
			cat "$src.tmp" | grep '\.\(jpg\|jpeg\|png\|svg\)' | sed 's/.*[ (]\(.*\.\(jpg\|jpeg\|png\|svg\)\).*/\1/' | while read url; do #for each image
				img="$(basename "$url")"
				download="false"; if [ ! -f "$img" ]; then wget "$url"; download="true"; fi
				if echo $img | grep -q '.svg$'; then
					rsvg-convert -f ps -o "$img.ps" "$img"
				else
					convert "$img" "$img.ps" #convert to PostScript (work with SVG too, but rsvg-convert is way better)
				fi
				sed "s@$url@$img.ps@" -i "$src.tmp"
				if [ "$download" == "true" ]; then rm -f "$img"; fi
			done
			lowdown $backshift -m title="$title" -stms "$src.tmp" | pdfroff -itk -mspdf > "$dst"
			rm -f *.png.ps *.jpg.ps *.jpeg.ps *.svg.ps 2>/dev/null
		elif [ "$dst_ext" == "odt" ]; then
			lowdown -m title="$title" -stfodt -o "$dst" "$src.tmp"
			
		elif [ "$dst_ext" == "1" ] || [ "$dst_ext" == "2" ] || [ "$dst_ext" == "3" ] || [ "$dst_ext" == "4" ] ||
			[ "$dst_ext" == "5" ] || [ "$dst_ext" == "6" ] || [ "$dst_ext" == "7" ] || [ "$dst_ext" == "8" ]; then
			lowdown -m title="$title" -m section=$dst_ext -stman "$src.tmp" | groff -itk -man -Tutf8 > "$dst"
			
		else
			echo "error: usupported '$dst_est' format"
			exit 3
		fi
		rm -f "$src.tmp"
	fi
fi
