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

export AWS_PAGER=""
DBAAS_ROOT=`pwd`
ROOT=${DBAAS_ROOT}
# load shell functions
source "${DBAAS_ROOT}/deployments/scripts/lib.sh"

SERVICE="metrics"

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

function usage() {
    cat<<EOF
This script use to build some specific components and push to remote ECR.

Options:
       -h,--help               prints the usage message
       -c,--components         specify the components [${ALL_COMPONENTS[@]}] that need to be built. we can use 'all' to build all components, can also specify multiple components with a comma.
       -p,--project            specify the project name, if not specified, the environment variable \${PROJECT} is used by default.
       -r,--region             specify the aws region, if not specified, the environment variable \${REGION} is used by default.

Usage:
     ## build all components [${ALL_COMPONENTS[@]}]
     $0 -c all -p <project> -r <region>

     ## build multiple components
     $0 -c pipeline -p <project> -r <region>

     ## build a single component
     $0 -c pipeline -p <project> -r <region>
EOF
}

while [[ $# -gt 0 ]]
do
    key="$1"
    case "$key" in
        -c|--components)
            components="$2"
            shift
            shift
            ;;
        -p|--project)
            project="${2}"
            shift
            shift
            ;;
        -r|--region)
            region="${2}"
            shift
            shift
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            common::fatal "unknown option: ${key}"
            ;;
    esac
done

project=${project:-$PROJECT}
region=${region:-$REGION}
export AWS_REGION=$region
SKIP_PROXY=${SKIP_PROXY:-""}


if [[ -z ${components:-} || -z ${project:-} || -z ${region} ]]
then
    usage
    common::fatal "Error: expected a components、project and region as argument"
fi


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

account_id=$(aws sts get-caller-identity --query Account --output text)
ecr_registry="${account_id}.dkr.ecr.${region}.amazonaws.com"

common::info "Target ECR: $ecr_registry"

aws ecr get-login-password --region ${region} | docker login --username AWS --password-stdin ${ecr_registry}
trap "docker logout https://${ecr_registry}" EXIT

for component in "${arr_components[@]}"
do
    if ! common::in_array ${component} "${ALL_COMPONENTS[@]}"
    then
        usage
        common::fatal "Unknown component ${component}"
    fi

    ## change work dir to $ROOT
    cd ${DBAAS_ROOT}
    make "docker-deploy-${SERVICE}-${component}"
    
    common::info "local deployment: pushing the local docker image for ${SERVICE}-${component}"
    local_image="dbaas-${SERVICE}-${component}:local"
    ecr_repo_name="dbaas/${PROJECT}/dbaas-${SERVICE}-${component}"

    if ! aws ecr describe-repositories --repository-name ${ecr_repo_name} >/dev/null 2>&1
    then
        common::info "create ecr repo: ${ecr_repo_name}"
        aws ecr create-repository --repository-name ${ecr_repo_name}
    fi
    remote_image=${ecr_registry}/${ecr_repo_name}:${IMAGE_TAG}
    docker tag ${local_image} ${remote_image}
    docker push ${remote_image}
    common::info "Successfully pushed to ${remote_image}\nRun command below to list the images:\naws ecr describe-images --repository-name ${ecr_repo_name}"
done
