#!/bin/zsh

# PRE-COMMIT HOOK
#
# The pre-commit hook is invoked before a Subversion txn is
# committed.  Subversion runs this hook by invoking a program
# (script, executable, binary, etc.) named `pre-commit' (for which
# this file is a template), with the following ordered arguments:
#
#   [1] REPO-PATH   (the path to this repository)
#   [2] TXN-NAME     (the name of the txn about to be committed)
#
# If the hook program exits with success, the txn is committed; but
# if it exits with failure (non-zero), the txn is aborted and no
# commit takes place.  The hook program can use the `svnlook'
# utility to help it examine the txn.
#
# On a Unix system, the normal procedure is to have `pre-commit'
# invoke other programs to do the real work, though it may do the
# work itself too.
#
# Note that `pre-commit' must be executable by the user(s) who will
# invoke it (typically the user httpd runs as), and that user must
# have filesystem-level permission to access the repository.
#
# On a Windows system, you should name the hook program
# `pre-commit.bat' or `pre-commit.exe',
# but the basic idea is the same.
#
# Here is an example hook script, for a Unix /bin/sh interpreter:

REPO="$1"
TXN="$2"

SVNLOOK=/usr/local/subversion/bin/svnlook

# do everything relative to hooks directory:
cd `dirname $0`


# Get the author, directories containing changed files, etc:
author=`$SVNLOOK author -t $TXN $REPO`
date=`$SVNLOOK date -t $TXN $REPO`
dirs=`$SVNLOOK dirs-changed -t $TXN $REPO`
files=`$SVNLOOK changed -t $TXN $REPO`
log=`$SVNLOOK log -t $TXN $REPO`
version=`$SVNLOOK youngest $REPO`


# no restrictions on what the svnadmin user can do:
if [ "x$author" = "xsvnadmin" ]; then
  # ?? maybe we should sanity check if /tag/<TAG-NAME> to
  # protect against modifying a tag...
  exit 0;
fi

# Make sure that the log message contains some text.
echo "$log" | grep "[a-zA-Z0-9]" > /dev/null || { ${error:?"Must have a log message"}; exit 1; }

# version is actually the current version, not the new version
# that we are trying to commit, so increment by one:
version=$((version+1))

# Build the checkin comment:
comment="";

function append_comment()
{
  comment="$comment$*\n";
}

append_comment "AUTOGENERATED CHECKIN COMMENT:";
append_comment "------------------------------";
append_comment "   Author:  $author";
append_comment "   Date:    $date";
append_comment "   Files:";
for file in ${(s:
:)files}; do
  append_comment "      $file";
done
append_comment "   Version: $version";
append_comment "";
append_comment "COMMIT LOG:";
append_comment "-----------";
append_comment $log;

# if we get this far, defer to java PreCommitHook program to do the rest:
exec java -cp .:xmlrpc-1.1.jar PreCommitHook $author $comment ${(s:
:)dirs}

