#!/usr/bin/env bash

set -e

declare -A versions
versions["5.0.0"]="v49"
versions["5.0.1"]="v50"
versions["5.0.2"]="v50.2"

versions["5.1.0"]="v51"
versions["5.1.1"]="v52"
versions["5.1.2"]="v53"
versions["5.1.3"]="v54"
versions["5.1.4"]="v54.1"

versions["5.2.0"]="v55"
versions["5.2.1"]="v56"
versions["5.2.2"]="v56.2"

versions["5.3.0"]="v57.1"
versions["5.3.1"]="v58"
versions["5.3.2"]="v58.1"

versions["5.4.0"]="v59"
versions["5.4.1"]="v59.2"

versions["5.5.0"]="v60.1"
versions["5.5.1"]="v61"
versions["5.5.2"]="v62"
versions["5.5.3"]="v62.1"
versions["5.5.4"]="v62.2"

versions["5.6.0"]="v63"
versions["5.6.1"]="v63.1"
versions["5.6.2"]="v63.2"

versions["5.7.0"]="v64"
versions["5.7.1"]="v64.1"
versions["5.7.2"]="v64.2"
versions["5.7.3"]="v64.3"
versions["5.7.4"]="v64.4"

versions["5.8.0"]="v65"

versions["5.9.0"]="v66"
versions["5.9.1"]="v66.1"
versions["5.9.2"]="v66.2"
versions["5.9.3"]="v66.3"
versions["5.10.0"]="5.10.0.catalog-0cbbe52"
versions["5.10.1"]="5.10.1.catalog-b1ce370"
versions["5.11.0"]="5.11.0.catalog-454f53d"
versions["5.11.1"]="5.11.1.catalog-55ee23e"
versions["5.11.2"]="5.11.2.catalog-2c777f5"
versions["5.11.3"]="5.11.3.catalog-8cb5e60"
versions["5.11.4"]="5.11.4.catalog-03af877"

if [ -z "$FLEET_COMMAND_TAG" ] && [ $# -ne 1 ]; then
  echo "A Domino release or fleetcommand-agent tag is required, for example 5.6.2 (release) or v49 (tag)."
  exit 1
fi

release_or_tag=${1:-$FLEET_COMMAND_TAG}
if [[ $release_or_tag == v* ]]; then
  tag=$release_or_tag
  # shellcheck disable=2076,2199
  [[ " ${versions[@]} " =~ " ${tag} " ]] || echo "WARNING: $tag not found. Will attempt to start fleetcommand-agent with this tag."
elif [ "${versions[$release_or_tag]}" ]; then
  tag="${versions[$release_or_tag]}"
else
  echo "No tag found for Domino $release_or_tag"
  echo "Valid releases are:"
  # shellcheck disable=2005,2046
  echo $(printf "%s\n" "${!versions[@]}" | sort -V)
  exit 1
fi

_="${QUAY_USERNAME:?}"
_="${QUAY_PASSWORD:?}"

set +x

kubectl delete po --ignore-not-found=true fleetcommand-agent-install

kubectl create secret \
  docker-registry \
  -o yaml --dry-run=client \
  --docker-server=quay.io \
  --docker-username="$QUAY_USERNAME" \
  --docker-password="$QUAY_PASSWORD" \
  --docker-email=. domino-quay-repos | kubectl apply -f -

kubectl create configmap \
  fleetcommand-agent-config \
  -o yaml --dry-run=client \
  --from-file=domino.yml | kubectl apply -f -

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin
  namespace: default
---
apiVersion: v1
kind: Pod
metadata:
  name: fleetcommand-agent-install
spec:
  serviceAccountName: admin
  imagePullSecrets:
    - name: domino-quay-repos
  restartPolicy: Never
  containers:
  - name: fleetcommand-agent
    image: quay.io/domino/fleetcommand-agent:$tag
    command:
      - /bin/bash
      - -c
      - |
        python -m fleetcommand_agent run -f /app/install/domino.yml || { cat logs/*.log; echo "Install failed."; exit 1; }
    imagePullPolicy: Always
    volumeMounts:
    - name: install-config
      mountPath: /app/install/
  volumes:
  - name: install-config
    configMap:
      name: fleetcommand-agent-config
EOF

set +e
while true; do
  sleep 5
  if kubectl logs -f fleetcommand-agent-install; then
    break
  fi
done
