A few more minor tweaks.
[pkgusr] / etc / pkgusr / skel-package / build
1 #!/bin/bash
2 #
3 # Build script for <PACKAGE>
4 #
5 # This build script is meant to be executed from within the build
6 # directory, regardless of whether that is outside the source tree or
7 # not.
8 #
9 # It will create up to 12 log files in the $HOME directory:
10 #
11 #   configure.log: All messages output during configure
12 #   configure.err: Just the errors output during configure
13 #        make.log: All messages output during make
14 #        make.err: Just the errors output during make
15 #       check.log: All messages output during make check/test
16 #       check.err: Just the errors output during make check/test
17 #     install.log: All messages output during make install
18 #     install.err: Just the errors output during make install
19 #         upd.log: Any messages from updating the package list
20 #         upd.err: Just the errors from updating the package list
21 #      verupd.log: Any messages from updating the package version
22 #      verupd.err: Just the errors from updating the package version
23 #
24 # After running the script you should check the *.err files to see
25 # if any problems have occurred. If that is the case, use the corresponding
26 # *.log files to see the error messages in context.
27 #
28 # Note: the ":" before the "}" in *_commands() is a no-op that makes sure 
29 # that the function remains syntactically valid, even if you remove its
30 # contents (e.g. remove the "configure" line, because there's nothing to 
31 # configure for the package).
32 #
33 # Comments throughout the script marked "#*" are places where you may
34 # have to change things for individual package circumstances.
35
36 ## Version info.
37 ourname=${0##*/}
38 VERSION=1.5
39 COPYRIGHT="Copyright (C) 2007 - 2014 Steve Youngs <steve@steveyoungs.com>"
40 version_str="${ourname}: ${VERSION}\n${COPYRIGHT}"
41
42 show_version()
43 {
44     echo -e $version_str
45     exit 0
46 }
47
48 #*
49 # Set $auto_version to 1 (one) if the package's version can be updated
50 # automatically.  Set $SRCTREE if building outside the source tree.
51 auto_version=0
52 SRCTREE='.'
53
54 #*
55 # Set the configure commands/options here.  Remove everything except
56 # the braces and colon if the package has no configure.
57 configure_commands()
58 { :
59     ${SRCTREE}/configure --prefix=/usr \
60         --infodir=/usr/share/info \
61         --mandir=/usr/share/man \
62         --sysconfdir=/etc \
63         --libexecdir=/usr/lib \
64         --localstatedir=/var
65 }
66
67 #*
68 # Set the make commands/options here.
69 make_commands()
70 { :
71     make
72 }
73
74 #*
75 # Set the test suite commands/options here.  Remove everything except
76 # the braces and colon if the package has no test suite
77 check_commands()
78 { :
79     make check
80 }
81
82 #*
83 # Set the install commands/options here.
84 install_commands()
85 { :
86     make install &&
87     # libtool .la files DO NOT need to be executable! (remove if not a
88     # libtool'd package)
89     forall_direntries_from $(whoami) -name \*.la -exec chmod -v 644 {} \;
90 }
91
92 #*
93 #  Set `arg' to command(s) that output just the version number of the
94 #  package.
95 version_commands()
96 { :
97     # Commands to update the version string in .project
98     arg=''  # replace with something that returns a version number.
99     sed -i "s|\(Version: \).*$|\1${arg}|" ${HOME}/.project
100 }
101
102 #======================================================================#
103 ### There shouldn't be anything beyond this point to tweak or change ###
104 #======================================================================#
105  
106 test_pipe()
107 {
108     for i in "${PIPESTATUS[@]}"; do
109         test $i != 0 && { echo FAILED! ; exit 1 ; }
110     done
111     echo successful!
112     return 0
113 }
114
115 update_commands()
116 { :
117     update-pkg-project $(whoami)
118 }
119
120 run_configure()
121 {
122     echo -n "Configuring ($(whoami))... "
123     { configure_commands 3>&1 1>&2 2>&3 | tee "$HOME/configure.err" ;} \
124         &>"$HOME/configure.log"
125     test_pipe
126     [[ ${only} = yes ]] && exit 0 || run_build
127 }
128
129 run_build()
130 {
131     echo -n "Building ($(whoami))... "
132     { make_commands 3>&1 1>&2 2>&3 | tee "$HOME/make.err" ;} \
133         &>"$HOME/make.log"
134     test_pipe
135     [[ ${only} = yes ]] && exit 0 || run_check
136 }
137
138 run_check()
139 {
140     echo -n "Checking ($(whoami))... "
141     { check_commands 3>&1 1>&2 2>&3 | tee "$HOME/check.err" ;} \
142         &>"$HOME/check.log"
143     test_pipe
144     [[ ${only} = yes ]] && exit 0 || run_install
145 }
146
147 run_install()
148 {
149     echo -n "Installing ($(whoami))... "
150     { install_commands 3>&1 1>&2 2>&3 | tee "$HOME/install.err" ;} \
151         &>"$HOME/install.log"
152     test_pipe
153     [[ ${only} = yes ]] && exit 0 || run_update
154 }
155
156 run_update()
157 {
158     echo -n "Updating package list ($(whoami))... "
159     { update_commands 3>&1 1>&2 2>&3 | tee "$HOME/upd.err" ;} \
160         &>"$HOME/upd.log"
161     test_pipe
162     # maybe update the version too
163     if [ $auto_version -eq 1 ];then
164         echo -n "Updating package version ($(whoami))... "
165         { version_commands 3>&1 1>&2 2>&3 | tee "$HOME/verupd.err" ;} \
166             &>"$HOME/verupd.log"
167         test_pipe
168     fi
169 }
170
171 # Help
172 usage()
173 {
174     # Look for a pager to display the help with.
175     local _cat
176
177     if [ ${PAGER} ]; then
178         # User has PAGER env var set, use that.
179         _cat=${PAGER}
180     else
181         # No PAGER var set, try most->less->more->cat
182         for pager in most less more cat; do
183             if [ -x "$(which $pager)" ]; then
184                 _cat=$pager
185                 break
186             else
187                 continue
188             fi
189         done
190     fi
191
192     $_cat<<EOF
193                                $ourname
194          ${VERSION}
195
196
197 Synopsis:
198 --------
199
200   $ourname
201   $ourname
202         [ -c | -C | -b | -B | -k | -K | -i | -I | -u ]
203         [ --conf | --conf_only | --build | --build_only | --check ]
204         [ --check_only | --install | --install_only | --upd_list ]
205   $ourname
206         [ -h | --help ]
207   $ourname
208         [ -v | --version ]
209
210 Description:
211 -----------
212
213   $ourname is a general purpose build script.  It should fit the bill
214   for most packages out of the box.  However you should ALWAYS check
215   through the script before running it blindly on a package.
216
217   The places in the script that will require your attention each time
218   you set up a new package have been marked with "#*".
219
220 Options:
221 -------
222
223   Most times you will not need to specify any command line options.
224   They exist mainly for those times when something has gone awry.
225
226   -c
227   --conf
228         Run the script from the configure stage, onwards.  This is
229         synonymous with running the script without any command line
230         options.
231
232   -C
233   --conf_only
234         Run just the configure stage and then exit.
235
236   -b
237   --build
238         Run the script from the build (make) stage, onwards.  Specifying
239         this option forces the script to skip the configure stage.  Do
240         not use this option simply because the package does not have a
241         configure, in that case, you are better off simply removing the
242         contents of the "configure_commands" function.
243
244   -B
245   --build_only
246         Run just the build (make) stage and then exit.
247
248   -k
249   --check
250         Run the script from the check (testsuite) stage, onwards.
251         Specifying this option forces the script to skip the configure
252         and build stages.
253
254   -K
255   --check_only
256         Run just the check (testsuite) stage and then exit.
257
258   -i
259   --install
260         Run the script from the install stage, onwards.  Specifying
261         this option forces the script to skip the configure, build,
262         and check stages.
263
264   -I
265   --install_only
266         Run just the install stage and then exit.
267
268   -u
269   --upd_list
270         Updates the package file list kept in the .project file.  This
271         option also updates the package version in that file too if
272         possible.
273
274   -h
275   --help
276         Display this usage info and exit.
277
278   -v
279   --version
280         Display version and copyright info and exit.
281
282 Files:
283 -----
284
285   ${HOME}/.project
286         Has information about the package, including (but not limited
287         to), website, repo location and type, version, date last
288         updated, installation notes, and complete file list.
289
290   ${HOME}/configure.log
291         Contains all messages output during configure.
292
293   ${HOME}/configure.err
294         Contains only error messages output during configure.
295
296   ${HOME}/make.log
297         Contains all messages output during make.
298
299   ${HOME}/make.err
300         Contains only error messages output during make.
301
302   ${HOME}/check.log
303         Contains all messages output from running a package testsuite.
304
305   ${HOME}/check.err
306         Contains only error messages output from running a package
307         testsuite.
308
309   ${HOME}/install.log
310         Contains all messages output during install.
311
312   ${HOME}/install.err
313         Contains only error messages output during install.
314
315   ${HOME}/upd.log
316         Contains all messages output during the update of the package
317         file list.  This log is nearly always empty, or at least it
318         should be.
319
320   ${HOME}/upd.err
321         Contains only error messages output during the update of the
322         package file list.  This log is nearly always empty, or at
323         least it should be.
324
325   ${HOME}/updver.log
326         Contains all messages output when updating the package version
327         info.  It should be empty.
328
329   ${HOME}/updver.err
330         Contains only error messages output when updating the package
331         version info.  It should be empty.
332
333 Exit Codes:
334 ----------
335
336   0 -- Successful completion.
337   1 -- Something bad happened.
338   2 -- Bad command line option.
339
340 $COPYRIGHT
341
342 EOF
343 }
344
345 # Command line parsing.
346 # Yes, it is possible to give more than one option on the command
347 # line, but that normally doesn't make much sense.  Consider:
348 # '../build --conf_only --install' the --conf_only option will
349 # cause the script to exit before the install happens.
350 args=cCbBkKiIuhV-:
351 only=no
352
353 if [ $1 ]; then
354     # We have cmdline args, deal with them.
355     while getopts $args opts; do
356         case $opts in
357             (-)
358                 case $OPTARG in
359                     (conf) run_configure ;;
360                     (conf_only) only=yes; run_configure ;;
361                     (build) run_build ;;
362                     (build_only) only=yes; run_build ;;
363                     (check) run_check ;;
364                     (check_only) only=yes; run_check ;;
365                     (install) run_install ;;
366                     (install_only) only=yes; run_install ;;
367                     (upd_list) run_update ;;
368                     (help|usage) usage ;;
369                     (version) show_version ;;
370                     (*)
371                         echo $ouname: error: bad option: --$OPTARG >&2
372                         exit 2
373                         ;;
374                 esac
375                 ;;
376             (c) run_configure ;;
377             (C) only=yes; run_configure ;;
378             (b) run_build ;;
379             (B) only=yes; run_build ;;
380             (k) run_check ;;
381             (K) only=yes; run_check ;;
382             (i) run_install ;;
383             (I) only=yes; run_install ;;
384             (h) usage ;;
385             (u) run_update ;;
386             (V) show_version ;;
387             (*)
388                 echo $ourname: error: bad option: -$OPTARG >&2
389                 exit 2
390                 ;;
391         esac
392     done
393     shift $(( $OPTIND - 1 ))
394 else
395     # There were no cmdline args given, just run from configure onwards
396     only=no                     # this should be "no" already, but make sure
397     run_configure
398 fi
399
400 ## build ends here