A truck load of updates/fixes/tweaks
[pkgusr] / etc / pkgusr / zsh / zsh-pkgtools
1 # -*- Shell-script -*-
2 # Copyright (C) 2007 - 2013 Steve Youngs <steve@steveyoungs.com>
3 #
4 #  A collection of Zsh functions that I use day to day for maintaining
5 #  and administering my machines.  These come in very handy if you set up
6 #  one of your users as a "package manager".  There is a corresponding
7 #  _zsh-pkgtools file that provides completion for most of what is here.
8 H-pkg ()
9 {
10         cat<<EOF
11
12         fpkg [REGEXP] -- finds pkg names matching REGEXP.                      
13         ppkg [NAME]   -- print pkg NAME pkg info.  
14         cpkg [CMD]    -- find what package provides CMD.     
15         lpkg          -- list installed packages.
16         Lpkg          -- list installed packages with dates last updated.
17         upkg [PKG]    -- date/time the last time PKG was updated.
18         vpkg [PKG]    -- print the version of PKG.
19         ipkg [PKG]    -- print install notes of PKG.
20         gpkg [PKG]    -- print general notes of PKG.
21         wpkg [PKG]    -- print PKG website URL.
22         dpkg [PKG]    -- print PKG dependencies.
23         pkgwant [PKG] -- print a list of packages that depend on PKG
24         pkgrepo [PKG] <t> -- print the source repo location of PKG
25                              with optional 2nd arg non-nil, also print
26                              repo type.
27         pkgsu [PKGUSR] -- switch to user PKGUSR.
28         pkg_install [DESCRIPTION] [USER] [GROUP]
29                 -- install a new package, DESCRIPTION must be quoted.
30         pkg_ldconfig  -- Run ldconfig.
31
32         xtar [FILE]   -- extract tarball FILE.  sets tar(1) options
33                          based on file type.
34         vtar [FILE]   -- view the contents of tarball FILE.  sets tar(1)
35                          options based on file type.
36         ctar [FILE] [DIRECTORY]
37                       -- create a tarball FILE of DIRECTORY.  Compression
38                          is automatically chosen from the filename.
39
40
41 EOF
42 }
43
44 fpkg () 
45 {
46         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
47                 echo Invalid or missing argument >&2
48                 echo "Usage: $0 [REGEXP]" >&2
49                 return 1
50         else
51                 sed -n '/^install:/,$p' /etc/group|cut -d: -f1| \
52                   grep -i --colour ${argv[1]}
53         fi
54 }
55
56 ppkg () 
57 {
58         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
59                 echo Invalid or missing argument >&2
60                 echo "Usage: $0 [NAME]" >&2
61                 return 1
62         else
63                 pinky -l $argv[1]|less
64         fi
65 }
66
67 cpkg ()
68 {
69         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
70                 echo Invalid or missing argument >&2
71                 echo "Usage: $0 [CMD]" >&2
72                 return 1
73         else
74                 find $path -type f -regex "^.*/$argv[1].*$" \
75                   -printf "%f -- (%u:%g)\n"|grep --colour '(.*)'
76         fi
77 }
78
79 lpkg () 
80 {
81         sed -n '/^install/p' /etc/group|cut -d: -f4|tr ',' '\n'|less
82 }
83
84 upkg()
85 {
86         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
87                 echo Invalid or missing argument >&2
88                 echo "Usage: $0 [PKG]" >&2
89                 return 1
90         else
91                 grep --colour 'Last_Updated:.*' ~$argv[1]/.project
92         fi
93 }
94
95 Lpkg ()
96 {
97         for pkg in $(sed -n '/^install/p' /etc/group|cut -d: -f4|tr ',' '\n'); do
98                 printf "${pkg}\t\t\t$(upkg ${pkg}|cut -d' ' -f3-)\n"
99         done|less
100 }
101
102 vpkg()
103 {
104         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
105                 echo Invalid or missing argument >&2
106                 echo "Usage: $0 [PKG]" >&2
107                 return 1
108         else
109                 grep --colour 'Version: ' ~$argv[1]/.project
110         fi
111 }
112
113 ipkg()
114 {
115         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
116                 echo Invalid or missing argument >&2
117                 echo "Usage: $0 [PKG]" >&2
118                 return 1
119         else
120                 local top=$(grep -n "Install Notes" ~$argv[1]/.project|cut -d: -f1)
121                 local bot=$(grep -n "General Notes" ~$argv[1]/.project|cut -d: -f1)
122                 sed -n ${top},${bot}p  ~$argv[1]/.project
123         fi
124 }
125                
126 gpkg()
127 {
128         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
129                 echo Invalid or missing argument >&2
130                 echo "Usage: $0 [PKG]" >&2
131                 return 1
132         else
133                 local top=$(grep -n "General Notes" ~$argv[1]/.project|cut -d: -f1)
134                 local bot=$(grep -n "CONTENTS" ~$argv[1]/.project|cut -d: -f1)
135                 sed -n ${top},${bot}p ~$argv[1]/.project
136         fi
137 }
138
139 dpkg()
140 {
141         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
142                 echo Invalid or missing argument >&2
143                 echo "Usage: $0 [PKG]" >&2
144                 return 1
145         else
146                 grep --colour 'Deps: ' ~$argv[1]/.project
147         fi
148 }
149
150 wpkg()
151 {
152         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
153                 echo Invalid or missing argument >&2
154                 echo "Usage: $0 [PKG]" >&2
155                 return 1
156         else
157                 grep --colour 'Web_Site: ' ~$argv[1]/.project
158         fi
159 }
160
161 pkgrepo()
162 {
163         if [[ $ARGC -lt 1 || $ARGC -gt 2 ]]; then
164                 echo Invalid or missing argument >&2
165                 echo "Usage: $0 [PKG] <t>" >&2
166                 return 1
167         else
168                 grep --colour 'Repo_Location: ' ~$argv[1]/.project
169                 [[ -n "$argv[2]" ]] && grep --colour 'Repo_Type: ' ~$argv[1]/.project
170         fi
171 }
172
173 pkgwant()
174 {
175         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
176                 echo Invalid or mission argument >&2
177                 echo "Usage: $0 [PKG]" >&2
178                 return 1
179         fi
180
181         for p in $(lpkg); do
182                 dpkg ${p} | grep -wq $argv[1] && print ${p}
183         done
184 }
185
186 xtar()
187 {
188         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
189                 echo Invalid or missing argument >&2
190                 echo "Usage: $0 [FILE]" >&2
191                 return 1
192         fi
193
194         local opts
195         local type
196         local fname=$argv[1]
197
198         type=$(file ${fname}|cut -d' ' -f2)
199
200         case $type in
201                 (tar)   opts=xf ;;
202                 (gzip)  opts=zxf ;;
203                 (bzip2) opts=jxf ;;
204                 (xz|XZ) opts=Jxf ;;
205                 (*)
206                         # try lzma
207                         if lzmainfo ${fname} &>/dev/null; then
208                                 opts=(--lzma -xf)
209                         else
210                                 printf "Unknown file type: %s\n" $type >&2
211                                 return 2
212                         fi
213                         ;;
214         esac
215
216         tar ${opts} ${fname}
217 }
218
219 vtar()
220 {
221         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
222                 echo Invalid or missing argument >&2
223                 echo "Usage: $0 [FILE]" >&2
224                 return 1
225         fi
226
227         local opts
228         local type
229         local fname=$argv[1]
230
231         type=$(file ${fname}|cut -d' ' -f2)
232
233         case $type in
234                 (tar)   opts=tvvvf ;;
235                 (gzip)  opts=ztvvvf ;;
236                 (bzip2) opts=jtvvvf ;;
237                 (xz|XZ) opts=Jtvvvf ;;
238                 (*)
239                         # lzma.  Here because lzmainfo is too stupid
240                         if lzmainfo ${fname} &>/dev/null; then
241                                 opts=(--lzma -tvvvf)
242                         else
243                                 printf "Unknown file type: %s\n" $type >&2
244                                 return 2
245                         fi
246                         ;;
247         esac
248
249         tar ${opts} ${fname}|less
250 }
251
252 ctar()
253 {
254         if [[ $ARGC -ne 2 ]]; then
255                 echo Invalid or missing argument >&2
256                 echo "Usage: $0 [FILE] [DIRECTORY]" >&2
257                 return 1
258         fi
259
260         local opts
261         opts=(--create --owner=0 --group=0 --auto-compress --file)
262
263         tar ${opts} $1 $2
264 }
265
266 pkgsu()
267 {
268         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
269                 echo Invalid or missing argument >&2
270                 echo "Usage: $0 [PKGUSR]" >&2
271                 return 1
272         else
273                 ssh -l root localhost -t su $argv[1]
274         fi
275 }
276
277 pkg_install()
278 {
279         if [[ $ARGC -lt 3 || $ARGC -gt 3 ]]; then
280                 echo Invalid or missing argument >&2
281                 echo "Usage: $0 [DESCRIPTION] [USER] [GROUP]" >&2
282                 return 1
283         else
284                 ssh -l root localhost -t install_package \
285                   ${argv[1]} $argv[2] $argv[3]
286         fi
287 }
288
289 alias pkg_ldconfig='ssh -l root localhost ldconfig'
290
291 ### End
292