#! /bin/sh
set -e

# add something like this into your post-commit:
# /path/to/svn-auto-checkout "$REPOS" "$REV" "part of repo to be checked out" "where to check out"
#
# Use it to give remote access to hooks - now on par with cvs, yay ! :)
# HOWTO for an apache-served repository:
# 1. install this script as hooks/bin/svn-auto-checkout in your repository
# 2. register the script as a hook in your post-commit script, by adding the following line:
#    ${REPOS}/hooks/bin/svn-auto-checkout "$REPOS" "$REV" _HOOKS ${REPOS}/hooks
# 3. import the hooks into the svn history:
#    cd hooks
#    sudo -u apache svn import . file:///where/the/repo/is/_HOOKS
# 4. replace the hooks dir with a suitable working copy, including correct ownership:
#    cd /where/the/repo/is
#    sudo -u apache svn co file:///where/the/repo/is/_HOOKS
#    mv hooks hooks.bak
#    mv _HOOKS hooks
# 5. that's all.  Checkout http://your/repo/_HOOKS anywhere, and when your commit will
#    automagically be reflected in the hooks/ dir

REPO="$1"
REV="$2"

# file/dir to auto-checkout ...
REPOPATH="$3"
# ... there
DEST="$4"

PATH=/bin:/usr/bin:/usr/local/bin
export PATH

me=`basename $0`

echo >&2 "$me trace: starting for $REPO r$REV"

if [ "x$REPOPATH" = "x" ]; then
    echo >&2 "$me error: no path inside repository specified"
    exit 1
fi
if [ "x$DEST" = "x" ]; then
    echo >&2 "$me error: no destination directory specified"
    exit 1
fi

if [ -d "$DEST" ]; then
    cd "$DEST"

    # assert working copy
    if svn info >/dev/null ; then
	:
    else
	echo >&2 "$me error: $DEST already exists but is not a svn working copy"
	exit 1
    fi

    # assert URL
    #TODO

    # update to $REV
    svn update -r "$REV"
else
    svn checkout -r "$REV" "file://$REPO/$REPOPATH" "$DEST"
fi