OSDN Git Service

[update] : Fully customizable AUR helper
[alterlinux/alterlinux.git] / system / aur.sh
index dc5fe01..2eaa284 100755 (executable)
@@ -9,9 +9,58 @@
 set -e -u
 
 aur_username="aurbuild"
+pacman_debug=false
+pacman_args=()
+failedpkg=()
+remove_list=()
+aur_helper_depends=("go")
+aur_helper_command="yay"
+aur_helper_package="yay"
+aur_helper_args=()
+pkglist=()
 
 trap 'exit 1' 1 2 3 15
 
+_help() {
+    echo "usage ${0} [option] [aur helper args] ..."
+    echo
+    echo "Install aur packages with ${aur_helper_command}" 
+    echo
+    echo " General options:"
+    echo "    -a [command]             Set the command of aur helper"
+    echo "    -c                       Enable pacman debug message"
+    echo "    -e [pkg]                 Set the package name of aur helper"
+    echo "    -d [pkg1,pkg2...]        Set the oackage of the depends of aur helper"
+    echo "    -p [pkg1,pkg2...]        Set the AUR package to install"
+    echo "    -u [user]                Set the user name to build packages"
+    echo "    -x                       Enable bash debug message"
+    echo "    -h                       This help message"
+}
+
+while getopts "a:cd:e:p:u:xh" arg; do
+    case "${arg}" in
+        a) aur_helper="${OPTARG}" ;;
+        c) pacman_debug=true ;;
+        e) aur_helper_command="${OPTARG}" ;;
+        p) readarray -t pkglist < <(tr "," "\n" <<< "${OPTARG}") ;;
+        d) readarray -t aur_helper_depends < <(tr "," "\n" <<< "${OPTARG}") ;;
+        u) aur_username="${OPTARG}" ;;
+        x) set -xv ;;
+        h) 
+            _help
+            exit 0
+            ;;
+        *)
+            _help
+            exit 1
+            ;;
+    esac
+done
+
+shift "$((OPTIND - 1))"
+aur_helper_args=("${@}")
+eval set -- "${pkglist[@]}"
+
 # Show message when file is removed
 # remove <file> <file> ...
 remove() {
@@ -31,7 +80,7 @@ if ! user_check "${aur_username}"; then
 fi
 mkdir -p "/aurbuild_temp"
 chmod 700 -R "/aurbuild_temp"
-chown ${aur_username}:${aur_username} -R "/aurbuild_temp"
+chown "${aur_username}:${aur_username}" -R "/aurbuild_temp"
 echo "${aur_username} ALL=(ALL) NOPASSWD:ALL" > "/etc/sudoers.d/aurbuild"
 
 # Setup keyring
@@ -41,56 +90,86 @@ pacman-key --populate
 # Un comment the mirror list.
 #sed -i "s/#Server/Server/g" "/etc/pacman.d/mirrorlist"
 
-# Install yay
-if ! pacman -Qq yay 1> /dev/null 2>&1; then
-    (
-        _oldpwd="$(pwd)"
-        pacman -Syy --noconfirm --config "/etc/alteriso-pacman.conf"
-        pacman --noconfirm -S --asdeps --needed go --config "/etc/alteriso-pacman.conf"
-        sudo -u "${aur_username}" git clone "https://aur.archlinux.org/yay.git" "/tmp/yay"
-        cd "/tmp/yay"
-        sudo -u "${aur_username}" makepkg --ignorearch --clean --cleanbuild --force --skippgpcheck --noconfirm
-        pacman --noconfirm --config "/etc/alteriso-pacman.conf" -U $(sudo -u "${aur_username}" makepkg --packagelist)
-        cd ..
-        rm -rf "/tmp/yay"
-        cd "${_oldpwd}"
-    )
+# Set pacman args
+pacman_args=("--config" "/etc/alteriso-pacman.conf" "--noconfirm")
+if [[ "${pacman_debug}" = true ]]; then
+    pacman_args+=("--debug")
 fi
 
-if ! type -p yay > /dev/null; then
-    echo "Failed to install yay"
+# Install
+if ! pacman -Qq "${aur_helper_package}" 1> /dev/null 2>&1; then
+    # Update database
+    _oldpwd="$(pwd)"
+    pacman -Syy "${pacman_args[@]}"
+
+    # Install depends
+    for _pkg in "${aur_helper_depends[@]}"; do
+        if ! pacman -Qq "${_pkg}" > /dev/null 2>&1 | grep -q "${_pkg}"; then
+            # --asdepsをつけているのでaur.shで削除される --neededをつけているので明示的にインストールされている場合削除されない
+            pacman -S --asdeps --needed "${pacman_args[@]}" "${_pkg}"
+            #remove_list+=("${_pkg}")
+        fi
+    done
+
+    # Build
+    sudo -u "${aur_username}" git clone "https://aur.archlinux.org/${aur_helper_package}.git" "/tmp/${aur_helper_package}"
+    cd "/tmp/${aur_helper_package}"
+    sudo -u "${aur_username}" makepkg --ignorearch --clean --cleanbuild --force --skippgpcheck --noconfirm
+
+    # Install
+    for _pkg in $(sudo -u "${aur_username}" makepkg --packagelist); do
+        pacman "${pacman_args[@]}" -U "${_pkg}"
+    done
+
+    # Remove debtis
+    cd ..
+    remove "/tmp/${aur_helper_package}"
+    cd "${_oldpwd}"
+fi
+
+if ! type -p "${aur_helper_command}" > /dev/null; then
+    echo "Failed to install ${aur_helper_package}"
     exit 1
 fi
 
+installpkg(){
+    yes | sudo -u "${aur_username}" \
+        "${aur_helper_command}" -Sy \
+            --color always \
+            --cachedir "/var/cache/pacman/pkg/" \
+            "${pacman_args[@]}" \
+            "${aur_helper_args[@]}" \
+            "${@}" || true
+}
+
 
 # Build and install
 chmod +s /usr/bin/sudo
 for _pkg in "${@}"; do
-    yes | sudo -u "${aur_username}" \
-        yay -Sy \
-            --mflags "-AcC" \
-            --aur \
-            --noconfirm \
-            --nocleanmenu \
-            --nodiffmenu \
-            --noeditmenu \
-            --noupgrademenu \
-            --noprovides \
-            --removemake \
-            --useask \
-            --color always \
-            --mflags "--skippgpcheck" \
-            --config "/etc/alteriso-pacman.conf" \
-            --cachedir "/var/cache/pacman/pkg/" \
-            "${_pkg}"
+    pacman -Qq "${_pkg}" > /dev/null 2>&1  && continue
+    installpkg "${_pkg}"
 
     if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
         echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
+        failedpkg+=("${_pkg}")
+    fi
+done
+
+# Reinstall failed package
+for _pkg in "${failedpkg[@]}"; do
+    installpkg "${_pkg}"
+    if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
+        echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
         exit 1
     fi
 done
 
-yay -Sccc --noconfirm --config "/etc/alteriso-pacman.conf"
+# Remove packages
+readarray -t -O "${#remove_list[@]}" remove_list < <(pacman -Qttdq)
+(( "${#remove_list[@]}" != 0 )) && pacman -Rsnc "${remove_list[@]}" "${pacman_args[@]}"
+
+# Clean up
+"${aur_helper_command}" -Sccc "${pacman_args[@]}"
 
 # remove user and file
 userdel "${aur_username}"