018268d2e54ea61c301ed899eeffff34bc4d83ed
[pkgusr] / etc / pkgusr / zsh / zsh-pkgtools
1 # -*- Shell-script -*-
2 # Copyright (C) 2007 - 2010 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         pkgrepo [PKG] <t> -- print the source repo location of PKG
23                              with optional 2nd arg non-nil, also print
24                              repo type.
25         pkgsu [PKGUSR] -- switch to user PKGUSR.
26         pkg_install [DESCRIPTION] [USER] [GROUP]
27                 -- install a new package, DESCRIPTION must be quoted.
28         pkg_ldconfig  -- Run ldconfig.
29
30         xtar [FILE]   -- extract tarball FILE.  sets tar(1) options
31                          based on file type.
32         vtar [FILE]   -- view the contents of tarball FILE.  sets tar(1)
33                          options based on file type.
34         ctar [FILE] [DIRECTORY]
35                       -- create a tarball FILE of DIRECTORY.  Compression
36                          is automatically chosen from the filename.
37
38
39 EOF
40 }
41
42 fpkg () 
43 {
44         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
45                 echo Invalid or missing argument >&2
46                 echo "Usage: $0 [REGEXP]" >&2
47                 return 1
48         else
49                 sed -n '/^install:/,$p' /etc/group|cut -d: -f1| \
50                   grep -i --colour ${argv[1]}
51         fi
52 }
53
54 ppkg () 
55 {
56         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
57                 echo Invalid or missing argument >&2
58                 echo "Usage: $0 [NAME]" >&2
59                 return 1
60         else
61                 pinky -l $argv[1]|less
62         fi
63 }
64
65 cpkg ()
66 {
67         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
68                 echo Invalid or missing argument >&2
69                 echo "Usage: $0 [CMD]" >&2
70                 return 1
71         else
72                 find $path -type f -regex "^.*/$argv[1].*$" \
73                   -printf "%f -- (%u:%g)\n"|grep --colour '(.*)'
74         fi
75 }
76
77 lpkg () 
78 {
79         sed -n '/^install/p' /etc/group|cut -d: -f4|tr ',' '\n'|less
80 }
81
82 upkg()
83 {
84         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
85                 echo Invalid or missing argument >&2
86                 echo "Usage: $0 [PKG]" >&2
87                 return 1
88         else
89                 echo -n "$argv[1]... "
90                 pinky -l $argv[1]|grep 'Last_Updated:.*'| \
91                   sed 's/^      //'|grep --colour '.*'
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$(pinky -l ${pkg}|grep Last_Updated|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                 echo -n "$argv[1]... "
110                 pinky -l $argv[1]|grep 'Version:.*'| \
111                   sed 's/^      //'|grep --colour '.*'
112         fi
113 }
114
115 ipkg()
116 {
117         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
118                 echo Invalid or missing argument >&2
119                 echo "Usage: $0 [PKG]" >&2
120                 return 1
121         else
122                 local top=$(pinky -l $argv[1]|grep -n "Install Notes"|cut -d: -f1)
123                 local bot=$(pinky -l $argv[1]|grep -n "General Notes"|cut -d: -f1)
124                 pinky -l $argv[1]|sed -n ${top},${bot}p
125         fi
126 }
127                
128 gpkg()
129 {
130         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
131                 echo Invalid or missing argument >&2
132                 echo "Usage: $0 [PKG]" >&2
133                 return 1
134         else
135                 local top=$(pinky -l $argv[1]|grep -n "General Notes"|cut -d: -f1)
136                 local bot=$(pinky -l $argv[1]|grep -n "CONTENTS"|cut -d: -f1)
137                 pinky -l $argv[1]|sed -n ${top},${bot}p
138         fi
139 }
140
141 wpkg()
142 {
143         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
144                 echo Invalid or missing argument >&2
145                 echo "Usage: $0 [PKG]" >&2
146                 return 1
147         else
148                 pinky -l $argv[1]|grep --colour 'Web_Site:.*$'
149         fi
150 }
151
152 pkgrepo()
153 {
154         if [[ $ARGC -lt 1 || $ARGC -gt 2 ]]; then
155                 echo Invalid or missing argument >&2
156                 echo "Usage: $0 [PKG] <t>" >&2
157                 return 1
158         else
159                 pinky -l $argv[1]|grep --colour 'Repo_Location:.*$'
160                 [[ -n "$argv[2]" ]] && pinky -l $argv[1]|grep --colour 'Repo_Type:.*$'
161         fi
162 }
163
164 xtar()
165 {
166         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
167                 echo Invalid or missing argument >&2
168                 echo "Usage: $0 [FILE]" >&2
169                 return 1
170         fi
171
172         local opts
173         local type
174         local fname=$argv[1]
175
176         type=$(file ${fname}|cut -d' ' -f2)
177
178         case $type in
179                 (tar)   opts=xf ;;
180                 (gzip)  opts=zxf ;;
181                 (bzip2) opts=jxf ;;
182                 (xz)    opts=Jxf ;;
183                 (*)
184                         # try lzma
185                         if lzmainfo ${fname} &>/dev/null; then
186                                 opts=(--lzma -xf)
187                         else
188                                 printf "Unknown file type: %s\n" $type >&2
189                                 return 2
190                         fi
191                         ;;
192         esac
193
194         tar ${opts} ${fname}
195 }
196
197 vtar()
198 {
199         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
200                 echo Invalid or missing argument >&2
201                 echo "Usage: $0 [FILE]" >&2
202                 return 1
203         fi
204
205         local opts
206         local type
207         local fname=$argv[1]
208
209         type=$(file ${fname}|cut -d' ' -f2)
210
211         case $type in
212                 (tar)   opts=tvvvf ;;
213                 (gzip)  opts=ztvvvf ;;
214                 (bzip2) opts=jtvvvf ;;
215                 (xz)    opts=Jtvvvf ;;
216                 (*)
217                         # lzma.  Here because lzmainfo is too stupid
218                         if lzmainfo ${fname} &>/dev/null; then
219                                 opts=(--lzma -tvvvf)
220                         else
221                                 printf "Unknown file type: %s\n" $type >&2
222                                 return 2
223                         fi
224                         ;;
225         esac
226
227         tar ${opts} ${fname}|less
228 }
229
230 ctar()
231 {
232         if [[ $ARGC -ne 2 ]]; then
233                 echo Invalid or missing argument >&2
234                 echo "Usage: $0 [FILE] [DIRECTORY]" >&2
235                 return 1
236         fi
237
238         local opts
239         opts=(--create --owner=0 --group=0 --auto-compress --file)
240
241         tar ${opts} $1 $2
242 }
243
244 pkgsu()
245 {
246         if [[ $ARGC -lt 1 || $ARGC -gt 1 ]]; then
247                 echo Invalid or missing argument >&2
248                 echo "Usage: $0 [PKGUSR]" >&2
249                 return 1
250         else
251                 ssh -l root localhost -t su $argv[1]
252         fi
253 }
254
255 pkg_install()
256 {
257         if [[ $ARGC -lt 3 || $ARGC -gt 3 ]]; then
258                 echo Invalid or missing argument >&2
259                 echo "Usage: $0 [DESCRIPTION] [USER] [GROUP]" >&2
260                 return 1
261         else
262                 ssh -l root localhost -t install_package \
263                   ${argv[1]} $argv[2] $argv[3]
264         fi
265 }
266
267 alias pkg_ldconfig='ssh -l root localhost -t ldconfig'
268
269 ### End
270