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