#!/usr/bin/sh
# pdssg - pandoc static site generator
# Makes a static site with Atom feeds.

main() {
	echo "### Begin compiling ###"

	prepare_dst
	find_feeds

	for post_dir in ${POSTS_DIRS}
	do cache_posts_metadata ${post_dir}
	done

	process_files ${MD_FILES}

	for post_dir in ${POSTS_DIRS}
	do make_atom_feed ${post_dir}
	done

	clearup_files ${MD_FILES}
	finish

	echo
	echo "### End compiling ###"
	echo
}


# main functions

prepare_dst() {
	NOW_DATETIME="$(date -uIs)"

	echo
	echo "# Preparing ./dst/ directory"
	echo :: $(pwd)

	[ -d ./src/ ] || {
		echo "No  ./src/  directory, exiting"
		exit
	}

	[ -d ./dst/ ] && {
		echo "Removing previous  ./dst/  build"
		rm -r ./dst/*
	}

	echo "Copying from  ./src/  to  ./dst/"
	cp -r ./src/* ./dst/
	cp ./src/.build.yml ./dst/.build.yml  # builds.sr.ht

	cd ./dst/
	echo :: $(pwd)

	echo "Preparing temporary directory for cache"
	# NOTE: POPF (Point Of Potential Failure)
	TMP_DIR="./_tmp"
	[ -d ${TMP_DIR} ] || mkdir ${TMP_DIR}

	echo "Gathering unignored Markdown files"
	IGNORE_MD_FILE="_ignore"
	touch ${IGNORE_MD_FILE}
	MD_FILES=$(
		find . -regex '.*\.md'  \
		| grep -v '.*/_.*'  \
		| grep -v -F -f ${IGNORE_MD_FILE}
	)
	rm ${IGNORE_MD_FILE}

	echo "Finding feeds home directory"
	FEEDS_DIR="$(cat ./_feeds)"
	FEEDS_DIR=${FEEDSDIR:-./feeds}
	echo "Dir: ${FEEDS_DIR}"

	ASSETS_DIR="./ast" # No ending slash!
	echo "Finding CSS files in assets directory ${ASSETS_DIR}"
	stylesheets=$(find -path "${ASSETS_DIR}/*.css")
	for stylesheet in ${stylesheets}
	do
		echo "Minifying ${stylesheet}"
		min_stylesheet="${stylesheet%.css}.min.css"
		cp ${stylesheet} ${min_stylesheet}
		vim ${min_stylesheet} -es --not-a-term +'
		%s/\s*:\s*/:/ge
		%s/\s*;\s*/;/ge
		%s/\s*,\s*/,/ge
		%s/\s*{\s*/{/ge
		%s/\s*}\s*/}/ge
		%s/\s*(\s*/(/ge
		%s/\s*)\s*/)/ge
		%s/^\s*//e
		%s/\n//e
		%s/\/\*.\{-}\*\///ge
		%s/;}/}/ge
		wq!
		'
	done
}

find_feeds() {
	echo
	echo "# Finding feed gen directories"

	ATOM_SEEDS=$(find ${FEEDS_DIR}  -type f  -path '*.md')
	POSTS_DIRS=$(echo "${ATOM_SEEDS}" | sed -e "s|^${FEEDS_DIR}|.|" -e 's|\.md$||')

	echo ${ATOM_SEEDS}

	for posts_dir in ${POSTS_DIRS}
	do
		[ -d ${posts_dir} ] || {
			echo "ERROR:"
			echo "Atom seed ${FEEDS_DIR}/${posts_dir}.md has no corresponding"
			echo "Directory ${posts_dir}. Exiting."
			exit 1
		}

		echo "${posts_dir} exists"
	done
}

