#!/bin/bash

# this file is automatically updated, although the pre-receive hook must be placed manually:
# 'pre-receive' -> '.git/hooks/pre-receive'

TARGET="/home/ubuntu/joshstock.in/deploy"
GIT_DIR="/home/ubuntu/joshstock.in/project.git"
BRANCH="master"
HTML_OUT="/var/www/html"

while read oldrev newrev ref
do
    if [ "$ref" = "refs/heads/$BRANCH" ];
    then
        echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
        echo "Installing required Python modules"
        sudo python3 -m pip install -r $TARGET/requirements.txt
        echo "Compiling root HTML"
        sudo $TARGET/root/compile.py $HTML_OUT
        echo "Installing nginx configuration"
        sudo $TARGET/nginx/copynginx prod
        echo "Installing resty-gitweb"
		sudo install $TARGET/resty-gitweb.yaml /etc/resty-gitweb.yaml
    else
        echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
done