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