#!/usr/bin/env bash
#===============================================================================
#
#             FILE: k9s.sh
#            USAGE: curl -fsSL https://install.gutocarvalho.net/k9s.sh | bash
#
#      DESCRIPTION: installs the latest k9s (terminal UI for Kubernetes) on
#                   compatible linux hosts (amd64), into /usr/local/bin.
#
#     REQUIREMENTS: curl, wget, tar, bash
#
#       OS SUPPORT: Linux
#   DISTRO SUPPORT: Debian-like
#             ARCH: amd64
#
#           AUTHOR: Guto Carvalho, guto@bolha.io
#             SITE: https://gutocarvalho.net
#         MASTODON: @gutocarvalho@bolha.us
#         PIXELFED: @gutocarvalho@bolha.photos
#      WRITEFREELY: @gutocarvalho@bolha.blog
#       MATTERMOST: @gutocarvalho:mattermost.bolha.chat
#
#     ORGANIZATION: gutocarvalho.net, gcn.sh, bolha.io
#
#          CREATED: 2026-08-15
#          UPDATED: 2026-08-15
#             REPO: https://gitlab.gutocarvalho.net/open/install
#
#     CONTRIBUTORS: name, email, mastodon
#
#     The code is simple on purpose: we want it to be friendly for people
#     that are starting with shell language and programming logic.
#
#     This script was designed to run on a clean, brand new machine/vm/vps.
#
#===============================================================================

set -o nounset                              # treat unset variables as an error

## 1. CONSTANTS/VARIABLES ######################################################

K9S_ARCH="Linux_amd64"     # amd64 build of k9s

## 2. HELPERS #################################################################

### abort: print an error message and stop the script
function abort() {
  echo -e "\nError: $1" >&2
  echo -e "Tip: run 'bash -x k9s.sh' to see exactly what failed.\n" >&2
  exit 1
}

### check_root: this script must run as root
function check_root() {
  if [ "$(id -u)" != "0" ]; then
    abort "this script needs to run as root. Become root with 'su -' or 'sudo -i', then try again."
  fi
}

### need_cmd: make sure a required command exists
function need_cmd() {
  if ! command -v "$1" >/dev/null 2>&1; then
    abort "'$1' is required but was not found. Please install it first."
  fi
}

### refuse_if_present: stop if the tool we are about to install already exists
function refuse_if_present() {
  if command -v "$1" >/dev/null 2>&1; then
    abort "'$1' is already installed at $(command -v "$1"). Remove it first, then run this script again."
  fi
}

## 3. INSTALL FUNCTIONS #######################################################

### install k9s
function install_k9s() {
  echo -e "\n>>> Installing k9s...\n"
  cd /tmp || abort "could not enter /tmp."
  local url version
  url=$(curl -s https://api.github.com/repos/derailed/k9s/releases/latest \
    | grep -i "$K9S_ARCH" | grep tar.gz | grep -v json | grep browser_download_url | cut -d '"' -f 4)
  version=$(echo "$url" | awk -F'/download/' '{print $2}' | awk -F'/' '{print $1}' | sed 's/v//' | uniq)
  wget -q "$url" -O "k9s_${K9S_ARCH}.tar.gz" || abort "k9s download failed."
  tar zxf "k9s_${K9S_ARCH}.tar.gz" k9s
  mv k9s /usr/local/bin/ || abort "k9s extract failed."
  chmod +x /usr/local/bin/k9s
  echo "k9s $version installed."
}

## 4. EXECUTING ###############################################################

check_root
need_cmd curl
need_cmd wget
need_cmd tar

refuse_if_present k9s

install_k9s

## 5. NOTIFICATIONS ###########################################################

echo -e "\nAll done! k9s is available at /usr/local/bin/k9s."