cache_posts_metadata() {
	echo
	echo "# Caching post metadata" "($1)"

	posts_dir=$1
	cache_file="${TMP_DIR}/${posts_dir}/meta.yaml"

	echo -e "Preparing cache file:\t${cache_file}"

	mkdir -p $(dirname ${cache_file})
	echo "log:" > ${cache_file}

	for post in ${posts_dir}/*.md
	do
		echo -e "Caching:\t${post}"

		post_yaml="$(_get_yaml_block ${post})"
		filename=$(basename ${post})
		slug_attr="slug: ${filename%.md}"
		yaml_block="$(echo -e "${post_yaml}\n${slug_attr}" | sed 's/^/    /')"

		echo "  -"         >> ${cache_file}
		echo "${yaml_block}" >> ${cache_file}
	done

	echo "Cache file done"
}

process_files() {
	echo
	echo "# Processing Markdown files"
	echo
	pandoc --version
	echo

	for md_file in $@
	do
		echo -e "Processing:\t${md_file}"

		### prepare flags for pandoc conversion ###
		new_filepath=${md_file%.md}.html

		block="$( _get_yaml_block "${md_file}" )"
		bool_toc="$(_get_safe_slug toc "${block}")"
		[ -n "${bool_toc}" ] && flag_toc="--toc"

		### feed gen directory test ###
		cache_file="${TMP_DIR}/${md_file%.md}/meta.yaml"
		[ -f "${cache_file}" ] && {
			flag_metadata_file="--metadata-file=${cache_file}"
		}

		### article test ###
		cache_file="${TMP_DIR}/$(dirname ${md_file})/meta.yaml" 
		[ -f "${cache_file}" ] && {
			flag_is_article="-M is_article:true"
		}

		### create full HTML page ###
		pandoc  \
			-f markdown  -t html  \
			${md_file}  -o ${new_filepath}  \
			-M lang:en  \
			-M document-css=false  \
			--standalone  \
			--template=./_templates/main.html  \
			-H ./_includes/meta.html  \
			-B ./_includes/header.html  \
			-A ./_includes/footer.html  \
			--email-obfuscation=references  \
			--highlight-style=breezedark  \
			--webtex='https://latex.codecogs.com/svg.latex?'  \
			${flag_toc}  \
			${flag_metadata_file}  \
			${flag_is_article}  \

		### prepare for next md file
		unset flag_toc
		unset flag_metadata_file
		unset flag_is_article
	done
}

make_atom_feed() {
	echo
	echo "# Making atom feed" "($1)"
	echo "Making base feed document"

	posts_dir="$1"
	cache_file="${TMP_DIR}/${posts_dir}/meta.yaml"
	atom_seed="${FEEDS_DIR}/${posts_dir}.md"
	atom_out="${FEEDS_DIR}/${posts_dir}.xml"

	pandoc  \
		-f markdown  -t html  \
		${atom_seed}  -o ${atom_out}  \
		--metadata-file=${cache_file}  \
		-M "date:${NOW_DATETIME}" \
		-M lang:en  \
		--standalone  \
		--template=./_templates/atom.xml

	rm -v ${atom_seed}
	cat ${atom_out}  > ${TMP_DIR}/atom.xml

	echo "Inserting contents"

	posts_reverse=$(echo ${posts_dir}/*.html | sed 's/ /\n/g' | sort -r)
	for post in ${posts_reverse}
	do
		echo -e "Inserting:\t${post}"

		sed -n '/<!--CONTENT-->/,$!p' ${TMP_DIR}/atom.xml   > ${TMP_DIR}/before.xml
		cat "${post}" | sed -n '/<article>/,/<\/article>/p' > ${TMP_DIR}/content.xml
		sed -n '1,/<!--CONTENT-->/!p' ${TMP_DIR}/atom.xml   > ${TMP_DIR}/after.xml
		cat ${TMP_DIR}/{before,content,after}.xml           > ${TMP_DIR}/atom.xml
	done

	echo "Finishing feed"
	cat ${TMP_DIR}/atom.xml  > ${atom_out}
}

clearup_files() {
	echo
	echo "# Clearing up Markdown files"

	rm -v $@
	rm -rv $( find . -regex '.*/_.*' | sort -r )
}

finish() {
	echo
	echo "# Finished compiling"

	cd ..
	echo :: $(pwd)
	echo "Finished"
}


# util functions

_get_yaml_block() {
	md_file="$1"
	block="$(sed -n -e '1,/---/p' ${md_file} | grep -v '\-\-\-')"
	echo "${block}"
}

_get_safe_slug() {
	key="$1"
	block="$2"

	line=$(echo "${block}" | grep "^${key}\s*:\s*")
	title=$(echo ${line} | sed -e "s/^${key}\s*:\s*//")
	slug=$(echo ${title} | sed -e 's/\s*$//' -e 's/\s\+/-/g')
	safe_slug=$(
		echo ${slug}  \
		| tr '[:upper:]' '[:lower:]'  \
		| tr -cd "[:alpha:][:digit:]-"
	)
	echo ${safe_slug}
}


# main

main

