OSDN Git Service

[update] : Supported --config-file
[alterlinux/aptpac.git] / aptpac
diff --git a/aptpac b/aptpac
index b345f5d..493e9b3 100755 (executable)
--- a/aptpac
+++ b/aptpac
-#!/bin/bash
+#!/usr/bin/env bash
 #
-#               __                             
-#              /\ \__                          
-#   __    _____\ \ ,_\ _____     __      ___   
-# /'__`\ /\ '__`\ \ \//\ '__`\ /'__`\   /'___\ 
-#/\ \L\.\\ \ \L\ \ \ \\ \ \L\ /\ \L\.\_/\ \__/ 
+#               __
+#              /\ \__
+#   __    _____\ \ ,_\ _____     __      ___
+# /'__`\ /\ '__`\ \ \//\ '__`\ /'__`\   /'___\
+#/\ \L\.\\ \ \L\ \ \ \\ \ \L\ /\ \L\.\_/\ \__/
 #\ \__/.\_\ \ ,__/\ \__\ \ ,__\ \__/.\_\ \____\
 # \/__/\/_/\ \ \/  \/__/\ \ \/ \/__/\/_/\/____/
-#           \ \_\        \ \_\                 
-#            \/_/         \/_/    
+#           \ \_\        \ \_\
+#            \/_/         \/_/
 # a pacman wrapper with syntax based on debian's apt
