#!/usr/bin/env bash
set -euo pipefail

root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

ROOT=${root_dir}
# load shell functions
source "${ROOT}/deployments/scripts/lib.sh"

pulumi_dir="${root_dir}/deployments/pulumi"
region=${REGION:-"$(pulumi -C "${pulumi_dir}" config get aws:region)"}
project=${PROJECT:-"$(pulumi -C "${pulumi_dir}" config get project)"}
components=${BUILD_COMPONENTS:-"all"}
IMAGE_TAG=${IMAGE_TAG:-"$(git rev-parse --short HEAD)"}
# Pulumi will only update the deployment if the tag changes.
# So it won't deploy the latest dirty changes we need.
# A simple approach would be to add a timestamp.
# Below is a proper approach that gets a git hash of dirty changes.
# It will work for example when deploying components one-by-one without making changes.
# Ignore tools and deploy directories, the dependency files will be changed during build
if ! git diff --quiet -- ':!deploy' ':!tools' && [ "$project" != "nightly" ] && [ "$project" != "stability" ] && [ "$project" != "staging" ] && [ "$project" != "prod" ]; then
  cp .git/index /tmp/git_index
  export GIT_INDEX_FILE=/tmp/git_index
  git add -u
  IMAGE_TAG="$IMAGE_TAG-$(git write-tree)"
fi

export ALL_COMPONENTS=("api" "server" "pipeline" "db-migration-worker")

echo "Build $components components for project $project in region $region using image tag: ${IMAGE_TAG}"
IMAGE_TAG=${IMAGE_TAG} PROJECT=${project} ${root_dir}/build-component.sh -c "${components}" -r "${region}"

if [[ ${components} == "all" ]]
then
    arr_components=(${ALL_COMPONENTS[@]})
else
    arr_components=(${components//,/ })
fi

function update_image_tag_only() {
  echo "Build succeed, execute the command below to specify pulumi metrics image tags:"
  
  for component in "${arr_components[@]}"
  do
    case "$component" in
        api)
            echo "pulumi -C ${pulumi_dir} config set metrics:apiImageTag ${IMAGE_TAG}"
            ;;
        server)
            echo "pulumi -C ${pulumi_dir} config set metrics:serverImageTag ${IMAGE_TAG}"
            ;;
        pipeline)
            echo "pulumi -C ${pulumi_dir} config set metrics:pipelineImageTag ${IMAGE_TAG}"
            ;;
        db-migration-worker)
            echo "pulumi -C ${pulumi_dir} config set metrics:dbMigrationWorkerImageTag ${IMAGE_TAG}"
            ;;
        *)
            common::fatal "unknown component: ${component}"
            ;;
    esac
  done
}

function update_image_tag_and_deploy() {
  echo "Build succeed, specify pulumi metrics image tags and deploy"

  for component in "${arr_components[@]}"
  do
    case "$component" in
        api)
            pulumi -C "${pulumi_dir}" config set metrics:apiImageTag "${IMAGE_TAG}"
            ;;
        server)
            pulumi -C "${pulumi_dir}" config set metrics:serverImageTag "${IMAGE_TAG}"
            ;;
        pipeline)
            pulumi -C "${pulumi_dir}" config set metrics:pipelineImageTag "${IMAGE_TAG}"
            ;;
        db-migration-worker)
            pulumi -C "${pulumi_dir}" config set metrics:dbMigrationWorkerImageTag "${IMAGE_TAG}"
            ;;
        *)
            common::fatal "unknown component: ${component}"
            ;;
    esac
  done

  pulumi -C "${pulumi_dir}" up --yes
}

UPDATE_PULUMI=${UPDATE_PULUMI:-""}
if [[ -z $UPDATE_PULUMI ]] ; then
  trap update_image_tag_only EXIT
else
  trap update_image_tag_and_deploy EXIT
fi
