#!/usr/bin/env bash
#===============================================================================
#
#             FILE: rke2.sh
#            USAGE: curl -fsSL https://install.gutocarvalho.net/rke2.sh | bash
#
#      DESCRIPTION: installs RKE2 (Rancher Kubernetes Engine 2) from get.rke2.io
#                   plus the k8s client tools (kubectl, kubetail, helm, velero
#                   and k9s) by running k8s-tools.sh as a dependency.
#                   It installs the rke2 binary and systemd units only; it does
#                   NOT start a cluster for you (that is a deploy decision).
#
#     REQUIREMENTS: curl, wget, tar, systemd, 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-14
#          UPDATED: 2026-08-14
#             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 ######################################################

# where to download the focused scripts from (override with INSTALL_BASE_URL)
BASE_URL="${INSTALL_BASE_URL:-https://install.gutocarvalho.net}"

# RKE2 install knobs (honored by the get.rke2.io installer)
RKE2_TYPE="${RKE2_TYPE:-server}"            # server or agent
RKE2_CHANNEL="${RKE2_CHANNEL:-stable}"      # stable, latest or a specific channel

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

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

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

### 3.1 install the k8s client tools (dependency)
# runs k8s-tools.sh, which installs kubectl, kubetail, helm, velero and k9s.
# k8s-tools.sh does its own root/pre-checks and fails closed, so if it aborts
# (e.g. a tool is already installed) we stop here too.
function install_k8s_tools() {
  echo -e "\n>>> Installing k8s client tools via k8s-tools.sh (kubectl, kubetail, helm, velero, k9s)...\n"
  curl -fsSL "$BASE_URL/k8s-tools.sh" | bash \
    || abort "the k8s-tools dependency failed. Fix that first, then run rke2.sh again."
}

### 3.2 install rke2
function install_rke2() {
  echo -e "\n>>> Installing RKE2 (type: $RKE2_TYPE, channel: $RKE2_CHANNEL)...\n"
  curl -sfL https://get.rke2.io \
    | INSTALL_RKE2_TYPE="$RKE2_TYPE" INSTALL_RKE2_CHANNEL="$RKE2_CHANNEL" sh - \
    || abort "rke2 install failed."
  command -v rke2 >/dev/null 2>&1 \
    || [ -x /usr/local/bin/rke2 ] \
    || abort "rke2 binary not found after install."
  echo "rke2 $(/usr/local/bin/rke2 --version 2>/dev/null | awk '/rke2 version/{print $3}') installed."
}

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

check_root
need_cmd curl

install_k8s_tools
install_rke2

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

echo -e "\n----------------------------------------------------------------------------------"
echo -e "\nAll done! RKE2 ($RKE2_TYPE) and the k8s client tools are installed."
echo -e "\nRKE2 was installed but NOT started. To bring up this node, run:"
echo -e "  systemctl enable --now rke2-${RKE2_TYPE}.service"
echo -e "\nAfter it is up:"
echo -e "  - kubeconfig:   /etc/rancher/rke2/rke2.yaml"
echo -e "  - rke2 binaries: /var/lib/rancher/rke2/bin (add to PATH)"
echo -e "  - on a server node, the node token for agents is at:"
echo -e "                  /var/lib/rancher/rke2/server/node-token"
echo -e "\nDocs: https://docs.rke2.io"
echo -e "\nWant to know more about our collective? Visit https://bolha.io and consider joining us! ;)"
echo -e "Come and visit our mastodon instance:    https://bolha.us"
echo -e "Come and visit our mattermost instance:  https://mattermost.bolha.chat"
echo -e "Say hello on the fediverse:              @gutocarvalho@bolha.us"
echo -e "\n----------------------------------------------------------------------------------\n"
