OSDN Git Service

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