OSDN Git Service

[update] : Supported --aur-helper
[alterlinux/aptpac.git] / aptpac
1 #!/usr/bin/env bash
2 #
3 #               __
4 #              /\ \__
5 #   __    _____\ \ ,_\ _____     __      ___
6 # /'__`\ /\ '__`\ \ \//\ '__`\ /'__`\   /'___\
7 #/\ \L\.\\ \ \L\ \ \ \\ \ \L\ /\ \L\.\_/\ \__/
8 #\ \__/.\_\ \ ,__/\ \__\ \ ,__\ \__/.\_\ \____\
9 # \/__/\/_/\ \ \/  \/__/\ \ \/ \/__/\/_/\/____/
10 #           \ \_\        \ \_\
11 #            \/_/         \/_/
12 # a pacman wrapper with syntax based on debian's apt
13 # (c) 2019-2020 Fascode Network.
14 # License: Do What The Fuck You Want To Public License
15 # maintained by Yamada Hayao
16
17 # Enter the path to the AUR helper you want to use here. 
18 # If it is empty, the corresponding AUR helper will be searched automatically.
19 AURHELPER=
20
21
22
23 set -e
24 APTPAC_VERSION="3.1.0"
25 PACMAN_OPTIONS=
26 HELPERS=(
27     "/usr/bin/yay"
28     "/usr/bin/yaourt"
29     "/usr/bin/aurman"
30     "/usr/bin/pikaur"
31     "/usr/bin/pacaur"
32     "/usr/bin/aura"
33 )
34 DEFAULT_PACMAN_COMMAND="/usr/bin/pacman"
35 debug=false
36 PACMAN_COMMAND="${DEFAULT_PACMAN_COMMAND}"
37 PACMAN_CONFIG="/etc/pacman.conf"
38 autoremove=false
39
40 _msg_error () {
41     echo -e "${@}" >&2
42 }
43
44 _msg_debug () {
45     if ${debug}; then
46         echo -e "${@}" >&1
47     fi
48 }
49
50 if [[ ! "${UID}" = 0 ]]; then
51     if [[ -z "${AURHELPER}" ]]; then
52         for AURHELPER in ${HELPERS[@]}; do
53             if [[ -f "${AURHELPER}" ]]; then
54                 PACMAN_COMMAND="${AURHELPER}"
55                 break
56             elif hash "$(basename "${AURHELPER}")" 2> /dev/null; then
57                 PACMAN_COMMAND="$(basename "${AURHELPER}")"
58                 break
59             fi
60         done
61         if [[ "${PACMAN_COMMAND}" == "${DEFAULT_PACMAN_COMMAND}" ]]; then
62             PACMAN_COMMAND="sudo ${DEFAULT_PACMAN_COMMAND}"
63         fi
64     else
65         if [[ -f "${AURHELPER}" ]]; then
66             PACMAN_COMMAND="${AURHELPER}"
67         elif hash "$(basename "${AURHELPER}")" 2> /dev/null; then
68             PACMAN_COMMAND="$(basename "${AURHELPER}")"
69         else
70             _msg_error "${AURHELPER} is not installed"
71             exit 1
72         fi
73     fi
74 fi
75
76
77 # List option
78 installed=false
79
80 _usage () {
81     echo "usage ${0} [options] [command] [packages]"
82     echo " apt commands:"
83     echo "    install                              Install the specified package"
84     echo "    remove                               Remove the specified package"
85     echo "    purge                                Permanently remove the package"
86     echo "    update                               Update package database"
87     echo "    upgrade | full-upgrade               Update packages"
88     echo "    edit-sources                         Edit config file of pacman"
89     echo "    search                               Search for a package"
90     echo "    autoremove                           Remove unnecessary packages"
91     echo "    clean                                Remove the package cache"
92     echo "    list                                 Displays a list of packages"
93     echo
94     echo " apt options:"
95     echo "    -y | --yes  | --assume-yes           Do not check"
96     echo "    -d | --download-only                 Only download the package"
97     echo "    -c | --config-file <file>            Config file for pacman"
98     echo "    -h | --help                          Display this help"
99     echo "    -v | --version                       Displays the version of aptpac and pacman"
100     echo "         --auto-remove | --autoremove    Remove unnecessary packages with other command"
101     echo "         --purge                         Delete the entire configuration file"
102     echo
103     echo " pacapt options:"
104     echo "         --aur-helper <command>          Specifies the command to use as the AUR helper"
105     echo "                                         Ignored if pacapt is run as root"
106     echo "                                         Specify AUR helper that supports common command line options with pacman"
107 }
108
109 _exit () {
110     exit ${1}
111 }
112
113 _version () {
114 cat << EOF
115 aptpac ${APTPAC_VERSION} - A pacman wrapper with syntax based on debian's apt
116 License: Do What The Fuck You Want To Public License
117 (c) 2019-2020 Fascode Network. Yamada Hayao
118 EOF
119 echo
120 pacman --version
121 }
122
123
124 ADD_OPTION () {
125     PACMAN_OPTIONS="${PACMAN_OPTIONS} ${@}"
126 }
127
128 # Argument analysis and processing
129 _opt_short="ydfc:hv"
130 _opt_long="yes,assume-yes,download-only,fix-broken,purse,installed,debug,help,version,config-file:,auto-remove,autoremove"
131 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
132 if [[ ${?} != 0 ]]; then
133     exit 1
134 fi
135
136 eval set -- "${OPT}"
137 #echo "Argument is \"${OPT}\""
138 unset OPT _opt_short _opt_long
139
140 while true; do
141     case ${1} in
142         -y | --yes | --assume-yes)
143             ADD_OPTION "--noconfirm"
144             shift 1
145             ;;
146         -d | --download-only)
147             ADD_OPTION "-w"
148             shift 1
149             ;;
150         -f | --fix-broken)
151             shift 1
152             ;;
153         -c | --config-file)
154             PACMAN_CONFIG="${2}"
155             shift 2
156             ;;
157         --purge)
158             ADD_OPTION "-n"
159             shift 1
160             ;;
161         --installed)
162             installed=true
163             shift 1
164             ;;
165         --debug)
166             debug=true
167             shift 1
168             ;;
169         -h | --help)
170             _usage
171             shift 1
172             exit 0
173             ;;
174         -v | --version)
175             _version
176             shift 1
177             exit 0
178             ;;
179         --autoremove | --auto-remove)
180             autoremove=true
181             shift 1
182             ;;
183         --aur-helper)
184             if [[ ! "${UID}" = 0 ]] && [[ -f "${2}" ]]; then
185                 PACMAN_COMMAND="${2}"
186             elif [[ "${UID}" = 0 ]]; then
187                 _msg_warn "The specified AUR helper is not used because pacapt is running on the root."
188             elif hash "$(basename "${2}")" 2> /dev/null; then
189                 PACMAN_COMMAND="$(basename "${2}")"
190             else
191                 _msg_error "${2} is not installed"
192                 exit 1
193             fi
194             ;;
195         --)
196             shift 1
197             break
198             ;;
199         *)
200             _msg_error "Invalid argument '${1}'"
201             _help
202             exit 1
203             ;;
204     esac
205 done
206
207 if [[ $# -lt 1 ]]; then
208     _msg_error "No command specified"
209     _usage
210     _exit 1
211 fi
212 COMMAND="${1}"
213
214 shift 1
215
216 PACKAGE="${@}"
217
218 case "${COMMAND}" in
219     install)
220         ADD_OPTION "-S"
221         ;;
222     remove)
223         ADD_OPTION "-Rsc"
224         ;;
225     purge)
226         ADD_OPTION "-Rsnc"
227         ;;
228     update)
229         ADD_OPTION "-Syy"
230         ;;
231     upgrade)
232         ADD_OPTION "-Syu"
233         ;;
234     search)
235         ADD_OPTION "-Ss"
236         ;;
237     full-upgrade)
238         ADD_OPTION "-Syu"
239         ;;
240     edit-sources)
241         if [[ -n "${EDITOR}" ]]; then
242             sudo ${EDITOR} "${PACMAN_CONFIG}"
243         else
244             sudo nano "${PACMAN_CONFIG}"
245         fi
246         _exit 0
247         ;;
248     dist-upgrade)
249         ADD_OPTION "-Syu"
250         ;;
251     huawei) 
252         source "/etc/locale.conf"
253         if [[ "${LANG}" = "ja_JP.UTF-8" ]]; then
254             echo "(ง •ᴗ•)ว ⁾⁾ファーウェイでウェイウェイ"
255             _exit 0
256         else
257             _msg_error "Invalid command '${COMMAND}'"
258             _exit 1
259         fi
260         ;;
261     moo)
262         echo "         (__) "
263         echo "         (oo) "
264         echo "   /------\/ "
265         echo "  / |    ||   "
266         echo " *  /\---/\ "
267         echo "    ~~   ~~   "
268         echo "...."Have you mooed today?"..."
269         exit 0
270         ;;
271     clean)
272         ADD_OPTION "-Sccc"
273         ;;
274     autoremove) 
275         if [[ -n $(${PACMAN_COMMAND} -Qttdq) ]]; then
276             ADD_OPTION "-Rsc"
277             PACKAGE="$(${PACMAN_COMMAND} -Qttdq)"
278          else
279             echo "No packages to remove"
280             exit 0
281         fi
282         ;;
283     list)
284         if ${installed}; then
285             ADD_OPTION "-Q | grep"
286         else
287             ADD_OPTION "-Ss"
288         fi
289         ;;
290     *)
291         _msg_error "Invalid command '${COMMAND}'"
292         _exit 1
293         ;;
294 esac
295
296 if [[ ! "${PACMAN_COMMAND}" = "pacman" ]] && [[ ! "${PACMAN_COMMAND}" = "sudo pacman" ]]; then
297     _msg_debug "Use AUR helper ${PACMAN_COMMAND}"
298 fi
299
300 # echo "${PACMAN_COMMAND} ${PACMAN_OPTIONS} ${PACKAGE}"
301 ${PACMAN_COMMAND} ${PACMAN_OPTIONS} --config "${PACMAN_CONFIG}" ${PACKAGE}
302
303 if ${autoremove}; then
304     if [[ -n $(${PACMAN_COMMAND} -Qttdq) ]]; then
305         ${PACMAN_COMMAND} -Rsc --config "${PACMAN_CONFIG}" $(${PACMAN_COMMAND} -Qttdq)
306     else
307         echo "No packages to remove"
308         exit 0
309     fi
310 fi