#!/usr/bin/env bash
#===============================================================================
#
#             FILE: lsd.sh
#            USAGE: bash lsd.sh
#
#      DESCRIPTION: installs lsd (a modern, colorful 'ls' with icons) and adds
#                   a few convenience aliases to root's ~/.bashrc.
#
#     REQUIREMENTS: apt-get, bash
#
#       OS SUPPORT: Linux
#   DISTRO SUPPORT: Debian-like (lsd is packaged on bookworm/trixie and newer)
#             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-09-15
#          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 ######################################################

BASHRC="/root/.bashrc"                       # where we drop the aliases
MARKER="# lsd aliases (added by install/lsd.sh)"

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

### abort: print an error message and stop the script
function abort() {
  echo -e "\nError: $1" >&2
  echo -e "Tip: run 'bash -x lsd.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 lsd via apt
function install_lsd() {
  echo -e "\n>>> Installing lsd...\n"
  apt-get update -y || abort "'apt-get update' failed."
  apt-get install -y lsd \
    || abort "'apt-get install lsd' failed. Your distro may be too old (lsd lands on Debian bookworm/trixie)."
  echo "lsd installed: $(lsd --version)"
}

### 3.2 add the aliases to root's bashrc (idempotent: only once)
function add_aliases() {
  if grep -qF "$MARKER" "$BASHRC" 2>/dev/null; then
    echo "aliases already present in $BASHRC, skipping."
    return
  fi
  echo -e "\n>>> Adding lsd aliases to $BASHRC ...\n"
  {
    echo ""
    echo "$MARKER"
    echo 'alias ls="lsd"'
    echo 'alias ll="lsd -l"'
    echo 'alias la="lsd -la"'
    echo 'alias lt="lsd --tree"'
  } >> "$BASHRC"
  echo "aliases added. Open a new shell (or 'source $BASHRC') to use them."
}

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

check_root
need_cmd apt-get

refuse_if_present lsd

install_lsd
add_aliases

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

echo -e "\n----------------------------------------------------------------------------------"
echo -e "\nAll done! lsd is available at $(command -v lsd), $(lsd --version)"
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"
