#!/bin/bash

# .git/hooks/pre-receive

SCRIPTPATH=$(dirname $(realpath -s $0))

while read oldrev newrev ref
do
	echo "Ref $ref received. Checking for post-receive hook modification..."
	if [[ $(! git diff --name-only $oldrev $newrev | grep post-receive) ]]; then
		echo "Updated post-receive hook detected. Installing..."
		mkdir -p /tmp/newrev
		git archive $newrev | tar -x -C /tmp/newrev
		mv /tmp/newrev/post-receive $SCRIPTPATH/post-receive
		rm -rf /tmp/newrev
	fi
done