OSDN Git Service

[update] : Updated to 3.3.0
[alterlinux/aptpac.git] / aptpac
diff --git a/aptpac b/aptpac
index 986e507..af9f085 100755 (executable)
--- a/aptpac
+++ b/aptpac
@@ -20,8 +20,8 @@
 
 
 set -e
-APTPAC_VERSION="3.2.1"
-PACMAN_OPTIONS=
+APTPAC_VERSION="3.3.0"
+PACMAN_OPTIONS=()
 HELPERS=(
     "/usr/bin/yay"
     "/usr/bin/paru"
@@ -35,9 +35,10 @@ HELPERS=(
 DEFAULT_PACMAN_COMMAND="/usr/bin/pacman"
 PACMAN_COMMAND="${DEFAULT_PACMAN_COMMAND}"
 PACMAN_CONFIG="/etc/pacman.conf"
+DEBUG=false
+RUN_WITH_SUDO=false
 
 direct_option=false
-debug=false
 autoremove=false
 
 _msg_error () {
@@ -45,14 +46,14 @@ _msg_error () {
 }
 
 _msg_debug () {
-    if ${debug}; then
+    if "${DEBUG}"; then
         echo -e "${@}" >&1
     fi
 }
 
 if [[ ! "${UID}" = 0 ]]; then
     if [[ -z "${AURHELPER+SET}" ]]; then
-        for AURHELPER in ${HELPERS[@]}; do
+        for AURHELPER in "${HELPERS[@]}"; do
             if [[ -f "${AURHELPER}" ]]; then
                 PACMAN_COMMAND="${AURHELPER}"
                 break
@@ -61,9 +62,6 @@ if [[ ! "${UID}" = 0 ]]; then
                 break
             fi
         done
-        if [[ "${PACMAN_COMMAND}" == "${DEFAULT_PACMAN_COMMAND}" ]]; then
-            PACMAN_COMMAND="sudo ${DEFAULT_PACMAN_COMMAND}"
-        fi
     else
         if [[ -f "${AURHELPER}" ]]; then
             PACMAN_COMMAND="${AURHELPER}"
@@ -93,15 +91,16 @@ _usage () {
     echo "    search                               Search for a package"
     echo "    autoremove                           Remove unnecessary packages"
     echo "    clean                                Remove the package cache"
-    echo "    list                                 Displays a list of packages"
-    echo "    show                                 Displays the package records"
+    echo "    list                                 Display a list of packages"
+    echo "    show | showpkg                       Display the package records"
+    echo "    rdepends                             Display the dependencies"
     echo
     echo " apt options:"
     echo "    -y | --yes  | --assume-yes           Do not check"
     echo "    -d | --download-only                 Only download the package"
     echo "    -c | --config-file <file>            Config file for pacman"
     echo "    -h | --help                          Display this help"
-    echo "    -v | --version                       Displays the version of aptpac and pacman"
+    echo "    -v | --version                       Display the version of aptpac and pacman"
     echo "         --auto-remove | --autoremove    Remove unnecessary packages with other command"
     echo "         --purge                         Delete the entire configuration file"
     echo
@@ -114,7 +113,7 @@ _usage () {
 }
 
 _exit () {
-    exit ${1}
+    exit "${1}"
 }
 
 _version () {
@@ -129,12 +128,12 @@ pacman --version
 
 
 ADD_OPTION () {
-    PACMAN_OPTIONS="${PACMAN_OPTIONS} ${@}"
+    PACMAN_OPTIONS+=("${@}")
 }
 
 # Argument analysis and processing
 set +e
-PACAPT_ARGUMENTS="${@}"
+PACAPT_ARGUMENTS=("${@}")
 _opt_short="ydfc:hvVDFQRSTU"
 _opt_long="yes,assume-yes,download-only,fix-broken,purse,installed,debug,help,version,config-file:,auto-remove,autoremove,aur-helper:"
 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}" 2> /dev/null)
@@ -148,7 +147,7 @@ eval set -- "${OPT}"
 unset OPT _opt_short _opt_long
 
 while true; do
-    case ${1} in
+    case "${1}" in
         -y | --yes | --assume-yes)
             ADD_OPTION "--noconfirm"
             shift 1
@@ -173,7 +172,7 @@ while true; do
             shift 1
             ;;
         --debug)
-            debug=true
+            DEBUG=true
             shift 1
             ;;
         -h | --help)
@@ -205,7 +204,7 @@ while true; do
             ;;
         -V | -D | -F | -Q | -R | -S | -T | -U)
             direct_option=true
