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

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

USAGE="install [ tool-name: upctl, etc | all | info] [--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 script
" 

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_tool
    local tool_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)" 

    UPCTL_VERSION=${UPCLOUD_UPCTL_VERSION:-}
    if [ -n "$UPCTL_VERSION" ]  && [ "$match" == "all" ] ||  [ "$match" == "upctl" ]  ; then
        has_upctl=$(type -P upctl)
        num_version="0"
        [ -n "$has_upctl" ] && upctl_version=$(upctl version | grep "Version" | cut -f2 -d":"  | sed 's/ //g') &&  num_version=${upctl_version//\./}
        expected_version_num=${UPCTL_VERSION//\./}
        if [ -z "$CHECK_ONLY" ] && [ "$num_version" -lt "$expected_version_num" ] ; then 
        mkdir -p upctl && cd upctl &&
        #curl -fsSLO $UPCLOUD_UPCTL_SOURCE/v${tool_version}/upcloud-cli_${tool_version}_${OS}_${ORG_ARCH}.tar.gz && 
        curl -fsSLO https://github.com/UpCloudLtd/upcloud-cli/releases/download/v${UPCTL_VERSION}/upcloud-cli_${UPCTL_VERSION}_${OS}_${ORG_ARCH}.tar.gz && 
            tar -xzf "upcloud-cli_${UPCTL_VERSION}_${OS}_${ORG_ARCH}.tar.gz" &&
            sudo mv upctl /usr/local/bin &&
            cd "$ORG" && rm -rf /tmp/upct "/upcloud-cli_${UPCTL_VERSION}_${OS}_${ORG_ARCH}.tar.gz"
            printf "%s\t%s\n" "upctl" "installed $UPCTL_VERSION"
        elif [ -n "$CHECK_ONLY" ] ; then
        printf "%s\t%s\t%s\n" "upctl" "$upctl_version" "expected $UPCTL_VERSION"
        else
        printf "%s\t%s\n" "upctl" "already $UPCTL_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
#[ -r ".env" ] && source .env set 
set +o allexport

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

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

PROVIDER_NAME="upcloud"
PROVIDER_TITLE="Upcloud"

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 "$@"
