eea3b0c641e76a00b1d893a5f5b05c85543f9f17
[pkgusr] / etc / pkgusr / handy_funcs
1 # -*- shell-script -*-
2 # Copyright (C) 2007 - 2021 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 B-tips()
8 {
9     less<<EOF
10
11 Always build outside of the source tree if possible.  Meson even
12 enforces this, CMake to a lesser degree does as well.  This is a good
13 thing.
14
15 Meson:
16 =====
17 *** KEEP THE BUILD TREE ***
18
19 A big difference with Meson compared to other build systems is that you
20 should only ever have to run it once per package.  You do not have to
21 re-run it every time there is a change in the source tree.  To that
22 end, here is a reasonable template to use in the 'configure_commands()'
23 section of the build script...
24
25     # If you change the options after the build tree has already been
26     # set up and configured, run the build as: 'FORCE=1 ../build'
27     if [[ ! -d meson-info || \${FORCE} -eq 1 ]]; then
28         if [[ \${FORCE} -eq 1 ]]; then
29             _cmd=configure TREE=\${PWD}
30             echo '***' No logs on a FORCE run
31             echo Check what happened with: \'meson introspect --buildoptions -i\'
32         else
33             TREE=\${SRCTREE}
34         fi
35         meson \$_cmd \\
36             -Dprefix=/usr \\
37             -Dlibdir=lib \\
38             -Dlibexecdir=lib \\
39             -Dbuildtype=release \\
40             -Db_lto=true \\
41             -Db_lto_threads=4 \\
42             any other options here \\
43             \${TREE}
44     fi
45
46 The Meson equivalent of configure --help
47
48   meson introspect --buildoptions -i
49
50 All "path" options (libdir, datadir, etc) are relative to prefix
51 unless the value begins with a '/' and then it is an absolute path.
52
53 Another nice thing about Meson is that when you set prefix to '/usr',
54 sysconfdir is automatically set to '/etc' and localstatedir to '/var'.
55
56 One headache that I have with Meson (and also CMake and Ninja) is
57 that file timestamps are very often not updated on install.  I use
58 timestamps to help with upgrades (see: Upgrading below) and cleaning
59 out the old stuff that no longer exists in the current version of a
60 package.  So here is a template for the 'install_commands()' section
61 of the build script...
62
63     ninja install &&
64     # Meson builds tend not to update timestamps
65     meson introspect --installed -i |
66         awk '{print \$2}'|tr -d '",'|xargs touch -h
67
68 CMake:
69 =====
70 *** KEEP THE BUILD TREE ***
71
72 Always use Ninja instead of old crusty unix makefiles.  Here is a
73 reasonable template for 'configure_commands()'...
74
75     cmake \\
76         -DCMAKE_BUILD_TYPE=Release \\
77         -DCMAKE_INSTALL_PREFIX=/usr \\
78         -DCMAKE_INSTALL_LIBDIR=lib \\
79         -DCMAKE_INSTALL_LIBEXECDIR=lib \\
80         -DCMAKE_INSTALL_LOCALSTATEDIR=/var \\
81         -DCMAKE_INSTALL_RUNSTATEDIR=/run \\
82         -DCMAKE_INSTALL_SYSCONFDIR=/etc \\
83         any other options here \\
84         -G Ninja \${SRCTREE}
85
86 Like Meson, CMake path options are relative to prefix unless the value
87 begins with a '/' and then it is absolute.
88
89 A nice thing about CMake is that it creates targets to edit and
90 rebuild the cache (think: reconfigure).  'ninja edit_cache' and
91 'ninja rebuld_cache', respectively.
92
93 Same headache as with Meson... timestamps not being updated on installs.
94 Here is the 'install_commands()' template for CMake builds...
95
96     ninja install &&
97     # CMake builds tend not to update timestamps
98     if [ -f install_manifest.txt ]; then
99         echo>>install_manifest.txt # we need a final newline
100         while read file; do
101             touch -h \"\${file}\"
102         done<install_manifest.txt
103     fi
104
105 Autotools:
106 =========
107 install_commands() template...
108
109     make install &&
110     # Libtool .la files are NOT needed on a modern Linux system
111     find /usr/lib -name \\*.la -user \$(whoami) -delete
112
113 Upgrading:
114 =========
115
116     1) touch ~/timestampfile
117     2) build and install the package
118     3) forall_direntries_from \$(whoami) -not -newer ~/timestampfile
119
120 If step 3 turns up libraries that are dependencies of other packages
121 move them to /usr/lib/deprecated until everything that needs them has
122 been updated/rebuilt. Remember to run ldconfig.
123
124 EOF
125 }
126
127 verr()
128 {
129     local arg=$1
130     if [ -z "$arg" ]; then
131         arg=all
132     fi
133     case $arg in
134         (conf)    less ${HOME}/configure.err ;;
135         (install) less ${HOME}/install.err ;;
136         (check)   less ${HOME}/check.err ;;
137         (make)    less ${HOME}/make.err ;;
138         (upd)     less ${HOME}/upd.err ;;
139         (ver)     less ${HOME}/verupd.err ;;
140         (all)     less ${HOME}/*.err ;;
141     esac
142 }
143
144 vlog()
145 {
146     local arg=$1
147     if [ -z "$arg" ]; then
148         arg=all
149     fi
150     case $arg in
151         (conf)    less ${HOME}/configure.log ;;
152         (install) less ${HOME}/install.log ;;
153         (check)   less ${HOME}/check.log ;;
154         (make)    less ${HOME}/make.log ;;
155         (upd)     less ${HOME}/upd.log ;;
156         (ver)     less ${HOME}/verupd.log ;;
157         (all)     less ${HOME}/*.log ;;
158     esac
159 }
160
161 verrlog()
162 {
163     local arg=$1
164     if [ -z "$arg" ]; then
165         arg=all
166     fi
167     case $arg in
168         (conf)    less ${HOME}/configure.{err,log} ;;
169         (install) less ${HOME}/install.{err,log} ;;
170         (check)   less ${HOME}/check.{err,log} ;;
171         (make)    less ${HOME}/make.{err,log} ;;
172         (upd)     less ${HOME}/upd.{err,log} ;;
173         (ver)     less ${HOME}/verupd.{err,log} ;;
174         (all)     less ${HOME}/*.{err,log} ;;
175     esac
176 }
177
178 instg()
179 {
180     local arg=$1
181     if [ -z "$arg" ]; then
182         arg=err
183     fi
184     grep --colour=always '^\*\*\*' ${HOME}/install.${arg}|less
185 }
186
187 chkg()
188 {
189     local arg=$1
190     if [ -z "$arg" ]; then
191         arg=err
192     fi
193     grep --colour=always '^\*\*\*' ${HOME}/check.${arg}|less
194 }
195
196 makeg()
197 {
198     local arg=$1
199     if [ -z "$arg" ]; then
200         arg=err
201     fi
202     grep --colour=always '^\*\*\*' ${HOME}/make.${arg}|less
203 }
204
205 dlog()
206 {
207     for log in configure make check install upd verupd; do
208         [[ -f ${HOME}/${log}.err ]] && rm -v ${HOME}/${log}.err
209         [[ -f ${HOME}/${log}.log ]] && rm -v ${HOME}/${log}.log
210     done
211 }
212
213 updver()
214 {
215     local arg=${1}
216     sed -i "s|\(Version: \).*$|\1${arg}|" ${HOME}/.project
217     echo -n "Version updated... "
218     grep --colour Version:.*$ ${HOME}/.project
219 }
220
221 ipkg()
222 {
223     local top=$(grep -n "^Install Notes:$" ${HOME}/.project|cut -d: -f1)
224     local bot=$(grep -n "^General Notes:$" ${HOME}/.project|cut -d: -f1)
225     sed -n ${top},${bot}p ${HOME}/.project|less
226 }
227
228 gpkg()
229 {
230     local top=$(grep -n "^General Notes:$" ${HOME}/.project|cut -d: -f1)
231     local bot=$(grep -n "^CONTENTS:$" ${HOME}/.project|cut -d: -f1)
232     sed -n ${top},${bot}p ${HOME}/.project|less
233 }
234
235 ## Check if there is a newer build script, maybe update.
236 #  NOTE: Updating needs SXEmacs.  It'll work in XEmacs and Emacs too,
237 #  but you'll need to change build-update() accordingly.
238 build-update()
239 {
240     if [ -x $(type -p sxemacs) ]; then
241         sxemacs -l /etc/pkgusr/bld-update.el
242     else
243         echo *** Sorry, you do not have SXEmacs installed.
244         echo *** Copying the new build script to ~/build-$(date +%Y%m%d)
245         cp -v /etc/pkgusr/skel-package/build \
246             ${HOME}/build-$(date +%Y%m%d)
247     fi
248 }
249
250 checkupdates()
251 {
252     local sysb=/etc/pkgusr/skel-package/build
253     local pkgb=${HOME}/build
254     local sysbv=$(${sysb} -V|awk '/build:/ {print $2;}')
255     local pkgbv=$(${pkgb} -V|awk '/build:/ {print $2;}')
256     
257     if [[ ${sysbv} > ${pkgbv} ]]; then
258         echo '*****************************************'
259         echo '*                                       *'
260         echo '*  B u i l d  S c r i p t  U p d a t e  *'
261         echo '*          A v a i l a b l e            *'
262         echo '*                                       *'
263         echo '*****************************************'
264         echo '       Your version:' ${pkgbv}
265         echo '  Available Version:' ${sysbv}
266         echo
267         echo 'For a SXEmacs based interactive update, run: "build-update"'
268         echo 'To turn this notice off, set $CHECKUPDATES to: "0"'
269         echo
270         echo -n 'Press [RETURN] to continue...'
271         read junk
272     fi
273 }
274
275 ## Convenience to allow existing pkgusrs to update their git
276 #  config
277 prep_git()
278 {
279     install -vdm755 ${HOME}/.config/git &&
280     ln -svf /etc/pkgusr/gitconfig ${HOME}/.config/git/config &&
281     # Nuke the old ~/.gitconfig
282     rm -v ${HOME}/.gitconfig 2>/dev/null || true
283 }
284
285 ## Aliases
286 # Repos/Websites
287 alias srepo='grep --colour Repo_Location:.*$ ${HOME}/.project'
288 alias rawrepo='srepo|cut -d" " -f2|tr -d "<>"'
289 alias trepo='grep --colour Repo_Type:.*$ ${HOME}/.project'
290 alias web='grep --colour Web_Site:.*$ ${HOME}/.project'
291 alias rawweb='web|tr -s " "|cut -d" " -f3|tr -d "<>"'
292 # Logs
293 alias alogs='ls -l ${HOME}/*.{err,log}'
294 alias lerr='ls -l ${HOME}/*.err'
295 alias llog='ls -l ${HOME}/*.log'
296 # Util
297 ## As of 2021-01-18 nano (v5.5) seems FUBAR'd
298 alias ebld='vi ${HOME}/build'
299 alias epro='vi ${HOME}/.project'
300 alias ebp='vi -a ${HOME}/{build,.project}'
301 # alias ebld='nano -w ${HOME}/build'
302 # alias epro='nano -w ${HOME}/.project'
303 # alias ebp='nano -w ${HOME}/{build,.project}'
304 alias deps='grep --colour "Deps: " ${HOME}/.project'
305 alias listp='pinky -l $(whoami)|less'
306 alias patches='find ${SLACKPKG}/**/$(whoami)|grep -E "(diff|patch)"'
307 alias vtar=less
308 alias xtar='tar xf'
309
310 H-pkg()
311 {
312         less<<EOF
313
314 Logs:
315
316         alogs           List of build logs (showing size)
317         lerr            List just the error logs
318         llog            List just the .log logs
319         verr [LOG]      Display LOG, which can be:
320                                \`conf' -- configure.err
321                               \`check' -- check.err
322                             \`install' -- install.err
323                                \`make' -- make.err
324                                 \`upd' -- upd.err
325                                 \`ver' -- verupd.err
326                                 \`all' -- all error logs (default)
327         vlog [LOG]      Same as for \`verr', but for the .log files.
328         verrlog [LOG]   Same as for \`verr', but displays both the .err,
329                         and the .log files.
330         dlog            Removes all build logs
331         instg {err|log} Greps install.err (default) or install.log for
332                         pkgusr notifications ("*** some message")
333         chkg {err|log}  Greps check.err (default) or check.log for
334                         pkgusr notifications ("*** some message")
335         makeg {err|log} Greps make.err (default) or make.log for
336                         pkgusr notifications ("*** some message")
337
338 Package Notes/Content:
339
340         ipkg            Displays the \`Install Notes'.
341         gpkg            Displays the \`General Notes'.
342         listp           Displays the entire package info (piped through less(1))
343         srepo           Display the package's source repo location.
344         rawrepo         Output just the repo URL (to use with lynx, curl, etc)
345         trepo           Display the type of repo (git, svn, mercurial, tla etc)
346         web             Display the package's homepage URL.
347         rawweb          Output just the web URL (to use with lynx etc)
348         deps            Display the package's dependencies
349
350 Tarball Handling:
351
352         xtar [TARBALL]  Extract TARBALL
353         vtar [TARBALL]  List the contents of TARBALL
354
355 Build Scripts:
356
357         ebld            Edit the build script.
358         epro            Edit the .project file.
359         ebp             Edit the build script and the .project file.
360         updver [NEWVER] Updates the version in the .project.  It MUST be
361                         quoted to protect it from shell expansion.
362
363 Miscellaneous Bobs & Bits:
364
365         prep_git        Sets up ~/.config/git/config
366                         In the normal course of events you shouldn't need to
367                         use this as it happens during login if it is needed.
368         patches         Check for any Slackware patches. (see SLACKPKG var in
369                         bash_profile)
370         B-tips          A few build tips.  Includes templates for Meson and
371                         CMake to use in the build script.
372
373
374 EOF
375 }
376
377 # Local variables:
378 # sh-basic-offset: 4
379 # End: