A few more minor tweaks.
[pkgusr] / etc / pkgusr / handy_funcs
1 # -*- shell-script -*-
2 # Copyright (C) 2007 - 2014 Steve Youngs <steve@sxemacs.org>
3
4 # What lies here is a collection of handy bash shell functions and
5 # aliases that make life a little easier for pkgusr.
6
7 less-or-cat()
8 {
9     local arg=$1
10     local al=$(wc -l<${arg})
11     local sl=$(($LINES-2))
12
13     if [ $al -le $sl ]; then
14         cat ${arg}
15     else
16         less ${arg}
17     fi
18 }
19
20 verr()
21 {
22     local arg=$1
23     if [ -z "$arg" ]; then
24         arg=all
25     fi
26     case $arg in
27         (conf)    less-or-cat ${HOME}/configure.err ;;
28         (install) less-or-cat ${HOME}/install.err ;;
29         (check)   less-or-cat ${HOME}/check.err ;;
30         (make)    less-or-cat ${HOME}/make.err ;;
31         (upd)     less-or-cat ${HOME}/upd.err ;;
32         (ver)     less-or-cat ${HOME}/verupd.err ;;
33         (all)     less ${HOME}/*.err ;;
34     esac
35 }
36
37 vlog()
38 {
39     local arg=$1
40     if [ -z "$arg" ]; then
41         arg=all
42     fi
43     case $arg in
44         (conf)    less-or-cat ${HOME}/configure.log ;;
45         (install) less-or-cat ${HOME}/install.log ;;
46         (check)   less-or-cat ${HOME}/check.log ;;
47         (make)    less-or-cat ${HOME}/make.log ;;
48         (upd)     less-or-cat ${HOME}/upd.log ;;
49         (ver)     less-or-cat ${HOME}/verupd.log ;;
50         (all)     less ${HOME}/*.log ;;
51     esac
52 }
53
54 verrlog()
55 {
56     local arg=$1
57     if [ -z "$arg" ]; then
58         arg=all
59     fi
60     case $arg in
61         (conf)    less ${HOME}/configure.{err,log} ;;
62         (install) less ${HOME}/install.{err,log} ;;
63         (check)   less ${HOME}/check.{err,log} ;;
64         (make)    less ${HOME}/make.{err,log} ;;
65         (upd)     less ${HOME}/upd.{err,log} ;;
66         (ver)     less ${HOME}/verupd.{err,log} ;;
67         (all)     less ${HOME}/*.{err,log} ;;
68     esac
69 }
70
71 instg()
72 {
73     local arg=$1
74     if [ -z "$arg" ]; then
75         arg=err
76     fi
77     grep --color '^\*\*\*' ${HOME}/install.${arg}
78 }
79
80 dlog()
81 {
82     for log in configure make check install upd verupd; do
83         [[ -f ${HOME}/${log}.err ]] && rm -v ${HOME}/${log}.err
84         [[ -f ${HOME}/${log}.log ]] && rm -v ${HOME}/${log}.log
85     done
86 }
87
88 updver()
89 {
90     local arg=${1}
91     sed -i "s|\(Version: \).*$|\1${arg}|" ${HOME}/.project
92     echo -n "Version updated... "
93     grep --colour Version:.*$ ${HOME}/.project
94 }
95
96 showinst()
97 {
98     local top=$(grep -n "^Install Notes:$" ${HOME}/.project|cut -d: -f1)
99     local bot=$(grep -n "^General Notes:$" ${HOME}/.project|cut -d: -f1)
100     sed -n ${top},${bot}p ${HOME}/.project
101 }
102
103 showgen()
104 {
105     local top=$(grep -n "^General Notes:$" ${HOME}/.project|cut -d: -f1)
106     local bot=$(grep -n "^CONTENTS:$" ${HOME}/.project|cut -d: -f1)
107     sed -n ${top},${bot}p ${HOME}/.project
108 }
109
110 xtar()
111 {
112     local opts
113     local type
114     local fname=$1
115
116     if [ -z "${fname}" ]; then
117         echo No filename specified >&2
118         return 1
119     fi
120
121     type=$(file ${fname}|cut -d' ' -f2)
122
123     case $type in
124         (tar)    opts=xf ;;
125         (gzip)   opts=zxf ;;
126         (bzip2)  opts=jxf ;;
127         (xz|XZ)  opts=Jxf ;;
128         (*)
129         # try lzma
130         if lzmainfo ${fname} &>/dev/null; then
131             opts="--lzma -xf"
132         else
133             printf "Unknown file type: %s\n" $type >&2
134             return 2
135         fi
136         ;;
137     esac
138
139     tar ${opts} ${fname}
140 }
141
142 vtar()
143 {
144     local opts
145     local type
146     local fname=$1
147
148     if [ -z "${fname}" ]; then
149         echo No filename specified >&2
150         return 1
151     fi
152
153     type=$(file ${fname}|cut -d' ' -f2)
154
155     case $type in
156         (tar)    opts=tvvvf ;;
157         (gzip)   opts=ztvvvf ;;
158         (bzip2)  opts=jtvvvf ;;
159         (xz|XZ)  opts=Jtvvvf ;;
160         (*)
161         # lzma.  Here because lzmainfo is too stupid
162         if lzmainfo ${fname} &>/dev/null; then
163             opts="--lzma -tvvvf"
164         else
165             printf "Unknown file type: %s\n" $type >&2
166             return 2
167         fi
168         ;;
169     esac
170
171     tar ${opts} ${fname}|less
172 }
173
174 ## Check if there is a newer build script, maybe update.
175 #  NOTE: Updating needs SXEmacs.  It'll work in XEmacs and Emacs too,
176 #  but you'll need to change build-update() accordingly.
177 build-update()
178 {
179     if [ -x $(type -p sxemacs) ]; then
180         sxemacs -l /etc/pkgusr/bld-update.el
181     else
182         echo *** Sorry, you do not have SXEmacs installed.
183         echo *** Copying the new build script to ~/build-$(date +%Y%m%d)
184         cp -v /etc/pkgusr/skel-package/build \
185             ${HOME}/build-$(date +%Y%m%d)
186     fi
187 }
188
189 checkupdates()
190 {
191     local sysb=/etc/pkgusr/skel-package/build
192     local pkgb=${HOME}/build
193     local sysbv=$(${sysb} -V|awk '/build:/ {print $2;}')
194     local pkgbv=$(${pkgb} -V|awk '/build:/ {print $2;}')
195     
196     if [ ${sysb} -nt ${pkgb} ]; then
197         echo '*****************************************'
198         echo '*                                       *'
199         echo '*  B u i l d  S c r i p t  U p d a t e  *'
200         echo '*          A v a i l a b l e            *'
201         echo '*                                       *'
202         echo '*****************************************'
203         echo '       Your version:' ${pkgbv}
204         echo '  Available Version:' ${sysbv}
205         echo
206         echo 'For a SXEmacs based interactive update, run: "build-update"'
207         echo 'To turn this notice off, set $CHECKUPDATES to: "0"'
208         echo
209         echo -n 'Press [RETURN] to continue...'
210         read junk
211     fi
212 }
213
214 ## Aliases
215 # Repos/Websites
216 alias srepo='grep --colour Repo_Location:.*$ ${HOME}/.project'
217 alias rawrepo='srepo|cut -d" " -f2|tr -d "<>"'
218 alias trepo='grep --colour Repo_Type:.*$ ${HOME}/.project'
219 alias web='grep --colour Web_Site:.*$ ${HOME}/.project'
220 alias rawweb='web|tr -s " "|cut -d" " -f3|tr -d "<>"'
221 # Logs
222 alias alogs='ls -l ${HOME}/*.{err,log}'
223 alias lerr='ls -l ${HOME}/*.err'
224 alias llog='ls -l ${HOME}/*.log'
225 # Util
226 alias ebld='nano -w ${HOME}/build'
227 alias epro='nano -w ${HOME}/.project'
228 alias ebp='nano -w ${HOME}/{build,.project}'
229 alias deps='grep --colour "Deps: " ${HOME}/.project'
230 alias ipkg=showinst
231 alias gpkg=showgen
232 alias listp='pinky -l $(whoami)|less'
233 alias lc=less-or-cat
234
235 H-pkg()
236 {
237         cat<<EOF|less
238
239 Logs:
240
241         alogs           List of build logs (showing size)
242         lerr            List just the error logs
243         llog            List just the .log logs
244         verr [LOG]      Display LOG, which can be:
245                                \`conf' -- configure.err
246                               \`check' -- check.err
247                             \`install' -- install.err
248                                \`make' -- make.err
249                                 \`upd' -- upd.err
250                                 \`ver' -- verupd.err
251                                 \`all' -- all error logs (default)
252         vlog [LOG]      Same as for \`verr', but for the .log files.
253         verrlog [LOG]   Same as for \`verr', but displays both the .err,
254                         and the .log files.
255         dlog            Removes all build logs
256         instg {err|log} Greps install.err (default) or install.log for
257                         pkgusr notifications ("*** some message")
258
259 Package Notes/Content:
260
261         showinst        Displays the \`Install Notes'. (alias: ipkg)
262         showgen         Displays the \`General Notes'. (alias: gpkg)
263         listp           Displays the entire package info (piped through less(1))
264         srepo           Display the package's source repo location.
265         rawrepo         Output just the repo URL (to use with lynx, curl, etc)
266         trepo           Display the type of repo (tla, git, svn, mercurial etc)
267         web             Display the package's homepage URL.
268         rawweb          Output just the web URL (to use with lynx etc)
269         deps            Display the package's dependencies
270
271 Tarball Handling:
272
273         xtar [TARBALL]  Extract TARBALL, automatically choosing the appropriate
274                         tar(1) options.
275         vtar [TARBALL]  List the contents of TARBALL, automatically choosing 
276                         appropriate options and piping through less(1).
277
278 Build Scripts:
279
280         ebld            Edit the build script.
281         epro            Edit the .project file.
282         ebp             Edit the build script and the .project file.
283         updver [NEWVER] Updates the version in the .project.  It MUST be
284                         quoted to protect it from shell expansion.
285
286
287 EOF
288 }
289
290 # Local variables:
291 # sh-basic-offset: 4
292 # End: