#!/bin/bash

# IMPORTANT: this script runs as the post-receive hook on the support server wlan-exp repository
#  If you modify this script you must commit/push twice - the first push will run the old script
#  which update's the server's local working copy. The second push will run the new script

BRANCH_TO_BUILD="support-docs"
PYPATH_DIR="/var/www/wlan-docs/wlan-exp-doc-wd/"
WD_DIR="/var/www/wlan-docs/wlan-exp-doc-wd/wlan_exp/docs" #sphinx dir

export PYTHONPATH=$PYTHONPATH:$PYPATH_DIR
# export BUILDING_DOCS_ON_SERVER=1 # support server has numpy, now required to generate entry type docs

while read oldrev newrev ref
do

  branch_received=`echo $ref | cut -d/ -f3`

  echo "**** Received [$branch_received] branch."

  if [ $branch_received = $BRANCH_TO_BUILD ]; then
    cd $WD_DIR
    # Unset to use current working directory.
    unset GIT_DIR

    echo "**** Pulling changes."
    git fetch --all
    git reset --hard origin/$BRANCH_TO_BUILD

    echo `date` > sphinx-build.log
    echo `git log -n1` >> sphinx-build.log
    echo "***********" >> sphinx-build.log

    /usr/local/bin/sphinx-build -Ea -b html source build/html &>> sphinx-build.log

    cp sphinx-build.log build/html

  else
    echo "**** Invalid branch, aborting."
    exit 0

  fi
done
