#!/bin/bash

### Helper functions

isCommand() {
	command -v "$1" &> /dev/null
}

overwriteLine() {
	printf "\e[1A\e[K$1\n"
}

_dur=0
startTimer() {
	_dur="$(date +%s%N)"
	printf "$1\n"
}
endTimer() {
	_dur="$(echo "scale=4 ; ($(date +%s%N) - $_dur) / 1000000000" | bc -l)"
	printf "\e[1A\e[K$1    %.3fs\n" $_dur
}

### Parameters and variables

csg_help() {
	echo 'Usage: csg <source_dir> <output_dir> <title_format> [<scripts_dir>]

Arguments:
  <source_dir>        Folder which contains source files
  <output_dir>        Folder in which the generated site will be put
  <title_format>      Format string for page title
                      Accepts any text or HTML, & is replaced with contents
                      title (first h1 header).
  <scripts_dir>       Optional, folder which contains awk script for editing of
                      generated webpages.

Variables:
  CSG_AWK
  CSG_MD_TO_HTML
  CSG_TIMESTAMPS_FILE
  CSG_TEMP_DIR

Exit status:
  1    Missing dependency
  2    Bad file type or missing read/write permissions

Dependencies:
  bash
  awk'
  exit $1
}

[ -z "$1" -o "$1" = '-h' -o "$1" = '-?' -o "$1" = '--help' ] && csg_help 0

SOURCE="$1"
[ ! -d "$SOURCE" ] && echo "[Error] Passed $SOURCE isn't a directory or doesn't exist!" \
	&& csg_help 2
[ ! -r "$SOURCE" ] && echo "[Error] No read access to $SOURCE!" \
	&& csg_help 2

OUTPUT="$(realpath "$2")"
TITLEF="$3"

AWKSCRIPTS="$4"
[ ! -d "$AWKSCRIPTS" ] && echo '[Error] Passed <scripts_dir> isn'\''t a directory or doesn'\''t exist!' \
	&& csg_help 2

if [ -n "$CSG_AWK" ]
then :
elif isCommand 'gawk'
then
	CSG_AWK='gawk --posix'
else
	echo '[Error] Couldn'\''t find an awk implementation!'
	csg_help 1
fi

if [ -n "$CSG_MD_TO_HTML" ]
then :
elif isCommand 'lowdown'
then
	CSG_MD_TO_HTML="lowdown \
		--html-no-escapehtml \
		--html-no-skiphtml \
		--parse-no-metadata \
		--parse-no-autolink"
else
	echo '[Error] Couldn'\''t find a Markdown to HTML convertion program!'
	csg_help 1
fi

: ${CSG_TIMESTAMP_FILE:="$(realpath ./timestamps.txt)"}

: ${CSG_TMP_DIR:='/tmp'}
[ ! -d "$CSG_TMP_DIR" ] && echo "[Error] Temporary directory $CSG_TMP_DIR doesn't exist!" \
	&& csg_help 2
[ ! -r "$CSG_TMP_DIR" -o ! -w "$CSG_TMP_DIR" ] && echo "[Error] No read or write permissions to temporary directory $CSG_TMP_DIR!" \
	&& csg_help 2

### Stage 0
### Create timestamps.txt file, if necessary

if isCommand 'git'
then
	timestamps_writeDates() {
		for file in $(find . -type f -name '*.md')
		do
			dates=($(git log --follow --pretty=format:%ad --date="format:%d %B %Y" -- "$file"))
			line="$OUTPUT${file:1:-2}html\t${dates[@]: -3:3}\t${dates[@]:0:3}"
			echo -e $line >> "$CSG_TIMESTAMP_FILE"
		done
	}

	headc=$(git rev-parse HEAD)
	if [ ! -f "$CSG_TIMESTAMP_FILE" ] || [ "$headc" != "$(head -1 "$CSG_TIMESTAMP_FILE")" ]
	then
	startTimer "[Stage 0] Creating timestamps file"
		echo "$headc" > "$CSG_TIMESTAMP_FILE"
		(cd "$SOURCE" && timestamps_writeDates)
	endTimer "[Stage 0] Done"
	fi
