125 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			125 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
#!/usr/bin/bash
 | 
						|
 | 
						|
# updates using a pacman-wrapper and flatpak-update with flags
 | 
						|
 | 
						|
source "$HOME"/.config/update.conf
 | 
						|
 | 
						|
VER="2.0.0"
 | 
						|
IGREEN="\033[0;92m"     # Intense Green
 | 
						|
IYELLOW="\033[0;93m"    # Intense Red
 | 
						|
NC="\033[0m"            # Text Reset
 | 
						|
TMP="/tmp/update"
 | 
						|
DATE="$(date +"%Y-%m-%d %H:%M:%S")"
 | 
						|
PRE_BACKUP_AMOUNT="$(ls -Ub "$BACKUP_LOCATION"/ | grep -c ^before-backup)"
 | 
						|
POST_BACKUP_AMOUNT="$(ls -Ub "$BACKUP_LOCATION"/ | grep -c ^after-backup)"
 | 
						|
FINAL_COMMAND=""
 | 
						|
 | 
						|
trap interrupt_function INT
 | 
						|
 | 
						|
Help() {
 | 
						|
   echo "Usage: update [OPTION]"
 | 
						|
   echo
 | 
						|
   echo "options:"
 | 
						|
   echo "no flag        same as -p"
 | 
						|
   echo "-f             updates using flatpak update only"
 | 
						|
   echo "-p             updates using a pacman-wrapper only"
 | 
						|
   echo "-a             updates using flatpak update and a pacman-wrapper"
 | 
						|
   echo "-g             shutdowns the computer afterwards"
 | 
						|
   echo "-r             reboots the computer afterwards"
 | 
						|
   echo "--help         displays this message"
 | 
						|
   echo "--preview      shows a preview of which pkg's can be updated"
 | 
						|
   echo "--version      prints out the version number"
 | 
						|
}
 | 
						|
 | 
						|
check_for_dblck() {
 | 
						|
    if [ -f /var/lib/pacman/db.lck ]; do {
 | 
						|
        echo -e "${IYELLOW}->${NC} /var/lib/pacman/db.lck exists"
 | 
						|
        echo -e "${IYELLOW}->${NC} there might be another instance of pacman running. exiting..."
 | 
						|
        exit 1
 | 
						|
    }
 | 
						|
    done
 | 
						|
}
 | 
						|
 | 
						|
delete_oldest_backup() {
 | 
						|
    if [[ $1 > $BACKUP_AMOUNT ]]; then
 | 
						|
        OLDEST_FILE="$(ls -t "$BACKUP_LOCATION" | grep $2 | tail -1)"
 | 
						|
        rm "$BACKUP_LOCATION"/"$OLDEST_FILE"
 | 
						|
    fi
 | 
						|
}
 | 
						|
 | 
						|
before_backup() {
 | 
						|
    check_for_dblck
 | 
						|
    delete_oldest_backup $PRE_BACKUP_AMOUNT before-backup
 | 
						|
    rm -r "$TMP" &> /dev/null
 | 
						|
    mkdir "$TMP" && mkdir "$TMP"/before-backup_"$DATE" && mkdir "$TMP"/after-backup_"$DATE"
 | 
						|
    pacman -Q > "$TMP"/before-backup_"$DATE"/pacman-pre.txt
 | 
						|
    [[ -f /usr/bin/flatpak ]] && flatpak list > "$TMP"/before-backup_"$DATE"/flatpak-pre.txt
 | 
						|
    sudo touch /var/lib/pacman/db.lck
 | 
						|
    tar -cJf "$TMP"/before-backup_"$DATE".tar.xz.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local &> /dev/null
 | 
						|
    sudo rm /var/lib/pacman/db.lck
 | 
						|
    cp "$TMP"/before-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"
 | 
						|
    rename before-backup_"$DATE".tar.xz.new before-backup_"$DATE".tar.xz "$BACKUP_LOCATION"/before-backup_"$DATE".tar.xz.new
 | 
						|
}
 | 
						|
 | 
						|
after_backup() {
 | 
						|
    delete_oldest_backup $POST_BACKUP_AMOUNT after-backup
 | 
						|
    tar -cJf "$TMP"/after-backup_"$DATE".tar.xz.new "$TMP"/after-backup_"$DATE" &> /dev/null
 | 
						|
    cp "$TMP"/after-backup_"$DATE".tar.xz.new "$BACKUP_LOCATION"
 | 
						|
    rename after-backup_"$DATE".tar.xz.new after-backup_"$DATE".tar.xz "$BACKUP_LOCATION"/after-backup_"$DATE".tar.xz.new
 | 
						|
    [[ $FINAL_COMMAND ]] && sleep 3s && $FINAL_COMMAND && exit 0
 | 
						|
}
 | 
						|
 | 
						|
update_with_pacman_wrapper() {
 | 
						|
	"$PACMAN_WRAPPER"
 | 
						|
    pacman -Q > "$TMP"/after-backup_"$DATE"/pacman-after.txt
 | 
						|
}
 | 
						|
 | 
						|
update_with_flatpak() {
 | 
						|
	flatpak update -u --noninteractive
 | 
						|
    flatpak list > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
 | 
						|
}
 | 
						|
 | 
						|
interrupt_function() {
 | 
						|
    echo "Interrupt has been detected"
 | 
						|
    sudo rm /var/lib/pacman/db.lck &> /dev/null
 | 
						|
    rm -r "$TMP" &> /dev/null
 | 
						|
    exit 1
 | 
						|
}
 | 
						|
 | 
						|
[[ $1 ]] || before_backup && echo -e "${IGREEN}pre-backup complete${NC}" && update_with_pacman_wrapper
 | 
						|
if [[ ${1:0:2} = -- ]]; then
 | 
						|
    case "${1:2}" in
 | 
						|
        help)
 | 
						|
            Help && exit 0;;
 | 
						|
        preview)
 | 
						|
            sudo pacman -Sy &> /dev/null && sudo pacman -Qu && exit 0;;
 | 
						|
        version)
 | 
						|
            echo "$VER" && exit 0;;
 | 
						|
        ?)
 | 
						|
            Help && exit 1;;
 | 
						|
    esac
 | 
						|
else
 | 
						|
    before_backup && echo -e "${IGREEN}pre-backup complete${NC}"
 | 
						|
 | 
						|
    while getopts 'fpagr' OPTIONS; do
 | 
						|
        case $OPTIONS in
 | 
						|
            f)
 | 
						|
                update_with_flatpak;;
 | 
						|
            p)
 | 
						|
                update_with_pacman_wrapper ;;
 | 
						|
            a)
 | 
						|
                update_with_pacman_wrapper; update_with_flatpak ;;
 | 
						|
            g)
 | 
						|
                [[ $1 = -g ]] && update_with_pacman_wrapper
 | 
						|
                FINAL_COMMAND="shutdown now" ;;
 | 
						|
            r)
 | 
						|
                [[ $1 = -r ]] && update_with_pacman_wrapper
 | 
						|
                FINAL_COMMAND="reboot" ;;
 | 
						|
            ?)
 | 
						|
                Help && exit 1;;
 | 
						|
        esac
 | 
						|
    done
 | 
						|
fi
 | 
						|
 | 
						|
after_backup && echo -e "${IGREEN}after-backup complete${NC}"
 |