#!/bin/sh
# automatically configured by git-annex
if git annex post-receive --help >/dev/null 2>&1; then git annex post-receive; fi

echo "git post-receive hook: starting"

GIT_DIR=.git 
WORKING_REPO=../content

CWD=$(pwd)
PYTHON='<path/to/python-virtual-env>'
WWW_SCRIPT='<path/to/build_image_cache.py script>'
CPU_CORE=<number of CPU cores available, run nproc --all on Linux>

while read oldrev newrev ref
do
    # synchronize git checkout folder
    
    BRANCH=`echo $ref | cut -d/ -f3`
    # avoid error with git-annex `synced` branches
    if ! [ $BRANCH = 'synced' ]; then
	cd $WORKING_REPO \
		&& git pull origin $BRANCH \
		&& git annex sync --content
    fi
done

# rebuild all changed images
echo "rebuild updated images..."
CPU_CORE=$CPU_CORE $PYTHON $WWW_SCRIPT build-all-images $WORKING_REPO

echo "git post-receive hook: ending"
