Assorted shell and Python scripts

Refactor nerdfont_installer

+38
.archived/btrfs_backup
···
+
#!/usr/bin/env bash
+
+
SNAP_DATE=$(date '+%Y-%m-%d_%H-%M-%S')
+
+
# Check if device is mounted
+
if ! grep "/mnt/storage" /etc/mtab >/dev/null; then
+
logger -p error -t btrfs_backup "ERROR: /mnt/storage is not mounted."
+
exit 1
+
fi
+
+
create_snapshot() {
+
if ! btrfs subvolume snapshot -r "$1" "${1}/.snapshots/$2-$SNAP_DATE" >/dev/null; then
+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Error creating snapshot of $1" | tee -a "$LOGFILE"
+
notify-send -i computer-fail "Error creating snapshot of $1"
+
exit 1
+
else
+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Create snapshot of $1: OK" | tee -a "$LOGFILE"
+
fi
+
}
+
+
send_snapshot() {
+
mkdir -p "/srv/backup/$SNAP_DATE"
+
if ! btrfs send -q "${1}/.snapshots/$2-$SNAP_DATE" | btrfs receive -q "/srv/backup/$SNAP_DATE"; then
+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Error sending snapshot of $1 to /srv/backup" | tee -a "$LOGFILE"
+
notify-send -i computer-fail "Error sending snapshot of $1 to /srv/backup"
+
exit 1
+
else
+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Send snapshot of $1 to /srv/backup: OK" | tee -a "$LOGFILE"
+
fi
+
}
+
+
# Create root and home snapshots
+
create_snapshot "/" "root"
+
create_snapshot "/home" "home"
+
+
# Send root and home snapshots
+
send_snapshot "/" "root"
+
send_snapshot "/home" "home"
-46
btrfs_backup
···
-
#!/usr/bin/env bash
-
-
LOGFILE="/var/log/btrfs-backup.log"
-
SNAP_DATE=$(date '+%Y-%m-%d_%H%M%S')
-
-
send_matrix_webhook() {
-
/usr/local/bin/send-matrix-webhook "btrfs-backup failed on $(hostname)"
-
}
-
-
trap send_matrix_webhook SIGINT SIGTERM
-
-
# Check if device is mounted
-
if ! grep "/srv/backup" /etc/mtab >/dev/null; then
-
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Backup device is not mounted." | tee -a "$LOGFILE"
-
notify-send -i computer-fail "Backup device is not mounted"
-
exit 1
-
fi
-
-
create_snapshot() {
-
if ! btrfs subvolume snapshot -r "$1" "${1}/.snapshots/$2-$SNAP_DATE" >/dev/null; then
-
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Error creating snapshot of $1" | tee -a "$LOGFILE"
-
notify-send -i computer-fail "Error creating snapshot of $1"
-
exit 1
-
else
-
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Create snapshot of $1: OK" | tee -a "$LOGFILE"
-
fi
-
}
-
-
send_snapshot() {
-
mkdir -p "/srv/backup/$SNAP_DATE"
-
if ! btrfs send -q "${1}/.snapshots/$2-$SNAP_DATE" | btrfs receive -q "/srv/backup/$SNAP_DATE"; then
-
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Error sending snapshot of $1 to /srv/backup" | tee -a "$LOGFILE"
-
notify-send -i computer-fail "Error sending snapshot of $1 to /srv/backup"
-
exit 1
-
else
-
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Send snapshot of $1 to /srv/backup: OK" | tee -a "$LOGFILE"
-
fi
-
}
-
-
# Create root and home snapshots
-
create_snapshot "/" "root"
-
create_snapshot "/home" "home"
-
-
# Send root and home snapshots
-
send_snapshot "/" "root"
-
send_snapshot "/home" "home"
btrfs_snapshot .archived/btrfs_snapshot
gumstarred .archived/gumstarred
install_cat_gtk .archived/install_cat_gtk
list_iommu_groups .archived/list_iommu_groups
+34 -77
nerdfont_installer
···
# Check for dependencies: curl and gum.
if ! test -x "$(command -v curl)"; then
-
echo "Missing dependencies: please install curl."
+
echo "Missing dependencies: curl"
exit 1
fi
if ! test -x "$(command -v gum)"; then
-
echo "Missing dependencies: please install the gum command."
+
echo "Missing dependencies: gum"
echo "See https://github.com/charmbracelet/gum"
exit 1
fi
-
# Define variables
-
NF_BASE_URL="https://github.com/ryanoasis/nerd-fonts/releases/download"
-
NF_VERSION=$(curl -sL https://github.com/ryanoasis/nerd-fonts/releases/latest | grep "<title>Release" | awk '{ print $2 }')
-
NF_URL="${NF_BASE_URL}/${NF_VERSION}"
+
if ! test -x "$(command -v unzip)"; then
+
echo "Missing dependencies: unzip"
+
exit 1
+
fi
+
+
# Directory on the local filesystem where the fonts will be installed.
LOCAL_FONT_DIR="${HOME}/.local/share/fonts"
# Fancy error output message.
···
exit 1
}
-
# Array of nerd font names.
-
nf_array=(
-
3270
-
Agave
-
AnonymousPro
-
Arimo
-
AurulentSansMono
-
BigBlueTerminal
-
BitstreamVeraSansMono
-
CascadiaCode
-
CodeNewRoman
-
Cousine
-
DaddyTimeMono
-
DejaVuSansMono
-
DroidSansMono
-
EnvyCodeR
-
FantasqueSansMono
-
FiraCode
-
FiraMono
-
Go-Mono
-
Gohu
-
Hack
-
Hasklig
-
HeavyData
-
Hermit
-
iA-Writer
-
IBMPlexMono
-
Inconsolata
-
InconsolataGo
-
InconsolataLGC
-
IntelOneMono
-
Iosevka
-
JetBrainsMono
-
Lekton
-
LiberationMono
-
Lilex
-
Meslo
-
Monofur
-
Monoid
-
Mononoki
-
MPlus
-
Noto
-
OpenDyslexic
-
Overpass
-
ProFont
-
ProggyClean
-
RobotoMono
-
ShareTechMono
-
SourceCodePro
-
SpaceMono
-
Terminus
-
Tinos
-
Ubuntu
-
UbuntuMono
-
VictorMono
-
)
-
# Print the startup message.
message=$(echo "Nerd font installer :nerd_face:" | gum format -t emoji)
gum style \
···
--margin "1 2" \
"$message"
-
# Print the `nf_array` line by line and pipe to `gum choose --no-limit`.
-
# Create `selection` array from the output of `gum choose --no-limit`.
-
# It will contain only the items that were selected by the user.
-
mapfile -t selection < <(printf "%s\n" "${nf_array[@]}" | gum choose --no-limit)
+
# Read the nerd-font download URLs into an array
+
readarray -t nf_url_array < <(curl --silent "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest" | jq '.assets.[].browser_download_url' | tr -d '"' | grep ".zip")
+
+
# Add the nerd-font basenames without file extension suffix into an associative
+
# array, using these as the keys and the download URLs as the values.
+
declare -A nf_array
+
for nf in "${nf_url_array[@]}"; do
+
name="$(basename -s .zip "$nf")"
+
nf_array[$name]="$nf"
+
done
+
+
# Sort the keys of the array.
+
IFS=$'\n'
+
readarray -t sorted_keys < <(sort <<<"${!nf_array[*]}")
+
unset IFS
+
+
# Display nerd-font selection menu with the keys of the associative array.
+
selection=$(gum choose --height=10 --limit=1 "${sorted_keys[@]}")
# Prompt for user confirmation and proceed with installation of nerd fonts.
#
···
#
# If user declines to proceed with installation, print a cancel message.
if gum confirm "Proceed with installation?"; then
-
for item in "${selection[@]}"; do
-
if ! gum spin --spinner dot --title "Downloading $item..." \
-
-- curl --create-dirs -f -sL -o "${LOCAL_FONT_DIR}/$item.zip" "${NF_URL}/$item.zip"; then
-
gum_error "Failed to download nerd font archive $item"
-
fi
-
if ! gum spin --spinner dot --title "Installing $item..." \
-
-- unzip -uo "${LOCAL_FONT_DIR}/$item.zip" -d "${LOCAL_FONT_DIR}"; then
-
gum_error "Failed to install nerd font archive $item"
-
fi
-
done
+
if ! gum spin --spinner dot --title "Downloading $selection..." \
+
-- curl --create-dirs -f -sL -o "${LOCAL_FONT_DIR}/$selection.zip" "${nf_array["$selection"]}"; then
+
gum_error "Failed to download nerdfont archive $selection"
+
fi
+
if ! gum spin --spinner dot --title "Installing $selection..." \
+
-- unzip -uo "${LOCAL_FONT_DIR}/$selection.zip" -d "${LOCAL_FONT_DIR}"; then
+
gum_error "Failed to install nerdfont archive $selection"
+
fi
else
gum style \
--foreground 212 \
roficlipster .archived/roficlipster
rofifox .archived/rofifox
rofimaim .archived/rofimaim
rofipower .archived/rofipower
rofiprompt .archived/rofiprompt
rofisearch .archived/rofisearch
rofistarred .archived/rofistarred
tznu .archived/tznu