#!/bin/ksh
# DCloud SVN post-commit hook
# Author: Perette Barella
# $Id: dcloud-post-commit 52 2019-08-30 13:28:35Z perette $
# Copyright 2008 - 2018 Perette Barella

### User-configurable variables ###
# To avoid clobbering by updates, override these by setting them
# in ~/.dcloudrc.

# Location of a Cloud workspace
cloud="${DCLOUD:-$HOME/Documents/Cloud}"

# Location of special documents within Cloud workspace
queue="${cloud}/queue.txt"
tasklist="${cloud}/tasks.txt"

# Location of website
publish="${DCLOUD_PUBLISH:-$HOME/Sites/Cloud}"

# Place to put documents within website
documents="$publish/Documents"

# Sort order for documents: type, name
document_sort_order=type

# Calendar category configuration. Lists are space-separated values
calendar_default_category="${DCLOUD_CALENDAR_DEFAULT_CATEGORY:-personal}"
calendar_completed_category="${DCLOUD_CALENDAR_COMPLETED_CATEGORY:-complete}"
calendar_published_categories="${DCLOUD_CALENDAR_PUBLISHED_CATEGORIES-Work}"
calendar_unpublished_categories="${DCLOUD_CALENDAR_UNPUBLISHED_CATEGORIES-Holiday}"

# Additional ICS files/URLs, and event categories, to merge for HTML calendar
additional_ics_calendars="${DCLOUD_CALENDAR_ADDITIONAL_ICS_CALENDARS}"
additional_ics_categories="${DCLOUD_CALENDAR_ADDITIONAL_ICS_CATEGORIES}"

# Format of category filenames: lowercase, uppercase, mixedcase
calendar_category_filenames="${DCLOUD_CALENDAR_CATEGORY_FILENAMES:-mixedcase}"

# Duration of calendar is in months
duration_of_calendar="${DCLOUD_CALENDAR_DURATION:-12}"
# Default event duration is in hours
default_event_duration="${DCLOUD_EVENT_DURATION:-2}"

### End of user-configurable stuff ###

# Allow user customization of settings that need to be retained on update
[ -f ~/.dcloudrc ] && . ~/.dcloudrc

arg0=$(basename $0)
repo_path="$1"
revision="$2"

# Do validation
case "$calendar_category_filenames" in
	mixedcase)
		typeset categoryfile ;;
	lowercase)
		typeset -l categoryfile ;;
	uppercase)
		typeset -u categoryfile ;;
	*)
		print "$arg0: calendar_category_filenames setting '$calendar_category_filenames' is invalid." 1>&2
		exit 1
esac

case "$document_sort_order" in
	type|name)
		: ;;
	*)
		print "$arg0: document_sort_order setting '$document_sort_order' is invalid." 1>&2
		exit 1
esac

# Do the work
cd "$publish" || exit 1
if [[ "$repo_path" == "--hg" ]]
then
	(cd "$cloud" && hg pull && hg update) || exit 1
elif [[ "$repo_path" == "--local" ]]
then
	if [ ! -d "$cloud" ]
	then
		print "$cloud: Does not exist or is not a directory."
	fi
elif [[ -d "$repo_path" ]]
then
	# Enable tracing; if a problem happens, SVN will helpfully report it.
	set -x
	svn update "$cloud" || exit 1
else
	print -- "$arg0: Invokation unrecognized: $*" 1>&2
	exit 1
fi

status=0



# Publish documents
publish_cloud.py -s $document_sort_order -i m4 "$cloud" "$documents" || status=$?


# Extract to-do items into an HTML bulleted list
if [[ -f "$tasklist" ]]
then
	tasks="$(tasks.py -f html "$tasklist" | tail -n +2)" || status=$?
	if [[ -z "$tasks" ]]
	then
		print "<ul class='todolist'><li>To-do list is empty.</li></ul>"
	else
		print "<ul class='todolist'>$tasks"
	fi
else
	print "<p><small>To-do list is not in use.</small></p>"
fi > "$publish/tasks.m4"



# Publish calendars
all_categories="$(print -- $calendar_published_categories $calendar_unpublished_categories)"
options="-L http://maps.google.com?q="
[ -n "$calendar_completed_category" ] &&
	options="$options -f $calendar_completed_category"

if [ ! -f "$queue" ]
then
	print "$queue: Not found.  Generating empty calendars." 1>&2
	queue=/dev/null
fi

# Create .ics with just the default category
categoryfile="${calendar_default_category}.ics"
queue2ics.py $options -c "$calendar_default_category" -X "${all_categories// /\|}" -s "$queue" -o "$categoryfile" || status=1

# Create .ics for each of the published categories
for category in $calendar_published_categories
do
	categoryfile="$category.ics"
	queue2ics.py $options -I $category -s "$queue" -o "$categoryfile" || status=1
done

# Create amalgamated .ics calendar
queue2ics.py $options -c "$calendar_default_category" -C "$all_categories" -s "$queue" -o "calendar.ics" -d $default_event_duration

# Convert .ics to .html calendar, adding any supplementary .ics files.
addl_ics_options="-L http://maps.google.com?q="
for uri in $additional_ics_calendars
do
	addl_ics_options="${addl_ics_options} -s $uri"
done
queue2ics.py -s "calendar.ics" $addl_ics_options -o "calendar.m4" -l month -u '&ndash;' -t -r $duration_of_calendar



# Invoke make in publications directory to assemble
# extracted documents into a completed web page.
actual_categories=""
grep '^CATEGORIES:' calendar.ics |
	sed -e 's/^CATEGORIES://' |
	tr $',' $'\n' |
	tr -d $'\r' |
	sort -u |
while read category
do
	[[ "$category" != "${calendar_completed_category}" ]] &&
		actual_categories="${actual_categories}${actual_categories:+ }${category}"
done
actual_categories="${actual_categories}${additional_ics_categories:+ }${additional_ics_categories//{2,}([ 	])/ }"
print "categories=$actual_categories"
make CATEGORIES="${actual_categories// /,}" || status=$?

# If all went well, publish the results
(( status == 0 )) && make publish

exit $status