-# (c) arcetera 2015 - wtfpl
-# maintained by alefir
-
-SYNTAX=$1
-INPUT=$2
-
-case $SYNTAX in
-        install)        sudo pacman -S $INPUT;;
-        search)         pacman -Ss $INPUT;;
-        remove)         sudo pacman -Rs $INPUT;;
-        upgrade)        sudo pacman -Syu;;
-        update)         sudo padman -Sy;
-                        echo "Run aptpac upgrade *immediately*. pacman does not support partial upgrades. running merely 'upgrade' would suffice. failure to do this could result in a broken installation.";;
-        download)       sudo pacman -Sw $INPUT;;
-        autoremove)     sudo pacman -Qdtq | pacman -Rs -;;
-        show)           padman -Qi $INPUT;;
-        clean)          sudo pacman -Sc;;
-        autoclean)      sudo pacman -Sc;;
-        policy)         less /etc/pacman.d/mirrorlist;;
-        list)           pacman -Q;;
-        listmore)       pacman -Qi;;
-        listless)       pacman -Q | wc -l;;
-        build)          makepkg -sri;;
-        *)              
-        echo "aptpac: a pacman wrapper with apt syntax";
-        echo "no argument/invalid argument - print this help";
-        echo "install - installs a package";
-        echo "search - searches for a package in the repos";
-        echo "remove - removes a package";
-        echo "upgrade - upgrades the system fully, refreshing repos and upgrading packages";
-        echo "update - only refreshes the repos (bad practice, do not run this without running 'upgrade' immediately after";
-        echo "download - only download a package into pacman's cache without installing it";
-        echo "autoremove - remove dependencies that are no longer needed (usually should not be needed as 'remove' should remove dependencies along with the package)";
-        echo "show - shows information about the package";
-        echo "clean/autoclean - clears pacman's cache";
-        echo "policy - prints mirrorlist";
-        echo "list - lists all installed packages";
-        echo "listmore - lists all installed packages with all info";
-        echo "listless - lists how many packages are installed";
-        echo "build - builds package from PKGBUILD";
+# (c) 2019-2020 Fascode Network.
+# License: Do What The Fuck You Want To Public License
+# maintained by Yamada Hayao
+
+# Enter the path to the AUR helper you want to use here. 
+# If it is empty, the corresponding AUR helper will be searched automatically.
+AURHELPER=
+
+
+
+set -e
+APTPAC_VERSION="3.0.1"
+PACMAN_OPTIONS=
+HELPERS=(
+    "/usr/bin/yay"
+    "/usr/bin/yaourt"
+    "/usr/bin/aurman"
+    "/usr/bin/pikaur"
+    "/usr/bin/pacaur"
+)
+DEFAULT_PACMAN_COMMAND="/usr/bin/pacman"
+debug=false
+PACMAN_COMMAND="${DEFAULT_PACMAN_COMMAND}"
+PACMAN_CONFIG="/etc/pacman.conf"
+
+_msg_error () {
+    echo -e "${@}" >&2
+}
+
+_msg_debug () {
+    if ${debug}; then
+        echo -e "${@}" >&1
+    fi
+}
+
+if [[ ! "${UID}" = 0 ]]; then
+    if [[ -z "${AURHELPER}" ]]; then
+        for AURHELPER in ${HELPERS[@]}; do
+            if [[ -f "${AURHELPER}" ]]; then
+                PACMAN_COMMAND="${AURHELPER}"
+                break
+            elif hash "$(basename "${AURHELPER}")" 2> /dev/null; then
+                PACMAN_COMMAND="$(basename "${AURHELPER}")"
+                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}"
+        elif hash "$(basename "${AURHELPER}")" 2> /dev/null; then
+            PACMAN_COMMAND="$(basename "${AURHELPER}")"
+        else
+            _msg_error "${AURHELPER} is not installed"
+            exit 1
+        fi
+    fi
+fi
+
+
+# List option
+installed=false
+
+_usage () {
+    echo "usage ${0} [options] [command] [packages]"
+    echo " commands:              "
+    echo "    install                       Install the specified package"
+    echo "    remove                        Remove the specified package"
+    echo "    purge                         Permanently remove the package"
+    echo "    update                        Update the package database"
+    echo "    upgrade                       Update the package"
+    echo "    full-upgrade                  Update packages and remove unnecessary packages"
+    echo "    edit-sources                  Edit /etc/pacman.conf"
+    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
+    echo " general 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 "         --purge                  Delete the entire configuration file"
+}
+
+_exit () {
+    exit ${1}
+}
+
+_version () {
+cat << EOF
+aptpac ${APTPAC_VERSION} - A pacman wrapper with syntax based on debian's apt
+License: Do What The Fuck You Want To Public License
+(c) 2019-2020 Fascode Network. Yamada Hayao
+EOF
+echo
+pacman --version
+}
+
+
+ADD_OPTION () {
+    PACMAN_OPTIONS="${PACMAN_OPTIONS} ${@}"
+}
+
+# Argument analysis and processing
+_opt_short="ydfc:hv"
+_opt_long="yes,assume-yes,download-only,fix-broken,purse,installed,debug,help,version,config-file:"
+OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
+if [[ ${?} != 0 ]]; then
+    exit 1
+fi
+
+eval set -- "${OPT}"
+#echo "Argument is \"${OPT}\""
+unset OPT _opt_short _opt_long
+
+while true; do
+    case ${1} in
+        -y | --yes | --assume-yes)
+            ADD_OPTION "--noconfirm"
+            shift 1
+            ;;
+        -d | --download-only)
+            ADD_OPTION "-w"
+            shift 1
+            ;;
+        -f | --fix-broken)
+            shift 1
+            ;;
+        -c | --config-file)
+            PACMAN_CONFIG="${2}"
+            shift 2
+            ;;
+        --purge)
+            ADD_OPTION "-n"
+            shift 1
+            ;;
+        --installed)
+            installed=true
+            shift 1
+            ;;
+        --debug)
+            debug=true
+            shift 1
+            ;;
+        -h | --help)
+            _usage
+            shift 1
+            exit 0
+            ;;
+        -v | --version)
+            _version
+            shift 1
+            exit 0
+            ;;
+        --)
+            shift 1
+            break
+            ;;
+        *)
+            _msg_error "Invalid argument '${1}'"
+            _help
+            exit 1
+            ;;
+    esac
+done
+
+if [[ $# -lt 1 ]]; then
+    _msg_error "No command specified"
+    _usage
+    _exit 1
+fi
+COMMAND="${1}"
+
+shift 1
+
+PACKAGE="${@}"
+
+case "${COMMAND}" in
+    install) ADD_OPTION "-S" ;;
+    remove) ADD_OPTION "-Rsc" ;;
+    purge) ADD_OPTION "-Rsnc" ;;
+    update) ADD_OPTION "-Syy" ;;
+    upgrade) ADD_OPTION "-Syu" ;;
+    search) ADD_OPTION "-Ss" ;;
+    full-upgrade) ADD_OPTION "-Syu" ;;
+    edit-sources) sudo nano /etc/pacman.conf; _exit 0 ;;
+    dist-upgrade) ADD_OPTION "-Syu" ;;
+    huawei) 
+        source "/etc/locale.conf"
+        if [[ "${LANG}" = "ja_JP.UTF-8" ]]; then
+            echo "(ง •ᴗ•)ว ⁾⁾ファーウェイでウェイウェイ"
+            _exit 0
+        else
+            _msg_error "Invalid command '${COMMAND}'"
+            _exit 1
+        fi
+        ;;
+    moo)
+cat << EOF
+         (__) 
+         (oo) 
+   /------\/ 
+  / |    ||   
+ *  /\---/\ 
+    ~~   ~~   
+...."Have you mooed today?"...
+EOF
+    exit 0
+    ;;
+    clean)
+        ADD_OPTION "-Sccc"
+        ;;
+    autoremove) if [[ -n $(${PACMAN_COMMAND} -Qttdq) ]]; then
+                    ADD_OPTION "=Rsc"
+                    PACKAGE="$(${PACMAN_COMMAND} -Qttdq)"
+                else
+                    echo "No packages to remove"
+                    exit 0
+                fi
+                ;;
+    list)
+        if ${installed}; then
+            ADD_OPTION "-Q | grep"
+        else
+            ADD_OPTION "-Ss"
+        fi
+        ;;
+    *)
+        _msg_error "Invalid command '${COMMAND}'"
+        _exit 1
+        ;;
 esac
+
+if [[ ! "${PACMAN_COMMAND}" = "pacman" ]] && [[ ! "${PACMAN_COMMAND}" = "sudo pacman" ]]; then
+    _msg_debug "Use AUR helper ${PACMAN_COMMAND}"
+fi
+
+# echo "${PACMAN_COMMAND} ${PACMAN_OPTIONS} ${PACKAGE}"
+${PACMAN_COMMAND} ${PACMAN_OPTIONS} --config "${PACMAN_CONFIG}" ${PACKAGE}