#!/bin/bash
#
# Return a string which will be helpful in identifying the config version used by Puppet
# for any given run.
#
# We return--
#  * the hash of the current commit, or
#  * an arbitrary (but ever-changing) value, if we can't get the hash
#
# This script is referenced by the config_version directive in environment.conf, and 
# called with its cwd inside the Puppet environment being used.  The directive should
# look like
#   config_version=/data/mysociety/bin/puppet-get-config-version $environment
#

GIT=/usr/bin/git


environment=${1:-YOU-MUST-CALL-THIS-SCRIPT-WITH-THE-ENVIRONMENT-NAME}
cd /etc/puppetlabs/code/environments/$environment

ref=$($GIT rev-parse HEAD 2>/dev/null)
if [ $? -eq 0 ]; then
    echo "$environment ($ref)"
else
    now=$(date +%s)
    echo "WARNING: no git commit hash available (time=$now)"
fi
