#!/bin/ksh
# todone ("to done")
# Author: Perette Barella
# Copyright 2018 Devious Fish.  All rights reserved.
VERSION='$Id: todone 41 2018-10-06 12:16:50Z perette $'

arg0=$(basename "$0")

# Validate that ksh supports the modern/extended getopts format.
# Author: Perette Barella 
# Copyright 2018 Devious Fish.  All rights reserved.
# $Id: modern_ksh_check 19 2018-07-28 23:40:39Z perette $  


function modern_ksh_check {
	if [[ $(getopts '[-][12:abc]' flag --abc; print -- 0$flag) != "012" ]]
	then
		print -- "$arg0${arg0+: }Outdated Korn shell." 1>&2
		exit 1
	fi
}

modern_ksh_check

USAGE=$'
[-1?'$VERSION$']
[+NAME?todone - Convert completed to-do items to ChangeLog entries]
[+DESCRIPTION?\b'$arg0$'\b extracts completed to-do items in markdown format
from a tracking document and converts them to a bulleted list preceded by
date and revision information.
Items are inserted before the earliest SVN version identifier in the ChangeLog,
and removed from the original list.
If no identifier is found, items are appended.]
[c:changelog?Specify location of ChangeLog, if not project root.]:[filename]
[p:project?Specifies a project, which must already be selected.]:[project]
[n:no-remove?Leave the completed items in the original document.]
[+EXIT STATUS?0 on success, non-0 on error.]
[+SEE ALSO?\btasks.py\b(1), \bprj\b(1)]

to-do-list

[-author?Perette Barella <perette@deviousfish.com>
'

# Verify there's a project.
if [ "$PROJECT" = "" -o "$WEBBASE" = "" ]
then
	print -- "$arg0: No current project." 1>&2
	exit 1
fi
if [ ! -d "$WEBBASE" ]
then
	print "$WEBBASE: Not a directory."
	exit 1
fi

# Process options
STRIP=true
target="$WEBBASE/ChangeLog"
project_ok=false

while getopts -a "$arg0" "$USAGE" option
do
	case "$option" in
   	    c)	target="$OPTARG" ;;
	    n)	STRIP=false ;;
	    p)  if [[ $PROJECT != $OPTARG ]]
		then
			print "$OPTARG: Wrong project." 1>&2
			exit 1
		fi
		project_ok=true ;;
	    ?)  HELP=true ;;
	esac
done
shift $((OPTIND - 1))

if (( $# != 1 ))
then
	OPTIND=0
	getopts -a "$arg0" "$USAGE" option --short
	exit 1
fi

# Process the parameters
if ! $project_ok
then
	print "$arg0: Must include -p project option." 1>&2
	exit 1
fi

# Verify source is there
source="$1"
if [ ! -a "$source" ]
then
	print -- "$source: Does not exist." 1>&2
	exit 1
fi
if [ ! -f "$source" ]
then
	print -- "$source: Not a file." 1>&2
	exit 1
fi
if $STRIP && [ ! -w "$source" ]
then
	print -- "$source: Cannot update." 1>&2
	exit 1
fi


# Verify ChangeLog is there
if [ ! -a "$target" ]
then
	print -- "$target: Does not exist." 1>&2
	exit 1
fi
if [ ! -f "$target" ]
then
	print -- "$target: Not a file." 1>&2
	exit 1
fi
if [ ! -w "$target" ]
then
	print -- "$target: Not writable." 1>&2
	exit 1
fi
(cd "$WEBBASE" && svn update) || exit $?
revision=$(cd "$WEBBASE" && svn info | grep Revision: | awk '{print $2}')
if [ "$revision" = "" ]
then
	print -- "$arg0: Can't get SVN version." 1>&2
	exit 1
fi



# Extract the tasks
tempfile=/var/tmp/$arg0.$$.temp
completed=/var/tmp/$arg0.$$.completed

tasks.py -c -r -o "$completed" -f markdown $strip "$source"

if [ ! -s "$completed" ]
then
	print "$source: No completed tasks." 1>&2
	rm "$completed"
	exit 0
fi



# Insert the extracted items into the ChangeLog
# after any boilerplate at the top, before other log entries.
integer line_num=0
while IFS="" read line
do
	let line_num++
	if [[ $line == r[0-9]* ]]
	then
		print "r$((revision + 1)) ($(date '+%Y-%m-%d')):"
		sed -e $'s/^[ \t-]*/- /' < "$completed"
		print
		print -- "$line"
		cat
	else
		print -- "$line"
	fi
done < "$target" > "$tempfile"

# If the items weren't inserted, add them at the end.
if cmp -s "$target" "$tempfile"
then
	let line_num+=2
	print
	print "r$((revision + 1)) ($(date '+%Y-%m-%d')):"
	sed -e $'s/^[ \t-]*/- /' < "$completed"
	print
fi  >> "$tempfile"

# Clean up
mv "$tempfile" "$target"
rm -f "$completed" "$tempfile"
$STRIP && tasks.py -c -s "$source"

# If on a terminal, invoke an editor to review/tweak the resulting ChangeLog.
[ -t 0 -a -t 1 -a -t 2 ] &&
	${EDITOR:-vi} +${line_num} "$target"

exit 0

