#! /bin/bash -e

set -e

# Ensure we have at least enough to check gecko out...
test $GECKO_BASE_REPOSITORY

# Workspace to checkout gecko into...
WORKSPACE=$1
mkdir -p $WORKSPACE

res=`curl --fail --retry 5 http://taskcluster/secrets/v1/secret/project/taskcluster/gecko/hgfingerprint`
FP=`echo $res | jq -r .secret.content`

if [[ ! "$FP" =~ ^[a-f0-9:]+$ ]]; then
    echo
    echo "[taskcluster:error] HG host fingerprint not properly loaded"
    exit 1
fi

mkdir /etc/mercurial
cat >/etc/mercurial/hgrc <<EOF
[hostfingerprints]
hg.mozilla.org = $FP
EOF

# Note that tc-vcs requires only the first two arguments so others are optional.
# This is intended to make it easier to clone local directories.
tc-vcs checkout $WORKSPACE/gecko \
  $GECKO_BASE_REPOSITORY \
  $GECKO_HEAD_REPOSITORY \
  $GECKO_HEAD_REV \
  $GECKO_HEAD_REF
