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