#!/bin/ksh

REPOS="$1"
TXN="$2"
REPOS_PATH=$REPOS
TRANSACTION=$TXN

# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
SVN=/usr/bin/svn
AWK=/bin/awk
SED=/bin/sed
GREP=/bin/grep
HEAD=/usr/bin/head
TAIL=/usr/bin/tail
CUT=/bin/cut
ECHO=/bin/echo
TR=/usr/bin/tr
MAIL=/bin/mail
CAT=/bin/cat
MKTEMP=/bin/mktemp

DOC_XLS_FILES=`$MKTEMP`
$SVNLOOK changed --transaction $TRANSACTION $REPOS_PATH | $CUT -c1,5- > $DOC_XLS_FILES

$CAT $DOC_XLS_FILES > /dev/stderr
for line in `$CAT $DOC_XLS_FILES`; do
   status=`$ECHO $line | $CUT -c1`
   file=`$ECHO $line | $CUT -c2-`

         # Check that the file is locked (Only for not new files)
         if [ "$status" != "A" ]; then
            # The quotes around $file are necessary as it can contain spaces which will screw the svnlook command
            LOCK_OWNER=`$SVNLOOK lock $REPOS_PATH "$file" | $GREP '^Owner: ' | $SED 's/Owner: //'`
            if [ "$LOCK_OWNER" = "" ]; then
               $ECHO "" > /dev/stderr
               $ECHO "*****************************************************************" > /dev/stderr
               $ECHO "ERROR: All .doc and .xls files need to be locked to be committed." > /dev/stderr
               $ECHO "       The following file is not:" > /dev/stderr
               $ECHO "       - "$file > /dev/stderr
               $ECHO "*****************************************************************" > /dev/stderr
               exit 1
            fi
         fi

         # Check if svn:needs-lock is set (Only for files that we are not deleting)
         if [ "$status" != "D" ]; then
            # The quotes around $file are necessary as it can contain spaces which will screw the svnlook command
            needslock=`$SVNLOOK proplist --transaction $TRANSACTION $REPOS_PATH "$file" | $GREP svn:needs-lock`
            if [ "$needslock" = "" ]; then
               $ECHO "" > /dev/stderr
               $ECHO "***************************************************************" > /dev/stderr
               $ECHO "ERROR: All .doc. and .xls files need to have the svn:needs-lock" > /dev/stderr
               $ECHO "       property set. At least the following file has not:" > /dev/stderr
               $ECHO "       - "$file > /dev/stderr
               $ECHO "***************************************************************" > /dev/stderr
               exit 1
            fi
         fi

done
