Try to make it easier for when I update the build script template.
[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 # Used primarily in the build script to update deps.
175 find_pkg_deps()
176 {
177     local arg=$1
178
179     [ -z "$arg" ] && arg=$(whoami)
180
181     for file in $(forall_direntries_from $arg -type f -executable -readable); do
182         (readelf -d $file ; ldd $file ) |
183             awk '/NEEDED/ { lib=substr($5,2,length($5)-2); LIBS[lib]=$5 } \
184                     /.*=>/ {if ( $1 in LIBS ) LIBS[$1]=$3 } END \
185                     { for (lib in LIBS) print LIBS[lib] }' |
186             xargs stat --printf "%U:%G\n"
187     done|sort -u|tr -s '\n' ' '
188 }
189
190 ## Check if there is a newer build script, maybe update.
191 #  NOTE: Updating needs SXEmacs.  It'll work in XEmacs and Emacs too,
192 #  but you'll need to change build-update() accordingly.
193 build-update()
194 {
195     if [ -x $(type -p sxemacs) ]; then
196         sxemacs -l /etc/pkgusr/bld-update.el
197     fi
198 }
199
200 checkupdates()
201 {
202     local sysb=/etc/pkgusr/skel-package/build
203     local pkgb=${HOME}/build
204     local sysbv=$(${sysb} -V|awk '/build:/ {print $2;}')
205     local pkgbv=$(${pkgb} -V|awk '/build:/ {print $2;}')
206     
207     if [ ${sysb} -nt ${pkgb} ]; then
208         echo '*****************************************'
209         echo '*                                       *'
210         echo '*  B u i l d  S c r i p t  U p d a t e  *'
211         echo '*          A v a i l a b l e            *'
212         echo '*                                       *'
213         echo '*****************************************'
214         echo '       Your version:' ${pkgbv}
215         echo '  Available Version:' ${sysbv}
216         echo
217         echo 'For a SXEmacs based interactive update, run: "build-update"'
218         echo 'To turn this notice off, set $CHECKUPDATES to: "0"'
219         echo
220         echo -n 'Press [RETURN] to continue...'
221         read junk
222     fi
223 }
224
225 ## Aliases
226 # Repos/Websites
227 alias srepo='grep --colour Repo_Location:.*$ ${HOME}/.project'
228 alias rawrepo='srepo|cut -d" " -f2|tr -d "<>"'
229 alias trepo='grep --colour Repo_Type:.*$ ${HOME}/.project'
230 alias web='grep --colour Web_Site:.*$ ${HOME}/.project'
231 alias rawweb='web|tr -s " "|cut -d" " -f3|tr -d "<>"'
232 # Logs
233 alias alogs='ls -l ${HOME}/*.{err,log}'
234 alias lerr='ls -l ${HOME}/*.err'
235 alias llog='ls -l ${HOME}/*.log'
236 # Util
237 alias ebld='nano -w ${HOME}/build'
238 alias epro='nano -w ${HOME}/.project'
239 alias ebp='nano -w ${HOME}/{build,.project}'
240 alias deps='grep --colour "Deps: " ${HOME}/.project'
241 alias ipkg=showinst
242 alias gpkg=showgen
243 alias listp='pinky -l $(whoami)|less'
244 alias lc=less-or-cat
245
246 H-pkg()
247 {
248         cat<<EOF|less
249
250 Logs:
251
252         alogs           List of build logs (showing size)
253         lerr            List just the error logs
254         llog            List just the .log logs
255         verr [LOG]      Display LOG, which can be:
256                                \`conf' -- configure.err
257                               \`check' -- check.err
258                             \`install' -- install.err
259                                \`make' -- make.err
260                                 \`upd' -- upd.err
261                                 \`ver' -- verupd.err
262                                 \`all' -- all error logs (default)
263         vlog [LOG]      Same as for \`verr', but for the .log files.
264         verrlog [LOG]   Same as for \`verr', but displays both the .err,
265                         and the .log files.
266         dlog            Removes all build logs
267         instg {err|log} Greps install.err (default) or install.log for
268                         pkgusr notifications ("*** some message")
269
270 Package Notes/Content:
271
272         showinst        Displays the \`Install Notes'. (alias: ipkg)
273         showgen         Displays the \`General Notes'. (alias: gpkg)
274         listp           Displays the entire package info (piped through less(1))
275         srepo           Display the package's source repo location.
276         rawrepo         Output just the repo URL (to use with lynx, curl, etc)
277         trepo           Display the type of repo (tla, git, svn, mercurial etc)
278         web             Display the package's homepage URL.
279         rawweb          Output just the web URL (to use with lynx etc)
280         deps            Display the package's dependencies
281
282 Tarball Handling:
283
284         xtar [TARBALL]  Extract TARBALL, automatically choosing the appropriate
285                         tar(1) options.
286         vtar [TARBALL]  List the contents of TARBALL, automatically choosing 
287                         appropriate options and piping through less(1).
288
289 Build Scripts:
290
291         ebld            Edit the build script.
292         epro            Edit the .project file.
293         ebp             Edit the build script and the .project file.
294         updver [NEWVER] Updates the version in the .project.  It MUST be
295                         quoted to protect it from shell expansion.
296
297
298 EOF
299 }
300
301 # Local variables:
302 # sh-basic-offset: 4
303 # End: