#!/usr/bin/env bash
#
# Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# 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.

set -e

if ! which yaml2json >/dev/null; then
  echo "ERROR: Missing `yaml2json` executable. Cannot run this script."
  exit 1
fi
if ! which jq >/dev/null; then
  echo "ERROR: Missing `jq` executable. Cannot run this script."
  exit 1
fi

EXTENSIONS_FILE="$(curl -s https://raw.githubusercontent.com/gardener/gardener-extensions/master/extensions.yaml)"
extensions="$(echo "$EXTENSIONS_FILE" | yaml2json | jq -r '.extensions')"
location="$(pwd)"

function end_execution() {
  trap - HUP QUIT PIPE INT TERM EXIT
  cd "$location"
  rm -rf tmp
}
trap end_execution INT TERM EXIT

function clone_and_checkout() {
  gitHubRepo="$(echo $extensions | jq -r ".[] | select(.name==\"$extension\") | .gitHubRepo")"
  git clone -q "$gitHubRepo" tmp

  pushd tmp >/dev/null
  latest_tag="$(git tag --sort=-creatordate | head -1)"
  git checkout -q "$latest_tag"
}

function install_latest() {
  extension="${1}"
  path="$(echo $extensions | jq -r ".[] | select(.name==\"$extension\") | .path")"
  example_path="$(echo $extensions | jq -r ".[] | select(.name==\"$extension\") | .examplePath")"

  if [[ "$path" == "null" ]]; then
      path="."
  fi

  if [[ ! -z "$example_path" ]] && [[ "$example_path" != "null" ]]; then
    controllerregistration="$path/$example_path"
  else
    controllerregistration="$path/example/controller-registration.yaml"
  fi

  if [[ ! -f "$controllerregistration" ]]; then
    echo "    > ERROR: Could not find the file $controllerregistration."
    continue
  fi
  if kubectl apply -f "$controllerregistration" | sed 's/^/     /'; then
    echo "    > Successfully installed."
  fi
}

function cleanup() {
  popd >/dev/null
  rm -rf tmp
}

if [ "${1:-}" = "all" ];
then
  echo "Installing all extensions as requested!"
  for extension in `echo "$extensions" | jq -r '.[].name'`; do
    clone_and_checkout
    install_latest $extension
    cleanup
  done
else
  for extension in `echo "$extensions" | jq -r '.[].name'`; do
    read -p "> Found extension '$extension'. Do you want to install it into your local Gardener setup? (y/n) " response
    [[ ! "$response" =~ ^(yes|y) ]] && echo "  > Installation skipped." && continue
    clone_and_checkout

    read -p "  > '$extension' will be installed in its latest released version '$latest_tag'. Proceed? (y/n) " response
    [[ ! "$response" =~ ^(yes|y) ]] && echo "    > Installation skipped." && continue
    install_latest $extension

    cleanup
  done
fi

