#!/usr/bin/env bash
# {{ftw.deploy version}}

set -e
oldrev=$1
newrev=$2

# Evaluate enviornment setup scripts
if [ -d ~/etc/env.d ]; then
    for env_script in `ls ~/etc/env.d` ; do
        # When evaulating our standard rbenv.sh, it will exit with code 1
        # and will break push deployment.
        if [[ "$env_script" != "rbenv.sh" ]]; then
            . ~/etc/env.d/$env_script
        fi
    done
fi

run() {
  echo "Running: $1 $oldrev $newrev"
  [ -x $1 ] && $1 $oldrev $newrev
}

echo files changed: $(git diff $oldrev $newrev --diff-filter=ACDMR --name-only | wc -l)

umask 002

git submodule sync && git submodule update --init --recursive

update_plone_script=`(grep -hriE "^deployment-update-plone-script *=" buildout.cfg ||
 echo "deployment-update-plone-script=deploy/update_plone") | sed -e 's/[^=]*= \{0,1\}//'`

run $update_plone_script
