#!/bin/bash if [[ -z "$@" ]]; then echo "No arguments passed." exit 1 fi function getroot() { trap "true" SIGINT if [ "$($SUDOBIN whoami)" != "root" ]; then printtext 5; exit 1; fi trap - SIGINT } SUDOBIN="sudo" AURBIN="yay" PACFLAGS="" PACINSTALLFLAGS="--needed" PACREMOVEFLAGS="" PACSYNCFLAGS="" PACUPGRADEFLAGS="" AURFLAGS="" AURINSTALLFLAGS="--needed" AURUPGRADEFLAGS="" IGNORE="" IGNOREGROUP="" SPLITUPGRADE="" SPLITSEARCH="" function loadcfg() { if [ -f "$1" ]; then local CFGFILENAME="$HOME/.$(basename $0)-tmp-config.$(date +%s%N)" echo "$(cat "$1" | sed 's/^[ \t]*//' | grep -oE '^[^0-9].*' | grep -oE '^[a-zA-Z0-9 ]*=.*' | sed -r 's#([^\])"#\1#g;s/=[ \t]*/="/;s/$/"/;s/(\".*\")|\s*/\1/g;s/^/export /')" >"$CFGFILENAME" chmod +x "$CFGFILENAME" source "$CFGFILENAME" rm -f "$CFGFILENAME" fi } function printtext() { N="$1" shift case "$N" in 0) printf "\r\e[0m\e[2K\e[1;32m::\e[0;1m Searching packages...\e[0m" "$@" ;; 1) printf "\r\e[0m\e[2K\e[1;32m::\e[0;1m Searching packages (100%%)...\e[0m%s" "$@" ;; 2) printf "\r\e[0m\e[2K\e[1;32m::\e[0;1m Searching packages (%s%%)... [%s]\e[0m" "$@" ;; 3) printf "\e[0m\e[33;1m -> \e[31mThe following packages could not be found:\e[0;1m %s\e[0m\n" "$@" ;; 4) printf "\r\e[0m\e[2K\e[1;32m::\e[0;1m Continue? [%s]:\e[0m " "$@" ;; 5) printf "\e[0m\e[33;1m -> \e[31mSuperuser authentication failed.\e[0m\n" ;; 6) printf "\e[0m\e[33;1m -> \e[31mNothing could be found for the following:\e[0;1m %s\e[0m\n" "$@" ;; 255) echo "Incorrect argument count." ;; esac } loadcfg "/etc/papmw.conf" loadcfg "${HOME}/.config/papmw.conf" loadcfg "${HOME}/.papmw.conf" PAPMWVER="0.7.2.3" PAC="" AUR="" NOTFOUND="" COMMAND="$1" shift case "$COMMAND" in "help" | "--help") if [ $# -gt 0 ]; then printtext 255; exit 1; fi echo "PAPMW - PQCraft's Arch Package Manager Wrapper v$PAPMWVER" echo " Available commands:" echo " help/--help - Displays this help text" echo " version/--version - Displays the version and license info" echo " install PACKAGE... - Installs packages" echo " uninstall/remove PACKAGE... - Uninstalls packages" echo " refresh/sync - Syncs repos" echo " update/upgrade - Upgrades packages" echo " Config variables:" echo " Configs are read from:" echo " /etc/papmw.conf" echo " ~/.config/papmw.conf" echo " ~/.papmw.conf" echo " EDITOR - The command used to edit config files" echo " SUDOBIN - Path to executable to run commands as root" echo " AURBIN - Path to the AUR helper" echo " PACFLAGS - Options to pass to pacman" echo " PACINSTALLFLAGS - Options to pass to pacman during an install command (default is '--needed')" echo " PACREMOVEFLAGS - Options to pass to pacman during an uninstall/remove command" echo " PACSYNCFLAGS - Options to pass to pacman during an refresh/sync command" echo " PACUPGRADEFLAGS - Options to pass to pacman during an update/upgrade command" echo " AURFLAGS - Options to pass to the AUR helper" echo " AURINSTALLFLAGS - Options to pass to the AUR helper during an install command (default is '--needed')" echo " AURUPGRADEFLAGS - Options to pass to the AUR helper during an update/upgrade command" echo " IGNORE - Comma separated list of packages to ignore during upgrade" echo " IGNOREGROUP - Comma separated list of package groups to ignore during upgrade" echo " SPLITUPGRADE - Set to true or yes if the AUR helper does not invoke a system upgrade" echo " SPLITSEARCH - Set to true or yes if the AUR helper only returns AUR packages in search results" ;; "version" | "--version") if [ $# -gt 0 ]; then printtext 255; exit 1; fi echo "PAPMW - PQCraft's Arch Package Manager Wraper version $PAPMWVER" echo "Licensed under the GNU GPL v3" ;; "install") if [ $# -lt 1 ]; then printtext 255; exit 1; fi printtext 0 p=0 for i in "$@"; do printtext 2 "$((p*100/$#))" "$i" if (pacman -Ss -- "^$i$" &> /dev/null); then PAC="$PAC$i " else if [ "$("$AURBIN" -Ss -- "$i" 2> /dev/null | grep -E "*/$i ")" ]; then AUR="$AUR$i " else NOTFOUND="$NOTFOUND$i " fi fi ((p=p+1)) done printtext 1 $'\n' if [ "$NOTFOUND" ]; then NOTFOUND="$(echo "$NOTFOUND" | sed -e 's/\s\+/, /g')" if [ ${#NOTFOUND} -gt 2 ]; then NOTFOUND="${NOTFOUND::-2}"; fi printtext 3 "$NOTFOUND" if [ ! -z "$PAC$AUR" ]; then printtext 4 "y/N" read -n 1 I if [[ ! -z "$I" ]]; then echo; fi [[ "$I" =~ ^[^Yy]$ || -z "$I" ]] && exit 1 else exit fi fi getroot if [ "$PAC" ]; then $SUDOBIN -- pacman $PACFLAGS $PACINSTALLFLAGS -S -- $PAC; fi if [ "$AUR" ]; then "$AURBIN" $AURFLAGS $AURINSTALLFLAGS -S -- $AUR; fi ;; "uninstall" | "remove") if [ $# -lt 1 ]; then printtext 255; exit 1; fi printtext 0 p=0 for i in "$@"; do printtext 2 "$((p*100/$#))" "$i" if (pacman -Q -- "$i" &> /dev/null); then PAC="$PAC$i " else NOTFOUND="$NOTFOUND$i " fi ((p=p+1)) done printtext 1 $'\n' if [ "$NOTFOUND" ]; then NOTFOUND="$(echo "$NOTFOUND" | sed -e 's/\s\+/, /g')" if [ ${#NOTFOUND} -gt 2 ]; then NOTFOUND="${NOTFOUND::-2}"; fi printtext 3 "$NOTFOUND" if [ "$PAC$AUR" ]; then printtext 4 "Y/n" read -n 1 I if [[ ! -z "$I" ]]; then echo [[ "$I" =~ ^[^Yy]$ ]] && exit 1 fi else exit fi fi getroot $SUDOBIN pacman $PACFLAGS $PACREMOVEFLAGS -Rns -- $PAC ;; "refresh" | "sync") if [ $# -gt 0 ]; then printtext 255; exit 1; fi getroot $SUDOBIN pacman $PACFLAGS $PACSYNCFLAGS -Sy ;; "update" | "upgrade") if [ $# -gt 0 ]; then printtext 255; exit 1; fi getroot if [ "$IGNORE" ]; then IGNORE="--ignore=$(echo "$IGNORE" | sed 's/[ \t]*//g')"; fi if [ "$IGNOREGROUP" ]; then IGNOREGROUP="--ignoregroup=$(echo "$IGNOREGROUP" | sed 's/[ \t]*//g')"; fi if [ "${SPLITUPGRADE^^}" = "YES" ] || [ "${SPLITUPGRADE^^}" = "TRUE" ]; then $SUDOBIN pacman $PACFLAGS $PACUPGRADEFLAGS $IGNORE $IGNOREGROUP -Syu; fi $AURBIN $AURFLAGS $AURUPGRADEFLAGS $IGNORE $IGNOREGROUP -Syu ;; "search" | "find") if [ $# -lt 1 ]; then printtext 255; exit 1; fi if [ "${SPLITSEARCH^^}" = "YES" ] || [ "${SPLITSEARCH^^}" = "TRUE" ]; then $SUDOBIN pacman $PACFLAGS -Ss -- "$@"; fi $AURBIN $AURFLAGS -Ss -- "$@" ;; "localsearch" | "localfind" | "installed") if [ $# -lt 1 ]; then printtext 255; exit 1; fi printtext 0 p=0 for i in "$@"; do printtext 2 "$((p*100/$#))" "$i" if (pacman -Qs -- "$i" &> /dev/null); then PAC="$PAC$i " else NOTFOUND="$NOTFOUND$i " fi ((p=p+1)) done printtext 1 $'\n' if [ "$NOTFOUND" ]; then NOTFOUND="$(echo "$NOTFOUND" | sed -e 's/\s\+/, /g')" if [ ${#NOTFOUND} -gt 2 ]; then NOTFOUND="${NOTFOUND::-2}"; fi printtext 6 "$NOTFOUND" fi if [ ! -z "$PAC" ]; then pacman $PACFLAGS -Qs -- $PAC; fi ;; "config") if [ -z "$EDITOR" ]; then EDITOR=vi; fi printf "\r\e[0m\e[2K\e[1;32m::\e[0;1m Choose a config to edit\n1. /etc/papmw.conf\n2. ~/.config/papmw.conf\n3. ~/.papmw.conf\nConfig to edit:\e[0m " read -n 1 I if [[ ! -z "$I" ]]; then echo; fi case "$I" in 1) CFG="/etc/papmw.conf" getroot ;; 2) CFG="${HOME}/.config/papmw.conf" SUDOBIN="" ;; 3) CFG="${HOME}/.papmw.conf" SUDOBIN="" ;; *) echo "Invalid option." exit 1 ;; esac $SUDOBIN $EDITOR -- "$CFG" ;; *) echo "Unkown command: $COMMAND." exit 1 ;; esac