#!/usr/bin/env bash
#===============================================================================
#
#             FILE: ctop.sh
#            USAGE: bash ctop.sh
#
#      DESCRIPTION: installs ctop (top-like interface for containers)
#                   on compatible linux hosts (amd64).
#
#     REQUIREMENTS: curl, wget, 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: 2025-01-29
#          UPDATED: 2026-08-13
#             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 ######################################################

CTOP_ARCH="linux-amd64"  # amd64 build of ctop

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

### abort: print an error message and stop the script
function abort() {
  echo -e "\nError: $1" >&2
  echo -e "Tip: run 'bash -x ctop.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 ctop
function install_ctop() {
  echo -e "\n>>> Installing ctop...\n"
  cd /tmp || abort "could not enter /tmp."
  local ctop_version
  ctop_version=$(curl -s https://api.github.com/repos/bcicen/ctop/releases/latest \
    | grep browser_download_url | grep "$CTOP_ARCH" | cut -d '"' -f 4 \
    | awk -F'/download/' '{print $2}' | awk -F'/' '{print $1}' | sed 's/v//')
  curl -s https://api.github.com/repos/bcicen/ctop/releases/latest \
    | grep browser_download_url | grep "$CTOP_ARCH" | cut -d '"' -f 4 | wget -qi -
  mv "ctop-${ctop_version}-${CTOP_ARCH}" /usr/local/bin/ctop \
    || abort "ctop download failed."
  chmod +x /usr/local/bin/ctop
  echo "ctop installed: $(ctop -v)"
}

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

check_root
need_cmd curl
need_cmd wget

refuse_if_present ctop

install_ctop

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

echo -e "\n----------------------------------------------------------------------------------"
echo -e "\nAll done! ctop is available at $(which ctop), $(ctop -v)"
echo -e "\nEnjoy it!"
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"
