#!/bin/sh

#
# Usage:
# make-dir-listing.sh <destination_dir> <base_dir> [current_dir]
#

# --- Data & Variables needed ---

#Webroot directory (the jekyll base directory)
WEBROOT="$HOME/dev/heavydeck-blog"

#Markdown front matter
FRONT_MATTER=`echo -ne '---\nlayout: default\n---\n'`

#Debug. Set to nonzero to enable debug output
DEBUG=0

#Save the absolute path where the script is for recursive calls
if [ "$SCRIPT_PATH" = '' ]
then
  export SCRIPT_PATH=`realpath "$0"`
fi


# --- CLI parsing ---
if [ -z "$1" ]
then
	echo "Missing destination directory"
	exit 1
else
	mkdir -p "$1"
	DESTINATION_DIR=`realpath "$1"`
fi

if [ -z "$2" ]
then
	echo "Missing base directory"
	exit 1
else
	BASE_DIR=`realpath "$2"`
fi

if [ -z "$3" ]
then
	#If no current dir, use base dir
	CURRENT_DIR="$BASE_DIR"
else
	CURRENT_DIR=`realpath "$3"`
fi

# --- Preprocess command line arguments ---
CURRENT_REL_DIR=`realpath --relative-to="$BASE_DIR" "$CURRENT_DIR"`
MD_FILE="$DESTINATION_DIR/index.md"

#Print info if base is the same as current
if [ "$CURRENT_DIR" = "$BASE_DIR" ]
then
	echo "Generating directory listing"
	echo "    Destination: $DESTINATION_DIR"
	echo "   Listing base: $BASE_DIR"
fi
#Extra debug info
if [ "$DEBUG" -ne 0 ]
then
	echo "DEBUG--DEBUG--DEBUG"
	echo "    Relative: <$CURRENT_REL_DIR>"
fi

#Chdir into current dir
cd "$CURRENT_DIR"

#Create MD file header
echo "$FRONT_MATTER" > "$MD_FILE"
echo '' >> "$MD_FILE"
_DIR_PATH=`realpath --relative-to="$BASE_DIR/../" "$PWD"`
#echo "### $_DIR_PATH" >> "$MD_FILE"
echo '' >> "$MD_FILE"
#echo '| File | Size    |' >> "$MD_FILE"
echo "| $_DIR_PATH | Size    |" >> "$MD_FILE"
echo '|------|--------:|' >> "$MD_FILE"
echo '| [▲ Parent directory](../) | DIR |' >> "$MD_FILE"

#For each directory, make a directory entry
for d in `find -maxdepth 1 -type d | sort -V`
do
	#Skip myself and hidden/unavailable dirs
	if [ "$d" = "." ]; then continue; fi
	if [ `echo "$d" | grep -oF './_'` ]; then continue; fi
	if [ `echo "$d" | grep -oF './.'` ]; then continue; fi

	#Build table entry
	_NAME=`basename "$d"`
	echo "| [$_NAME/]($d) | DIR |" >> "$MD_FILE"
done

#For each file, make a file entry
for f in `find -maxdepth 1 -type f | sort -V`
do
	#Skip myself and hidden/unavailable dirs
	if [ `echo "$d" | grep -oF './_'` ]; then continue; fi
	if [ `echo "$d" | grep -oF './.'` ]; then continue; fi

	#Build file entry
	_NAME=`basename "$f"`
	_SIZE=`stat -t --format="%s" "$f" | numfmt --to=iec`
	_LINK=`realpath --relative-to="$WEBROOT" "$f"`
	#If link is not .md use a {% link %} to the file itself
	#OTherwise link to the generated .html
	if [ -z `echo "$_LINK" | grep -o '\.md$'` ]
	then
		echo "| [$_NAME]({% link /$_LINK %}) | $_SIZE |" >> "$MD_FILE"
	else
		_LINK=`echo "$_LINK" | sed 's/\.md$/.html/'`
		echo "| [$_NAME]({{ \"$_LINK\" | absolute_url }}) | $_SIZE |" >> "$MD_FILE"
	fi
done

#For each directory, go deeper
for d in `find -maxdepth 1 -type d`
do
	#Skip myself and hidden/unavailable dirs
	if [ "$d" = "." ]; then continue; fi
	if [ `echo "$d" | grep -oF './_'` ]; then continue; fi
	if [ `echo "$d" | grep -oF './.'` ]; then continue; fi

	echo "->" `realpath --relative-to="$BASE_DIR" "$d"`
	#Create destination dir and process $d
	if [ '!' -d "$DESTINATION_DIR/$d" ]
	then
		mkdir "$DESTINATION_DIR/$d"
	fi
	$SCRIPT_PATH "$DESTINATION_DIR/$d" "$BASE_DIR" "$d"
done
