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