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