#!/bin/bash

# this file must be placed manually at '.git/hooks/post-receive'

TARGET="/home/ubuntu/joshstockin-deploy"
GIT_DIR="/home/ubuntu/joshstockin.git"
BRANCH="master"

while read oldrev newrev ref
do
    if [ "$ref" = "refs/heads/$BRANCH" ];
    then
        echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
        mkdir $TARGET
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
        echo "Running deploy script"
        sudo $TARGET/deploy prod
        echo "Removing deploy directory"
        sudo rm -rf $TARGET
    else
        echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
done