#!/bin/bash
# Info: Script to install aws tools
# Author: JesusPerezLorenzo  
# Release: 1.0 
# Date: 15-04-2024

[ "$DEBUG" == "-x" ] && set -x 

USAGE="install-tools [ tool-name: tera k9s, etc | all] [--update]
As alternative use environment var TOOL_TO_INSTALL with a list-of-tools (separeted with spaces)
Versions are set in ./versions file 

This can be called by directly with an argumet or from an other srcipt
" 

ORG=$(pwd)
function _info_tools {
  local match=$1
  local info_keys
  info_keys="info version site"

  if [ -z "$match" ] || [ "$match" == "all" ] || [ "$match" == "-" ]; then 
    match="all"
  fi
  echo "$PROVIDER_TITLE"
  [ ! -r "$PROVIDERS_PATH/$PROVIDER_NAME/provisioning.yaml" ] && return 
  echo "-------------------------------------------------------"
  case "$match" in 
    "i" | "?" | "info") 
      for key in $info_keys
      do
        echo -n "$key:"
        [ "$key" != "version" ] && echo -ne "\t"
        echo " $(grep "^$key:" "$PROVIDERS_PATH/$PROVIDER_NAME/provisioning.yaml" | sed "s/$key: //g")"
      done
      ;;
    "all")
        cat "$PROVIDERS_PATH/$PROVIDER_NAME/provisioning.yaml"
       ;;
    *)
      echo -e "$match:\t $(grep "^$match:" "$PROVIDERS_PATH/$PROVIDER_NAME/provisioning.yaml" | sed "s/$match: //g")"
  esac
  echo "________________________________________________________"
}
function _install_tools {
  local match=$1
  shift
  local options
  options="$*"
  local has_aws
  local aws_version

  OS="$(uname | tr '[:upper:]' '[:lower:]')" 
  ORG_OS=$(uname)
  ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" 
  ORG_ARCH="$(uname -m)" 

  AWS_VERSION=${AWS_AWS_VERSION:-} 
  if [ -n "$AWS_VERSION" ] && [ "$match" == "all" ] ||  [ "$match" == "aws" ]  ; then
    [ -r "/usr/bin/aws" ] && mv /usr/bin/aws /usr/bin/_aws 
    has_aws=$(type -P aws)
    num_version=0
    [ -n "$has_aws" ] && aws_version=$(aws --version | cut -f1 -d" " | sed 's,aws-cli/,,g') &&  num_version=${aws_version//\./}
    [ -z "$num_version" ] && num_version=0
    expected_version_num=${AWS_VERSION//\./}
    if [ -z "$CHECK_ONLY" ] && [ "$num_version" -ne "$expected_version_num" ] ; then 
      cd "$ORG" || exit 1
      curl "https://awscli.amazonaws.com/awscli-exe-${OS}-${ORG_ARCH}.zip" -o "/tmp/awscliv2.zip"
      cd /tmp
      unzip awscliv2.zip >/dev/null
      [ "$1" != "-update" ] && [ -d "/usr/local/aws-cli" ] && sudo rm -rf "/usr/local/aws-cli" 
      sudo ./aws/install && printf "%s\t%s\n" "aws" "installed $AWS_VERSION"
      #sudo ./aws/install $options && echo "aws cli installed"
      cd "$ORG" && rm -rf /tmp/awscliv2.zip /tmp/aws
    elif [ -n "$CHECK_ONLY" ] ; then
      printf "%s\t%s\t%s\n" "aws" "$aws_version" "expected $AWS_VERSION"
    else
      printf "%s\t%s\n" "aws" "already $AWS_VERSION"
    fi
  fi
}
function _on_tools {
  local tools_list=$1
  [ -z "$tools_list" ] || [[ "$tools_list" == -* ]] && tools_list=${TOOL_TO_INSTALL:-all}
   case $tools_list in 
   "all") 
      _install_tools "all" "$@"
      ;;
   "info" | "i" | "?")
      shift
      _info_tools "$@"
    ;;
    *)
      for tool in $tools_list
      do
        [[ "$tool" == -* ]] && continue
        _install_tools "$tool" "${*//$tool/}"
      done
  esac
} 

set -o allexport 
## shellcheck disable=SC1090
[ -n "$PROVISIONING_ENV" ] && [ -r "$PROVISIONING_ENV" ] && source "$PROVISIONING_ENV"
[ -r "../env-provisioning" ] && source ../env-provisioning
[ -r "env-provisioning" ] && source ./env-provisioning
set +o allexport

export PROVISIONING=${PROVISIONING:-/usr/local/provisioning}

PROVIDERS_PATH=${PROVIDERS_PATH:-"$PROVISIONING/providers"}

PROVIDER_NAME="aws"
PROVIDER_TITLE="AWS"

if [ -r "$(dirname "$0")/../versions" ] ; then
  . "$(dirname "$0")"/../versions
elif [ -r "$(dirname "$0")/versions" ] ; then
  . "$(dirname "$0")"/versions
fi

[ "$1" == "-h" ] && echo "$USAGE" && shift
[ "$1" == "check" ] && CHECK_ONLY="yes" && shift
[ -n "$1" ] && cd /tmp && _on_tools "$@"
[ -z "$1" ] && _on_tools 
