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