#!/bin/bash
#
#   Synchronise configs to S3 bucket
#
cd $(dirname $0)
. SETENV

# Leave none of these directories empty
date > Scripts/_timestamp
date > Deploy/_timestamp
date > Config/_timestamp
cp SETENV Scripts/SETENV

# Prepare the deploy cloudformation template.
# Our CodePipeline pulled Deploy.zip out of the SecureConfig repo (in CodeCommit)
# using a lambda, and passes the template to CloudFormation to deploy the Docker
# container to the ECS cluster.
(cd Deploy; zip ../Deploy.zip _timestamp *.cf)

# Push everything to the SecureConfig repo in AWS CodeCommit.
git add . && \
git commit -m "$(date "+%H:%M %d%b%Y")" && \
git push --set-upstream origin master

if [ $? != 0 ] ; then
  echo ""
  echo ""
  echo "An error occurred uploading to CodeCommit."
  echo "If you are on a Mac and get a 403 error, your credentials are probably being cached by 'Keychain Access',"
  echo "but AWS resets them every 15 minutes. See a permanent fix here (option 3 works best):"
  echo ""
  echo "    https://jeanlescure.io/blog/solved-aws-codecommit-git-on-osx-fatal-unable-to-access-returned-error-403"
  echo ""
  echo "If you are getting a 404 error, you might be using the wrong AWS account and need to set the AWS_PROFILE"
  echo "environment variable to refer to a profile defined in ~/.aws/credentials:"
  echo ""
  echo "    export AWS_PROFILE=production"
  echo ""
  exit 1
fi


echo ""
echo "Config uploaded."
exit 0
