#!/bin/sh

# Change a commit message whithin 3 hours after the commit
# svn propset -r <your rev> --revprop svn:log "new log message" https://svn.my.domain.com/REPOSITORY
#
# PRE-REVPROP-CHANGE HOOK
#
# The pre-revprop-change hook is invoked before a revision property
# is modified.  Subversion runs this hook by invoking a program
# (script, executable, binary, etc.) named 'pre-revprop-change' (for which
# this file is a template), with the following ordered arguments:
#
#   [1] REPOS-PATH   (the path to this repository)
#   [2] REVISION     (the revision being tweaked)
#   [3] USER         (the username of the person tweaking the property)
#   [4] PROPNAME     (the property being set on the revision)
#
#   [STDIN] PROPVAL  ** the property value is passed via STDIN.
#
# If the hook program exits with success, the propchange happens; but
# if it exits with failure (non-zero), the propchange doesn't happen.
# The hook program can use the 'svnlook' utility to examine the 
# existing value of the revision property.
#
# WARNING: unlike other hooks, this hook MUST exist for revision
# properties to be changed.  If the hook does not exist, Subversion 
# will behave as if the hook were present, but failed.  The reason
# for this is that revision properties are UNVERSIONED, meaning that
# a successful propchange is destructive;  the old value is gone
# forever.  We recommend the hook back up the old value somewhere.
#
# On a Unix system, the normal procedure is to have 'pre-revprop-change'
# invoke other programs to do the real work, though it may do the
# work itself too.
#
# Note that 'pre-revprop-change' 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-revprop-change.bat' or 'pre-revprop-change.exe',
# but the basic idea is the same.
#
# Here is an example hook script, for a Unix /bin/sh interpreter:

REPOS="$1"
REV="$2"
USER="$3"
PROPNAME="$4"
ACTION="$5"

#This is the default
#if [ "$PROPNAME" = "svn:log" ]; then exit 0; fi
#exit 1

#
# Custom prop change hook.
#
date2stamp () {
    date --date "$1" +%s
}

stamp2date (){
    date --utc --date "1970-01-01 $1 sec" "+%Y-%m-%d %T"
}

dateDiff (){
    case $1 in
        -s)   sec=1;      shift;;
        -m)   sec=60;     shift;;
        -h)   sec=3600;   shift;;
        -d)   sec=86400;  shift;;
        *)    sec=86400;;
    esac
    dte1=$(date2stamp "$1")
    dte2=$(date2stamp "$2")
    diffSec=$((dte2-dte1))
    if ((diffSec < 0)); then abs=-1; else abs=1; fi
    echo $((diffSec/sec*abs))
}

DATESTAMP=$( /usr/bin/svnlook date -r $REV $REPOS)
AUTHOR=$( /usr/bin/svnlook author -r $REV $REPOS)
HOURS=$(dateDiff -h "$DATESTAMP" "now")

# Administrators can change anyone's svn:log.
if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" -a "$GROUP" = "admin" ]; then exit 0; fi

# Allow users to change own svn:log for 3 hours.
if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" -a "$USER" = "$AUTHOR" -a "$HOURS" -lt "4" ]; then exit 0; fi

# Error message for svn:log changes that do not meet above criteria.
if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]
then
  echo "Changing svn:log can only be performed by the original submitter 3 hours from commit." >&2
  echo "Please contact the administrator for further assistance." >&2
  exit 1
fi

# Default diallow change. exit 1 fails the prop change
echo "Changing revision properties other than svn:log is prohibited." >&2
exit 1