-            ADD_OPTION "${PACAPT_ARGUMENTS}"
+            ADD_OPTION "${PACAPT_ARGUMENTS[@]}"
             break
             ;;
         --)
@@ -262,7 +261,7 @@ if [[ "${direct_option}" = false ]]; then
             ;;
         edit-sources)
             if [[ -n "${EDITOR}" ]]; then
-                sudo ${EDITOR} "${PACMAN_CONFIG}"
+                sudo "${EDITOR}" "${PACMAN_CONFIG}"
             else
                 sudo nano "${PACMAN_CONFIG}"
             fi
@@ -290,7 +289,9 @@ if [[ "${direct_option}" = false ]]; then
         autoremove) 
             if [[ -n "$(${PACMAN_COMMAND} -Qttdq)" ]]; then
                 ADD_OPTION "-Rsc"
-                PACKAGE=($(${PACMAN_COMMAND} -Qttdq))
+                while read -r pkg; do
+                    PACKAGE+=("${pkg}")
+                done < <(${PACMAN_COMMAND} -Qttdq)
             else
                 echo "No packages to remove"
                 exit 0
@@ -303,16 +304,20 @@ if [[ "${direct_option}" = false ]]; then
                 ADD_OPTION "-Ss"
             fi
             ;;
-        show)
+        show | showpkg)
             for pkg in "${PACKAGE[@]}"; do
                 if pacman -Qq "${pkg}" 2> /dev/null 1>&2; then
-                    ${PACMAN_COMMAND} ${PACMAN_OPTIONS} -Qi --config "${PACMAN_CONFIG}" "${pkg}"
+                    ${PACMAN_COMMAND} "${PACMAN_OPTIONS[@]}" -Qi --config "${PACMAN_CONFIG}" "${pkg}"
                 else
-                    ${PACMAN_COMMAND} ${PACMAN_OPTIONS} -Si --config "${PACMAN_CONFIG}" "${pkg}"
+                    ${PACMAN_COMMAND} "${PACMAN_OPTIONS[@]}" -Si --config "${PACMAN_CONFIG}" "${pkg}"
                 fi
             done
+            unset pkg
             _exit 0
             ;;
+        rdepends)
+            ADD_OPTION "-Sii"
+            ;;
         *)
             _msg_error "Invalid command '${COMMAND}'"
             _exit 1
@@ -320,16 +325,25 @@ if [[ "${direct_option}" = false ]]; then
     esac
 fi
 
-if [[ ! "${PACMAN_COMMAND}" = "pacman" ]] && [[ ! "${PACMAN_COMMAND}" = "sudo pacman" ]]; then
+if [[ ! "${PACMAN_COMMAND}" = "${DEFAULT_PACMAN_COMMAND}" ]] && [[ ! "$(basename "${PACMAN_COMMAND}")" = "$(basename "${DEFAULT_PACMAN_COMMAND}")" ]]; then
     _msg_debug "Use AUR helper ${PACMAN_COMMAND}"
+else
+    RUN_WITH_SUDO=true
 fi
 
-# echo "${PACMAN_COMMAND} ${PACMAN_OPTIONS} ${PACKAGE}"
-${PACMAN_COMMAND} ${PACMAN_OPTIONS} --config "${PACMAN_CONFIG}" "${PACKAGE[@]}"
+_sudo(){
+    if "${RUN_WITH_SUDO}"; then
+        eval sudo "${@}"
+    else
+        eval "${@}"
+    fi
+}
+
+_sudo "${PACMAN_COMMAND}" "${PACMAN_OPTIONS[@]}" --config "${PACMAN_CONFIG}" "${PACKAGE[@]}"
 
-if ${autoremove}; then
+if "${autoremove}"; then
     if [[ -n $(${PACMAN_COMMAND} -Qttdq) ]]; then
-        ${PACMAN_COMMAND} -Rsc --config "${PACMAN_CONFIG}" $(${PACMAN_COMMAND} -Qttdq)
+        "${PACMAN_COMMAND}" -Qttdq | _sudo "${PACMAN_COMMAND}" -Rsc --config "${PACMAN_CONFIG}" -
     else
         echo "No packages to remove"
         exit 0