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