Skip to content

Instantly share code, notes, and snippets.

@Aikufurr
Last active May 16, 2024 19:28
Show Gist options
  • Save Aikufurr/b9829a01d02602ac3933364dbe3a43cc to your computer and use it in GitHub Desktop.
Save Aikufurr/b9829a01d02602ac3933364dbe3a43cc to your computer and use it in GitHub Desktop.
EndeavourOS Repo/AUR/Flatpak Updater, recommended to run with "--paru --flatpak"
#!/bin/bash
# Package updater for EndeavourOS and Arch.
# Handles db lock file, keyrings, syncing, and optionally AUR with given AUR helper as well as Flatpak.
echo2() { echo -e "$@" >&2 ; }
echo3() { echo -e "\n${BLUE}[>>]${RESET}${BOLD} $@${RESET}" >&2 ; }
echo_err() { echo -e "\n${RED}[!!]${RESET}${BOLD} $@${RESET}" >&2 ; }
printf2() { printf "$@" >&2 ; }
WARN() { echo2 "==> $progname: warning: $1"; }
DIE() { echo2 "==> $progname: error: $1"; exit 1; }
RED='\033[1;31m'
BLUE='\033[1;34m'
BOLD='\033[1m'
RESET='\033[0m'
SetHelper() {
for helper in "$@" "$EOS_AUR_HELPER" "$EOS_AUR_HELPER_OTHER" yay paru pacman ; do
which "${helper%% *}" &>/dev/null && return 0
done
WARN "AUR helper not found"
helper=pacman
}
Cmd() { # Show a command, run it, and exit on issues.
echo2 "==>" "$@"
"$@" || DIE "'$*' failed."
}
ResetKeyrings() {
Cmd sudo mv /etc/pacman.d/gnupg /root/pacman-key.bak.$(date +%Y%m%d-%H%M).by.$progname
Cmd sudo pacman-key --init
Cmd sudo pacman-key --populate archlinux endeavouros
Cmd sudo pacman -Syy --noconfirm archlinux-keyring endeavouros-keyring
Cmd sudo pacman -Syu
echo2 "Keyrings reset."
exit 0
}
Options() {
local opts
local lopts="aur,dump-options,keyrings-reset,nvidia,no-keyring,helper:,min-free-bytes:,paru,yay,pacman,help,flatpak"
local sopts="h"
opts="$(/usr/bin/getopt -o=$sopts --longoptions $lopts --name "$progname" -- "$@")" || {
Options -h
return 1
}
eval set -- "$opts"
while true ; do
case "$1" in
--nvidia)
IsEndeavourOS && nvidia=yes || WARN "sorry, option $1 works only in EndeavourOS. Option ignored."
;;
--no-keyring) keyring=no ;;
--flatpak) flatpak=yes ;;
--keyrings-reset) ResetKeyrings ;;
--min-free-bytes) min_free_bytes="$2" ; shift ;;
--helper) SetHelper "$2" ; shift ;;
--aur) SetHelper ;;
--paru | --yay | --pacman) SetHelper "${1/--/}" ;;
--dump-options)
lopts="${lopts//:/}"
lopts="--${lopts//,/ --}"
sopts="${sopts//:/}"
sopts="$(echo "$sopts" | sed -E 's|([a-z])| -\1|g')"
echo $lopts $sopts
exit 0
;;
-h | --help)
cat <<EOF >&2
Package updater for EndeavourOS and Arch
Handles:
- keyrings
- pacman db lock
- disk space check
- sync after update
and optionally
- AUR with given AUR helper
- Flatpak updates
- Nvidia driver vs. kernel updates (only on EndeavourOS)
Usage: $progname [options]
Options:
--help, -h This help.
--nvidia Check also nvidia driver vs. kernel updates.
--no-keyring Do not try to update keyrings first.
--keyrings-reset Resets Arch and EndeavourOS keyrings.
Try this if keyring issues constantly make system update fail.
--helper AUR helper name. Supported: yay, paru, pacman.
Default: pacman
Other AUR helpers supporting option -Sua like yay should work as well.
--paru Same as --helper=paru.
--yay Same as --helper=yay.
--flatpak Update Flatpak applications
--aur Uses the AUR helper configured in /etc/eos-script-lib-yad.conf.
--pacman Same as --helper=pacman. Default. (Note: pacman does not support AUR directly).
--min-free-bytes Minimal amount of free space (in bytes) that the root partition should have
before updating. Otherwise a warning message will be displayed.
Default: $min_free_bytes
--dump-options Shows all supported options. Used for bash command completion.
EOF
exit 0
;;
--) shift ; break ;;
esac
shift
done
}
IsEndeavourOS() {
if [ "$isEndeavourOS" = "yes" ] || [ -r /usr/lib/endeavouros-release ] || [ -n "$(grep -iw endeavouros /etc/*-release)" ] ; then
isEndeavourOS=yes
return 0
fi
isEndeavourOS=no
return 1
}
DiskSpace() {
local available=$(findmnt / -nbo avail)
local min=$min_free_bytes
if [ $available -lt $min ] ; then
{
WARN "your root partition (/) has only $available bytes of free space."
if [ $(du -b -d0 /var/cache/pacman/pkg | awk '{print $1}') -gt $min ] ; then
printf "\nFor example, cleaning up the package cache may help.\n"
printf "Command 'sudo paccache -rk1' would do this:"
paccache -dk1
printf "Command 'sudo paccache -ruk0' would do this:"
paccache -duk0
echo ""
fi
} >&2
fi
}
Main() {
local progname="${0##*/}"
source /usr/share/endeavouros/scripts/eos-script-lib-yad || return 1
local helper="pacman"
local min_free_bytes=$((1000 * 1000 * 1000)) # default: 1 GB
local helper2=":"
local lock=/var/lib/pacman/db.lck
local rmopt=f
local isEndeavourOS=""
local subopts=()
local keyring=yes # user may disable keyring check with --no-keyring
local nvidia=no # user may enable Nvidia check with --nvidia
local flatpak=no # user may enable Flatpak update with --flatpak
Options "$@"
[ $nvidia = yes ] && subopts+=(--nvidia)
[ $keyring = yes ] && subopts+=(--keyrings)
echo2 "$progname: package updater with additional features"
DiskSpace
if [ -e $lock ] && fuser $lock &>/dev/null ; then
rmopt=i
fi
# Remove lock file
sudo rm "-$rmopt" "$lock"
# Check if lock file was removed successfully
if [ ! -e "$lock" ]; then
# Update system packages
echo3 "Updating system packages"
sudo pacman -Sy
sudo eos-update-extras "${subopts[@]}"
sudo pacman -Su
# If AUR helper set, update AUR packages
if [ "$helper" != "pacman" ]; then
echo3 "Updating AUR packages"
$helper -Sua
fi
# Update Flatpak applications if flag set
if [ "$flatpak" == "yes" ]; then
# Check if Flatpak is installed
if command -v flatpak >/dev/null; then
# Flatpak is installed
echo3 "Updating Flatpak applications"
flatpak update
else
# Flatpak is not installed
echo_err "Flatpak is not installed. Please install Flatpak to update Flatpak applications or remove --flatpak."
fi
fi
# Synchronize filesystem
sync
else
echo_err "Failed to remove the lock file: $lock"
fi
}
Main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment