#! /usr/bin/env bash
set -e

# This file deploys a new AASA file into staging or production. The proper
# bucket, CF distribution, and credentials are passed in through the Jenkins
# job configuration. The job configurations simply include the secret, ID,and
# bucket to which it should be published. **Please note** that headers that
# should be returned on HTTP requests can be configured with the S3 copy
# command.

# Header config
STATIC_CACHE_VALUE='public, max-age=86400'

# Assets
AASA=apple-app-site-association

# Destination Paths
AASA_PREFIX=.well-known

usage() {
	echo "Usage: $0 -s <S3 bucket>" 1>&2
	exit 1
}

while getopts ":s:d:" o; do
	case "${o}" in
		s)
			BUCKET=${OPTARG}
			;;
    d)
      DISTRIBUTION_ID=${OPTARG}
      ;;
		*)
			usage
			;;
	esac
done

if [ -z "${BUCKET}" ]; then
	usage
fi

echo -n "Uploading ${AASA} to ${BUCKET}…"
aws s3 cp "${AASA}" "s3://${BUCKET}/${AASA_PREFIX}/${AASA}" --cache-control "${STATIC_CACHE_VALUE}"
echo "done"