fi

### Stage 1
### Copy everything from SOURCE to OUTPUT
startTimer "[Stage 1] Moving files to output"

(cd "$SOURCE" && \
	find . -type f -not -name "\.*" -not -name '_header.html' -not -name '_footer.html' | \
	cpio -pdu "$OUTPUT" >/dev/null 2>&1)

find "$OUTPUT" -type f -name "*index.md" -links +1 | while read -r file
do
	mv "$file" "${file%md}html"
done

overwriteLine "[Stage 1] Combining awk scripts"

scr_path=()
scr_recip=()
while read -r reg; read -r scripts
do
	cat $scripts > $CSG_TMP_DIR/csg-${#scr_path[@]}.awk
	scr_path+=("$CSG_TMP_DIR/csg-${#scr_path[@]}.awk")
	scr_recip+=("$(find $OUTPUT -type f -regex "$reg")")
done < <($CSG_AWK '
{
	# Remove first field
	$1=""; sub("^["FS"]*", "", $0)
	# 
	if (awks[$0] == "") awks[$0] = FILENAME
	else                awks[$0] = awks[$0] " " FILENAME

	nextfile
}
END {
	for (reg in awks) {
		print reg
		print awks[reg]
	}
}
' $(find $AWKSCRIPTS -type f -name "[A-Z]*.awk"))

overwriteLine "[Stage 1] Finding awk libraries"

scr_libs=""
while read -r file
do
	scr_libs+=" -f $file "
done < <(find $AWKSCRIPTS -type f -name "#*.awk")

endTimer "[Stage 1] Done"

### Stage 2
### Transforming Markdown
startTimer "[Stage 2] Transforming Markdown to HTML"

transform-file() {
	local file="$1"
	local outf="${file%md}html"
	local scripts="$2"

	# Markdown stats for a file: title, description, words, date created, date edited
	local title="$($CSG_AWK -v titlef="$TITLEF" '
	/^# / {
		$1 = ""
		sub(FS, "")
		sub(/.*/, titlef)
		print
		exit
	}' "$file")"
	local descr="$($CSG_AWK '
	/^[[:alpha:]]/ {
		startedGathering = 1
		# Take only anchor text
		gsub(/\]\([^)]*\)/, "")
		gsub(/\[/, "")
		# Append line
		if (desc == "") desc = $0
		else desc = desc " " $0
		# Trim line to 200 characters
		if (length(desc) > 200) {
			desc = substr(desc, 1, 197) "..."
			exit
		}
	}
	startedGathering && /^$/ { exit }
	END { print desc }' "$file")"
	local words="$($CSG_AWK '{total += NF} END{print total}' "$file")"

	# Generate
	$CSG_MD_TO_HTML "$file" > "$outf"

	# Run custom awk scripts
	$CSG_AWK \
		-v title="$title" \
		-v description="$descr" \
		-v words="$words" \
		$scr_libs $scripts -f "$CSG_TMP_DIR/csg-print.awk" \
		"$outf" > "$outf.tmp"
	mv "$outf.tmp" "$outf"

	# Create final file
	$CSG_AWK -v title="$title" -v description="$descr" '
	/\[DESCRIPTION\]/ {
		gsub(/\[DESCRIPTION\]/, description)
	}
	/\[TITLE\]/ {
		gsub(/\[TITLE\]/, title)
	}
	{print}' "$SOURCE/_header.html" > "$file"
	cat "$outf" "$SOURCE/_footer.html" >> "$file"
	mv "$file" "$outf"
}

echo '$0 != 0 {print}' > $CSG_TMP_DIR/csg-print.awk

while read -r file
do
	scripts=""
	for i in ${!scr_recip[@]}
	do
		[ "${scr_recip[$i]}" != "${scr_recip[$i]/${file%md}html/}" ] && scripts+=" -f ${scr_path[$i]} "
	done
	transform-file "$file" "$scripts" &
done < <(find "$OUTPUT" -type f -name "*.md")
wait
endTimer "[Stage 2] Done"
