#!/bin/bash
#######################################################################################################
# Compile and Publish Pelican website
#######################################################################################################
GIT="/home/git/deploy/your_repo_name/.git/" # Location of .git file in the source clone like /foo/bar/.git
GITWT="/home/git/deploy/your_repo_name/" # Location of the working tree like /foo/bar
pelicanRepo="/home/git/deploy/your_repo_name/" # Location of the pelican's content directory
pelicanBranch="master" # Name of the git branch that trigger the publication

while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "$branch" = "${pelicanBranch}"  ]; then
        echo "Let's go publish Pelican!"
        echo "##########################################################################"
        echo "First step: update content through git"
        ## Get the content with git
        cd ${pelicanRepo}
        git --git-dir=$GIT --work-tree=$GITWT checkout ${pelicanBranch}
        git --git-dir=$GIT --work-tree=$GITWT fetch --all
        git --git-dir=$GIT --work-tree=$GITWT reset --hard origin/${pelicanBranch}
        git --git-dir=$GIT --work-tree=$GITWT clean -f -d
        echo "##########################################################################"
        echo "Second step: publish Pelican content"
        echo "we use make rsync_upload so be careful to have configure your Makefile accordingly"
        ## Compile the content
        make rsync_upload
        echo "##########################################################################"
        echo "done"
    fi
done

exit 0
