#!/usr/bin/env bash

# Copyright 2021 The KCP Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

export DEMO_DIR="$( dirname "${BASH_SOURCE[0]}" )"
source "${DEMO_DIR}"/../.setupEnv

source ${DEMOS_DIR}/demo-magic

TYPE_SPEED=30
#PROMPT_AFTER=1
DEMO_PROMPT="☸️ $ "

function pause() {
  if [[ -n "${NO_WAIT}" ]]; then
    sleep 2
  else
    if [[ -n "${1-}" ]]; then
      sleep "$1"
    else
      wait
    fi
  fi
}

export KUBECONFIG=${KUBECONFIG:-${KCP_DIR}/.kcp/admin.kubeconfig}
if ! kubectl get namespaces &>/dev/null; then
  echo "kcp server not started, run 'bin/kcp start'"
  exit 1
fi

kubectl config use-context admin  &>/dev/null

clear

pe "kubectl apply -f ${KCP_DIR}/config"

kubectl wait crd/workloadclusters.workload.kcp.io --for condition=established &>/dev/null
kubectl wait crd/negotiatedapiresources.apiresource.kcp.io --for condition=established &>/dev/null
kubectl wait crd/apiresourceimports.apiresource.kcp.io --for condition=established &>/dev/null

pe "kubectl api-resources | grep deployments"

pe "kubectl apply -f ${CLUSTERS_DIR}/us-east1.yaml"
pe "kubectl get workloadclusters -o wide"
pe "kubectl get apiresourceimports -o wide"
pe "kubectl get negotiatedapiresources -o wide"
pe "kubectl patch negotiatedapiresources deployments.v1.apps --type=\"merge\" -p '{\"spec\":{\"publish\":true}}'"
pe "kubectl get negotiatedapiresources -o wide"
pe "kubectl get crds -o custom-columns=NAME:.metadata.name | grep deployments"

kubectl wait crd/deployments.apps --for condition=established &>/dev/null

pe "kubectl api-resources | grep deployments"
pe "kubectl get negotiatedapiresources -o wide"
pe "kubectl get crds deployments.apps -o yaml | grep ephemeralContainers"
pe "kubectl get apiresourceimports -o wide"
pe "kubectl apply -f ${CLUSTERS_DIR}/us-west1.yaml"
pe "kubectl get apiresourceimports -o wide"
pe "kubectl get apiresourceimports deployments.us-west1.v1.apps -o=go-template='{{ range .status.conditions }}{{ if eq .type \"Compatible\" }}{{ .message }}{{ end }}{{ end }}'"
echo ""
pe "kubectl patch apiresourceimports deployments.us-west1.v1.apps --type=\"merge\" -p '{\"spec\":{\"schemaUpdateStrategy\":\"UpdatePublished\"}}'"
pe "kubectl get apiresourceimports -o wide"
pe "kubectl get negotiatedapiresource deployments.v1.apps -o yaml | grep ephemeralContainers"
pe "kubectl get crds deployments.apps -o yaml | grep ephemeralContainers"
pe "kubectl delete workloadclusters us-west1"
pe "kubectl get apiresourceimports -o wide"
pe "kubectl get negotiatedapiresource deployments.v1.apps -o yaml | grep ephemeralContainers"
pe "kubectl get crds deployments.apps -o yaml | grep ephemeralContainers"
pe "kubectl delete workloadclusters us-east1"
pe "kubectl get apiresourceimports -o wide"
pe "kubectl get negotiatedapiresources -o wide"
pe "kubectl api-resources | grep deployments"

pause
clear

pe "kubectl apply -f ${KCP_DIR}/contrib/crds/apps/apps_deployments.yaml"
kubectl wait crd/deployments.apps --for condition=established &>/dev/null

pe "kubectl apply -f ${CLUSTERS_DIR}/us-west1.yaml"
pe "kubectl get apiresourceimports -o wide"
pe "kubectl get negotiatedapiresources -o wide"
pe "kubectl patch apiresourceimports deployments.us-west1.v1.apps --type=\"merge\" -p '{\"spec\":{\"schemaUpdateStrategy\":\"UpdatePublished\"}}'"
pe "kubectl get apiresourceimports -o wide"
pe "kubectl get negotiatedapiresources -o wide"
pe "kubectl apply -f ${CLUSTERS_DIR}/us-east1.yaml"
pe "kubectl get apiresourceimports -o wide"
pe "kubectl get negotiatedapiresources -o wide"

pause
