#!/bin/bash


TYPE="$1"
SVNID="$2"
REPOS="$3"
TXN="$4"


TMPDIR="/tmp/svn.$SVNID"
TMPTREE="$TMPDIR/files"

SVNLOOK="/usr/bin/svnlook"

case $TYPE in
   pre-commit)
      if [ -z $TXN ]; then
         echo "E: pre-commit requires a TXN" >&2
         exit 1
      fi
      echo "I: Creating $TMPTREE" >&2
      mkdir -p "$TMPTREE"
      cd "$TMPTREE"

      # Dump all files in $TMPTREE
      $SVNLOOK changed -t "$TXN" "$REPOS" | while read s f; do
         [[ "x$s" = "xD" ]] && continue
         [[ "x${f%/}" = "x$f" ]] || continue
         dirname=$(dirname "$f")
         mkdir -p "$dirname"
         $SVNLOOK cat -t "$TXN" "$REPOS" "$f" > "$f"
      done

      /usr/bin/augeas-validator -re *
      [[ $? = 1 ]] && exit 1
      ;;
   post-commit)
      if [ -d "$TMPDIR" ]; then
         cd "$TMPTREE"
         /usr/bin/augeas-validator -re * || exit 1
         rm -rf "$TMPDIR"
      else
         echo "E: Could not find $TMPDIR, cannot run tests." >&2
         exit 1
      fi
      ;;
   *)
      echo "E: Unknown hook type $TYPE." >&2
      exit 1
      ;;
esac

# All checks passed, so allow the commit.
exit 0


