#!/usr/bin/env bash
#===============================================================================
#
#             FILE: vimrc.sh
#            USAGE: bash vimrc.sh
#
#      DESCRIPTION: installs a minimal /root/.vimrc for servers.
#
#     REQUIREMENTS: wget, bash
#
#       OS SUPPORT: Linux
#   DISTRO SUPPORT: any
#
#           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-02-01
#          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 ######################################################

MY_VIMRC="/root/.vimrc"

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

### abort: print an error message and stop the script
function abort() {
  echo -e "\nError: $1" >&2
  echo -e "Tip: run 'bash -x vimrc.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_file_present: stop if the target file already exists
function refuse_if_file_present() {
  if [ -f "$1" ]; then
    abort "'$1' already exists. Please back it up and remove it, then run this script again."
  fi
}

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

### 3.1 install our minimal vimrc for root
function install_vimrc() {
  echo -e "\n>>> Installing a minimal vimrc for root...\n"
  wget -qO "$MY_VIMRC" https://install.gutocarvalho.net/vimrc \
    || abort "could not download the vimrc."
  echo "vimrc installed at $MY_VIMRC"
}

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

check_root
need_cmd wget

refuse_if_file_present "$MY_VIMRC"

install_vimrc

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

echo -e "\n----------------------------------------------------------------------------------"
echo -e "\nAll done! Your minimal vimrc is now at $MY_VIMRC"
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"
