#!/bin/bash

# configurable parameters
GITHUB="https://github.com"
GITHUBUSERCONTENT="https://raw.githubusercontent.com"
REGION="North America"
GITUSER=dchassin
GITREPO="gridlabd-template"
GITBRANCH="master"
CACHEDIR="$GLDDIR_ETC/.gridlabd-template"

# load the configuration (if any)
CONFIGFILE="$GLDDIR_ETC/gridlabd-template.conf"
if [ -f $CONFIGFILE ]; then
	source $CONFIGFILE
fi

function config()
{
	if [ $# == 0 -o "$1" == "show" ]; then
		echo "GITHUB=\"$GITHUB\""
		echo "GITHUBUSERCONTENT=\"$GITHUBUSERCONTENT\""
		echo "REGION=\"$REGION\""
		echo "GITUSER=\"$GITUSER\""
		echo "GITREPO=\"$GITREPO\""
		echo "GITBRANCH=\"$GITBRANCH\""
		echo "CACHEDIR=\"$CACHEDIR\""
	elif [ "$1" == "reset" ]; then
		rm -f $CONFIGFILE
	else
		echo "'%s' is an invalid config option"
		exit 1
	fi
}

# set the internal parameters
GITPROJ="$GITHUB/$GITUSER/$GITREPO"
GITFILE="$GITHUBUSERCONTENT/$GITUSER/$GITREPO/$GITBRANCH"
COMMIT_ID=$(git ls-remote "$GITPROJ" | grep "$GITBRANCH" | head -n 1 | cut -f1)
INDEX=$CACHEDIR/$COMMIT_ID

function fetch_index()
{
	if [ ! -d $CACHEDIR ]; then
		mkdir -p $CACHEDIR
	fi
	if [ ! -f $INDEX ]; then
		echo -n "Downloading ${GITBRANCH} index of $REGION template archive from ${GITPROJ}... "
		svn ls $GITPROJ/branches/$GITBRANCH/$REGION > $INDEX
		echo "done"
		echo $(wc -l $INDEX | cut -c1-8) "template files found"
	fi
}

#
# command line interface
#
function help()
{	
	if [ $# == 0 ]; then
		cat <<-END
			Syntax: gridlabd-template <command> [...]
			Commands:
			  help               Get the list of template subcommands
			  index <pattern>    Index of available template files matching <pattern> in archive
			  list <pattern>     List of available downloaded template files matching <pattern>
			  get <pattern>      Download template files matching <pattern> from archive 
			  delete <pattern>   Delete downloaded template files matching <pattern>
			  info <pattern>     Get information about template files in <pattern>
			  create <name>      Create a template from the files in current folder
			  submit <name>      Submit the template <name> to the template archive
			  clean              Clean up the local cache of the archive index
			  config             Show the current configuration
			END
	fi
}

function clean()
{
	rm -rf $CACHEDIR
	fetch_index
}

function index()
{
	fetch_index
	if [ $# == 0 ]; then
		cat $INDEX
	else
		grep "$1" $INDEX
	fi
}

function list()
{
	fetch_index
	LIST=$(index $1)
	for f in $(index $1); do
		if [ -f $GLDDIR_ETC/$f ]; then
			echo $f
		fi
	done
}

function get()
{
	fetch_index
	for f in $(index $1); do
		if [ ! -f $GLDDIR_ETC/$f ]; then
			echo -n "Downloading $f... "
			curl -s $GITFILE/$REGION/$f > $GLDDIR_ETC/$f
			echo "done"
		else
			echo -n "Refreshing $f... "
			curl -s $GITFILE/$REGION/$f -z $GLDDIR_ETC/$f > $GLDDIR_ETC/$f
			echo "done"
		fi
	done
}

function info()
{
	fetch_index
	LIST=$(index $1)
	first="yes"
	for f in $(index $1); do
		if [ -f $f ]; then
			if [ "$first" == "yes" ]; then
				echo "Template,Variable,Default,Unit,Validation,Description"
				first="no"
			fi
			echo -n "\"$PWD/$f\","
			# TODO
		fi
	done
}

function extract()
{
	fetch_index
	LIST=$(index $1)
	for f in $(index $1); do
		if [ -f $f ]; then
			# TODO
		fi
	done
}

function create()
{
	fetch_index
	# TODO
}

function submit()
{
	fetch_index
	# TODO
}

function delete()
{
	fetch_index
	LIST=$(index $1)
	for f in $(index $1); do
		if [ -f $GLDDIR_ETC/$f ]; then
			rm $GLDDIR_ETC/$f
		fi
	done

}

if [ $# == 0 -o "$1" == "help" ]; then
	help $2
	exit 0
elif [ "$(type -t $1)" = "function" ]; then
	$*
	exit 0
else
	echo "ERROR: '$1' is not a valid template command"
fi
