2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
9 # Copyright 2024 Ian Kelling
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
15 # http://www.apache.org/licenses/LICENSE-2.0
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
23 # basic yum/apt package manager abstraction, plus a few minor conveniences
24 if command -v yum
&> /dev
/null
; then
27 local s
; [[ $EUID != 0 ]] && s
=sudo
32 local s
; [[ $EUID != 0 ]] && s
=sudo
33 $s yum
-y install "$@"
37 local s
; [[ $EUID != 0 ]] && s
=sudo
40 # package remove/uninstall
42 local s
; [[ $EUID != 0 ]] && s
=sudo
43 $s yum autoremove
"$@"
46 local s
; [[ $EUID != 0 ]] && s
=sudo
47 $s yum
-y distro-sync full
"$@"
54 yum whatprovides \
*/$1
57 elif command -v apt-get
&>/dev
/null
; then
61 while fuser
/var
/lib
/dpkg
/lock
&>/dev
/null
; do
64 if (( i
> 300 )); then
65 echo "error: timed out waiting for /var/lib/dpkg/lock" >&2
72 if [[ $1 == -* ]]; then
78 if dpkg
-s -- "$@" |
& grep -Fx "Status: install ok installed" &>/dev
/null
; then
83 pp
() { # package policy
87 local s
; [[ $EUID != 0 ]] && s
=sudo
90 $s apt-get
--purge --auto-remove "$@"
98 local now t s f cachetime limittime
; [[ $EUID != 0 ]] && s
=sudo
99 # update package list if its more than an 2 hours old
100 f
=/var
/cache
/apt
/pkgcache.bin
102 cachetime
=$
(stat
-c %Y
$f )
107 limittime
=$
(( now
- 60*60*2 ))
108 for f
in /etc
/apt
/sources.list
/etc
/apt
/sources.list.d
/*.list
; do
111 if (( t
> limittime
)); then
116 if (( cachtime
> limittime
)); then
121 pcheck
"$@" ||
return 0
123 if [[ $
- != *i
* ]]; then
126 if [[ $EUID == 0 ]]; then
127 DEBIAN_FRONTEND
=noninteractive apt-get
-y install --purge --auto-remove "$@"
129 sudo DEBIAN_FRONTEND
=noninteractive apt-get
-y install --purge --auto-remove "$@"
136 pcheck
"$@" ||
return 0
139 local f
=/usr
/sbin
/policy-rc.d
140 if [[ $
- != *i
* ]]; then
143 if [[ $EUID == 0 ]]; then
144 dd of
=$f status
=none
<<EOF
149 DEBIAN_FRONTEND
=noninteractive apt-get
-y install --purge --auto-remove "$@" || ret
=$?
152 sudo
dd of
=$f status
=none
<<EOF
157 sudo DEBIAN_FRONTEND
=noninteractive apt-get
-y install --purge --auto-remove "$@" || ret
=$?
162 # package find description
164 # package name and descriptions
165 apt-cache search
"$@"
169 local s
; [[ $EUID != 0 ]] && s
=sudo
170 # nice aptitude search from emacs shell. package description width as
171 # wide as the screen, and package name field small aptitude
172 # manual can't figure out how wide emacs terminal is, of course
173 # it doesn't consult the $COLUMNS variable... and in a normal
174 # terminal, it makes the package name field ridiculously big
175 # also, remove that useless dash before the description
176 aptitude
-F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search
"$@"
179 local s
; [[ $EUID != 0 ]] && s
=sudo
182 if dpkg
-s -- "$arg" &>/dev
/null
; then
189 $s apt-get
-y remove
--purge --auto-remove "$@"
190 # seems slightly redundant, but it removes more stuff sometimes.
191 $s apt-get
-y autoremove
196 local s
; [[ $EUID != 0 ]] && s
=sudo
197 $s apt-get
-y dist-upgrade
--purge --auto-remove "$@"
198 $s apt-get
-y autoremove
202 if type -p aptitude
&>/dev
/null
; then
209 # -a = search all repos
219 # ucfq can tell us about config files which are not tracked
220 # with apt-file. but, for at least a few files I tested
221 # which are tracked with apt-file, ucfq doesn't show their
222 # package name. So, commenting this, waiting to find
223 # a config file only tracked by ucfq to see if it gives the
224 # package name and if I can identify this kind of file.
225 # if [[ $file == /* ]] && ! ucfq -w $file | grep ::: &>/dev/null; then
228 if [[ $file == /* ]]; then
232 arg
=(--filter-origins "$(positive-origins)")
234 if [[ $file == /* ]]; then
235 apt-file
"${arg[@]}" find -x /"$file"\$
236 update-alternatives
--list "$file" 2>/dev
/null
238 apt-file
"${arg[@]}" find -x "$file"\$
243 if dpkg
-s "$1" &>/dev
/null
; then
244 dpkg-query
-L $1 |
while read -r l
; do [[ -f $l ]] && printf "%s\n" "$l"; done
246 apt-file
-x list
"^$1$"
250 elif command -v pacman
&>/dev
/null
; then
255 pacaur
-S --noconfirm --needed --noedit "$@"
261 pacaur
-Rs --noconfirm "$@"
262 if p
=$
(pacaur
-Qdtq); then
268 aur
='https://aur.archlinux.org'
269 curl
-s $aur/$
(curl
-s "$aur/rpc.php?type=info&arg=$p" \
270 | jq
-r .results.URLPath
) |
tar xz
275 local s
; [[ $EUID != 0 ]] && s
=sudo
277 curl
-s "https://www.archlinux.org/mirrorlist/\
278 ?country=US&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" \
279 |
sed -r 's/^[ #]*(Server *=)/\1/' > $x
280 if (( $
(stat
-c %s
$x ) > 10 )); then
281 $s cp $x /etc
/pacman.d
/mirrorlist
286 local s
; [[ $EUID != 0 ]] && s
=sudo
287 # file_time + 24 hours > current_time
288 if ! (( $
(stat
-c%Y
/etc
/pacman.d
/mirrorlist
) + 60*60*24 > $
(date +%s
) ))
292 pacaur
-Syu --noconfirm "$@"
302 if pacaur
-Qs "^$1$" &>/dev
/null
; then