#!/usr/bin/env bash

# omarchy:summary=Install kubectl, Helm, kops, krew, kubectx, k9s and kubecm
# omarchy:requires-sudo=true

# Based: the Kubernetes tools through asdf. kubecm has no Debian package and no
# reviewed plugin, so Debian takes its release binary into /usr/local/bin; the
# Mac has a brew formula.

set -eo pipefail

asdf_data=${ASDF_DATA_DIR:-$HOME/.asdf}
omarchy-pkg-add asdf
export PATH="$asdf_data/shims:$PATH"

for plugin in kubectl helm kops krew kubectx k9s; do
  echo -e "Installing $plugin...\n"
  omarchy-dev-env-tool "$plugin"
done

echo -e "\nInstalling kubecm...\n"
if [[ $(omarchy-os) == darwin ]]; then
  omarchy-pkg-add kubecm
elif ! omarchy-cmd-present kubecm; then
  version=v0.27.1
  case $(uname -m) in
  x86_64) arch=x86_64 ;;
  aarch64) arch=arm64 ;;
  *)
    echo "kubecm publishes no build for $(uname -m)" >&2
    exit 1
    ;;
  esac
  tmp=$(mktemp -d)
  trap 'rm -rf "$tmp"' EXIT
  curl -fsSL -o "$tmp/kubecm.tar.gz" "https://github.com/sunny0826/kubecm/releases/download/$version/kubecm_${version}_Linux_${arch}.tar.gz"
  tar -xzf "$tmp/kubecm.tar.gz" -C "$tmp" kubecm
  sudo install -m 0755 "$tmp/kubecm" /usr/local/bin/kubecm
fi
