Various optimizations.
[autodist.git] / apps / autodist / autodist.in
1 #!/bin/sh
2 #
3 # Author: Pekka Riikonen <priikone@silcnet.org>
4 #
5 # Copyright (C) 2005 Pekka Riikonen
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
10 # met:
11 #
12 #   1. Redistributions of source code must retain the above copyright
13 #      notice, this list of conditions and the following disclaimer.
14 #   2. Redistributions in binary form must reproduce the above copyright
15 #      notice, this list of conditions and the following disclaimer in the
16 #      documentation and/or other materials provided with the distribution.
17 #   3. The name of the author may not be used to endorse or promote
18 #      products derived from this software without specific prior written
19 #      permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
24 # NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #
32
33 ###############################################################################
34 # Shell compatibility
35
36 # Be Bourne compatible
37 if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
38   # Despite of this, we have managed to segfault some zsh's.
39   emulate sh
40   NULLCMD=:
41   # Zsh 3.x and 4.x performs word splitting on ${1+"$@"}, which
42   # is contrary to our usage.  Disable this feature.
43   alias -g '${1+"$@"}'='"$@"'
44 fi
45 DUALCASE=1; export DUALCASE # for MKS sh
46
47 # Support unset when possible.
48 if ( (MAIL=60; unset MAIL) || exit) >/dev/null 2>&1; then
49   as_unset=unset
50 else
51   as_unset=false
52 fi
53
54 # Work around bugs in pre-3.0 UWIN ksh.
55 $as_unset ENV MAIL MAILPATH
56 PS1='$ '
57 PS2='> '
58 PS4='+ '
59
60 # NLS nuisances.
61 for as_var in \
62   LANG LANGUAGE LC_ADDRESS LC_ALL LC_COLLATE LC_CTYPE LC_IDENTIFICATION \
63   LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER \
64   LC_TELEPHONE LC_TIME
65 do
66   if (set +x; test -z "`(eval $as_var=C; export $as_var) 2>&1`"); then
67     eval $as_var=C; export $as_var
68   else
69     $as_unset $as_var
70   fi
71 done
72
73
74 ###############################################################################
75 # Global variables
76
77 # Packaging and compressing
78 ad_gzip=true
79 ad_bzip2=false
80 ad_compress=false
81 ad_zip=false
82
83 # Distribution subdirectory
84 distdir="distdir"
85 am_distdir=
86
87 # This current distribution
88 distribution=default
89 distfile=$distribution
90 dist_version=0.0
91 package=
92 bug_report=
93
94 # All inherited distributions in this distribution
95 inherits=
96
97 # All distribution defines for this distribution
98 distdefs=
99
100 # All distribution undefines for this distribution
101 undistdefs=
102
103 # All distribution options
104 doptions=
105 opt_template=false
106 opt_no_dist=false
107 opt_no_inherit=false
108
109 # All includes
110 includes=
111
112 # All excludes
113 excludes=
114
115 # All noprocesses
116 noprocess=
117
118 # All hooks
119 pre_hooks=
120 post_hooks=
121 pre_dist_hooks=
122 post_dist_hooks=
123 pre_p_dist_hooks=
124 post_p_dist_hooks=
125
126 # Distribution license and license header
127 license=
128 licenseh=
129
130 # Whether to output ad_debug information
131 debug=false
132
133 # Autodist version
134 ver=@VERSION@
135
136 ###############################################################################
137 # Configuration file
138 if test -f "$distdir/autodist.conf"; then
139   . $distdir/autodist.conf
140 fi
141
142 DP=$DISTPREFIX
143
144
145 ###############################################################################
146 # Functions
147
148 #
149 # Print out debug information if debugging is enabled.  To enable debugging
150 # set the global variable "debug" to true value.
151 #
152 # Arguments: ad_debug <ad_debug string>
153 #
154 ad_debug()
155 {
156   if test x$debug = xtrue; then
157     set -f
158     echo autodist: $1
159     set +f
160   fi
161 }
162
163 #
164 # Prints out error message and exits the script.
165 #
166 # Arguments: ad_fatal <error message>
167 #
168 ad_fatal()
169 {
170   set -f
171   echo autodist: error: $1
172   set +f
173   exit 1
174 }
175
176 #
177 # Prints out warning message
178 #
179 # Arguments: ad_warning <warning message>
180 #
181 ad_warning()
182 {
183   set -f
184   echo autodist: warning: $1
185   set +f
186 }
187
188 #
189 # Initializes the Autodist environment, creates default distribution
190 # directory, and default distribution.
191 #
192 # Arguments: ad_initialize
193 #
194 ad_initialize()
195 {
196   ad_debug ">ad_initialize"
197
198   # Create default distdir
199   if test '!' -f $distdir; then
200     mkdir -p $distdir
201   fi
202
203   # Create Autodist configuration file
204   if test -f @AUTODISTDIR@/autodist.conf; then
205     cp -p @AUTODISTDIR@/autodist.conf $distdir
206   fi
207
208   # Create default distribution
209   if test -f @AUTODISTDIR@/default; then
210     cp -p @AUTODISTDIR@/default $distdir
211   fi
212
213   ad_debug "<ad_initialize"
214 }
215
216 #
217 # Creates the distdefs header file including defined distdefs
218 #
219 # Arguments: ad_create_distdefs_h
220 #
221 ad_create_distdefs_h()
222 {
223   ad_debug ">ad_create_distdefs_h"
224
225   fname=$DISTDEFS
226   rm -f $fname
227   cat > $fname <<EOF
228 /*
229   Automatically generated by Autodist $ver.  Do not edit.
230
231   Generated: `date` by `whoami`
232   Distribution: $distribution
233   License: $license
234 */
235
236 #ifndef _`echo $DP`_DISTDEFS_H
237 #define _`echo $DP`_DISTDEFS_H
238
239 EOF
240
241   for i in $distdefs
242   do
243     echo "#define $i 1" >>$fname
244   done
245
246   cat >> $fname <<EOF
247
248 #endif /* _`echo $DP`_DISTDEFS_H */
249 EOF
250   ad_debug "<ad_create_distdefs_h"
251 }
252
253 #
254 # Creates the main configure script for the distribution.  This runs
255 # the aclocal, autoheader and autoconf tools.
256 #
257 # Arguments: ad_make_configure
258 #
259 ad_make_configure()
260 {
261   local run_autoconf=false
262
263   ad_debug ">ad_make_configure"
264
265   rm -f configure
266
267   if test "$ACLOCAL"; then
268     ad_debug "Running aclocal"
269     if test x$debug = xtrue; then
270       $ACLOCAL
271     else
272       $ACLOCAL 1>/dev/null 2>/dev/null
273     fi
274     if test $? != 0; then
275       ad_fatal "aclocal failed"
276     fi
277   fi
278
279   if test "$AUTOCONF"; then
280     ad_debug "Running autoconf"
281     $AUTOCONF
282     if test $? != 0; then
283       ad_fatal "autoconf failed"
284     fi
285     run_autoconf=true
286   fi
287
288   if test "$AUTOHEADER"; then
289     ad_debug "Running autoheader"
290     $AUTOHEADER
291     if test $? != 0; then
292       ad_fatal "autoheader failed"
293     fi
294   fi
295
296   if test "$LIBTOOLIZE"; then
297     ad_debug "Running libtoolize"
298     $LIBTOOLIZE
299     if test $? != 0; then
300       ad_fatal "libtoolize failed"
301     fi
302   fi
303
304   if test x$run_autoconf = xtrue; then
305     if test '!' -f configure; then
306       ad_fatal "creating configure script failed"
307     fi
308   fi
309
310   ad_debug "<ad_make_configure"
311 }
312
313 #
314 # Creates the configure.ac script from the configure.ad fragments in
315 # the source tree.  Takes the source configure file as argument which
316 # is used to create the actual configure.ac.
317 #
318 # Arguments: ad_make_configure_ac <configure_ac_source>
319 #
320 ad_make_configure_ac()
321 {
322   local check
323
324   ad_debug ">ad_make_configure_ac: $1"
325
326   if test '!' -f $1; then
327     ad_fatal "The configure file '$1' does not exist"
328   fi
329
330   check="`sed 's/^[     ]*//' < $1 | grep -v "^#" | grep -e "AD_INIT"`"
331   if test -z $check; then
332     rm -f configure.ad.cfs
333     rm -f $fname $fname.tmp
334     ad_fatal "The 'AD_INIT' macro has not been set in configure.ac"
335   fi
336
337   rm -f configure.ac configure.ad.cfs
338
339   cfs=`find . -name configure\*\.ad`
340   for i in $cfs
341   do
342     if test "x$i" = "x$1"; then
343       continue
344     fi
345     ad_debug "including $i"
346     cat $i >> configure.ad.cfs
347   done
348
349   if test -f configure.ad.cfs; then
350     check="`sed 's/^[   ]*//' < $1 | grep -v "^#" | grep -e "AD_INCLUDE_CONFIGURE"`"
351     if test -z $check; then
352       rm -f configure.ad.cfs
353       ad_warning "configure.ad fragments found but 'AD_INCLUDE_CONFIGURE' is not set"
354     fi
355   fi
356
357   # Header for configure.ac
358   fname="configure.tmp.ac"
359   cat > $fname <<EOF
360 # Automatically generated by Autodist $ver.  Do not edit.
361 # To make changes edit the configure.ad file in the source tree.
362
363 # Source: configure.ad
364 # Generated: `date` by `whoami`
365 # Distribution: $distribution
366 # License: $license
367
368 EOF
369
370   ad_debug "creating configure.ac"
371   if test -f configure.ad.cfs; then
372     sed '/AD_INCLUDE_CONFIGURE/ r configure.ad.cfs' $1 > $fname.tmp
373     sed -e "/AD_INCLUDE_CONFIGURE/s///" $fname.tmp >> $fname
374     rm -f configure.ad.cfs $fname.tmp
375   else
376     cat $1 >> $fname.tmp
377   fi
378
379   # Process AD_INIT
380   sed -e "/AD_INIT/s//AC_INIT([$distribution], [$dist_version], [$bug_report], [$package])/" $fname > $fname.tmp
381
382   # Process for distribution
383   rm -f $fname
384   ad_process_file $fname.tmp $fname
385
386   # Remove any trailing backslashes
387   if test -f "$fname"; then
388     sed -e :a -e '/\\$/N; s/[   ]*\\\n//; ta' < $fname > configure.ac
389   else
390     cp -p $fname.tmp configure.ac
391   fi
392   rm -f $fname $fname.tmp
393
394   ad_debug "<ad_make_configure_ac: $1"
395 }
396
397 #
398 # Creates the Makefile.in files by running the automake tool.
399 #
400 # Arguments: ad_make_makefile_ins
401 #
402 ad_make_makefile_ins()
403 {
404   ad_debug ">ad_make_makefile_ins"
405
406   if test "$AUTOMAKE"; then
407     ad_debug "Running automake"
408     $AUTOMAKE
409     if test $? != 0; then
410       ad_fatal "automake failed"
411     fi
412   fi
413
414   ad_debug "<ad_make_makefile_ins"
415 }
416
417 #
418 # Creates the Makefile.am files from the Makefile.ad files in the
419 # source tree.  This runs the distribution specific processing for the
420 # Makefile.ad files.
421 #
422 # Arguments: ad_make_makefile_ams
423 #
424 ad_make_makefile_ams()
425 {
426   ad_debug ">ad_make_makefile_ams"
427
428   files=`find . -name Makefile\.ad`
429   for ff in $files
430   do
431     fname=`echo $ff | sed s/\.ad//`
432
433     # Header for the Makefile.am
434   cat > $fname.am <<EOF
435 # Automatically generated by Autodist $ver from Makefile.ad.  Do not edit.
436 # To make changes edit the $ff file in the source tree.
437
438 # Source: $ff
439 # Generated: `date` by `whoami`
440 # Distribution: $distribution
441 # License: $license
442
443 EOF
444
445     # Run the distribution processing for this Makefile.ad
446     ad_debug "Processing $ff to be $fname.am"
447     ad_process_file $ff $fname.tmp
448
449     # Remove any trailing backslashes
450     if test -f "$fname.tmp"; then
451       sed -e :a -e '/\\$/N; s/[         ]*\\\n//; ta' < $fname.tmp >> $fname.am
452     else
453       cat $ff >> $fname.am
454     fi
455
456     rm -f $fname.tmp
457   done
458
459   ad_debug "<ad_make_makefile_ams"
460 }
461
462 #
463 # Processes all files with .ad suffix, with exception of configure*.ad
464 # and Makefile.ad files, for distribution from the source tree.
465 #
466 # Arguments: ad_process_ads
467 #
468 ad_process_ads()
469 {
470   ad_debug ">ad_process_ads"
471
472   files=`find . -name \*\.ad \! -name configure\*\.ad \! -name Makefile\.ad`
473   for i in $files
474   do
475     fname=`echo $i | sed s/\.ad//`
476
477     # Header
478   cat > $fname <<EOF
479 # Automatically generated by Autodist $ver.  Do not edit.
480 # To make changes edit the $i file in the source tree.
481
482 # Source: $i
483 # Generated: `date` by `whoami`
484 # Distribution: $distribution
485 # License: $license
486
487 EOF
488
489     # Run the distribution processing for this file
490     ad_debug "Processing $i to be $fname"
491     ad_process_file $i $fname
492   done
493
494   ad_debug "<ad_process_ads"
495 }
496
497 #
498 # Includes files specified in the distribution for inclusion.  Used when
499 # creating the distribution for packaging.
500 #
501 # include has the following format in distfile:
502 #
503 #   include <path> [<dest path>]
504 #
505 # If only source path, which may be file, directory or regular expression,
506 # is specified the path will be same in distribution.  If the destination
507 # path is specified that will be the new name and/or new location of the
508 # source path.  This, in effect, is a cp utility with ability to create
509 # directories if they do not exist.
510 #
511 # Arguments: ad_dist_includes <includeslist> <recursive>
512 #
513 ad_dist_includes()
514 {
515   local incs
516
517   ad_debug ">ad_dist_includes: $1 $2"
518
519   # By default do not expand pathnames
520   set -f
521
522   # Add : separator at the end
523   incs="`echo "$1" | sed 's/$/ : /'`"
524
525   src=
526   dst=
527   for i in $incs
528   do
529     if test "$i" = ":" && test -z "$src"; then
530       continue
531     fi
532     if test -z "$src"; then
533       src=$i
534       continue
535     fi
536     if test -z "$dst" && test "$i" != ":"; then
537       dst=$i
538     else
539       dst=$src
540     fi
541
542     ad_debug "Including $src into $dst"
543
544     if test -f "$src"; then
545       # Add file
546
547       if test "$src" = "$dst"; then
548         # Add to same location
549         d=`echo $src | sed 's,/[^/]*$,,'`
550         if test "$d" != "$src" && test "$d" != "." && \
551            test '!' -d $am_distdir/$d; then
552           mkdir -p $am_distdir/$d || exit 1
553         fi
554       else
555         # Add to different location
556         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
557         if test "$check" != "$dst"; then
558           ad_fatal "Invalid destination in 'include $src $dst'"
559         fi
560
561         d=`echo $dst | sed 's,/[^/]*$,,'`
562         if test "$d" != "$dst" && test "$d" != "." && \
563            test '!' -d $am_distdir/$d; then
564           mkdir -p $am_distdir/$d || exit 1
565         fi
566       fi
567
568       cp -p $src $am_distdir/$d || exit 1
569
570     elif test -d "$src"; then
571       # Add directory
572
573       if test "$src" = "$dst"; then
574         # Add to same location
575         d=`echo $src | sed 's,/[^/]*$,,'`
576         ds=`echo $src | sed 's/\/$//'`
577         if test "$ds" = "$d"; then
578           d=`echo $d | sed 's,/[^/]*$,,'`
579         fi
580         if test '!' -d $am_distdir/$d && test "$ds" != "$d"; then
581           mkdir -p $am_distdir/$d || exit 1
582         fi
583
584         cp -pR $src $am_distdir/$d || exit 1
585       else
586         # Add to different location
587         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
588         if test "$check" != "$dst"; then
589           ad_fatal "Invalid destination in 'include $src $dst'"
590         fi
591
592         d=`echo $dst | sed 's,/[^/]*$,,'`
593         ds=`echo $dst | sed 's/\/$//'`
594         if test "$ds" = "$d"; then
595           d=`echo $d | sed 's,/[^/]*$,,'`
596         fi
597         if test '!' -d $am_distdir/$d && test "$dst" != "$d"; then
598           mkdir -p $am_distdir/$d || exit 1
599         fi
600
601         cp -pR $src $am_distdir/$dst || exit 1
602       fi
603
604     elif test x$2 != xtrue; then
605       # We assume regular expression in filename
606       check=`echo "$src" | sed 's/?//; s/*//; s/\[//; s/\]//'`
607       if test "$check" == "$src"; then
608         if test '!' -a $src; then
609           ad_fatal "Including $src: No such file or directory"
610         fi
611         src=
612         dst=
613         continue
614       fi
615
616       # Recursively call this function with expanded pathnames.  The
617       # reason why we don't let sh by default expand pathnames is that
618       # the include's destination is optional.  If sh expands by default
619       # we don't know the destination.  For this reason, we handle the
620       # expansion here ourselves.
621
622       # If src and dst are same, then expand the pathname as we'll copy
623       # matches to their own locations.
624       if test "$src" = "$dst"; then
625         # Expand pathnames, and format to our include format
626         set +f
627         srcs=`echo $src | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
628         set -f
629       else
630         # Destination is new, and it has to be a directory.
631         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
632         if test "$check" != "$dst"; then
633           ad_fatal "Invalid destination in 'include $src $dst'"
634         fi
635
636         # Make sure dst has / at the end, as this must be a directory
637         dst=`echo $dst | sed 's/\/$//; s/$/\//'`
638
639         # Escape dst for sed
640         dste=`echo $dst | sed 's/\\//\\\\\//g'` || exit 1
641
642         # Expand pathnames, and format to our include format
643         set +f
644         srcs=`echo $src | sed -e "s/ / $dste : /g" \
645           -e 's/^/ : /' -e "s/$/ $dste/"` || exit 1
646         set -f
647       fi
648
649       # Include recursively
650       ad_dist_includes "$srcs" true
651
652     elif test '!' -a $src; then
653       ad_fatal "Including $src: No such file or directory"
654     fi
655
656     src=
657     dst=
658   done
659
660   set +f
661
662   ad_debug "<ad_dist_includes: $1 $2"
663 }
664
665 #
666 # Excludes files specified in the distribution for exclusion.  Used when
667 # creating the distribution for packaging.
668 #
669 # exclude has the following format in distfile:
670 #
671 #  exclude <path>
672 #
673 # The path may be file, directory or regular expression.
674 #
675 # Arguments: ad_dist_includes <excludelist>
676 #
677 ad_dist_excludes()
678 {
679   ad_debug ">ad_dist_excludes: $1"
680
681   for i in $1
682   do
683     ad_debug "Excluding $i"
684     rm -rf $am_distdir/$i
685   done
686
687   ad_debug "<ad_dist_excludes: $1"
688 }
689
690 #
691 # Processes the entire tree for distribution.  This inspects files other
692 # than source and header files, with exception of any file with .ad
693 # suffix, and performs distribution processing for the file.  The original
694 # file is replaced with the processed file.  This function is used when
695 # creating the distribution for packaging.
696 #
697 # Arguments: ad_process_tree <directory>
698 #
699 ad_process_tree()
700 {
701   ad_debug ">ad_process_tree: $1"
702
703   # We take all files, other than *.ad, including source files, in case
704   # they use the non-C-compiler-friendly format of distdefs, which is
705   # possible.
706
707   files=`find $am_distdir \! -name \*\.ad`
708   files=`echo $files | sed 's/$am_distdir//'`
709
710   # Take away noprocess list
711   if test -f autodist.noprocess; then
712     files=`echo $files | sh autodist.noprocess` || exit 1
713   fi
714
715   for ff in $files
716   do
717     ad_process_file $ff $ff.tmp
718     if test -f $ff.tmp; then
719       rm -f $ff || exit 1
720       mv -f $ff.tmp $ff || exit 1
721     fi
722   done
723
724   ad_debug "<ad_process_tree: $1"
725 }
726
727 #
728 # Processes the entire source tree for distribution.  This inspects files
729 # in the source tree, with exception of any file with .ad suffix, and
730 # performs distribution processing for the file.  The original file is
731 # replaced with the processed file.  This function is used when creating
732 # the distribution for packaging.
733 #
734 # Call this before ad_process_tree().
735 #
736 # Arguments: ad_process_source_tree <directory>
737 #
738 ad_process_source_tree()
739 {
740   ad_debug ">ad_process_source_tree: $1"
741
742   # We take only C/C++ files since they use the C compiler friendly
743   # version of distdefs.  Other files are not assumed to use them.
744   files=`find $am_distdir \! -name \*\.ad \( \
745         -name \*\.[cC] -o \
746         -name \*\.[cC][cCpP] -o \
747         -name \*\.[cC][xX][xX] -o \
748         -name \*\.[cC][pP][pP] -o \
749         -name \*\.[cC]++ -o \
750         -name \*\.m -o \
751         -name \*\.[hH] -o \
752         -name \*\.hh \)`
753
754   # Take away noprocess list
755   if test -f autodist.noprocess; then
756     files=`echo $files | sh autodist.noprocess` || exit 1
757   fi
758
759   for ff in $files
760   do
761     ad_process_source_file $ff $ff.tmp
762     if test -f $ff.tmp; then
763       rm -f $ff || exit 1
764       mv -f $ff.tmp $ff || exit 1
765     fi
766   done
767
768   ad_debug "<ad_process_source_tree: $1"
769 }
770
771 #
772 # Makes distribution sane, ala modtimes.  Since we modify the distribution
773 # we need to make it sane after that.
774 #
775 # Arguments: ad_makedist_makesane
776 #
777 ad_makedist_makesane()
778 {
779   ad_debug ">ad_makedist_makesane:"
780
781   # DO NOT change these order unless you know what you are doing.
782   if test -f $am_distdir/configure.ac; then
783     touch $am_distdir/configure.ac
784   fi
785
786   if test -f $am_distdir/aclocal.m4; then
787     touch $am_distdir/aclocal.m4
788   fi
789
790   if test '!' -f Makefile; then
791     ad_fatal "Makefile: No such file or directory"
792   fi
793
794   configh=`grep "^CONFIG_HEADER" Makefile | cut -d= -f2 | sed 's/^[     ]*//'`
795   touch $am_distdir/$configh.in 1>/dev/null 2>/dev/null
796
797   files=`find $am_distdir -name Makefile\.in`
798   for i in $files
799   do
800     touch $i
801   done
802
803   if test -f $am_distdir/configure; then
804     touch $am_distdir/configure
805   fi
806
807   if test -f $am_distdir/config.status; then
808     touch $am_distdir/config.status
809   fi
810
811   ad_debug "<ad_makedist_makesane:"
812 }
813
814 #
815 # Creates distribution of the source tree.  All files in the distribution
816 # will be processed and the distribution will be packaged.
817 #
818 # Arguments: ad_makedist
819 #
820 ad_makedist()
821 {
822   ad_debug ">ad_makedist"
823
824   if test '!' -f autodist.dist; then
825     ad_fatal "Autodist has not been run yet to prepare source tree"
826   fi
827
828   if test -z $MAKE; then
829     ad_fatal "The MAKE variable is not set in autodist.conf"
830   fi
831
832   # Get distdir from Makefile
833   if test '!' -f Makefile; then
834     ad_fatal "The source tree is not configured, run ./configure first"
835   fi
836
837   # Parse the requested distribution
838   distribution=`grep "dist:" < autodist.dist | cut -d: -f2` || exit 1
839   dist_version=`grep "ver:" < autodist.dist | cut -d: -f2` || exit 1
840   am_distdir=`grep "distdir:" < autodist.dist | cut -d: -f2` || exit 1
841   ad_parse_distribution $distribution false
842   ad_process_distdefs
843
844   if test x$opt_no_dist = xtrue; then
845     ad_fatal "The '$distribution' distribution cannot be packaged"
846   fi
847
848   # Run pre-dist-hooks
849   ad_run_dist_hooks "$pre_dist_hooks"
850
851   # Create distribution directory
852   ad_debug "Creating distribution directory $am_distdir"
853   $MAKE distdir || exit 1
854   chmod -R a+r $am_distdir
855
856   if test '!' -d $am_distdir; then
857     ad_fatal "Distribution directory $am_distdir not created"
858   fi
859
860   # Run pre-process-dist-hooks
861   ad_run_dist_hooks "$pre_p_dist_hooks"
862
863   # Run excludes
864   ad_dist_excludes "$excludes"
865
866   # Run includes
867   ad_dist_includes "$includes"
868
869   # Include specific license file if specified
870   if test "$license" != ""; then
871     cp -p $license $am_distdir/COPYING || exit 1
872   fi
873
874   # Process noprocesses
875   ad_process_noprocess
876
877   # Process source files
878   ad_debug "Process distribution source tree"
879   ad_process_source_tree $am_distdir
880
881   # Process non-source files
882   ad_debug "Process distribution tree"
883   ad_process_tree $am_distdir
884
885   # Run post-process_dist-hooks
886   ad_run_dist_hooks "$post_p_dist_hooks"
887
888   # Make distribution sane
889   ad_makedist_makesane
890
891   # Package
892   ad_debug "Packaging distribution"
893   tar chof $am_distdir.tar $am_distdir || exit 1
894
895   # Compress
896   ad_debug "Compressing distribution package"
897   if test x$ad_gzip = xtrue; then
898     gzip -9 -c $am_distdir.tar > $am_distdir.tar.gz || exit 1
899   fi
900   if test x$ad_bzip2 = xtrue; then
901     bzip2 -9 -c $am_distdir.tar > $am_distdir.tar.bz2 || exit 1
902   fi
903   if test x$ad_compress = xtrue; then
904     compress -c $am_distdir.tar > $am_distdir.tar.Z || exit 1
905   fi
906   if test x$ad_zip = xtrue; then
907     rm -f $am_distdir.zip
908     zip -rq $am_distdir.zip $am_distdir || exit 1
909   fi
910   rm -f $am_distdir.tar
911
912   # Run post-dist-hooks
913   ad_run_dist_hooks "$post_dist_hooks"
914
915   # Cleanup
916   rm -rf $am_distdir
917   rm -f autodist.noprocess
918
919   ad_debug "<ad_makedist"
920 }
921
922 #
923 # Handles distribution options.
924 #
925 # option has the following format in distfile:
926 #
927 #   option <option>
928 #
929 # Following options are supported:
930 #
931 #   template
932 #   no-dist
933 #   no-inherit
934 #
935 # Arguments: ad_handle_options <options>
936 #
937 ad_handle_options()
938 {
939   ad_debug ">ad_handle_options: $1"
940
941   for i in $1
942   do
943     if test "$i" = "template"; then
944       opt_template=true
945       continue
946     elif test "$i" = "no-dist"; then
947       opt_no_dist=true
948       continue
949     elif test "$i" = "no-inherit"; then
950       opt_no_inherit=true
951       continue
952     fi
953   done
954
955   ad_debug "<ad_handle_options: $1"
956 }
957
958 #
959 # Clears set options
960 #
961 # Arguments: ad_clear_options
962 #
963 ad_clear_options()
964 {
965   ad_debug ">ad_clear_options"
966   opt_template=false
967   opt_no_dist=false
968   opt_no_inherit=false
969   ad_debug "<ad_clear_options"
970 }
971
972 #
973 # Parses the distribution.  Gets all distribution defines from the
974 # distribution.  This also expands all inherited distributions recursively
975 # to get all inherited distribution defines.  From inherited distributions
976 # their name and package name is not inherited.
977 #
978 # Arguments: ad_parse_distribution <distribution name> <inherit>
979 #
980 ad_parse_distribution()
981 {
982   local inhs
983   local defs
984   local undefs
985   local incs
986   local excs
987   local nops
988   local opts
989   local dname
990   local dpname
991   local bugr
992   local prh
993   local poh
994   local prdh
995   local podh
996   local prpdh
997   local popdh
998
999   ad_debug ">ad_parse_distribution: $1 $2"
1000
1001   if test '!' -f $distdir/$1; then
1002     ad_fatal "Distribution '$1' is not declared"
1003   fi
1004
1005   # Get inherited
1006   inhs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1007     | grep "inherit " | cut -d' ' -f2 | sort | uniq`
1008
1009   # Get distdefs
1010   defs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1011    | grep "define " | cut -d' ' -f2 | sort | uniq`
1012
1013   if test "$inhs" = "" && test "$defs" = ""; then
1014     ad_fatal "Distribution '$1' does not define anything"
1015   fi
1016
1017   # Get undefined distdefs
1018   undefs=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1019    | grep "undef " | cut -d' ' -f2 | sort | uniq`
1020
1021   # Get includes
1022   incs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1023     | grep "include " | sed 's/include / : /'`
1024
1025   # Get excludes
1026   excs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1027     | grep "exclude " | cut -d' ' -f2- | sort | uniq`
1028
1029   # Get noprocesses
1030   nops=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1031     | grep "noprocess " | cut -d' ' -f2- | sort | uniq`
1032
1033   # Get options
1034   opts=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1035     | grep "option " | cut -d' ' -f2- | sort | uniq`
1036
1037   # Check options
1038   ad_handle_options "$opts"
1039   if test x$2 = xtrue && test x$opt_no_inherit = xtrue; then
1040     ad_fatal "Distribution '$1' cannot be inherited"
1041   fi
1042   if test x$2 = xfalse && test x$opt_template = xtrue; then
1043     ad_fatal "Template distribution '$1' cannot be prepared or packaged"
1044   fi
1045
1046   ad_debug "inherits: $inhs"
1047   ad_debug "distdefs: $defs"
1048   ad_debug "includes: $incs"
1049   ad_debug "excludes: $excs"
1050   ad_debug "noprocess: $nops"
1051   ad_debug "undistdefs: $undefs"
1052   ad_debug "options: $opts"
1053
1054   # Expand distdefs from inherited distributions
1055   for i in $inhs
1056   do
1057     if test x$1 = x$i; then
1058       ad_fatal "Infinite recursion detected.  Fix the '$distdir/$1' \
1059             distribution to not have 'inherit $i' declared."
1060     fi
1061
1062     if test '!' -f $distdir/$i; then
1063       ad_fatal "Distribution '$i' is not declared (inherited from '$1')"
1064     fi
1065
1066     ad_parse_distribution $i true
1067     ad_clear_options
1068   done
1069
1070   # Get license
1071   license=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1072     | grep "license " | cut -d' ' -f2`
1073   licenseh=`sed 's/^[   ]*//' < $distdir/$1 | grep -v "^#" \
1074     | grep "license-header " | sed 's/license-header / : /'`
1075
1076   ad_debug "license: $license"
1077   ad_debug "licenseh: $licenseh"
1078
1079   if test x$2 = xfalse; then
1080     # Get distribution name
1081     dname=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1082       | grep "name " | cut -d' ' -f2-`
1083
1084     if test "$dname"; then
1085       distribution=$dname
1086     fi
1087
1088     # Get distribution package name (optional)
1089     dpname=`sed 's/^[   ]*//' < $distdir/$1 | grep -v "^#" \
1090      | grep "package " | cut -d' ' -f2`
1091
1092     if test "$dpname"; then
1093       package=$dpname
1094     else
1095       package=$distribution
1096     fi
1097
1098     # Get Bug-report email address (optional)
1099     bugr=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1100      | grep "bug-report " | cut -d' ' -f2-`
1101
1102     if test "$bugr"; then
1103       bug_report=$bugr
1104     fi
1105
1106     ad_debug "distribution: $distribution"
1107     ad_debug "package: $package"
1108     ad_debug "bug-report: $bug_report"
1109
1110     # Get hooks (optional)
1111     prh=`sed 's/^[      ]*//' < $distdir/$1 | grep -v "^#" \
1112      | grep "pre-hook " | cut -d' ' -f2-`
1113     poh=`sed 's/^[      ]*//' < $distdir/$1 | grep -v "^#" \
1114      | grep "post-hook " | cut -d' ' -f2-`
1115     prdh=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1116      | grep "pre-dist-hook " | cut -d' ' -f2-`
1117     podh=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1118      | grep "post-dist-hook " | cut -d' ' -f2-`
1119     prpdh=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1120      | grep "pre-process-dist-hook " | cut -d' ' -f2-`
1121     popdh=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1122      | grep "post-process-dist-hook " | cut -d' ' -f2-`
1123
1124     pre_hooks="$pre_hooks $prh"
1125     post_hooks="$post_hooks $poh"
1126     pre_dist_hooks="$pre_dist_hooks $prdh"
1127     post_dist_hooks="$post_dist_hooks $podh"
1128     pre_p_dist_hooks="$pre_p_dist_hooks $prpdh"
1129     post_p_dist_hooks="$post_p_dist_hooks $popdh"
1130     doptions="$doptions $opts"
1131
1132     ad_handle_options "$doptions"
1133     ad_debug "options: $doptions"
1134   fi
1135
1136   # Return to caller
1137   inherits="$inherits $inhs"
1138   distdefs="$distdefs $defs"
1139   includes="$includes $incs"
1140   excludes="$excludes $excs"
1141   noprocess="$noprocess $nops"
1142   undistdefs="$undistdefs $undefs"
1143
1144   ad_debug "<ad_parse_distribution: $1 $2"
1145 }
1146
1147 #
1148 # Processes parsed distdefs.  Removes duplicates, and undefined distdefs
1149 # from the distdefs.
1150 #
1151 # Arguments: ad_process_distdefs
1152 #
1153 ad_process_distdefs()
1154 {
1155   ad_debug ">ad_process_distdefs"
1156
1157   # Remove all undefined distribution defines
1158   for i in $undistdefs
1159   do
1160     ad_debug "undefining $i distdef"
1161     distdefs=`echo $distdefs | sed s/$i//`
1162   done
1163
1164   rm -f autodist.tmp.defs
1165
1166   # Remove duplicate distdefs
1167   for i in $distdefs
1168   do
1169     echo $i >>autodist.tmp.defs
1170   done
1171   distdefs=`sort < autodist.tmp.defs | uniq`
1172   distdefs=`echo $distdefs`
1173   rm -f autodist.tmp.defs
1174
1175   ad_debug "distdefs=$distdefs"
1176
1177   ad_debug "<ad_process_distdefs"
1178 }
1179
1180 #
1181 # Processes for a license header change.
1182 #
1183 # Arguments: ad_process_license_header <scriptfile>
1184 #
1185 ad_process_license_header()
1186 {
1187   ad_debug ">ad_process_license_header"
1188
1189   # Add : separator at the end
1190   lics=`echo "$licenseh" | sed 's/$/ : /'`
1191
1192   src=
1193   dst=
1194   for i in $lics
1195   do
1196     if test "$i" = ":" && test -z "$src"; then
1197       continue
1198     fi
1199     if test -z "$src"; then
1200       src=$i
1201       continue
1202     fi
1203     if test -z "$dst" && test "$i" != ":"; then
1204       dst=$i
1205     else
1206       ad_fatal "Missing argument in 'license-header $src'"
1207     fi
1208
1209     ad_debug "Replacing $src license with $dst license"
1210
1211     if test '!' -f $src; then
1212       ad_fatal "License header $src: No such file or directory"
1213     fi
1214
1215     if test '!' -f $dst; then
1216       ad_fatal "License header $dst: No such file or directory"
1217     fi
1218
1219     # Awk script to replace the license header
1220     fl=`sed q $src | sed 's/\\//\\\\\//g' > autodist.lsrc` || exit 1
1221     ll=`sed -n '$p' $src | sed 's/\\//\\\\\//g' > autodist.ldst` || exit 1
1222     echo "/`cat autodist.lsrc`/,/`cat autodist.ldst`/ { FILE1=\"$src\"; FILE2=\"$dst\"; getline F1 < FILE1; getline F2 < FILE2; if (F1) sub(F1, F2); else { F1=\"\$\"; sub(F1, F2); } }" >> $1
1223     rm -f autodist.lsrc autodist.ldst
1224
1225     src=
1226     dst=
1227   done
1228
1229   ad_debug "<ad_process_license_header"
1230 }
1231
1232 #
1233 # Process specified noprocesses.  This is called during makedist.
1234 #
1235 # Arguments: ad_process_noprocess
1236 #
1237 ad_process_noprocess()
1238 {
1239   local np=false
1240   local n="sed "
1241
1242   ad_debug ">ad_process_noprocess"
1243
1244   for i in $noprocess
1245   do
1246     # Escape
1247     ie=`echo $i | sed 's/\\//\\\\\//g'` || exit 1
1248
1249     n="$n -e 's/$am_distdir\\/$ie//'"
1250     np=true
1251   done
1252
1253   rm -f autodist.noprocess
1254   if test x$np = xtrue; then
1255     echo $n > autodist.noprocess || exit 1
1256   fi
1257
1258   ad_debug "<ad_process_noprocess"
1259 }
1260
1261 #
1262 # Process a file given as argument for the distribution.
1263 #
1264 # Arguments: ad_process_file <filepath> <dest_filepath>
1265 #
1266 ad_process_file()
1267 {
1268   local found=false
1269   local f
1270   local defs
1271   local ndefs
1272
1273   # Process only regular files
1274   if test '!' -f $1; then
1275     return
1276   fi
1277
1278   ad_debug ">ad_process_file: $1 $2"
1279
1280   f="autodist.tmp.script"
1281   rm -f $f
1282
1283   # If license header is provided, replace the license header in the file.
1284   ad_process_license_header $f
1285
1286   ad_debug "Getting #ifdef's and #ifndef's"
1287
1288   # Get defined distribution defines
1289   defs=`awk "/^#ifdef "$DP"_DIST_|^#else "$DP"_DIST_/ { print; }" \
1290     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
1291
1292   # Get explicitly not-defined distribution defines
1293   ndefs=`awk "/^#ifndef "$DP"_DIST_|^#else !"$DP"_DIST_/ { print; }" \
1294     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
1295
1296   ad_debug "defs in $1: $defs"
1297   ad_debug "ndefs in $1: $ndefs"
1298
1299   # Create the script to include and exclude stuff in the file according
1300   # to the distribution defines
1301
1302   # ifdefs
1303   ad_debug "processing ifdefs"
1304   for d in $defs
1305   do
1306     found=false
1307     for i in $distdefs
1308     do
1309       if test x$d = x$i; then
1310         found=true
1311         break
1312       fi
1313     done
1314
1315     # If distribution define was not found exclude those lines from the file.
1316     # This also handles the #ifdef's #else (ie. #ifndef) branch.
1317     if test x$found = xfalse; then
1318       ad_debug "ifdef $d will be excluded (it is NOT defined)"
1319       echo "/^#ifdef $d/,/^#else !$d|^#endif $d/ { next; }" >> $f
1320     else
1321       echo "/^#else !$d/,/^#endif $d/ { next; }" >> $f
1322     fi
1323   done
1324
1325   # ifndefs
1326   ad_debug "processing ifndefs"
1327   for d in $ndefs
1328   do
1329     found=false
1330     for i in $distdefs
1331     do
1332       if test x$d = x$i; then
1333         found=true
1334         break
1335       fi
1336     done
1337
1338     # If distribution define was found exclude those lines from the file.
1339     # This also handles the #ifndef's #else (ie. #ifdef) branch.
1340     if test x$found = xtrue; then
1341       ad_debug "ifndef $d will be excluded (it IS defined)"
1342       echo "/^#ifndef $d/,/^#else $d|^#endif $d/ { next; }" >> $f
1343     else
1344       echo "/^#else $d/,/^#endif $d/ { next; }" >> $f
1345     fi
1346   done
1347
1348   # Now process the file with the script
1349   if test -f $f; then
1350
1351     # Those distdef lines that remain in the file are removed to make
1352     # the appearance prettier
1353     echo "/^#ifdef "$DP"_DIST_|^#endif "$DP"_DIST_|^#else "$DP"_DIST_|^#else !"$DP"_DIST_|^#ifndef "$DP"_DIST_/ { next; }" >> $f
1354     echo "{ print; }" >> $f
1355
1356     # Execute the script
1357     cp -p $1 $2 || exit 1
1358     awk -f $f $1 > $2 || exit 1
1359   fi
1360
1361   rm -f $f
1362
1363   ad_debug "<ad_process_file: $1 $2"
1364 }
1365
1366 #
1367 # Process a source file given as argument for the distribution.
1368 #
1369 # Arguments: ad_process_source_file <filepath> <dest_filepath>
1370 #
1371 ad_process_source_file()
1372 {
1373   local found=false
1374   local f
1375   local defs
1376   local ndefs
1377
1378   # Process only regular files
1379   if test '!' -f $1; then
1380     return
1381   fi
1382
1383   ad_debug ">ad_process_source_file: $1 $2"
1384
1385   f="autodist.tmp.script"
1386   rm -f $f
1387
1388   # If license header is provided, replace the license header in the file.
1389   ad_process_license_header $f
1390
1391   ad_debug "Getting #ifdef's and #ifndef's"
1392
1393   # Get defined distribution defines
1394   defs=`awk '/^#ifdef SILC_DIST_|^#else \/\* SILC_DIST_/ { print; }' \
1395     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
1396
1397   # Get explicitly not-defined distribution defines
1398   ndefs=`awk '/^#ifndef SILC_DIST_|^#else \/\* \!SILC_DIST_/ { print; }' \
1399     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
1400
1401   ad_debug "defs in $1: $defs"
1402   ad_debug "ndefs in $1: $ndefs"
1403
1404   # Create the script to include and exclude stuff in the file according
1405   # to the distribution defines
1406
1407   # ifdefs
1408   ad_debug "processing ifdefs"
1409   for d in $defs
1410   do
1411     found=false
1412     for i in $distdefs
1413     do
1414       if test x$d = x$i; then
1415         found=true
1416         break
1417       fi
1418     done
1419
1420     # If distribution define was not found exclude those lines from the file.
1421     # This also handles the #ifdef's #else (ie. #ifndef) branch.
1422     if test x$found = xfalse; then
1423       ad_debug "ifdef $d will be excluded (it is NOT defined)"
1424       echo "/^#ifdef $d/,/^#else \/\* \!$d|^#endif \/\* $d/ { next; }" >> $f
1425     else
1426       echo "/^#else \/\* \!$d/,/^#endif \/\* $d/ { next; }" >> $f
1427     fi
1428   done
1429
1430   # ifndefs
1431   ad_debug "processing ifndefs"
1432   for d in $ndefs
1433   do
1434     found=false
1435     for i in $distdefs
1436     do
1437       if test x$d = x$i; then
1438         found=true
1439         break
1440       fi
1441     done
1442
1443     # If distribution define was found exclude those lines from the file.
1444     # This also handles the #ifndef's #else (ie. #ifdef) branch.
1445     if test x$found = xtrue; then
1446       ad_debug "ifndef $d will be excluded (it IS defined)"
1447       echo "/^#ifndef $d/,/^#else \/\* $d|^#endif \/\* $d/ { next; }" >> $f
1448     else
1449       echo "/^#else \/\* $d/,/^#endif \/\* $d/ { next; }" >> $f
1450     fi
1451   done
1452
1453   # Now process the file with the script
1454   if test -f $f; then
1455
1456     # Those distdef lines that remain in the file are removed to make
1457     # the appearance prettier
1458     echo "/^#ifdef SILC_DIST_|^#endif \/\* SILC_DIST_|^#else \/\* SILC_DIST_|^#else \/\* \!SILC_DIST_|^#ifndef SILC_DIST_/ { next; }" >> $f
1459     echo "{ print; }" >> $f
1460
1461     # Execute the script
1462     cp -p $1 $2 || exit 1
1463     awk -f $f $1 > $2 || exit 1
1464   fi
1465
1466   rm -f $f
1467
1468   ad_debug "<ad_process_source_file: $1 $2"
1469 }
1470
1471 #
1472 # Run hooks
1473 #
1474 # Arguments: ad_run_hooks <hooks>
1475 #
1476 ad_run_hooks()
1477 {
1478   ad_debug ">ad_run_hooks: $1"
1479
1480   for i in $1
1481   do
1482     if test '!' -f $i; then
1483       ad_fatal "Hook script $i does not exist"
1484     fi
1485     sh $i "$distribution" "$dist_version" "$package" || exit 1
1486   done
1487
1488   ad_debug "<ad_run_hooks: $1"
1489 }
1490
1491 #
1492 # Run dist hooks
1493 #
1494 # Arguments: ad_run_dist_hooks <hooks>
1495 #
1496 ad_run_dist_hooks()
1497 {
1498   ad_debug ">ad_run_dist_hooks: $1"
1499
1500   for i in $1
1501   do
1502     if test '!' -f $i; then
1503       ad_fatal "Dist hook script $i does not exist"
1504     fi
1505     sh $i "$distribution" "$dist_version" "$package" "$am_distdir" || exit 1
1506   done
1507
1508   ad_debug "<ad_run_dist_hooks: $1"
1509 }
1510
1511 ###############################################################################
1512 # Autodist code
1513
1514 usage="Usage: autodist [options] [distribution] [version]"
1515 help="\
1516 Autodist prepares source tree for configuration, compilation and
1517 distribution.  Prepares the source tree from the \`autodist.ad'
1518 configuration file.  Generates Automake.am files from Automake.ad
1519 files, configure.ac file from configure.ad file(s), generates the
1520 configure script by running Autoconf tool, and generates Makefile.in
1521 files by running Automake tool.
1522
1523 Operation modes:
1524   -h, --help                print this help, then exit
1525   -V, --version             print version number, then exit
1526   -v, --verbose             verbosely report processing
1527   -d, --distdir <dir>       search distributions from <dir>
1528   -s, --distdefs [<dist>]   print distribution defines of <dist>, then exit
1529   -i, --init                initialize Autodist environment, create default
1530                             distribution directory and distribution, then exit
1531   -m, --makedist            create and package distribution
1532       --gzip                create package compressed with gzip (default)
1533       --bzip2               create also package compressed with bzip2
1534       --compress            create also package compressed with compress
1535       --zip                 create also package compressed with zip"
1536
1537 #
1538 # Process command line arguments
1539 #
1540 while test $# -gt 0; do
1541   case "${1}" in
1542
1543   -d |--distdir)
1544     shift;
1545     test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
1546     distdir="${1}";
1547     shift;;
1548
1549   --list)
1550     exit 0;;
1551
1552   -s | --distdefs)
1553     shift;
1554     if test $# -eq 0; then
1555       ad_parse_distribution $distribution false
1556       echo "Distribution: ${distribution}" 1>&2;
1557     else
1558       ad_parse_distribution $1 false
1559       echo "Distribution: ${1}" 1>&2;
1560     fi
1561     ad_process_distdefs
1562     echo "Distdefs:" 1>&2;
1563     echo "${distdefs}";
1564     exit 0;;
1565
1566   -i | --init)
1567     ad_initialize;
1568     exit 0;;
1569
1570   -m | --makedist)
1571     ad_makedist
1572     exit 0;;
1573
1574   --gzip)
1575     ad_gzip=true
1576     shift;;
1577
1578   --bzip2)
1579     ad_bzip2=true
1580     shift;;
1581
1582   --compress)
1583     ad_compress=true
1584     shift;;
1585
1586   --zip)
1587     ad_zip=true
1588     shift;;
1589
1590   -v | --verbose)
1591     debug=true
1592     shift;;
1593
1594   -h | --help | --h*)
1595     echo "${usage}" 1>&2;
1596     echo 1>&2;
1597     echo "${help}" 1>&2;
1598     echo 1>&2;
1599     exit 0;;
1600
1601   -V | --version)
1602     echo "@PACKAGE@ (@PACKAGE_NAME@) $ver" 1>&2;
1603     echo "Written by Pekka Riikonen" 1>&2;
1604     echo 1>&2;
1605     echo "Copyright (C) 2004 - 2005 SILC Project" 1>&2;
1606     echo "\
1607 This is free software; see the source for copying conditions.  There is NO
1608 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " 1>&2;
1609     exit 0;;
1610
1611   --)
1612     shift;
1613     break;;
1614
1615   -*)
1616     echo "${usage}" 1>&2;
1617     exit 1;;
1618
1619   *)
1620     break;;
1621
1622   esac
1623 done
1624
1625 #
1626 # Parse the requested distribution
1627 #
1628 if test $# != 0; then
1629   distribution="${1}";
1630   distfile=$distribution
1631   shift
1632 fi
1633 ad_parse_distribution $distribution false
1634 ad_process_distdefs
1635
1636 if test $# != 0; then
1637   dist_version="${1}";
1638 fi
1639
1640 ad_debug "Preparing source tree for configuration and compilation..."
1641 ad_debug "Preparing $distribution distribution version $dist_version"
1642
1643 #
1644 # Create the distribution defines header file
1645 #
1646 if test "$DISTDEFS"; then
1647   ad_create_distdefs_h
1648 else
1649   ad_fatal "DISTDEFS not defined in $distdir/autodist.conf"
1650 fi
1651
1652 #
1653 # Run pre-hooks
1654 #
1655 ad_run_hooks "$pre_hooks"
1656
1657 #
1658 # Generate the Makefile.am files from Makefile.ad files
1659 #
1660 ad_make_makefile_ams
1661
1662 #
1663 # Generate the configure.ac from configure.ad file(s)
1664 #
1665 ad_make_configure_ac ./configure.ad
1666
1667 #
1668 # Process all files with .ad suffix for distribution processing
1669 #
1670 ad_process_ads
1671
1672 #
1673 # Generate configure script
1674 #
1675 ad_make_configure
1676
1677 #
1678 # Generate Makefile.in files
1679 #
1680 ad_make_makefile_ins
1681
1682 #
1683 # Create autodist.dist
1684 #
1685 ad_debug "Creating autodist.dist"
1686 echo "dist:$distfile" > autodist.dist
1687 echo "ver:$dist_version" >> autodist.dist
1688 echo "distdir:$package-$dist_version" >> autodist.dist
1689
1690 #
1691 # Run post-hooks
1692 #
1693 ad_run_hooks "$post_hooks"
1694
1695 ad_debug "Done, now run ./configure and make."
1696 exit 0