#!/bin/sh
# Gitobox hook: do not edit!
# This file is installed by Gitobox and will be overridden the next time it is
# started

GITOBOX_PASSWORD={{PASSWORD}}
GITOBOX_PORT={{PORT}}
GITOBOX_BRANCH={{BRANCH}}

# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"

# --- Safety check
if [ -z "$GIT_DIR" ]; then
    echo "Don't run this script from the command line." >&2
    echo " (if you want, you could supply GIT_DIR then run" >&2
    echo "  $0 <ref> <oldrev> <newrev>)" >&2
    exit 1
fi

if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
    echo "usage: $0 <ref> <oldrev> <newrev>" >&2
    exit 1
fi

# --- Check types
# if $newrev is 0000...0000, it's a commit to delete a ref.
zero="0000000000000000000000000000000000000000"
if [ "$newrev" = "$zero" ]; then
    newrev_type=delete
else
    newrev_type=$(git cat-file -t $newrev)
fi

# --- Gitobox communication logic
do_sync(){
    (echo "$GITOBOX_PASSWORD"; echo "$1") | \
    nc 127.0.0.1 $GITOBOX_PORT | \
    (while read line; do
        if [ "$line" = "OK" ]; then
            exit 0
        elif [ "$line" = "ERROR" ]; then
            echo "gitobox: fail" 1>&2
            exit 1
        fi
        echo "gitobox: $line" 1>&2
    done)
}

# --- Dispatches according to event type
case "$refname","$newrev_type" in
    refs/heads/$GITOBOX_BRANCH,commit)
        do_sync "$newrev" || exit 1
        ;;
    refs/heads/$GITOBOX_BRANCH,delete)
        echo "*** You can't delete branch 'gitobox'" >&2
        exit 1
        ;;
esac

# --- Finished
exit 0
