#!/bin/bash

# POST-COMMIT HOOK
#
# The post-commit hook is invoked after a commit.  Subversion runs
# this hook by invoking a program (script, executable, binary, etc.)
# named 'post-commit' (for which this file is a template) with the 
# following ordered arguments:
#
#   [1] REPOS-PATH   (the path to this repository)
#   [2] REV          (the number of the revision just committed)
#
# The default working directory for the invocation is undefined, so
# the program should set one explicitly if it cares.
#
# Because the commit has already completed and cannot be undone,
# the exit code of the hook program is ignored.  The hook program
# can use the 'svnlook' utility to help it examine the
# newly-committed tree.
#
# On a Unix system, the normal procedure is to have 'post-commit'
# invoke other programs to do the real work, though it may do the
# work itself too.
#
# Note that 'post-commit' 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
# 'post-commit.bat' or 'post-commit.exe',
# but the basic idea is the same.
# 
# The hook program typically does not inherit the environment of
# its parent process.  For example, a common problem is for the
# PATH environment variable to not be set to its usual value, so
# that subprograms fail to launch unless invoked via absolute path.
# If you're having unexpected problems with a hook program, the
# culprit may be unusual (or missing) environment variables.
# 
# Here is an example hook script, for a Unix /bin/bash interpreter.
# For more examples and pre-written hooks, see those in
# the Subversion repository at
# http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/ and
# http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/


REPOS="$1"
REV="$2"
TOOLS_DIR=$REPOS/hooks/scripts

source $REPOS/hooks/parse_ini.sh
CONFIG=$REPOS/conf/hooks.ini
readconf post_commit
readconf email 
readconf trac

## Sync with downstream mirror sites using svnsync
#SVNSYNCCMD=$REPOS/hooks/svnsync.commit
#if [ -x "$SVNSYNCCMD" ]; then
#  $SVNSYNCCMD
#fi

if [ "$email_notify_enable" = "yes" ]; then
    #$TOOLS_DIR/log-commit.py --repository "$REPOS" --revision "$REV"
    #$TOOLS_DIR/commit-email.pl "$REPOS" "$REV" -m "." --diff y --from noreply@foo.bar -r noreply@foo.bar -s "[Prefix]" list@foo.bar
    mailcmd="$TOOLS_DIR/commit-email.pl \"$REPOS\" \"$REV\" $email_notify_config"
    eval $mailcmd
fi

############################################################
# Trac post commit hook
TRAC_POST_COMMIT_HOOK=/usr/share/doc/trac/contrib/trac-post-commit-hook
if [ "$trac_post_commit_enabled" = "yes" ]; then
    if [ ! -z "$trac_env" ]; then
        traccmd="$TRAC_POST_COMMIT_HOOK -p $trac_env -r $REV"
        if [ ! -z "$trac_repos_name" ]; then
            traccmd="$traccmd -R $trac_repos_name"
        fi
        if [ ! -z "$trac_fixed_status" ]; then
            traccmd="$traccmd --status $trac_fixed_status"
        fi
        eval $traccmd
    fi 
fi

############################################################
# Mantisbt integration
if [ "$mantis_integration" = "yes" ]; then
    SVNLOOK=/usr/bin/svnlook
    MANTISBT=/opt/mantis/web
    PHP=/usr/bin/php5

    if [ -f "$MANTISBT/core/checkin.php" ]; then
      export LC_ALL=zh_CN.utf8
      commitlog=`$SVNLOOK  log  -r "$REV" "$REPOS"`
      commitauthor=`$SVNLOOK  author  -r "$REV" "$REPOS"`
      commitdiff=`$SVNLOOK diff -r "$REV" "$REPOS" | head -25`
      $PHP -q $MANTISBT/core/checkin.php $commitauthor << EOF
Author: $commitauthor
Commit Log:
$commitlog

****** Source code change ******
Repository: $REPOS, Revision: $REV.

$commitdiff
EOF
    fi
fi

