lots of new stuff, good working version
[distro-functions] / src / package-manager-abstractions
1 #!/bin/bash
2 # Copyright (C) 2014 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # basic yum/apt package manager abstraction, plus a few minor conveniences
17 if command -v yum &> /dev/null; then
18 # package manager
19 p() {
20 local s; [[ $EUID != 0 ]] && s=sudo
21 $s yum "$@"
22 }
23 # package install
24 pi() {
25 local s; [[ $EUID != 0 ]] && s=sudo
26 $s yum -y install "$@"
27 }
28 # package find
29 pf() {
30 local s; [[ $EUID != 0 ]] && s=sudo
31 $s yum search "$@"
32 }
33 # package remove/uninstall
34 pu() {
35 local s; [[ $EUID != 0 ]] && s=sudo
36 $s yum autoremove "$@"
37 }
38 pup() { # upgrade
39 local s; [[ $EUID != 0 ]] && s=sudo
40 $s yum -y distro-sync full "$@"
41 }
42 # package list info
43 pl() {
44 yum info "$@"
45 }
46 pfile() {
47 yum whatprovides \*/$1
48 }
49
50 elif command -v apt-get &>/dev/null; then
51 p() {
52 local s; [[ $EUID != 0 ]] && s=sudo
53 $s apt-get "$@"
54 }
55 pupdate() {
56 local s f; [[ $EUID != 0 ]] && s=sudo
57 # update package list if its more than an 2 hours old
58 f=/var/cache/apt/pkgcache.bin
59 if [[ ! -r $f ]] \
60 || (( $(( $(date +%s) - $(stat -c %Y $f ) )) > 60*60*2 )); then
61 $s apt-get update
62 fi
63 }
64 pi() {
65 pupdate
66 local s; [[ $EUID != 0 ]] && s=sudo
67 $s apt-get -y install "$@"
68 }
69 pi-nostart() {
70 local s; [[ $EUID != 0 ]] && s=sudo
71 local f=/usr/sbin/policy-rc.d
72 $s dd of=$f <<EOF
73 #!/bin/sh
74 exit 101
75 EOF
76 $s chmod +x $f
77 pi "$@"
78 $s rm $f
79 }
80 pf() {
81 # scratch a very annoying itch. package description width as
82 # wide as the screen, and package name field small aptitude
83 # manual can't figure out how wide emacs terminal is, of course
84 # it doesn't consult the $COLUMNS variable... and in a normal
85 # terminal, it makes the package name field ridiculously big
86 # also, remove that useless dash before the description
87 local s; [[ $EUID != 0 ]] && s=sudo
88 $s aptitude -F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search "$@"
89 }
90 pu() {
91 local s; [[ $EUID != 0 ]] && s=sudo
92 $s apt-get -y purge "$@"
93 $s apt-get -y autoremove
94 }
95 pup() { # upgrade
96 pupdate
97 local s; [[ $EUID != 0 ]] && s=sudo
98 $s apt-get -y dist-upgrade "$@"
99 $s apt-get -y autoremove
100 }
101 # package info
102 pl() {
103 aptitude show "$@"
104 }
105 pfile() {
106 if [[ $file == /* ]] && ucfq -w $file | grep -v ::: &>/dev/null; then
107 ucfq $file
108 elif [[ $file == */* ]]; then
109 apt-file find -x "$1"\$
110 else
111 apt-file find -x /"$1"\$
112 fi
113 }
114 pkgfiles() {
115 if dpkg -s "$1" &>/dev/null; then
116 dpkg-query -L $1
117 else
118 apt-file -x list "^$1$"
119 fi
120 }
121
122 elif command -v pacman &>/dev/null; then
123 p() {
124 pacaur "$@"
125 }
126 pi() {
127 pacaur -S --noconfirm --needed --noedit "$@"
128 }
129 pf() {
130 pacaur -Ss "$@"
131 }
132 pu() {
133 pacaur -Rs --noconfirm "$@"
134 if p=$(pacaur -Qdtq); then
135 pacaur -Rs $p
136 fi
137 }
138 aurex() {
139 p="$1"
140 aur='https://aur.archlinux.org'
141 curl -s $aur/$(curl -s "$aur/rpc.php?type=info&arg=$p" \
142 | jq -r .results.URLPath) | tar xz
143 cd "$p"
144
145 }
146 pmirror() {
147 local s; [[ $EUID != 0 ]] && s=sudo
148 local x=$(mktemp)
149 curl -s "https://www.archlinux.org/mirrorlist/\
150 ?country=US&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" \
151 | sed -r 's/^[ #]*(Server *=)/\1/' > $x
152 if (( $(stat -c %s $x ) > 10 )); then
153 $s cp $x /etc/pacman.d/mirrorlist
154 rm $x
155 fi
156 }
157 pup() { # upgrade
158 local s; [[ $EUID != 0 ]] && s=sudo
159 # file_time + 24 hours > current_time
160 if ! (( $(stat -c%Y /etc/pacman.d/mirrorlist) + 60*60*24 > $(date +%s) ))
161 then
162 pmirror
163 fi
164 pacaur -Syu --noconfirm "$@"
165 }
166 # package info
167 pl() {
168 pacaur -Si "$@"
169 }
170 pfile() {
171 pkgfile "$1"
172 }
173 pkgfiles() {
174 if pacaur -Qs "^$1$" &>/dev/null; then
175 pacman -Ql $1
176 else
177 pkgfile -l $1
178 fi
179 }
180 fi