#!/usr/bin/env bash
#===============================================================================
#
#             FILE: velero.sh
#            USAGE: curl -fsSL https://install.gutocarvalho.net/velero.sh | bash
#
#      DESCRIPTION: installs the latest velero client 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 ######################################################

VELERO_ARCH="linux-amd64"  # amd64 build of velero

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

### abort: print an error message and stop the script
function abort() {
  echo -e "\nError: $1" >&2
  echo -e "Tip: run 'bash -x velero.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 velero
# NOTE: the repo was renamed vmware-tanzu/velero -> velero-io/velero, so the
# GitHub API answers 301 on the old path; we use the new org and follow
# redirects (-L) so the download URL always resolves.
function install_velero() {
  echo -e "\n>>> Installing velero...\n"
  cd /tmp || abort "could not enter /tmp."
  local url version
  url=$(curl -sL https://api.github.com/repos/velero-io/velero/releases/latest \
    | grep browser_download_url | grep "$VELERO_ARCH" | cut -d '"' -f 4)
  version=$(echo "$url" | awk -F'/download/' '{print $2}' | awk -F'/' '{print $1}' | sed 's/v//' | uniq)
  wget -q "$url" -O velero.tar.gz || abort "velero download failed."
  tar zxf velero.tar.gz
  mv "/tmp/velero-v${version}-${VELERO_ARCH}/velero" /usr/local/bin/ || abort "velero extract failed."
  chmod +x /usr/local/bin/velero
  echo "velero $version installed."
}

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

check_root
need_cmd curl
need_cmd wget
need_cmd tar

refuse_if_present velero

install_velero

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

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