5a53927efc6cdd8c4c9f0e0720f407cc6c3279df
[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.0
42 COPYRIGHT="Copyright (C) 2007 - 2010 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 update_commands()
95 { :
96     sed -i s/"Last_Updated.*"/"Last_Updated: $(date +%c)"/g ${HOME}/.project
97     awk '/^CONTENTS:/ { print; exit; } {print}' ${HOME}/.project > ${HOME}/.projtmp
98     echo "--------" >> ${HOME}/.projtmp
99     list_package $(whoami) >> ${HOME}/.projtmp
100     mv ${HOME}/.projtmp ${HOME}/.project
101 }
102
103 #*
104 #  Set `arg' to command(s) that output just the version number of the
105 #  package.
106 version_commands()
107 { :
108     # Commands to update the version string in .project
109     arg=''  # replace with something that returns a version number.
110     sed -i "s|\(Version: \).*$|\1${arg}|" ${HOME}/.project
111 }
112
113 #======================================================================#
114 ### There shouldn't be anything beyond this point to tweak or change ###
115 #======================================================================#
116  
117 test_pipe()
118 {
119     for i in "${PIPESTATUS[@]}"; do
120         test $i != 0 && { echo FAILED! ; exit 1 ; }
121     done
122     echo successful!
123     return 0
124 }
125
126
127 run_configure()
128 {
129     echo -n "Configuring ($(whoami))... "
130     { configure_commands 3>&1 1>&2 2>&3 | tee "$HOME/configure.err" ;} \
131         &>"$HOME/configure.log"
132     test_pipe
133     [[ ${only} = yes ]] && exit 0 || run_build
134 }
135
136 run_build()
137 {
138     echo -n "Building ($(whoami))... "
139     { make_commands 3>&1 1>&2 2>&3 | tee "$HOME/make.err" ;} \
140         &>"$HOME/make.log"
141     test_pipe
142     [[ ${only} = yes ]] && exit 0 || run_check
143 }
144
145 run_check()
146 {
147     echo -n "Checking ($(whoami))... "
148     { check_commands 3>&1 1>&2 2>&3 | tee "$HOME/check.err" ;} \
149         &>"$HOME/check.log"
150     test_pipe
151     [[ ${only} = yes ]] && exit 0 || run_install
152 }
153
154 run_install()
155 {
156     echo -n "Installing ($(whoami))... "
157     { install_commands 3>&1 1>&2 2>&3 | tee "$HOME/install.err" ;} \
158         &>"$HOME/install.log"
159     test_pipe
160     [[ ${only} = yes ]] && exit 0 || run_update
161 }
162
163 run_update()
164 {
165     echo -n "Updating package list ($(whoami))... "
166     { update_commands 3>&1 1>&2 2>&3 | tee "$HOME/upd.err" ;} \
167         &>"$HOME/upd.log"
168     test_pipe
169     # maybe update the version too
170     if [ $auto_version -eq 1 ];then
171         echo -n "Updating package version ($(whoami))... "
172         { version_commands 3>&1 1>&2 2>&3 | tee "$HOME/verupd.err" ;} \
173             &>"$HOME/verupd.log"
174         test_pipe
175     fi
176 }
177
178 # Help
179 usage()
180 {
181     # Look for a pager to display the help with.
182     local _cat
183
184     if [ ${PAGER} ]; then
185         # User has PAGER env var set, use that.
186         _cat=${PAGER}
187     else
188         # No PAGER var set, try most->less->more->cat
189         for pager in most less more cat; do
190             if [ -x "$(which $pager)" ]; then
191                 _cat=$pager
192                 break
193             else
194                 continue
195             fi
196         done
197     fi
198
199     $_cat<<EOF
200                                $ourname
201          ${VERSION}
202
203
204 Synopsis:
205 --------
206
207   $ourname
208   $ourname
209         [ -c | -C | -b | -B | -k | -K | -i | -I | -u ]
210         [ --conf | --conf_only | --build | --build_only | --check ]
211         [ --check_only | --install | --install_only | --upd_list ]
212   $ourname
213         [ -h | --help ]
214   $ourname
215         [ -v | --version ]
216
217 Description:
218 -----------
219
220   $ourname is a general purpose build script.  It should fit the bill
221   for most packages out of the box.  However you should ALWAYS check
222   through the script before running it blindly on a package.
223
224   The places in the script that will require your attention each time
225   you set up a new package have been marked with "#*".
226
227 Options:
228 -------
229
230   Most times you will not need to specify any command line options.
231   They exist mainly for those times when something has gone awry.
232
233   -c
234   --conf
235         Run the script from the configure stage, onwards.  This is
236         synonymous with running the script without any command line
237         options.
238
239   -C
240   --conf_only
241         Run just the configure stage and then exit.
242
243   -b
244   --build
245         Run the script from the build (make) stage, onwards.  Specifying
246         this option forces the script to skip the configure stage.  Do
247         not use this option simply because the package does not have a
248         configure, in that case, you are better off simply removing the
249         contents of the "configure_commands" function.
250
251   -B
252   --build_only
253         Run just the build (make) stage and then exit.
254
255   -k
256   --check
257         Run the script from the check (testsuite) stage, onwards.
258         Specifying this option forces the script to skip the configure
259         and build stages.
260
261   -K
262   --check_only
263         Run just the check (testsuite) stage and then exit.
264
265   -i
266   --install
267         Run the script from the install stage, onwards.  Specifying
268         this option forces the script to skip the configure, build,
269         and check stages.
270
271   -I
272   --install_only
273         Run just the install stage and then exit.
274
275   -u
276   --upd_list
277         Updates the package file list kept in the .project file.  This
278         option also updates the package version in that file too if
279         possible.
280
281   -h
282   --help
283         Display this usage info and exit.
284
285   -v
286   --version
287         Display version and copyright info and exit.
288
289 Files:
290 -----
291
292   ${HOME}/.project
293         Has information about the package, including (but not limited
294         to), website, repo location and type, version, date last
295         updated, installation notes, and complete file list.
296
297   ${HOME}/configure.log
298         Contains all messages output during configure.
299
300   ${HOME}/configure.err
301         Contains only error messages output during configure.
302
303   ${HOME}/make.log
304         Contains all messages output during make.
305
306   ${HOME}/make.err
307         Contains only error messages output during make.
308
309   ${HOME}/check.log
310         Contains all messages output from running a package testsuite.
311
312   ${HOME}/check.err
313         Contains only error messages output from running a package
314         testsuite.
315
316   ${HOME}/install.log
317         Contains all messages output during install.
318
319   ${HOME}/install.err
320         Contains only error messages output during install.
321
322   ${HOME}/upd.log
323         Contains all messages output during the update of the package
324         file list.  This log is nearly always empty, or at least it
325         should be.
326
327   ${HOME}/upd.err
328         Contains only error messages output during the update of the
329         package file list.  This log is nearly always empty, or at
330         least it should be.
331
332   ${HOME}/updver.log
333         Contains all messages output when updating the package version
334         info.  It should be empty.
335
336   ${HOME}/updver.err
337         Contains only error messages output when updating the package
338         version info.  It should be empty.
339
340 Exit Codes:
341 ----------
342
343   0 -- Successful completion.
344   1 -- Something bad happened.
345   2 -- Bad command line option.
346
347 $COPYRIGHT
348
349 EOF
350 }
351
352 # Command line parsing.
353 # Yes, it is possible to give more than one option on the command
354 # line, but that normally doesn't make much sense.  Consider:
355 # '../build --conf_only --install' the --conf_only option will
356 # cause the script to exit before the install happens.
357 args=cCbBkKiIuhV-:
358 only=no
359
360 if [ $1 ]; then
361     # We have cmdline args, deal with them.
362     while getopts $args opts; do
363         case $opts in
364             (-)
365                 case $OPTARG in
366                     (conf) run_configure ;;
367                     (conf_only) only=yes; run_configure ;;
368                     (build) run_build ;;
369                     (build_only) only=yes; run_build ;;
370                     (check) run_check ;;
371                     (check_only) only=yes; run_check ;;
372                     (install) run_install ;;
373                     (install_only) only=yes; run_install ;;
374                     (upd_list) run_update ;;
375                     (help|usage) usage ;;
376                     (version) show_version ;;
377                     (*)
378                         echo $ouname: error: bad option: --$OPTARG >&2
379                         exit 2
380                         ;;
381                 esac
382                 ;;
383             (c) run_configure ;;
384             (C) only=yes; run_configure ;;
385             (b) run_build ;;
386             (B) only=yes; run_build ;;
387             (k) run_check ;;
388             (K) only=yes; run_check ;;
389             (i) run_install ;;
390             (I) only=yes; run_install ;;
391             (h) usage ;;
392             (u) run_update ;;
393             (V) show_version ;;
394             (*)
395                 echo $ourname: error: bad option: -$OPTARG >&2
396                 exit 2
397                 ;;
398         esac
399     done
400     shift $(( $OPTIND - 1 ))
401 else
402     # There were no cmdline args given, just run from configure onwards
403     only=no                     # this should be "no" already, but make sure
404     run_configure
405 fi
406
407 ## build ends here