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

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

SERVICE="central"

ALL_COMPONENTS=("server")

function usage() {
    cat<<EOF
This script use to build some specific compoents 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 central -p <project> -r <region>

     ## build a single component
     $0 -c central -p <projectst
     > -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

cert_manager_role="cert-manager"
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

billing2_image=""
if [[ ${components} == "all" ]]
then
    common::info "ensure billing2 image"
    cd ${DBAAS_ROOT}
    make "docker-deploy-billing2"
    cd "${BUILD_ROOT}"
    local_image="dbaas-billing2:local"
    ecr_repo_name="dbaas/${PROJECT}/dbaas-billing2"
    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
    billing2_image=${ecr_registry}/${ecr_repo_name}:${IMAGE_TAG}
    docker tag ${local_image} ${billing2_image}
    docker push ${billing2_image}
    common::info "Successfully pushed to ${billing2_image}"
else
    common::info "Skipping billing2"
fi

metrics_server_image=""
if [[ ${components} == "all" ]]
then
    common::info "ensure metrics server image"
    cd ${DBAAS_ROOT}
    make "docker-deploy-metrics-server"
    cd "${BUILD_ROOT}"
    local_image="dbaas-metrics-server:local"
    ecr_repo_name="dbaas/${PROJECT}/dbaas-metrics-server"
    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
    metrics_server_image=${ecr_registry}/${ecr_repo_name}:${IMAGE_TAG}
    docker tag ${local_image} ${metrics_server_image}
    docker push ${metrics_server_image}
    common::info "Successfully pushed to ${metrics_server_image}"
else
    common::info "Skipping metrics"
fi

metrics_pipeline_image=""
if [[ ${components} == "all" ]]
then
    common::info "ensure metrics pipeline image"
    cd ${DBAAS_ROOT}
    make "docker-deploy-metrics-pipeline"
    cd "${BUILD_ROOT}"
    local_image="dbaas-metrics-pipeline:local"
    ecr_repo_name="dbaas/${PROJECT}/dbaas-metrics-pipeline"
    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
    metrics_pipeline_image=${ecr_registry}/${ecr_repo_name}:${IMAGE_TAG}
    docker tag ${local_image} ${metrics_pipeline_image}
    docker push ${metrics_pipeline_image}
    common::info "Successfully pushed to ${metrics_pipeline_image}"
else
    common::info "Skipping metrics pipeline"
fi

management_portal_server_image=""
if [[ ${components} == "all" ]]
then
    common::info "ensure management portal server image"
    cd ${DBAAS_ROOT}
    make "docker-deploy-management-portal-server"
    cd "${BUILD_ROOT}"
    local_image="management-portal-server:local"
    ecr_repo_name="dbaas/${PROJECT}/management-portal-server"
    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
    management_portal_server_image=${ecr_registry}/${ecr_repo_name}:${IMAGE_TAG}
    docker tag ${local_image} ${management_portal_server_image}
    docker push ${management_portal_server_image}
    common::info "Successfully pushed to ${management_portal_server_image}"
else
    common::info "Skipping management portal"
fi

internal_portal_server_image=""
if [[ ${components} == "all" ]]
then
    common::info "ensure internal portal server image"
    cd ${DBAAS_ROOT}
    make "docker-deploy-internal-portal-server"
    cd "${BUILD_ROOT}"
    local_image="internal-portal-server:local"
    ecr_repo_name="dbaas/${PROJECT}/internal-portal-server"
    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
    internal_portal_server_image=${ecr_registry}/${ecr_repo_name}:${IMAGE_TAG}
    docker tag ${local_image} ${internal_portal_server_image}
    docker push ${internal_portal_server_image}
    common::info "Successfully pushed to ${internal_portal_server_image}"
else
    common::info "Skipping internal portal"
fi


core_server_image=""
if [[ ${components} == "all" ]]
then
    common::info "ensure core server image"
    cd ${DBAAS_ROOT}
    make "docker-deploy-core-server"
    cd "${BUILD_ROOT}"
    local_image="dbaas-core-server:local"
    ecr_repo_name="dbaas/${PROJECT}/dbaas-core-server"
    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
    core_server_image=${ecr_registry}/${ecr_repo_name}:${IMAGE_TAG}
    docker tag ${local_image} ${core_server_image}
    docker push ${core_server_image}
    common::info "Successfully pushed to ${core_server_image}"
else
    common::info "Skipping core server"
fi

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-${component}"
    ## change work dir to $DEPLOY_ROOT
    cd "${BUILD_ROOT}"

    common::info "local deployment: pushing the local docker image for ${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
