#!/usr/bin/env bash
#===============================================================================
#
#             FILE: kubectl.sh
#            USAGE: curl -fsSL https://install.gutocarvalho.net/kubectl.sh | bash
#
#      DESCRIPTION: installs the latest stable kubectl on compatible linux
#                   hosts (amd64), into /usr/local/bin.
#
#     REQUIREMENTS: curl, 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 ######################################################

KUBECTL_ARCH="amd64"       # amd64 build of kubectl
KUBECTL_OS="linux"         # linux build of kubectl

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

### abort: print an error message and stop the script
function abort() {
  echo -e "\nError: $1" >&2
  echo -e "Tip: run 'bash -x kubectl.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 #######################################################

### 3.1 install kubectl
function install_kubectl() {
  echo -e "\n>>> Installing kubectl...\n"
  cd /tmp || abort "could not enter /tmp."
  local version
  version=$(curl -L -s https://dl.k8s.io/release/stable.txt)
  curl -LO "https://dl.k8s.io/release/${version}/bin/${KUBECTL_OS}/${KUBECTL_ARCH}/kubectl" \
    || abort "kubectl download failed."
  mv kubectl /usr/local/bin/kubectl
  chmod +x /usr/local/bin/kubectl
  echo "kubectl $version installed."
}

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

check_root
need_cmd curl

refuse_if_present kubectl

install_kubectl

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

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