#!/usr/bin/env bash

# cfn-submit-helper.sh
#
#
# This tool works by running `cfn submit` on each resource
# By default it will build and submit every resource found
# in this directory.
# There are some options.
#
# BUILD_ONLY=true|false
# SUBMIT_ONLY=true|false
# LOG_LEVEL=logrus valid string loglevel
#
# Example with DEBUG logging enabled by default for set of resources:
# LOG_LEVEL=debug ./cfn-submit-helper.sh project database-user project-ip-access-list cluster network-peering
#
set -eo
# shellcheck source=/dev/null
. ./cfn-submit-helper.config
env | grep CFN_SUBMIT_

_DRY_RUN=${DRY_RUN:-false}
_BUILD_ONLY=${BUILD_ONLY:-false}
_SUBMIT_ONLY=${SUBMIT_ONLY:-false}

[[ "${_DRY_RUN}" == "true" ]] && echo "*************** DRY_RUN mode enabled **************"

# By default, submit the entire library of active custom resources, but usually
# this is run one resource at a time.
resources="${1:-project}"
echo "$(basename "$0") running for the following resources: ${resources}"

echo "Step 1/2: Building"
if [[ "${_SUBMIT_ONLY}" == "true" ]]; then
	echo "SUBMIT_ONLY, skipping build."
else
	for resource in ${resources}; do
		[[ "${_DRY_RUN}" == "true" ]] && echo "[dry-run] would have run make on:${resource}" && continue
		echo "Working on resource:${resource}"
		cd "${resource}"
		echo "resource: ${resource}"

		if [[ "${CFN_SUBMIT_LOG_LEVEL}" == "debug" ]]; then
			make debug
		else
			make
		fi
		# CFN Autogenerated role doesn't work cof AWS CloudFormation test-type command, so replacing with proper role.
		curl https://mdb-cfn-publish.s3.amazonaws.com/resource-role.yaml >resource-role.yaml
		cd -
	done
fi
if [[ "${_BUILD_ONLY}" == "true" ]]; then
	echo "BUILD_ONLY true, skipping submit to CloudFormation"
	exit 0
fi
echo "Step 2/2: Submit resource type to CloudFormation Private Registry"
for resource in ${resources}; do
	echo "Working on resource:${resource}"
	[[ "${_DRY_RUN}" == "true" ]] && echo "[dry-run] would have run 'cfn submit' on:${resource}" && continue
	cd "${resource}"
	echo "resource: ${resource}"

	echo "Deleting role stack if present"
	roleStack="mongodb-atlas-${resource//-/}-role-stack"
	command="aws cloudformation update-termination-protection --no-enable-termination-protection --stack-name ${roleStack}"
	echo "${command}"
	${command} || true
	command="aws cloudformation delete-stack --stack-name ${roleStack}"
	echo "${command}"
	${command} || true
	command=" aws cloudformation wait stack-delete-complete --stack-name ${roleStack}"
	echo "${command}"
	${command}

	echo "Submitting to CloudFormation with flags: ${CFN_SUBMIT_CFN_FLAGS}"
	# ensure error is thrown if registration to private registry fails
	cfn submit "${CFN_SUBMIT_CFN_FLAGS}"
	cd -
done
