OSDN Git Service

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