OSDN Git Service

[fix] : Fixed deb file path
[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 set -e
22
23
24 # APTPAC Version
25 APTPAC_VERSION="3.3.0"
26
27 # Initilize List
28 PACMAN_PACKAGE_FILE=()
29 PACMAN_PACKAGE=()
30 DEB_PACKAGE_FILE=()
31
32 # PACMAN Config
33 HELPERS=(
34     "/usr/bin/yay"
35     "/usr/bin/paru"
36     "/usr/bin/yaourt"
37     "/usr/bin/aurman"
38     "/usr/bin/pikaur"
39     "/usr/bin/pacaur"
40     "/usr/bin/aura"
41     "/usr/bin/wfa"
42 )
43 PACMAN_OPTIONS=()
44 DEFAULT_PACMAN_COMMAND="/usr/bin/pacman"
45 PACMAN_COMMAND="${DEFAULT_PACMAN_COMMAND}"
46 PACMAN_CONFIG="/etc/pacman.conf"
47
48 # APTPAC Config
49 DEBTAP_WORK="/tmp/aptpac-debtap"
50 DEBUG=false
51 RUN_WITH_SUDO=false
52 DIRECT_PACMAN=false
53 AUTOREMOVE=false
54 INSTALL=false
55
56 _msg_error () {
57     echo -e "${@}" >&2
58 }
59
60 _msg_warn () {
61     echo -e "${@}" >&2
62 }
63
64 _msg_debug () {
65     if "${DEBUG}"; then
66         echo -e "${@}" >&1
67     fi
68 }
69
70
71 # List option
72 INSTALLED_PKGLIST=false
73
74 _usage () {
75     echo "usage ${0} [options] [command] [packages]"
76     echo
77     echo " apt commands:"
78     echo "    install                              Install the specified package"
79     echo "    remove                               Remove the specified package"
80     echo "    purge                                Permanently remove the package"
81     echo "    update                               Update package database"
82     echo "    upgrade | full-upgrade               Update packages"
83     echo "    edit-sources                         Edit config file of pacman"
84     echo "    search                               Search for a package"
85     echo "    autoremove                           Remove unnecessary packages"
86     echo "    clean                                Remove the package cache"
87     echo "    list                                 Display a list of packages"
88     echo "    show | showpkg                       Display the package records"
89     echo "    rdepends                             Display the dependencies"
90     echo
91     echo " apt options:"
92     echo "    -y | --yes  | --assume-yes           Do not check"
93     echo "    -d | --download-only                 Only download the package"
94     echo "    -c | --config-file <file>            Config file for pacman"
95     echo "    -h | --help                          Display this help"
96     echo "    -v | --version                       Display the version of aptpac and pacman"
97     echo "         --auto-remove | --autoremove    Remove unnecessary packages with other command"
98     echo "         --purge                         Delete the entire configuration file"
99     echo
100     echo " pacapt options:"
101     echo "         --aur-helper <command>          Specifies the command to use as the AUR helper"
102     echo "                                         Ignored if pacapt is run as root"
103     echo "                                         Specify AUR helper that supports common command line options with pacman"
104     echo "         --pflags <flags>                Pass arguments to pacman"
105     echo
106     echo " Notes:"
107     echo "    - aptpac supports not only above options but also options of pacman"
108     echo "    - If you specify the deb file, you can install using debtap"
109 }
110
111 _exit () {
112     exit "${1}"
113 }
114
115 _version () {
116 cat << EOF
117 aptpac ${APTPAC_VERSION} - A pacman wrapper with syntax based on debian's apt
118 License: Do What The Fuck You Want To Public License
119 (c) 2019-2020 Fascode Network. Yamada Hayao
120 EOF
121     echo
122     pacman --version
123 }
124
125 ADD_OPTION () {
126     _msg_debug "Added pacman option '${*}'"
127     PACMAN_OPTIONS+=("${@}")
128 }
129
130 _sudo(){
131     if "${RUN_WITH_SUDO}"; then
132         _msg_debug "Run sudo ${*}"
133         eval sudo "${@}"
134     else
135         _msg_debug "Run ${*}"
136         eval "${@}"
137     fi
138 }
139
140 _pacman(){
141     _sudo "${PACMAN_COMMAND}" "${PACMAN_OPTIONS[@]}" --config "${PACMAN_CONFIG}" "${@}"
142 }
143
144 _run_detect_aur_helper(){
145     if [[ ! "${UID}" = 0 ]]; then
146         if [[ -z "${AURHELPER+SET}" ]]; then
147             for AURHELPER in "${HELPERS[@]}"; do
148                 if [[ -f "${AURHELPER}" ]]; then
149                     PACMAN_COMMAND="${AURHELPER}"
150                     break
151                 elif hash "$(basename "${AURHELPER}")" 2> /dev/null; then
152                     PACMAN_COMMAND="$(basename "${AURHELPER}")"
153                     break
154                 fi
155             done
156         else
157             if [[ -f "${AURHELPER}" ]]; then
158                 PACMAN_COMMAND="${AURHELPER}"
159             elif hash "$(basename "${AURHELPER}")" 2> /dev/null; then
160                 PACMAN_COMMAND="$(basename "${AURHELPER}")"
161             else
162                 _msg_error "${AURHELPER} is not installed"
163                 exit 1
164             fi
165         fi
166     fi
167 }
168
169 _run_aur_message(){
170     if [[ ! "${PACMAN_COMMAND}" = "${DEFAULT_PACMAN_COMMAND}" ]] && [[ ! "$(basename "${PACMAN_COMMAND}")" = "$(basename "${DEFAULT_PACMAN_COMMAND}")" ]]; then
171         _msg_debug "Use AUR helper ${PACMAN_COMMAND}"
172     else
173         RUN_WITH_SUDO=true
174     fi
175 }
176
177 _run_autoremove(){
178     if "${AUTOREMOVE}"; then
179         if [[ -n $(${PACMAN_COMMAND} -Qttdq) ]]; then
180             "${PACMAN_COMMAND}" -Qttdq | _sudo "${PACMAN_COMMAND}" -Rsc --config "${PACMAN_CONFIG}" -
181         else
182             echo "No packages to remove"
183             exit 0
184         fi
185     fi
186 }
187
188 _run_distinguish_package(){
189     local pkg
190     for pkg in "${PACKAGE[@]}"; do
191         if [[ ! -f "${pkg}" ]]; then
192             PACMAN_PACKAGE+=("${pkg}")
193         elif [[ "$(file -b --mime-type "${pkg}" 2> /dev/null)" = "application/vnd.debian.binary-package" ]]; then
194             DEB_PACKAGE_FILE+=("${pkg}")
195         else
196             PACMAN_PACKAGE_FILE+=("${pkg}")
197         fi
198     done
199 }
200
201 _run_pacman(){
202     if [[ "${INSTALL}" = true ]]; then
203         _pacman -S "${PACMAN_PACKAGE[@]}"
204     else
205         _pacman "${PACMAN_PACKAGE[@]}"
206     fi
207 }
208
209 _run_debtap(){
210     _msg_warn "It is not recommended to install deb package"
211     _msg_warn "The package conversion is not perfect and can lead to errors in some cases"
212
213     if (( "${#DEB_PACKAGE_FILE[@]}" != 0 )) && ! hash "debtap"; then
214         _msg_error "debtap was not found"
215         exit 1
216     fi
217
218     if [[ -z "$(find "/var/cache/pkgfile" -maxdepth 1 -mindepth 1 -name "*.files" 2> /dev/null)" ]] || [[ -z "$(find "/var/cache/debtap" -maxdepth 1 -mindepth 1 -name "*-packages" 2> /dev/null)" ]] || [[ -z "$(find "/var/cache/debtap" -maxdepth 1 -mindepth 1 -name "*-files" 2> /dev/null)" ]]; then
219         _msg_debug "Updating debtap datebase"
220         sudo debtap -u
221     fi
222
223     (
224         sudo mkdir -p "${DEBTAP_WORK}"
225         local pkg work
226         for pkg in "${DEB_PACKAGE_FILE[@]}"; do
227             work="${DEBTAP_WORK}/$(basename "${pkg}")/"
228             file="${work}/$(basename "${pkg}")"
229             _msg_debug "Work dir: ${work}"
230             _msg_debug "Deb file:${work}"
231             sudo mkdir -p "${work}"
232             sudo cp "${pkg}" "${file}"
233             cd "${work}"
234             sudo debtap --Quiet "${file}"
235             while read -r archpkg; do
236                 _msg_debug "Install ${archpkg} with pacman"
237                 _pacman -U "${archpkg}"
238             done < <(find "${work}" -maxdepth 1 -mindepth 1 -type f -name "*.pkg.tar.*")
239         done
240     )
241 }
242
243
244 # Argument analysis and processing
245 set +e
246 PACAPT_ARGUMENTS=("${@}")
247 _opt_short="ydfc:hvVDFQRSTU"
248 _opt_long="yes,assume-yes,download-only,fix-broken,purse,installed,debug,help,version,config-file:,auto-remove,autoremove,aur-helper:,pflags:"
249 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}" 2> /dev/null)
250 set -e
251 #if [[ ${?} != 0 ]]; then
252 #    exit 1
253 #fi
254
255 eval set -- "${OPT}"
256 #echo "Argument is \"${OPT}\""
257 unset OPT _opt_short _opt_long
258
259 while true; do
260     case "${1}" in
261         -y | --yes | --assume-yes)
262             ADD_OPTION "--noconfirm"
263             shift 1
264             ;;
265         -d | --download-only)
266             ADD_OPTION "-w"
267             shift 1
268             ;;
269         -f | --fix-broken)
270             shift 1
271             ;;
272         -c | --config-file)
273             PACMAN_CONFIG="${2}"
274             shift 2
275             ;;
276         --purge)
277             ADD_OPTION "-n"
278             shift 1
279             ;;
280         --installed)
281             INSTALLED_PKGLIST=true
282             shift 1
283             ;;
284         --debug)
285             DEBUG=true
286             shift 1
287             ;;
288         -h | --help)
289             _usage
290             shift 1
291             exit 0
292             ;;
293         -v | --version)
294             _version
295             shift 1
296             exit 0
297             ;;
298         --autoremove | --auto-remove)
299             AUTOREMOVE=true
300             shift 1
301             ;;
302         --aur-helper)
303             if [[ ! "${UID}" = 0 ]] && [[ -f "${2}" ]]; then
304                 PACMAN_COMMAND="${2}"
305             elif [[ "${UID}" = 0 ]]; then
306                 _msg_warn "The specified AUR helper is not used because pacapt is running on the root."
307             elif hash "$(basename "${2}")" 2> /dev/null; then
308                 PACMAN_COMMAND="$(basename "${2}")"
309             else
310                 _msg_error "${2} is not installed"
311                 exit 1
312             fi
313             shift 2
314             ;;
315         --pflags)
316             # shellcheck disable=SC2086
317             ADD_OPTION ${2}
318             shift 2
319             ;;
320         -V | -D | -F | -Q | -R | -S | -T | -U)
321             DIRECT_PACMAN=true
322             ADD_OPTION "${PACAPT_ARGUMENTS[@]}"
323             break
324             ;;
325         --)
326             shift 1
327             break
328             ;;
329         *)
330             _msg_error "Invalid argument '${1}'"
331             _help
332             exit 1
333             ;;
334     esac
335 done
336
337 if [[ "${DIRECT_PACMAN}" = false ]]; then
338     if [[ $# -lt 1 ]]; then
339         _msg_error "No command specified"
340         _usage
341         _exit 1
342     fi
343     COMMAND="${1,,}"
344
345     shift 1
346
347     PACKAGE=("${@}")
348
349     case "${COMMAND}" in
350         install)
351             #ADD_OPTION "-S"
352             INSTALL=true
353             ;;
354         remove)
355             ADD_OPTION "-Rsc"
356             ;;
357         purge)
358             ADD_OPTION "-Rsnc"
359             ;;
360         update)
361             ADD_OPTION "-Syy"
362             ;;
363         upgrade)
364             ADD_OPTION "-Syu"
365             ;;
366         search)
367             ADD_OPTION "-Ss"
368             ;;
369         full-upgrade)
370             ADD_OPTION "-Syu"
371             ;;
372         clean)
373             ADD_OPTION "-Sccc"
374             ;;
375         dist-upgrade)
376             ADD_OPTION "-Syu"
377             ;;
378         edit-sources)
379             if [[ -n "${EDITOR}" ]]; then
380                 sudo "${EDITOR}" "${PACMAN_CONFIG}"
381             else
382                 sudo nano "${PACMAN_CONFIG}"
383             fi
384             _exit 0
385             ;;
386         huawei) 
387             # shellcheck disable=SC1091
388             if [[ "$(source "/etc/locale.conf" 2> /dev/null; echo -n "${LANG}")" = "ja_JP.UTF-8" ]]; then
389                 echo "(ง •ᴗ•)ว ⁾⁾ファーウェイでウェイウェイ"
390                 _exit 0
391             else
392                 _msg_error "Invalid command '${COMMAND}'"
393                 _exit 1
394             fi
395             ;;
396         moo)
397             echo "         (__) "
398             echo "         (oo) "
399             echo "   /------\/ "
400             echo "  / |    ||   "
401             echo " *  /\---/\ "
402             echo "    ~~   ~~   "
403             echo "....\"Have you mooed today?\"..."
404             exit 0
405             ;;
406         autoremove) 
407             if [[ -n "$(${PACMAN_COMMAND} -Qttdq)" ]]; then
408                 ADD_OPTION "-Rsc"
409                 while read -r pkg; do
410                     PACKAGE+=("${pkg}")
411                 done < <(${PACMAN_COMMAND} -Qttdq)
412             else
413                 echo "No packages to remove"
414                 exit 0
415             fi
416             ;;
417         list)
418             if [[ "${INSTALLED_PKGLIST}" = true ]]; then
419                 ADD_OPTION "-Q | grep"
420             else
421                 ADD_OPTION "-Ss"
422             fi
423             ;;
424         show | showpkg)
425             for pkg in "${PACKAGE[@]}"; do
426                 if pacman -Qq "${pkg}" 2> /dev/null 1>&2; then
427                     "${PACMAN_COMMAND}" "${PACMAN_OPTIONS[@]}" -Qi --config "${PACMAN_CONFIG}" "${pkg}"
428                 else
429                     "${PACMAN_COMMAND}" "${PACMAN_OPTIONS[@]}" -Si --config "${PACMAN_CONFIG}" "${pkg}"
430                 fi
431             done
432             unset pkg
433             _exit 0
434             ;;
435         rdepends)
436             ADD_OPTION "-Sii"
437             ;;
438         help)
439             _usage
440             exit 0
441             ;;
442         *)
443             _msg_error "Invalid command '${COMMAND}'"
444             _exit 1
445             ;;
446     esac
447 fi
448
449
450 _run_detect_aur_helper
451 _run_aur_message
452 _run_distinguish_package
453 _run_pacman
454 if [[ "${INSTALL}" = true ]]; then
455     _run_debtap  
456 fi
457 _run_autoremove