#!/bin/bash
# Info: Script to install providers
# Author: JesusPerezLorenzo
# Release: 1.0
# Date: 12-11-2023

[ "$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 _install_cmds {

  OS="$(uname | tr '[:upper:]' '[:lower:]')"
  local has_cmd
  for cmd in $CMDS_PROVISIONING
  do
    has_cmd=$(type -P $cmd)
    if [ -z "$has_cmd" ] ; then
      case "$(OS)" in
        darwin) brew install $cmd ;;
        linux) sudo apt install $cmd ;;
        *) echo "Install $cmd in your PATH" ;;
      esac
    fi
  done
}

function _install_tools {
  local match=$1
  shift
  local options
  options="$*"
  # local has_jq
  # local jq_version
  # local has_yq
  # local yq_version
  local has_nickel
  local nickel_version
  local has_tera
  local tera_version
  local has_k9s
  local k9s_version
  local has_age
  local age_version
  local has_sops
  local sops_version
  # local has_upctl
  # local upctl_version
  # 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)"

  if [ -z "$CHECK_ONLY" ] and [ "$match" == "all" ] ; then
    _install_cmds
  fi

  # if [ -n "$JQ_VERSION" ] && [ "$match" == "all" ] ||  [ "$match" == "jq" ]  ; then
  #   has_jq=$(type -P jq)
  #   num_version="0"
  #   [ -n "$has_jq" ] && jq_version=$(jq -V | sed 's/jq-//g') && num_version=${jq_version//\./}
  #   expected_version_num=${JQ_VERSION//\./}
  #   if [ -z "$CHECK_ONLY" ] && [ "$num_version" -lt "$expected_version_num" ] ; then
  #     curl -fsSLO "https://github.com/jqlang/jq/releases/download/jq-${JQ_VERSION}/jq-${OS}-${ARCH}" &&
  #       chmod +x "jq-${OS}-${ARCH}" &&
  #       sudo mv "jq-${OS}-${ARCH}" /usr/local/bin/jq &&
  #       printf "%s\t%s\n" "jq" "installed $JQ_VERSION"
  #   elif [ -n "$CHECK_ONLY" ] ; then
  #     printf "%s\t%s\t%s\n" "jq" "$jq_version" "expected $JQ_VERSION"
  #   else
  #     printf "%s\t%s\n" "jq" "already $JQ_VERSION"
  #   fi
  # fi
  # if [ -n "$YQ_VERSION" ] && [ "$match" == "all" ] ||  [ "$match" == "yq" ]  ; then
  #   has_yq=$(type -P yq)
  #   num_version="0"
  #   [ -n "$has_yq" ] && yq_version=$(yq -V | cut -f4 -d" " | sed 's/v//g') && num_version=${yq_version//\./}
  #   expected_version_num=${YQ_VERSION//\./}
  #   if [ -z "$CHECK_ONLY" ] && [ "$num_version" -lt "$expected_version_num" ] ; then
  #     curl -fsSLO "https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/yq_${OS}_${ARCH}.tar.gz" &&
  #       tar -xzf "yq_${OS}_${ARCH}.tar.gz" &&
  #       sudo mv "yq_${OS}_${ARCH}" /usr/local/bin/yq &&
  #       sudo ./install-man-page.sh &&
  #       rm -f install-man-page.sh yq.1 "yq_${OS}_${ARCH}.tar.gz" &&
  #       printf "%s\t%s\n" "yq" "installed $YQ_VERSION"
  #   elif [ -n "$CHECK_ONLY" ] ; then
  #     printf "%s\t%s\t%s\n" "yq" "$yq_version" "expected $YQ_VERSION"
  #   else
  #     printf "%s\t%s\n" "yq" "already $YQ_VERSION"
  #   fi
  # fi
  if [ -n "$NICKEL_VERSION" ] && [ "$match" == "all" ] ||  [ "$match" == "nickel" ]  ; then
    has_nickel=$(type -P nickel)
    num_version="0"
    [ -n "$has_nickel" ] && nickel_version=$(nickel --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1) && num_version=${nickel_version//\./}
    expected_version_num=${NICKEL_VERSION//\./}
    if [ -z "$CHECK_ONLY" ] && [ "$num_version" -lt "$expected_version_num" ] ; then
      echo "⚠️  Nickel installation/update required"
      echo "   Recommended method: nix profile install nixpkgs#nickel"
      echo "   Alternative: cargo install nickel-lang-cli --version ${NICKEL_VERSION}"
      echo "   https://nickel-lang.org/getting-started"
    elif [ -n "$CHECK_ONLY" ] ; then
      printf "%s\t%s\t%s\n" "nickel" "$nickel_version" "expected $NICKEL_VERSION"
    else
      printf "%s\t%s\n" "nickel" "already $NICKEL_VERSION"
    fi
  fi
  if [ -n "$TERA_VERSION" ] && [ "$match" == "all" ] ||  [ "$match" == "tera" ]  ; then
    has_tera=$(type -P tera)
    num_version="0"
    [ -n "$has_tera" ] && tera_version=$(tera -V | cut -f2 -d" " | sed 's/teracli//g') && num_version=${tera_version//\./}
    expected_version_num=${TERA_VERSION//\./}
    if [ -z "$CHECK_ONLY" ] && [ "$num_version" -lt "$expected_version_num" ] ; then
      if [ -x "$(dirname "$0")/../tools/tera_${OS}_${ARCH}" ] ; then
	      sudo cp "$(dirname "$0")/../tools/tera_${OS}_${ARCH}" /usr/local/bin/tera && printf "%s\t%s\n" "tera" "installed $TERA_VERSION"
      else
        echo "Error: $(dirname "$0")/../ttools/tera_${OS}_${ARCH} not found !!"
        exit 2
      fi
    elif [ -n "$CHECK_ONLY" ] ; then
      printf "%s\t%s\t%s\n" "tera" "$tera_version" "expected $TERA_VERSION"
    else
      printf "%s\t%s\n" "tera" "already $TERA_VERSION"
    fi
  fi
  if [ -n "$K9S_VERSION" ]  && [ "$match" == "all" ] ||  [ "$match" == "k9s" ]  ; then
    has_k9s=$(type -P k9s)
    num_version="0"
    [ -n "$has_k9s" ] && k9s_version="$( k9s version | grep Version | cut -f2 -d"v" | sed 's/ //g')" && num_version=${k9s_version//\./}
    expected_version_num=${K9S_VERSION//\./}
    if [ -z "$CHECK_ONLY" ] && [ "$num_version" -lt "$expected_version_num" ] ; then
      mkdir -p k9s && cd k9s &&
      curl -fsSLO https://github.com/derailed/k9s/releases/download/v${K9S_VERSION}/k9s_${ORG_OS}_${ARCH}.tar.gz &&
        tar -xzf "k9s_${ORG_OS}_${ARCH}.tar.gz" &&
        sudo mv k9s /usr/local/bin &&
        cd "$ORG" && rm -rf /tmp/k9s "/k9s_${ORG_OS}_${ARCH}.tar.gz" &&
        printf "%s\t%s\n" "k9s" "installed $K9S_VERSION"
    elif [ -n "$CHECK_ONLY" ] ; then
      printf "%s\t%s\t%s\n" "k9s" "$k9s_version" "expected $K9S_VERSION"
    else
      printf "%s\t%s\n" "k9s" "already $K9S_VERSION"
    fi
  fi
  if [ -n "$AGE_VERSION" ]  && [ "$match" == "all" ] ||  [ "$match" == "age" ]  ; then
    has_age=$(type -P age)
    num_version="0"
    [ -n "$has_age" ] && age_version="${AGE_VERSION}" && num_version=${age_version//\./}
    expected_version_num=${AGE_VERSION//\./}
    if [ -z "$CHECK_ONLY" ] && [ "$num_version" -lt "$expected_version_num" ] ; then
      curl -fsSLO https://github.com/FiloSottile/age/releases/download/v${AGE_VERSION}/age-v${AGE_VERSION}-${OS}-${ARCH}.tar.gz &&
        tar -xzf age-v${AGE_VERSION}-${OS}-${ARCH}.tar.gz &&
        sudo mv age/age /usr/local/bin &&
        sudo mv age/age-keygen /usr/local/bin &&
        rm -rf age "age-v${AGE_VERSION}-${OS}-${ARCH}.tar.gz" &&
        printf "%s\t%s\n" "age" "installed $AGE_VERSION"
    elif [ -n "$CHECK_ONLY" ] ; then
      printf "%s\t%s\t%s\n" "age" "$age_version" "expected $AGE_VERSION"
    else
      printf "%s\t%s\n" "age" "already $AGE_VERSION"
    fi
  fi
  if [ -n "$SOPS_VERSION" ]  && [ "$match" == "all" ] ||  [ "$match" == "sops" ]  ; then
    has_sops=$(type -P sops)
    num_version="0"
    [ -n "$has_sops" ] && sops_version="$(sops -v | cut -f2 -d" " | sed 's/ //g')" && num_version=${sops_version//\./}
    expected_version_num=${SOPS_VERSION//\./}
    if [ -z "$CHECK_ONLY" ] && [ "$num_version" -lt "$expected_version_num" ] ; then
      mkdir -p sops && cd sops &&
      curl -fsSLO https://github.com/getsops/sops/releases/download/v${SOPS_VERSION}/sops-v${SOPS_VERSION}.${OS}.${ARCH} &&
     	mv sops-v${SOPS_VERSION}.${OS}.${ARCH} sops &&
	    chmod +x sops &&
      sudo mv sops /usr/local/bin  &&
    	rm -f sops-v${SOPS_VERSION}.${OS}.${ARCH} sops &&
      printf "%s\t%s\n" "sops" "installed $SOPS_VERSION"
    elif [ -n "$CHECK_ONLY" ] ; then
      printf "%s\t%s\t%s\n" "sops" "$sops_version" "expected $SOPS_VERSION"
    else
      printf "%s\t%s\n" "sops" "already $SOPS_VERSION"
    fi
  fi
  # 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 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
  # 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//\./}
  #   expected_version_num=${AWS_VERSION//\./}
  #   if [ -z "$CHECK_ONLY" ] && [ "$num_version" -lt "$expected_version_num" ] ; then
  #     cd "$ORG" || exit 1
  #     curl "https://awscli.amazonaws.com/awscli-exe-${OS}-${ORG_ARCH}.zip" -o "awscliv2.zip"
  #     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 awscliv2.zip
  #   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 get_providers {
  local list
  local name

  for item in $PROVIDERS_PATH/*
  do
    name=$(basename $item)
    [[ "$name"  == _* ]] && continue
    [ ! -d "$item/templates" ] && [ ! -r "$item/provisioning.yam" ] && continue
    if [ -z "$list" ] ; then
      list="$name"
    else
      list="$list $name"
    fi
  done
  echo $list
}
function _on_providers {
  local providers_list=$1
  [ -z "$providers_list" ] || [[ "$providers_list" == -* ]] && providers_list=${PROVISIONING_PROVIDERS:-all}
  if [ "$providers_list" == "all" ] ; then
     providers_list=$(get_providers)
  fi
  for provider in $providers_list
  do
    [ ! -d "$PROVIDERS_PATH/$provider/templates" ] && [ ! -r "$PROVIDERS_PATH/$provider/provisioning.yam" ] && continue
    if [ ! -r "$PROVIDERS_PATH/$provider/bin/install.sh" ] ; then
      echo "🛑 Error  on $provider no $PROVIDERS_PATH/$provider/bin/install.sh found"
      continue
    fi
    "$PROVIDERS_PATH/$provider/bin/install.sh" "$@"
  done
}

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}
export PROVIDERS_PATH=${PROVIDERS_PATH:-"$PROVISIONING/providers"}

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