#!/bin/bash

set -o errexit

set -o pipefail

if [ $# -lt 2 ]
then
    echo "Usage: $0 <env> <image> <ft_image>(optional)"
    echo
    echo "Available env: dev, staging, prod"
    echo "Available images: the global server image name available in ecr"
    echo "Available ft service images: the ft service image name available in ecr"
    exit 1
fi

ENV=$1
IMAGE=$2
FT_SVC_IMAGE=${3:-""}

echo "start deploying global in ${ENV} using ${IMAGE}"
echo "using ft service image: ${FT_SVC_IMAGE}"
echo "deploy global server manifests start"
for file in "./manifests/${ENV}"/*.yaml; do
    if [ -f "$file" ]; then
        kubectl apply -f "$file"
    fi
done
kubectl create configmap observability --from-file=./manifests/${ENV}/meta.json -n observability --dry-run=client -o yaml | kubectl apply -f -
echo "deploy manifests end"

echo "deploy Deployment start"
cp ./globalserver_${ENV}_template.yaml ./globalserver.yaml
sed -i "s|{image}|${IMAGE}|g" ./globalserver.yaml
kubectl apply -f ./globalserver.yaml
echo "deploy Deployment end"

echo "=======  deploy metrics start ============"
echo "deploy central rbac start"
kubectl apply -f ./metrics/${ENV}/central-rbac.yaml
echo "deploy central rbac end"

echo "deploy central configmap start"
kubectl apply -f ./metrics/${ENV}/central-conf.yaml
echo "deploy central configmap end"

echo "deploy oneconfig eventbus configmap start"
kubectl apply -f ./metrics/${ENV}/oneconfig-eventbus.yaml
echo "deploy oneconfig eventbus configmap end"

echo "deploy external rpc svc start"
kubectl apply -f ./metrics/${ENV}/external-rpc-svc.yaml
echo "deploy external rpc svc end"

echo "deploy metrics db migration worker start"
kubectl apply -f ./metrics/${ENV}/metrics-db-migration-worker.yaml
echo "deploy metrics db migration worker end"

echo "deploy metrics api start"
kubectl apply -f ./metrics/${ENV}/metrics-api.yaml
kubectl apply -f ./metrics/${ENV}/metrics-api-http.yaml
kubectl apply -f ./metrics/${ENV}/metrics-api-metrics.yaml
kubectl apply -f ./metrics/${ENV}/metrics-api-monitoring.yaml
echo "deploy metrics api end"

echo "deploy metrics server start"
kubectl apply -f ./metrics/${ENV}/metrics-server.yaml
kubectl apply -f ./metrics/${ENV}/metrics-server-http.yaml
kubectl apply -f ./metrics/${ENV}/metrics-server-monitoring.yaml
echo "deploy metrics server end"

echo "deploy metrics pipeline start"
kubectl apply -f ./metrics/${ENV}/metrics-pipeline-conf.yaml
kubectl apply -f ./metrics/${ENV}/metrics-pipeline.yaml
kubectl apply -f ./metrics/${ENV}/metrics-pipeline-http.yaml
kubectl apply -f ./metrics/${ENV}/metrics-pipeline-monitoring.yaml
echo "deploy metrics pipeline end"
echo "=======  deploy metrics end ============"

ROOT=$(cd $(dirname ${BASH_SOURCE[0]}) && pwd)
CHART_PATH="${ROOT}/../charts"

if [[ -f "./kong/values-${ENV}.yaml" ]]; then
    echo "install kong helm chart start"
    helm upgrade -i kong --create-namespace --namespace kong $CHART_PATH/kong -f ./kong/values-${ENV}.yaml
    echo "install kong helm chart end"
fi
# only needed for prod
if [[ "${ENV}" == "prod" ]]; then
    echo "install karma helm chart start"
    helm upgrade -i karma --namespace observability $CHART_PATH/karma -f ./karma/values-${ENV}.yaml
    echo "install karma helm chart end"

    echo "install promxy helm chart start"
    helm upgrade -i promxy --namespace observability $CHART_PATH/promxy -f ./promxy/values-${ENV}.yaml
    echo "install promxy helm chart end"
fi
