#!/bin/bash
# This script prepares a Let's Encrypt certificate before all-in-one Teleport starts for the first time (if needed)
set -e
if [[ "${DEBUG:-false}" == "true" ]]; then
    set -x
fi

# Source variables from user-data (if present)
if [ -f /etc/teleport.d/conf ]; then
    source /etc/teleport.d/conf
fi

# check for Let's Encrypt
if [[ "${USE_LETSENCRYPT}" != "true" ]]; then
    echo "Not using Let's Encrypt, exiting with success"
    exit 0
fi

# copy certificates into place
/bin/aws s3 sync --exact-timestamps s3://${TELEPORT_S3_BUCKET}/live/${TELEPORT_DOMAIN_NAME} /var/lib/teleport
