From bc17ff70ec652dd52dff4776a0ad449de03e583a Mon Sep 17 00:00:00 2001 From: Eduard T Date: Sun, 9 Jun 2024 14:00:57 -0500 Subject: [PATCH] Add option to use auto-configure pacman-static in case that pacman is broken using the `USE_PACMAN_STATIC=true` environment variable --- pkgrecover.sh | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/pkgrecover.sh b/pkgrecover.sh index 0d6c8a2..e4caef9 100644 --- a/pkgrecover.sh +++ b/pkgrecover.sh @@ -1,17 +1,48 @@ #!/usr/bin/bash +USE_PACMAN_STATIC=${USE_PACMAN_STATIC:-"false"} + +install_pacman_static() { + if [[ ! -f /tmp/pacman-static.installed ]]; then + echo "Downloading and installing pacman-static. In case that curl fails, you can download the binary from:" + echo "https://pkgbuild.com/~morganamilo/pacman-static/x86_64/bin/pacman-static" + echo "and then move it to /usr/bin/pacman" + + mv /usr/bin/pacman /usr/bin/pacman.bak + curl -Lo /usr/bin/pacman https://pkgbuild.com/~morganamilo/pacman-static/x86_64/bin/pacman-static + chmod +x /usr/bin/pacman + touch /tmp/pacman-static.installed + fi +} + check_dependencies() { + if [[ $USE_PACMAN_STATIC == "true" ]]; then + install_pacman_static + fi + if ! command -v pacman &>/dev/null; then echo "pacman is not installed." exit 1 fi - if ! command -v paclog &>/dev/null; then + if [[ "$1" == "paclog" ]] && ! command -v paclog &>/dev/null; then echo "paclog is not installed." exit 1 fi } +cleanup() { + echo "Cleanup..." + + if [[ -f /usr/bin/pacman.bak ]]; then + mv /usr/bin/pacman.bak /usr/bin/pacman + fi + + if [[ -f /tmp/pacman-static.installed ]]; then + rm /tmp/pacman-static.installed + fi +} + check_date_format() { if ! date -d "$1" &>/dev/null; then echo "Please provide a date in the format 'YYYY-MM-DD HH:MM:SS'. See --help for more information." @@ -57,6 +88,8 @@ using_pacman_db() { echo "Reinstalling the following packages:" sudo pacman -S "${matching_packages[@]}" --overwrite='*' fi + + cleanup } using_paclog() { @@ -97,10 +130,14 @@ using_paclog() { echo "Reinstalling the following packages:" sudo pacman -S "${matching_packages[@]}" --overwrite='*' fi + + cleanup } usage() { - echo "Usage: $0 [OPTIONS]" + echo "Usage: [ENVIRONMENT] $0 [OPTIONS]" + echo "ENVIRONMENT:" + echo " USE_PACMAN_STATIC=true $0 [OPTIONS] Use pacman-static instead of the default pacman. Useful in cases that pacman is broken. Default is false." echo "Options:" echo " --pacman-db \"\" [--dry-run] Reinstall packages upgraded after the given date using pacman database. Please quote the date in the format 'YYYY-MM-DD HH:MM:SS'" echo " --paclog \"\" [--no-db] [--dry-run] Reinstall packages upgraded after the given date using pacman log. Please quote the date in the format 'YYYY-MM-DD HH:MM:SS'" @@ -111,6 +148,12 @@ if [[ $# -eq 0 ]]; then usage fi +# Check that we are running as root +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root." + exit 1 +fi + # Parse the command line arguments while [[ $# -gt 0 ]]; do case $1 in @@ -120,7 +163,7 @@ while [[ $# -gt 0 ]]; do exit 0 ;; --paclog) - check_dependencies + check_dependencies "paclog" using_paclog "$2" "$3" "$4" exit 0 ;;