Last active
March 10, 2018 17:14
-
-
Save wallabra/526d82e9d89f65720c3ceb719193c2cc to your computer and use it in GitHub Desktop.
Install AUR packages automatically.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| #------------ | |
| # This script installs Arch User Repository packages automatically. | |
| # Learn more here: | |
| # https://wiki.archlinux.org/index.php/Arch_User_Repository | |
| # | |
| # (C)2018 Gustavo R. Rehermann. The MIT License. | |
| #------------ | |
| BAD_DEP_EXTS=("so" "h" "dll" "la" "a") | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "You need to run this program with 'sudo' to be able to install AUR packages." | |
| exit 1 | |
| else | |
| if [ -z "$SUDO_USER" ]; then | |
| echo "You can't run the aur-install script as root, for security reasons." | |
| exit 1 | |
| fi | |
| fi | |
| if [ -z "$1" ]; then | |
| echo "Select a package to install!" | |
| echo "Syntax: aur-install <package>" | |
| exit 1 | |
| fi | |
| if [ "$(curl -o "/dev/null" --silent --head --write-out '%{http_code}\n' "https://aur.archlinux.org/cgit/aur.git/snapshot/$1.tar.gz")" == "404" ]; then | |
| echo "Error: Repository snapshot not found in the AUR! Are you sure it exists? Try using 'pacman -S $1' to check if it already exists in the official repos." | |
| exit 1 | |
| fi | |
| begindir="$(pwd)" | |
| printf "Preparing folders and permission stuff... " | |
| SUHOME="/home/$SUDO_USER" | |
| mkdir -p "/var/log/aurins" "/var/cache/aurins" >/dev/null 2>"/var/log/aurins/$1.log" || { | |
| echo "error starting up folders! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| chown -R "$SUDO_USER:$SUDO_USER" "$SUHOME/.aurins-build" >/var/log/aurins/$1.log 2>&1 || { | |
| echo "error disowning build folder from root! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| chmod -R 775 "$SUHOME/.aurins-build" >/var/log/aurins/$1.log 2>&1 || { | |
| echo "error changing build folder permissions! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| printf "done.\\nDownloading the latest snapshot for %s... " "$1" | |
| curl -s -o "/var/cache/aurins/$1.tar.gz" "https://aur.archlinux.org/cgit/aur.git/snapshot/$1.tar.gz" 2>"/var/log/aurins/$1.log" || { | |
| echo "error downloading the package! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| printf "done.\\nExtracting the snapshot file... " | |
| { sudo -u $SUDO_USER mkdir -p "$SUHOME/.aurins-build/$1" >/dev/null 2>"/var/log/aurins/$1.log"; } || { | |
| echo "error starting up folders! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| { sudo -u $SUDO_USER tar -xzf "/var/cache/aurins/$1.tar.gz" -C "$SUHOME/.aurins-build" >>"/var/log/aurins/$1.log" 2>&1; } || { | |
| echo "error extracting the package! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| echo "done." | |
| cd "$SUHOME/.aurins-build/$1" 2>>"/var/log/aurins/$1.log" || { | |
| echo "Can't cd to the extracted package directory! (Are you sure sudo is fine? Maybe he needs some medication...)" | |
| exit 3 | |
| } | |
| eval $(pcregrep -M "depends\=\(([\'\"].*?[\'\"];?\s*)*\)" "./PKGBUILD") | |
| test -v makedepends && { | |
| depends+=( "${makedepends[@]}" ) | |
| } | |
| if [ ${#depends[@]} -ne 0 ]; then | |
| pacdeps=() | |
| for dep in "${depends[@]}"; do | |
| ( for ext in "${BAD_DEP_EXTS[@]}"; do | |
| if [[ $dep == *.$ext ]]; then | |
| exit 1 | |
| fi | |
| pacman -Qi $dep >/dev/null 2>&1 && exit 1 | |
| done ) && pacdeps+=($dep) | |
| done | |
| if [ ${#pacdeps[@]} -ne 0 ]; then | |
| echo "Found dependencies:" | |
| for pd in "${pacdeps[@]}"; do | |
| echo " - $pd" | |
| done | |
| printf "Installing the package's dependencies... " | |
| pacman -S --noconfirm "${pacdeps[@]}" >>"/var/log/aurins/$1.log" 2>&1 || { | |
| echo "error installing dependencies! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| echo "done." | |
| fi | |
| fi | |
| printf "Building the package... " | |
| { sudo -u $SUDO_USER makepkg; } >>"/var/log/aurins/$1.log" 2>&1 || { | |
| echo "error building the package! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| printf "done.\\nInstalling the package..." | |
| { sudo -u $SUDO_USER makepkg --install; } >>"/var/log/aurins/$1.log" 2>&1 || { | |
| echo "error installing the package! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| { sudo -u $SUDO_USER makepkg --clean; } >>"/var/log/aurins/$1" 2>&1 || { | |
| echo "error cleaning up the package folder! Check '/var/log/aurins/$1.log' for more." | |
| exit 2 | |
| } | |
| printf "done.\\nInstalled everything succesfully." | |
| cd "$begindir" >/dev/null 2>&1 || { | |
| echo "Whoops! For some reason I couldn't cd back to your starting directory. Could you do it for me? Thanks!" | |
| } | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment