This repository has been archived on 2024-09-01. You can view files and clone it, but cannot push or open issues or pull requests.
al-skel/home/bin/baph

190 lines
7.0 KiB
Plaintext
Raw Normal View History

2018-09-15 23:19:32 -05:00
#!/bin/bash
# Simple tool to update and install AUR packages for Arch Linux
# Written by Nathaniel Maia, September 2018
# This software is provided free of charge and WITHOUT warranty
2018-10-23 23:11:58 -05:00
declare -g NOVIEW="" # disable viewing the PKGBUILD
declare -g NOCONF="" # disable extra confirmations
declare -i CACHE=0 # keep cached package downloads
readonly BUILD_DIR="$HOME/.cache/aur_builds" # AUR build directory
readonly AUR_ADDR="https://aur.archlinux.org" # AUR web address
readonly PKG_ADDR="$AUR_ADDR/packages" # AUR package address
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
## ----------------------------------------------------------------- ##
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
readonly SCRIPT="$(readlink -f "$0")" # real path to this file
declare -a AUR_PKGS=() # array of AUR packages
declare -a PAC_PKGS=() # array of official packages
usage() { # show the standard UNIX style help message.. usage: usage
2018-09-15 23:19:32 -05:00
cat <<- EOF
2018-10-23 23:11:58 -05:00
baph - Basic AUR Package Helper, a simple script to install and update packages
usage: baph <operation> [package(s)] [options]
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
operations:
baph {-h --help}
baph {-u --update} [package(s)] [options]
baph {-i --install} <package(s)> [options]
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
options:
--noview Skip viewing the PKGBUILD for AUR packages and dependencies
--noconfirm Skip most confirmations when building also passed to pacman
2018-09-15 23:19:32 -05:00
EOF
}
2018-10-23 23:11:58 -05:00
#-- Utility functions --#
2018-11-07 20:47:47 -06:00
msg() { printf "$1\\e[0m \e[1m$2\e[0m\n"; }
2018-10-23 23:11:58 -05:00
die() { printf "\e[1m\e[31merror:\e[0m $1\n"; exit ${2:-1}; }
comp() { # compare AUR package versions, returns 0 or 1.. usage: comp "package" "current version"
NEW="$(curl --silent "$PKG_ADDR/$1" | awk '/Details:/ {sub(/<\/h.?>/, ""); print $4}')"
2018-11-07 20:47:47 -06:00
[[ $i =~ -git$ ]] && NEW="$(grep -ow '[0-9]*\.[0-9]*\.[0-9]*' <<< "$NEW")"
2018-10-23 23:11:58 -05:00
[[ -n $NEW && "$2" == "$NEW" ]] && return 0 || return 1
2018-09-15 23:19:32 -05:00
}
2018-10-23 23:11:58 -05:00
yesno() { # only shown when not using --noconfirm, returns 0 or 1.. usage: yesno "question"
2018-11-07 20:47:47 -06:00
[[ $NOCONF ]] && return 0
printf "\n\e[34m::\e[0m \e[1m$1? [Y/n]\e[0m "; read -r conf
[[ $conf =~ [nN] ]] && return 1 # return non zero when confirm n/N
2018-10-23 23:11:58 -05:00
return 0
2018-09-15 23:19:32 -05:00
}
2018-10-23 23:11:58 -05:00
#-- Update functions --#
prepupdate() { # prep for package update.. usage: prepupdate
(( ${#AUR_PKGS[@]} == 0 )) || update # exits here if passed packages to update
sudo pacman -Syyu $NOCONF || exit 1
AUR_PKGS=($(pacman -Qqm 2>/dev/null))
(( ${#AUR_PKGS[@]} == 0 )) && msg '\e[34m::' "No AUR packages installed.." || update
2018-09-15 23:19:32 -05:00
}
2018-10-23 23:11:58 -05:00
update() { # check update for each package in AUR_PKGS=().. usage: update
2018-11-07 20:47:47 -06:00
local ver
2018-10-23 23:11:58 -05:00
local up=()
msg '\e[34m::' "Synchronizing AUR package versions..."
for i in "${AUR_PKGS[@]}"; do
ver="$(pacman -Qs "^$i$" 2>/dev/null | awk 'NR==1 {print $2}')"
2018-11-07 20:47:47 -06:00
[[ $i =~ -git$ ]] && ver="$(grep -ow '[0-9]*\.[0-9]*\.[0-9]*' <<< "$ver")"
2018-10-23 23:11:58 -05:00
if [[ $ver ]]; then
2018-11-07 20:47:47 -06:00
printf "\e7 $i-$ver \e[1m\e[33m?\e[0m\e8"
comp "$i" "$ver" && printf " $i-$ver \e[1m\e[32m✓\e[0m\n" ||
{ up+=("$i"); printf " $i-$ver \e[1m\e[33m*\e[0m\n"; }
2018-10-23 23:11:58 -05:00
fi
done
msg '\e[34m::' "Starting AUR package upgrade..."
if (( ${#up[@]} > 0 )); then
printf "\n\e[1mPackages (${#up[@]})\e[0m %s\n\n" "${up[@]}"
2018-11-07 20:47:47 -06:00
for i in "${up[@]}"; do getpkg "$i" 0; done
2018-09-15 23:19:32 -05:00
else
2018-10-23 23:11:58 -05:00
printf " there is nothing to do\n"
2018-09-15 23:19:32 -05:00
fi
exit 0
}
2018-10-23 23:11:58 -05:00
#-- Install functions --#
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
prepinstall() { # loop package arrays if set and install each.. usage: prepinstall
(( ${#PAC_PKGS[@]} == 0 && ${#AUR_PKGS[@]} == 0 )) && die "no targets specified" 2
(( ${#PAC_PKGS[@]} > 0 )) && sudo pacman -S ${PAC_PKGS[*]} $NOCONF
for pkg in "${AUR_PKGS[@]}"; do
response="$(curl --silent --head "$PKG_ADDR/$pkg" | awk 'NR==1 {print $2}')"
(( response == 200 )) || die "$response response from $PKG_ADDR/$pkg"
getpkg "$pkg" || msg '\e[33m::' "Exiting $pkg build early"
done
}
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
getpkg() { # install an AUR package.. usage: getpkg "package"
local bd="$BUILD_DIR/$1"
local pkgbuild="$bd/PKGBUILD"
2018-09-15 23:19:32 -05:00
2018-11-07 20:47:47 -06:00
(( $# > 1 )) && comp "$1" && msg '\e[1m'$'\e[33mwarning:' "$1-$OLD is up to date -- reinstalling"
2018-10-23 23:11:58 -05:00
(( CACHE == 1 )) || rm -rf "$bd" >/dev/null 2>&1
[[ -d $bd ]] || mkdir -p "$bd"
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
if [[ ! -e $pkgbuild || $CACHE -eq 0 ]]; then
git clone "$AUR_ADDR/$1" "$bd" || die "failed to clone $AUR_ADDR/$1"
elif [[ -e $pkgbuild ]]; then
2018-11-07 20:47:47 -06:00
cd "$bd" && git pull || die "failed to pull from $AUR_ADDR/$1"
2018-10-23 23:11:58 -05:00
fi
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
[[ -e $pkgbuild ]] && cd "$bd" || die "$bd doesn't contain a PKGBUILD"
view "$1" "$pkgbuild" && { yesno "Continue building $1" || return 1; }
buildpkg "$1" "$pkgbuild"
return 0
}
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
view() { # view PKGBUILD.. usage: view "package" "/path/to/PKGBUILD"
2018-11-07 20:47:47 -06:00
[[ $NOVIEW ]] && return 0
yesno "View the PKGBUILD for $1" || return 0
if hash nvimpager >/dev/null 2>&1; then
nvimpager "$2"
elif [[ $PAGER ]]; then
$PAGER "$2"
else
less -P " -- Viewing PKGBUILD for $1 -- (press q to exit)" "$2"
2018-09-15 23:19:32 -05:00
fi
}
2018-10-23 23:11:58 -05:00
buildpkg() { # build package.. usage: buildpkg "package" "/path/to/PKGBUILD"
2018-11-07 20:47:47 -06:00
local depends=()
local validpgpkeys=()
2018-10-23 23:11:58 -05:00
eval "$(cat "$BUILD_DIR/$1/PKGBUILD")"
(( ${#validpgpkeys[@]} > 0 )) && importkeys ${validpgpkeys[*]}
(( ${#depends[@]} > 0 )) && builddeps "$1" ${depends[*]}
makepkg -sicr
}
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
importkeys() { # import PGP keys from package.. usage: importkeys ${KEYS[@]}
for key in "$@"; do
pacman-key --list-keys | grep -q "$key" && continue
msg '\e[33m::' "resolving missing key $key"
gpg --receive-keys "$key"
sudo pacman-key -r "$key" && sudo pacman-key --lsign-key "$key"
2018-09-15 23:19:32 -05:00
done
2018-10-23 23:11:58 -05:00
}
2018-09-15 23:19:32 -05:00
2018-10-23 23:11:58 -05:00
builddeps() { # build package depends.. usage: builddeps "package" ${DEPENDS[@]}
local pdir="$1"; shift
for i in "$@"; do
depend="$(sed 's/[=<>]=\?[0-9]*\?\.\?[0-9]*\?\.\?[0-9]*\?//g' <<< "$i")"
available="$(pacman -Ssq "^$depend$" 2>/dev/null)"
[[ $available ]] && continue
msg '\e[33m::' "resolving AUR dependency -> $depend"
getpkg "$depend" || die "failed to build dependency $depend"
done
2018-11-07 20:47:47 -06:00
cd "$BUILD_DIR/$pdir" || die "failed to cd $BUILD_DIR/$pdir"
2018-10-23 23:11:58 -05:00
}
#-- Argument parsing & execution --#
pkg_args() { # parse additional command line arguments.. usage: pkg_args $@
for i in "$@"; do case $i in
--noview) NOVIEW="--noview" ;;
--noconfirm) NOCONF="--noconfirm" ;;
--*|-[a-z]) die "invalid option -- '$i'" ;;
*) pacman -Ssq "^$i$" >/dev/null 2>&1 && PAC_PKGS+=("$i") || AUR_PKGS+=("$i")
esac done
}
# catch Ctrl-C and exit
trap 'die "^C caught.."' SIGINT
if (( $# == 0 )); then
die "no operation specified (use -h for help)" 2
elif ! hash pacman git curl >/dev/null 2>&1; then
die "this script requires the following packages installed: pacman git curl"
else
for arg in "$@"; do case $arg in
--help|-h) usage && exit 0 ;;
--update|-u) shift; pkg_args "$@"; prepupdate; break ;;
--install|-i) shift; pkg_args "$@"; prepinstall; break ;;
*) die "invalid option -- '$arg'"
esac done
[[ $CACHE -eq 0 && -d $BUILD_DIR ]] && rm -rf "$BUILD_DIR" >/dev/null 2>&1
fi