#!/bin/sh
###
### disallow-tabs REPOSITORY TRANSACTION SUFFIX...
###
################################################################
## See usage message below
################################################################

Repository=''
Transaction=''
Suffixes=''

while [ $# -ne 0 ]
do
  if [ -z "${Repository}" ]
  then
      Repository=${1}
  elif [ -z "${Transaction}" ]
  then
      Transaction=${1}
  elif [ -z "${Suffixes}" ]
  then
      Suffixes=${1}
  else
      Suffixes="${Suffixes}|${1}"
  fi
  shift
done

if [ -z "${Suffixes}" ]
then
    cat <<EOF
usage:
    disallow-tabs <repository> <transaction> <suffix>...

    A subversion pre-commit hook script that checks for tab characters
    in commited files whose name ends in '.' followed by any of the
    specified suffixes (at least one must be given).
EOF
    exit 1;
fi

## temporary file to record all files where prohibited tabs are found
TabsFoundIn=/tmp/disallow-tabs.$$
cat /dev/null > $TabsFoundIn

## for each matching file modified or added in the transaction
svnlook changed ${Repository} --transaction ${Transaction} \
| perl -ne "m/^[AU]. +(.*\\.(${Suffixes}))\$/ && print \"\$1\\n\"" \
| while read sourcefile
do
    ## scan for tab characters
    if ! svnlook cat ${Repository} ${sourcefile} --transaction ${Transaction} \
         | perl -ne 'm/\t/ && exit 1'
    then
        ## record the offending file
        echo "    $sourcefile" >> $TabsFoundIn
    fi
done

if [ -s $TabsFoundIn ] # any files were found to have tabs
then
    ## tell the commiter what files are the problem
    echo "Tab characters found in:" \
    | cat - $TabsFoundIn 1>&2
    ## disallow the commit
    TabsFound=1
else
    ## no tabs found - allow the commit
    TabsFound=0
fi

## clean up 
rm -f $TabsFoundIn

exit $TabsFound

    


