#!/bin/bash

# this is a file checkout – do nothing
if [ "$3" == "0" ]; then exit; fi

BRANCH_NAME=$(git symbolic-ref --short -q HEAD)
NUM_CHECKOUTS=`git reflog --date=local | grep -o ${BRANCH_NAME} | wc -l`

if [ "$BRANCH_NAME" == "devel" ]; then
    git submodule foreach -q --recursive '([ "$name" = "lib/Velis" ] && (echo "Switching lib/Velis..." && git checkout devel)) || echo ""'
fi

if [ "$BRANCH_NAME" == "master" ] || [ "$BRANCH_NAME" == "release" ]; then
    git submodule foreach -q --recursive '([ "$name" = "lib/Velis" ] && (echo "Switching lib/Velis... " && git checkout master)) || echo ""'
fi

if [ "$BRANCH_NAME" == "stage" ]; then
    git submodule foreach -q --recursive '([ "$name" = "lib/Velis" ] && (echo "Switching lib/Velis... " && git checkout stage)) || echo ""'
fi

#if the refs of the previous and new heads are the same 
#AND the number of checkouts equals one, a new branch has been created
if [ "$1" == "$2"  ] && [ ${NUM_CHECKOUTS} -eq 1 ]; then
    echo "${BRANCH_NAME} created"
fi
