#!/usr/bin/env bash
set -o errexit

. script/env.sh

if [[ -z "$1" ]]
then
  echo "You need to set a namespace in order to deploy. Try script/deploy.sh development"
  exit 1
fi

K8S_ENV=$1

if  [[ $K8S_ENV == 'production' ]] || [[ $K8S_ENV == 'tvadmin-prod' ]]
then
  if [ ! -z $BUILDKITE_TAG ] #If is tag, then $BUILDKIT_BRANCH == $BUILDKITE_TAG
  then
    K8S_CLUSTER='production-1.kube.democracy.works'
    export K8S_SHORTNAME="production"
  fi
else
  K8S_CLUSTER='working-1.kube.democracy.works'  
  export K8S_SHORTNAME="working"
fi

echo "Deploying to the ${K8S_ENV} namespace on ${K8S_CLUSTER}"


install-rok8s-requirements
get-remote-kubecfg s3://3b93aea27083da9f97d202edd7520ecc buildkite buildkite-ci
decrypt-kubecfg buildkite-ci
prepare-kubectl

kubectl config get-contexts

k8s-deploy-and-verify -f deploy/${K8S_ENV}.config
