Header changes.
[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 . -type f -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 . -type f -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 . -type f -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> <log>
556 #
557 ad_dist_includes()
558 {
559   local incs
560
561   ad_debug "Starting running includes: $1 $2"
562
563   if test x$3 = xtrue; then
564     ad_log ""
565     ad_log "Following files and directories were included in distribution:"
566     ad_log "--------------------------------------------------------------"
567   fi
568
569   # By default do not expand pathnames
570   set -f
571
572   # Add : separator at the end
573   incs="`echo "$1" | sed 's/$/ : /'`"
574
575   src=
576   dst=
577   for i in $incs
578   do
579     if test "$i" = ":" && test -z "$src"; then
580       continue
581     fi
582     if test -z "$src"; then
583       src=$i
584       continue
585     fi
586     if test -z "$dst" && test "$i" != ":"; then
587       dst=$i
588     else
589       dst=$src
590     fi
591
592     ad_debug "Including $src into $dst"
593
594     if test -f "$src"; then
595       # Add file
596
597       if test "$src" = "$dst"; then
598         # Add to same location
599         d=`echo $src | sed 's,/[^/]*$,,'`
600         if test "$d" != "$src" && test "$d" != "." && \
601            test '!' -d $am_distdir/$d; then
602           mkdir -p $am_distdir/$d || exit 1
603         fi
604       else
605         # Add to different location
606         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
607         if test "$check" != "$dst"; then
608           ad_fatal "Invalid destination in 'include $src $dst'"
609         fi
610
611         d=`echo $dst | sed 's,/[^/]*$,,'`
612         if test "$d" != "$dst" && test "$d" != "." && \
613            test '!' -d $am_distdir/$d; then
614           mkdir -p $am_distdir/$d || exit 1
615         fi
616       fi
617
618       if test x$3 = xtrue; then
619         ad_log "  $src into $am_distdir/$d"
620       fi
621       cp -p $src $am_distdir/$d || exit 1
622
623     elif test -d "$src"; then
624       # Add directory
625
626       if test "$src" = "$dst"; then
627         # Add to same location
628         d=`echo $src | sed 's,/[^/]*$,,'`
629         ds=`echo $src | sed 's/\/$//'`
630         if test "$ds" = "$d"; then
631           d=`echo $d | sed 's,/[^/]*$,,'`
632         fi
633         if test "$ds" = "$d"; then
634           d=""
635         fi
636         if test '!' -d $am_distdir/$d && test "$ds" != "$d"; then
637           mkdir -p $am_distdir/$d || exit 1
638         fi
639
640         if test x$3 = xtrue; then
641           ad_log "  $src into $am_distdir/$d"
642         fi
643         cp -pR $src $am_distdir/$d || exit 1
644       else
645         # Add to different location
646         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
647         if test "$check" != "$dst"; then
648           ad_fatal "Invalid destination in 'include $src $dst'"
649         fi
650
651         d=`echo $dst | sed 's,/[^/]*$,,'`
652         ds=`echo $dst | sed 's/\/$//'`
653         if test "$ds" = "$d"; then
654           d=`echo $d | sed 's,/[^/]*$,,'`
655         fi
656         if test '!' -d $am_distdir/$d && test "$dst" != "$d"; then
657           mkdir -p $am_distdir/$d || exit 1
658         fi
659
660         if test x$3 = xtrue; then
661           ad_log "  $src into $am_distdir/$dst"
662         fi
663         cp -pR $src $am_distdir/$dst || exit 1
664       fi
665
666     elif test x$2 != xtrue; then
667       # We assume regular expression in filename
668       check=`echo "$src" | sed 's/?//; s/*//; s/\[//; s/\]//'`
669       if test "$check" == "$src"; then
670         if test '!' -a $src; then
671           ad_fatal "Including $src: No such file or directory"
672         fi
673         src=
674         dst=
675         continue
676       fi
677
678       # Recursively call this function with expanded pathnames.  The
679       # reason why we don't let sh by default expand pathnames is that
680       # the include's destination is optional.  If sh expands by default
681       # we don't know the destination.  For this reason, we handle the
682       # expansion here ourselves.
683
684       # If src and dst are same, then expand the pathname as we'll copy
685       # matches to their own locations.
686       if test "$src" = "$dst"; then
687         # Expand pathnames, and format to our include format
688         set +f
689         srcs=`echo $src | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
690         set -f
691       else
692         # Destination is new, and it has to be a directory.
693         check=`echo "$dst" | sed 's/?//; s/*//; s/\[//; s/\]//'`
694         if test "$check" != "$dst"; then
695           ad_fatal "Invalid destination in 'include $src $dst'"
696         fi
697
698         # Make sure dst has / at the end, as this must be a directory
699         dst=`echo $dst | sed 's/\/$//; s/$/\//'`
700
701         # Escape dst for sed
702         dste=`echo $dst | sed 's/\\//\\\\\//g'` || exit 1
703
704         # Expand pathnames, and format to our include format
705         set +f
706         srcs=`echo $src | sed -e "s/ / $dste : /g" \
707           -e 's/^/ : /' -e "s/$/ $dste/"` || exit 1
708         set -f
709       fi
710
711       # Include recursively
712       ad_dist_includes "$srcs" true
713
714     elif test '!' -a $src; then
715       ad_fatal "Including $src: No such file or directory"
716     fi
717
718     src=
719     dst=
720   done
721
722   if test x$3 = xtrue; then
723     ad_log ""
724   fi
725
726   set +f
727
728   ad_debug "Ending running includes: $1 $2"
729 }
730
731 #
732 # Excludes files specified in the distribution for exclusion.  Used when
733 # creating the distribution for packaging.
734 #
735 # exclude has the following format in distfile:
736 #
737 #  exclude <path>
738 #
739 # The path may be file, directory or regular expression.
740 #
741 # Arguments: ad_dist_includes <excludelist> <log>
742 #
743 ad_dist_excludes()
744 {
745   ad_debug "Starting running excludes: $1"
746
747   if test x$2 = xtrue; then
748     ad_log ""
749     ad_log "Following files and directories were excluded from distribution:"
750     ad_log "----------------------------------------------------------------"
751   fi
752
753   cur=`pwd`
754   cd $am_distdir || exit 1
755   for i in $1
756   do
757     ad_debug "Excluding $i"
758     if test x$2 = xtrue; then
759       ad_log "  $1"
760     fi
761     rm -rf $i
762   done
763   cd $cur || exit 1
764
765   if test x$2 = xtrue; then
766     ad_log ""
767   fi
768
769   ad_debug "Ending running excludes: $1"
770 }
771
772 #
773 # Processes the entire tree for distribution.  This inspects files other
774 # than source and header files, with exception of any file with .ad
775 # suffix, and performs distribution processing for the file.  The original
776 # file is replaced with the processed file.  This function is used when
777 # creating the distribution for packaging.
778 #
779 # Arguments: ad_process_tree <directory>
780 #
781 ad_process_tree()
782 {
783   ad_debug "Starting processing non-source files: $1"
784
785   # Take files, except source and .ad files.
786   files=`find $am_distdir -type f \! -name \*\.ad \( \
787         \! -name \*\.[cC] -a \
788         \! -name \*\.[cC][cCpP] -a \
789         \! -name \*\.[cC][xX][xX] -a \
790         \! -name \*\.[cC][pP][pP] -a \
791         \! -name \*\.[cC]++ -a \
792         \! -name \*\.m -a \
793         \! -name \*\.[hH] -a \
794         \! -name \*\.hh -a \
795         \! -name \*\.[cC]\.in -a \
796         \! -name \*\.[cC][cCpP]\.in -a \
797         \! -name \*\.[cC][xX][xX]\.in -a \
798         \! -name \*\.[cC][pP][pP]\.in -a \
799         \! -name \*\.[cC]++\.in -a \
800         \! -name \*\.m\.in -a \
801         \! -name \*\.[hH]\.in -a \
802         \! -name \*\.hh\.in \)`
803   files=`echo $files | sed 's/$am_distdir//'`
804
805   for ff in $files
806   do
807     ad_process_file $ff $ff.tmp true
808     if test -f $ff.tmp; then
809       rm -f $ff || exit 1
810       mv -f $ff.tmp $ff || exit 1
811     fi
812   done
813
814   ad_debug "Ending processing non-source files: $1"
815 }
816
817 #
818 # Processes the entire source tree for distribution.  This inspects files
819 # in the source tree, with exception of any file with .ad suffix, and
820 # performs distribution processing for the file.  The original file is
821 # replaced with the processed file.  This function is used when creating
822 # the distribution for packaging.
823 #
824 # Call this before ad_process_tree().
825 #
826 # Arguments: ad_process_source_tree <directory>
827 #
828 ad_process_source_tree()
829 {
830   ad_debug "Starting processing source files: $1"
831
832   # We take only C/C++ files since they use the C compiler friendly
833   # version of distdefs.  Other files are not assumed to use them.
834   files=`find $am_distdir -type f \! -name \*\.ad \( \
835         -name \*\.[cC] -o \
836         -name \*\.[cC][cCpP] -o \
837         -name \*\.[cC][xX][xX] -o \
838         -name \*\.[cC][pP][pP] -o \
839         -name \*\.[cC]++ -o \
840         -name \*\.m -o \
841         -name \*\.[hH] -o \
842         -name \*\.hh -o \
843         -name \*\.[cC]\.in -o \
844         -name \*\.[cC][cCpP]\.in -o \
845         -name \*\.[cC][xX][xX]\.in -o \
846         -name \*\.[cC][pP][pP]\.in -o \
847         -name \*\.[cC]++\.in -o \
848         -name \*\.m\.in -o \
849         -name \*\.[hH]\.in -o \
850         -name \*\.hh\.in \)`
851
852   for ff in $files
853   do
854     ad_process_source_file $ff $ff.tmp true
855     if test -f $ff.tmp; then
856       rm -f $ff || exit 1
857       mv -f $ff.tmp $ff || exit 1
858     fi
859   done
860
861   ad_debug "Ending processing source files: $1"
862 }
863
864 #
865 # Makes distribution sane, ala modtimes.  Since we modify the distribution
866 # we need to make it sane after that.
867 #
868 # Arguments: ad_makedist_makesane
869 #
870 ad_makedist_makesane()
871 {
872   ad_debug "Making distribution file modtimes sane"
873
874   # DO NOT change these order unless you know what you are doing.
875   if test -f $am_distdir/configure.ac; then
876     touch $am_distdir/configure.ac
877   fi
878
879   if test -f $am_distdir/aclocal.m4; then
880     touch $am_distdir/aclocal.m4
881   fi
882
883   if test '!' -f Makefile; then
884     ad_fatal "Makefile: No such file or directory"
885   fi
886
887   configh=`grep "^CONFIG_HEADER" Makefile | cut -d= -f2 | sed 's/^[     ]*//'`
888   touch $am_distdir/$configh.in 1>/dev/null 2>/dev/null
889
890   files=`find $am_distdir -type f -name Makefile\.in`
891   for i in $files
892   do
893     touch $i
894   done
895
896   if test -f $am_distdir/configure; then
897     touch $am_distdir/configure
898   fi
899
900   if test -f $am_distdir/config.status; then
901     touch $am_distdir/config.status
902   fi
903
904   ad_debug "Distribution made sane"
905 }
906
907 #
908 # Creates distribution of the source tree.  All files in the distribution
909 # will be processed and the distribution will be packaged.
910 #
911 # Arguments: ad_makedist
912 #
913 ad_makedist()
914 {
915   ad_log_open "makedist.log"
916   ad_debug "Starting distribution creation"
917
918   ad_log "Created distribution"
919   ad_log "--------------------"
920   ad_log ""
921
922   if test '!' -f autodist.dist; then
923     ad_fatal "Autodist has not been run yet to prepare source tree"
924   fi
925
926   if test -z $MAKE; then
927     ad_fatal "The MAKE variable is not set in autodist.conf"
928   fi
929
930   # Get distdir from Makefile
931   if test '!' -f Makefile; then
932     ad_fatal "The source tree is not configured, run ./configure first"
933   fi
934
935   # Parse the requested distribution
936   distribution=`grep "dist:" < autodist.dist | cut -d: -f2` || exit 1
937   dist_version=`grep "ver:" < autodist.dist | cut -d: -f2` || exit 1
938   am_distdir=`grep "distdir:" < autodist.dist | cut -d: -f2` || exit 1
939   ad_parse_distribution $distribution false
940   ad_log "  Distribution: $distribution $dist_version"
941   ad_log "  Destination directory: $am_distdir"
942   ad_log ""
943   ad_process_distdefs
944
945   if test x$opt_no_dist = xtrue; then
946     ad_fatal "The '$distribution' distribution cannot be packaged"
947   fi
948
949   # Run pre-dist-hooks
950   ad_run_dist_hooks "$pre_dist_hooks"
951
952   # Create distribution directory
953   ad_debug "Creating distribution directory $am_distdir"
954   $MAKE distdir || exit 1
955   chmod -R a+r $am_distdir
956
957   if test '!' -d $am_distdir; then
958     ad_fatal "Distribution directory $am_distdir not created"
959   fi
960
961   # Run pre-process-dist-hooks
962   ad_run_dist_hooks "$pre_p_dist_hooks"
963
964   # Run excludes
965   ad_dist_excludes "$excludes" true
966
967   # Run includes
968   ad_dist_includes "$includes" false true
969
970   # Include specific license file if specified
971   if test "$license" != ""; then
972     ad_log ""
973     ad_log "License file in distribution:"
974     ad_log "-----------------------------"
975     ad_log "  $license into $am_distdir/COPYING"
976     ad_log ""
977     cp -p $license $am_distdir/COPYING || exit 1
978   fi
979
980   # Process noprocesses, first pass
981   ad_process_noprocess true
982
983   ad_log ""
984   ad_log "Following files were not re-licensed:"
985   ad_log "-------------------------------------"
986
987   # Process source files
988   ad_debug "Process distribution source tree"
989   ad_process_source_tree $am_distdir
990
991   # Process non-source files
992   ad_debug "Process distribution tree"
993   ad_process_tree $am_distdir
994
995   ad_log ""
996
997   # Process noprocesses, second pass
998   ad_process_noprocess false
999
1000   # Run post-process_dist-hooks
1001   ad_run_dist_hooks "$post_p_dist_hooks"
1002
1003   # Make distribution sane
1004   ad_makedist_makesane
1005
1006   # Package
1007   ad_debug "Packaging distribution"
1008   tar chof $am_distdir.tar $am_distdir || exit 1
1009
1010   # Compress
1011   ad_debug "Compressing distribution package"
1012   if test x$ad_gzip = xtrue; then
1013     ad_debug "Compressing distribution package $am_distdir.tar.gz"
1014     gzip -9 -c $am_distdir.tar > $am_distdir.tar.gz || exit 1
1015   fi
1016   if test x$ad_bzip2 = xtrue; then
1017     ad_debug "Compressing distribution package $am_distdir.tar.bz2"
1018     bzip2 -9 -c $am_distdir.tar > $am_distdir.tar.bz2 || exit 1
1019   fi
1020   if test x$ad_compress = xtrue; then
1021     ad_debug "Compressing distribution package $am_distdir.tar.Z"
1022     compress -c $am_distdir.tar > $am_distdir.tar.Z || exit 1
1023   fi
1024   if test x$ad_zip = xtrue; then
1025     rm -f $am_distdir.zip
1026     ad_debug "Compressing distribution package $am_distdir.zip"
1027     zip -rq $am_distdir.zip $am_distdir || exit 1
1028   fi
1029   rm -f $am_distdir.tar
1030
1031   # Run post-dist-hooks
1032   ad_run_dist_hooks "$post_dist_hooks"
1033
1034   # Cleanup
1035   rm -rf $am_distdir
1036
1037   ad_log "Distribution created successfully."
1038
1039   ad_debug "Ending distribution creation"
1040 }
1041
1042 #
1043 # Handles distribution options.
1044 #
1045 # option has the following format in distfile:
1046 #
1047 #   option <option>
1048 #
1049 # Following options are supported:
1050 #
1051 #   template
1052 #   no-dist
1053 #   no-inherit
1054 #
1055 # Arguments: ad_handle_options <options>
1056 #
1057 ad_handle_options()
1058 {
1059   ad_debug "Handling options: $1"
1060
1061   for i in $1
1062   do
1063     if test "$i" = "template"; then
1064       opt_template=true
1065       continue
1066     elif test "$i" = "no-dist"; then
1067       opt_no_dist=true
1068       continue
1069     elif test "$i" = "no-inherit"; then
1070       opt_no_inherit=true
1071       continue
1072     fi
1073   done
1074 }
1075
1076 #
1077 # Clears set options
1078 #
1079 # Arguments: ad_clear_options
1080 #
1081 ad_clear_options()
1082 {
1083   opt_template=false
1084   opt_no_dist=false
1085   opt_no_inherit=false
1086 }
1087
1088 #
1089 # Parses the distribution.  Gets all distribution defines from the
1090 # distribution.  This also expands all inherited distributions recursively
1091 # to get all inherited distribution defines.  From inherited distributions
1092 # their name and package name is not inherited.
1093 #
1094 # Arguments: ad_parse_distribution <distribution name> <inherit>
1095 #
1096 ad_parse_distribution()
1097 {
1098   local inhs
1099   local defs
1100   local undefs
1101   local incs
1102   local excs
1103   local nops
1104   local opts
1105   local dname
1106   local dpname
1107   local bugr
1108   local prh
1109   local poh
1110   local prdh
1111   local podh
1112   local prpdh
1113   local popdh
1114
1115   ad_debug "Starting parsing distribution: $1 $2"
1116
1117   if test '!' -f $distdir/$1; then
1118     ad_fatal "Distribution '$1' is not declared"
1119   fi
1120
1121   # Get inherited
1122   inhs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1123     | grep "inherit " | cut -d' ' -f2 | sort | uniq`
1124
1125   # Get distdefs
1126   defs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1127    | grep "define " | cut -d' ' -f2 | sort | uniq`
1128
1129   if test "$inhs" = "" && test "$defs" = ""; then
1130     ad_fatal "Distribution '$1' does not define anything"
1131   fi
1132
1133   # Get undefined distdefs
1134   undefs=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1135    | grep "undef " | cut -d' ' -f2 | sort | uniq`
1136
1137   # Get includes
1138   incs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1139     | grep "include " | sed 's/include / : /'`
1140
1141   # Get excludes
1142   excs=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1143     | grep "exclude " | cut -d' ' -f2- | sort | uniq`
1144
1145   # Get noprocesses
1146   nops=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1147     | grep "noprocess " | cut -d' ' -f2- | sort | uniq`
1148
1149   # Get options
1150   opts=`sed 's/^[       ]*//' < $distdir/$1 | grep -v "^#" \
1151     | grep "option " | cut -d' ' -f2- | sort | uniq`
1152
1153   # Check options
1154   ad_handle_options "$opts"
1155   if test x$2 = xtrue && test x$opt_no_inherit = xtrue; then
1156     ad_fatal "Distribution '$1' cannot be inherited"
1157   fi
1158   if test x$2 = xfalse && test x$opt_template = xtrue; then
1159     ad_fatal "Template distribution '$1' cannot be prepared or packaged"
1160   fi
1161
1162   ad_debug "inherits: $inhs"
1163   ad_debug "distdefs: $defs"
1164   ad_debug "includes: $incs"
1165   ad_debug "excludes: $excs"
1166   ad_debug "noprocess: $nops"
1167   ad_debug "undistdefs: $undefs"
1168   ad_debug "options: $opts"
1169
1170   # Expand distdefs from inherited distributions
1171   for i in $inhs
1172   do
1173     if test x$1 = x$i; then
1174       ad_fatal "Infinite recursion detected.  Fix the '$distdir/$1' \
1175             distribution to not have 'inherit $i' declared."
1176     fi
1177
1178     if test '!' -f $distdir/$i; then
1179       ad_fatal "Distribution '$i' is not declared (inherited from '$1')"
1180     fi
1181
1182     ad_parse_distribution $i true
1183     ad_clear_options
1184   done
1185
1186   # Get license
1187   license=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1188     | grep "license " | cut -d' ' -f2`
1189   licenseh=`sed 's/^[   ]*//' < $distdir/$1 | grep -v "^#" \
1190     | grep "license-header " | sed 's/license-header / : /'`
1191
1192   ad_debug "license: $license"
1193   ad_debug "licenseh: $licenseh"
1194
1195   if test x$2 = xfalse; then
1196     # Take rest of the stuff from top distribution
1197
1198     # We take precedence on defined and undefined distdefs.  Remove
1199     # undefined distdefs if we have defined them.
1200     for d in $defs
1201     do
1202       ad_debug "defining undefined $d distdef"
1203       undistdefs=`echo $undistdefs | sed s/$d//g`
1204     done
1205
1206     # Get distribution name
1207     dname=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1208       | grep "name " | cut -d' ' -f2-`
1209
1210     if test "$dname"; then
1211       distribution=$dname
1212     fi
1213
1214     # Get distribution package name (optional)
1215     dpname=`sed 's/^[   ]*//' < $distdir/$1 | grep -v "^#" \
1216      | grep "package " | cut -d' ' -f2`
1217
1218     if test "$dpname"; then
1219       package=$dpname
1220     else
1221       package=$distribution
1222     fi
1223
1224     # Get Bug-report email address (optional)
1225     bugr=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1226      | grep "bug-report " | cut -d' ' -f2-`
1227
1228     if test "$bugr"; then
1229       bug_report=$bugr
1230     fi
1231
1232     ad_debug "distribution: $distribution"
1233     ad_debug "package: $package"
1234     ad_debug "bug-report: $bug_report"
1235
1236     # Get hooks (optional)
1237     prh=`sed 's/^[      ]*//' < $distdir/$1 | grep -v "^#" \
1238      | grep "pre-hook " | cut -d' ' -f2-`
1239     poh=`sed 's/^[      ]*//' < $distdir/$1 | grep -v "^#" \
1240      | grep "post-hook " | cut -d' ' -f2-`
1241     prdh=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1242      | grep "pre-dist-hook " | cut -d' ' -f2-`
1243     podh=`sed 's/^[     ]*//' < $distdir/$1 | grep -v "^#" \
1244      | grep "post-dist-hook " | cut -d' ' -f2-`
1245     prpdh=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1246      | grep "pre-process-dist-hook " | cut -d' ' -f2-`
1247     popdh=`sed 's/^[    ]*//' < $distdir/$1 | grep -v "^#" \
1248      | grep "post-process-dist-hook " | cut -d' ' -f2-`
1249
1250     pre_hooks="$pre_hooks $prh"
1251     post_hooks="$post_hooks $poh"
1252     pre_dist_hooks="$pre_dist_hooks $prdh"
1253     post_dist_hooks="$post_dist_hooks $podh"
1254     pre_p_dist_hooks="$pre_p_dist_hooks $prpdh"
1255     post_p_dist_hooks="$post_p_dist_hooks $popdh"
1256     doptions="$doptions $opts"
1257
1258     ad_handle_options "$doptions"
1259     ad_debug "options: $doptions"
1260   fi
1261
1262   # Return to caller
1263   inherits="$inherits $inhs"
1264   distdefs="$distdefs $defs"
1265   includes="$includes $incs"
1266   excludes="$excludes $excs"
1267   noprocess="$noprocess $nops"
1268   undistdefs="$undistdefs $undefs"
1269
1270   ad_debug "Ending parsing distribution: $1 $2"
1271 }
1272
1273 #
1274 # Processes parsed distdefs.  Removes duplicates, and undefined distdefs
1275 # from the distdefs.
1276 #
1277 # Arguments: ad_process_distdefs
1278 #
1279 ad_process_distdefs()
1280 {
1281   ad_debug "Starting processing distdefs"
1282
1283   ad_log ""
1284   ad_log "Following distdefs were processed:"
1285   ad_log "----------------------------------"
1286
1287   # Remove all undefined distribution defines
1288   for i in $undistdefs
1289   do
1290     ad_debug "undefining $i distdef"
1291     distdefs=`echo $distdefs | sed s/$i//g`
1292   done
1293
1294   rm -f autodist.tmp.defs
1295
1296   # Remove duplicate distdefs
1297   for i in $distdefs
1298   do
1299     echo $i >>autodist.tmp.defs
1300   done
1301   distdefs=`sort < autodist.tmp.defs | uniq`
1302   distdefs=`echo $distdefs`
1303   rm -f autodist.tmp.defs
1304
1305   # Log
1306   for i in $distdefs
1307   do
1308     ad_log "  $i"
1309   done
1310   ad_log ""
1311
1312   ad_debug "distdefs=$distdefs"
1313
1314   ad_debug "Ending processing distdefs"
1315 }
1316
1317 #
1318 # Processes for a license header change.
1319 #
1320 # Arguments: ad_process_license_header <scriptfile> <sourcefile>
1321 #
1322 ad_process_license_header()
1323 {
1324   ad_debug "Starting license header processing"
1325
1326   # Add : separator at the end
1327   lics=`echo "$licenseh" | sed 's/$/ : /'`
1328
1329   src=
1330   dst=
1331   for i in $lics
1332   do
1333     if test "$i" = ":" && test -z "$src"; then
1334       continue
1335     fi
1336     if test -z "$src"; then
1337       src=$i
1338       continue
1339     fi
1340     if test -z "$dst" && test "$i" != ":"; then
1341       dst=$i
1342     else
1343       ad_fatal "Missing argument in 'license-header $src'"
1344     fi
1345
1346     ad_debug "Replacing $src license with $dst license"
1347
1348     if test '!' -f $src; then
1349       ad_fatal "License header $src: No such file or directory"
1350     fi
1351
1352     if test '!' -f $dst; then
1353       ad_fatal "License header $dst: No such file or directory"
1354     fi
1355
1356     # Awk script to replace the license header
1357     fl=`sed q $src | sed 's/\\//\\\\\//g' > autodist.lsrc` || exit 1
1358     ll=`sed -n '$p' $src | sed 's/\\//\\\\\//g' > autodist.ldst` || exit 1
1359     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
1360     rm -f autodist.lsrc autodist.ldst
1361
1362     src=
1363     dst=
1364   done
1365
1366   ad_debug "Ending license header processing"
1367 }
1368
1369 #
1370 # Process specified noprocesses.  This is called during makedist.
1371 # The noprocess first copies the noprocess files and dirs into a temp
1372 # directory, and then removes them from the distdir.  This way they are
1373 # not processed by Autodist.  After processing they are returned to their
1374 # correct locations from the temp dir.  Dirty, yeah, but the way we do
1375 # this until better one comes along.
1376 #
1377 # Arguments: ad_process_noprocess <process>
1378 #
1379 # If <process> is true this excludes and if false this includes.
1380 #
1381 ad_process_noprocess()
1382 {
1383   ad_debug "Starting running noprocesses"
1384
1385   set -f
1386
1387   cur=`pwd`
1388
1389   if test x$1 = xtrue; then
1390     ad_log ""
1391     ad_log "Following files and directories were not processed:"
1392     ad_log "---------------------------------------------------"
1393
1394     f="$cur/autodist__noprocess"
1395     rm -rf $f
1396     mkdir -p $f || exit 1
1397
1398     # First, include them to new location with correct directory structure.
1399     old_am_distdir="$am_distdir"
1400     cd $am_distdir || exit 1
1401     am_distdir="$f"
1402     nops=" $noprocess"
1403     nops=`echo $nops | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
1404     ad_dist_includes "$nops" false false
1405     am_distdir="$old_am_distdir"
1406
1407     # Then, remove from distdir (they are in temp dir now)
1408     for i in $noprocess
1409     do
1410       ad_log "  $i"
1411       rm -rf $i
1412     done
1413     cd $cur || exit 1
1414
1415     ad_log ""
1416   else
1417     # Copy from the temp dir back to distdir
1418     cd autodist__noprocess || exit 1
1419     old_am_distdir="$am_distdir"
1420     am_distdir="$cur/$old_am_distdir"
1421     nops=" $noprocess"
1422     nops=`echo $nops | sed -e 's/ / : /g' -e 's/^/ : /'` || exit 1
1423
1424     ad_dist_includes "$nops" false false
1425
1426     am_distdir="$old_am_distdir"
1427     cd $cur || exit 1
1428     rm -rf autodist__noprocess
1429   fi
1430
1431   set +f
1432
1433   ad_debug "Ending running noprocesses"
1434 }
1435
1436 #
1437 # Process a file given as argument for the distribution.
1438 #
1439 # Arguments: ad_process_file <filepath> <dest_filepath> <re-license>
1440 #
1441 ad_process_file()
1442 {
1443   local found=false
1444   local f
1445   local defs
1446   local ndefs
1447
1448   # Process only regular files
1449   if test '!' -f $1; then
1450     return
1451   fi
1452
1453   ad_debug "Starting processing file: $1 $2"
1454
1455   f="autodist.tmp.script"
1456   rm -f $f
1457
1458   # If license header is provided, replace the license header in the file.
1459   if test x$3 = xtrue; then
1460     ad_process_license_header $f $1
1461   fi
1462
1463   ad_debug "Getting #ifdef's and #ifndef's"
1464
1465   # Get defined distribution defines
1466   defs=`awk "/^#ifdef "$DP"_DIST_|^#else "$DP"_DIST_/ { print; }" \
1467     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
1468
1469   # Get explicitly not-defined distribution defines
1470   ndefs=`awk "/^#ifndef "$DP"_DIST_|^#else !"$DP"_DIST_/ { print; }" \
1471     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
1472
1473   ad_debug "defs in $1: $defs"
1474   ad_debug "ndefs in $1: $ndefs"
1475
1476   # Create the script to include and exclude stuff in the file according
1477   # to the distribution defines
1478
1479   # ifdefs
1480   ad_debug "processing ifdefs"
1481   for d in $defs
1482   do
1483     found=false
1484     for i in $distdefs
1485     do
1486       if test x$d = x$i; then
1487         found=true
1488         break
1489       fi
1490     done
1491
1492     # If distribution define was not found exclude those lines from the file.
1493     # This also handles the #ifdef's #else (ie. #ifndef) branch.
1494     if test x$found = xfalse; then
1495       ad_debug "ifdef $d will be excluded (it is NOT defined)"
1496       echo "/^#ifdef $d/,/^#else !$d|^#endif $d/ { next; }" >> $f
1497     else
1498       echo "/^#else !$d/,/^#endif $d/ { next; }" >> $f
1499     fi
1500   done
1501
1502   # ifndefs
1503   ad_debug "processing ifndefs"
1504   for d in $ndefs
1505   do
1506     found=false
1507     for i in $distdefs
1508     do
1509       if test x$d = x$i; then
1510         found=true
1511         break
1512       fi
1513     done
1514
1515     # If distribution define was found exclude those lines from the file.
1516     # This also handles the #ifndef's #else (ie. #ifdef) branch.
1517     if test x$found = xtrue; then
1518       ad_debug "ifndef $d will be excluded (it IS defined)"
1519       echo "/^#ifndef $d/,/^#else $d|^#endif $d/ { next; }" >> $f
1520     else
1521       echo "/^#else $d/,/^#endif $d/ { next; }" >> $f
1522     fi
1523   done
1524
1525   # Now process the file with the script
1526   if test -f $f; then
1527
1528     # Those distdef lines that remain in the file are removed to make
1529     # the appearance prettier
1530     echo "/^#ifdef "$DP"_DIST_|^#endif "$DP"_DIST_|^#else "$DP"_DIST_|^#else !"$DP"_DIST_|^#ifndef "$DP"_DIST_/ { next; }" >> $f
1531     echo "{ print; }" >> $f
1532
1533     # Execute the script
1534     cp -p $1 $2 || exit 1
1535     awk -f $f $1 > $2 || exit 1
1536   fi
1537
1538   rm -f $f
1539
1540   # Log if file was not relicensed
1541   if test -f "$1.norelicense"; then
1542     ad_log "  $1"
1543     rm -f $1.norelicense
1544   fi
1545
1546   ad_debug "Ending processing file: $1 $2"
1547 }
1548
1549 #
1550 # Process a source file given as argument for the distribution.
1551 #
1552 # Arguments: ad_process_source_file <filepath> <dest_filepath> <re-license>
1553 #
1554 ad_process_source_file()
1555 {
1556   local found=false
1557   local f
1558   local defs
1559   local ndefs
1560
1561   # Process only regular files
1562   if test '!' -f $1; then
1563     return
1564   fi
1565
1566   ad_debug "Starting processing source file: $1 $2"
1567
1568   f="autodist.tmp.script"
1569   rm -f $f
1570
1571   # If license header is provided, replace the license header in the file.
1572   if test x$3 = xtrue; then
1573     ad_process_license_header $f $1
1574   fi
1575
1576   ad_debug "Getting #ifdef's and #ifndef's"
1577
1578   # Get defined distribution defines
1579   defs=`awk '/^#ifdef SILC_DIST_|^#else \/\* SILC_DIST_/ { print; }' \
1580     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
1581
1582   # Get explicitly not-defined distribution defines
1583   ndefs=`awk '/^#ifndef SILC_DIST_|^#else \/\* \!SILC_DIST_/ { print; }' \
1584     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
1585
1586   ad_debug "defs in $1: $defs"
1587   ad_debug "ndefs in $1: $ndefs"
1588
1589   # Create the script to include and exclude stuff in the file according
1590   # to the distribution defines
1591
1592   # ifdefs
1593   ad_debug "processing ifdefs"
1594   for d in $defs
1595   do
1596     found=false
1597     for i in $distdefs
1598     do
1599       if test x$d = x$i; then
1600         found=true
1601         break
1602       fi
1603     done
1604
1605     # If distribution define was not found exclude those lines from the file.
1606     # This also handles the #ifdef's #else (ie. #ifndef) branch.
1607     if test x$found = xfalse; then
1608       ad_debug "ifdef $d will be excluded (it is NOT defined)"
1609       echo "/^#ifdef $d/,/^#else \/\* \!$d|^#endif \/\* $d/ { next; }" >> $f
1610     else
1611       echo "/^#else \/\* \!$d/,/^#endif \/\* $d/ { next; }" >> $f
1612     fi
1613   done
1614
1615   # ifndefs
1616   ad_debug "processing ifndefs"
1617   for d in $ndefs
1618   do
1619     found=false
1620     for i in $distdefs
1621     do
1622       if test x$d = x$i; then
1623         found=true
1624         break
1625       fi
1626     done
1627
1628     # If distribution define was found exclude those lines from the file.
1629     # This also handles the #ifndef's #else (ie. #ifdef) branch.
1630     if test x$found = xtrue; then
1631       ad_debug "ifndef $d will be excluded (it IS defined)"
1632       echo "/^#ifndef $d/,/^#else \/\* $d|^#endif \/\* $d/ { next; }" >> $f
1633     else
1634       echo "/^#else \/\* $d/,/^#endif \/\* $d/ { next; }" >> $f
1635     fi
1636   done
1637
1638   # Now process the file with the script
1639   if test -f $f; then
1640
1641     # Those distdef lines that remain in the file are removed to make
1642     # the appearance prettier
1643     echo "/^#ifdef SILC_DIST_|^#endif \/\* SILC_DIST_|^#else \/\* SILC_DIST_|^#else \/\* \!SILC_DIST_|^#ifndef SILC_DIST_/ { next; }" >> $f
1644     echo "{ print; }" >> $f
1645
1646     # Execute the script
1647     cp -p $1 $2 || exit 1
1648     awk -f $f $1 > $2 || exit 1
1649   fi
1650
1651   rm -f $f
1652
1653   # Log if file was not relicensed
1654   if test -f "$1.norelicense"; then
1655     ad_log "  $1"
1656     rm -f $1.norelicense
1657   fi
1658
1659   ad_debug "Ending processing source file: $1 $2"
1660 }
1661
1662 #
1663 # Run hooks
1664 #
1665 # Arguments: ad_run_hooks <hooks>
1666 #
1667 ad_run_hooks()
1668 {
1669   ad_debug "Running hooks: $1"
1670
1671   ad_log ""
1672   ad_log "Hooks executed:"
1673   ad_log "--------------"
1674
1675   for i in $1
1676   do
1677     if test '!' -f $i; then
1678       ad_fatal "Hook script $i does not exist"
1679     fi
1680     ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\""
1681     sh $i "$distribution" "$dist_version" "$package" || exit 1
1682   done
1683   ad_log ""
1684
1685   ad_debug "Ending running hooks: $1"
1686 }
1687
1688 #
1689 # Run dist hooks
1690 #
1691 # Arguments: ad_run_dist_hooks <hooks>
1692 #
1693 ad_run_dist_hooks()
1694 {
1695   ad_debug "Starting running distributions hooks: $1"
1696
1697   ad_log ""
1698   ad_log "Distribution hooks executed:"
1699   ad_log "----------------------------"
1700
1701   for i in $1
1702   do
1703     if test '!' -f $i; then
1704       ad_fatal "Dist hook script $i does not exist"
1705     fi
1706
1707     ad_log "  sh $i \"$distribution\" \"$dist_version\" \"$package\" \"$am_distdir\""
1708     sh $i "$distribution" "$dist_version" "$package" "$am_distdir" || exit 1
1709   done
1710   ad_log ""
1711
1712   ad_debug "Ending running distribution hooks"
1713 }
1714
1715 ###############################################################################
1716 # Autodist code
1717
1718 usage="Usage: autodist [options] [distribution] [version]"
1719 help="\
1720 Autodist prepares source tree for configuration, compilation and
1721 distribution.  Prepares the source tree from the \`autodist.ad'
1722 configuration file.  Generates Automake.am files from Automake.ad
1723 files, configure.ac file from configure.ad file(s), generates the
1724 configure script by running Autoconf tool, and generates Makefile.in
1725 files by running Automake tool.
1726
1727 Operation modes:
1728   -h, --help                print this help, then exit
1729   -V, --version             print version number, then exit
1730   -v, --verbose             verbosely report processing
1731   -d, --distdir <dir>       search distributions from <dir>
1732   -s, --distdefs [<dist>]   print distribution defines of <dist>, then exit
1733   -i, --init                initialize Autodist environment, create default
1734                             distribution directory and distribution, then exit
1735   -m, --makedist            create and package distribution
1736       --gzip                create package compressed with gzip (default)
1737       --bzip2               create also package compressed with bzip2
1738       --compress            create also package compressed with compress
1739       --zip                 create also package compressed with zip"
1740
1741 #
1742 # Process command line arguments
1743 #
1744 while test $# -gt 0; do
1745   case "${1}" in
1746
1747   -d |--distdir)
1748     shift;
1749     test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
1750     distdir="${1}";
1751     shift;;
1752
1753   --list)
1754     exit 0;;
1755
1756   -s | --distdefs)
1757     shift;
1758     if test $# -eq 0; then
1759       ad_parse_distribution $distribution false
1760       echo "Distribution: ${distribution}" 1>&2;
1761     else
1762       ad_parse_distribution $1 false
1763       echo "Distribution: ${1}" 1>&2;
1764     fi
1765     ad_process_distdefs
1766     echo "Distdefs:" 1>&2;
1767     for i in $distdefs
1768     do
1769       echo "$i";
1770     done
1771     exit 0;;
1772
1773   -i | --init)
1774     ad_initialize;
1775     exit 0;;
1776
1777   -m | --makedist)
1778     ad_makedist
1779     exit 0;;
1780
1781   --gzip)
1782     ad_gzip=true
1783     shift;;
1784
1785   --bzip2)
1786     ad_bzip2=true
1787     shift;;
1788
1789   --compress)
1790     ad_compress=true
1791     shift;;
1792
1793   --zip)
1794     ad_zip=true
1795     shift;;
1796
1797   -v | --verbose)
1798     debug=true
1799     shift;;
1800
1801   -h | --help | --h*)
1802     echo "${usage}" 1>&2;
1803     echo 1>&2;
1804     echo "${help}" 1>&2;
1805     echo 1>&2;
1806     exit 0;;
1807
1808   -V | --version)
1809     echo "@PACKAGE@ (@PACKAGE_NAME@) $ver" 1>&2;
1810     echo "Written by Pekka Riikonen" 1>&2;
1811     echo 1>&2;
1812     echo "Copyright (C) 2004 - 2005 SILC Project" 1>&2;
1813     echo "\
1814 This is free software; see the source for copying conditions.  There is NO
1815 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " 1>&2;
1816     exit 0;;
1817
1818   --)
1819     shift;
1820     break;;
1821
1822   -*)
1823     echo "${usage}" 1>&2;
1824     exit 1;;
1825
1826   *)
1827     break;;
1828
1829   esac
1830 done
1831
1832 # Open log file
1833 ad_log_open "autodist.log"
1834
1835 ad_log "Processing source tree for compilation and configuration"
1836 ad_log "--------------------------------------------------------"
1837 ad_log ""
1838
1839 #
1840 # Parse the requested distribution
1841 #
1842 if test $# != 0; then
1843   distribution="${1}";
1844   distfile=$distribution
1845   shift
1846 fi
1847 ad_parse_distribution $distribution false
1848 ad_log "  Distribution: $distribution $dist_version"
1849 ad_log ""
1850 ad_process_distdefs
1851
1852 if test $# != 0; then
1853   dist_version="${1}";
1854 fi
1855
1856 ad_debug "Preparing source tree for configuration and compilation..."
1857 ad_debug "Preparing $distribution distribution version $dist_version"
1858
1859 #
1860 # Create the distribution defines header file
1861 #
1862 if test "$DISTDEFS"; then
1863   ad_create_distdefs_h
1864 else
1865   ad_fatal "DISTDEFS not defined in $distdir/autodist.conf"
1866 fi
1867
1868 #
1869 # Run pre-hooks
1870 #
1871 ad_run_hooks "$pre_hooks"
1872
1873 #
1874 # Generate the Makefile.am files from Makefile.ad files
1875 #
1876 ad_make_makefile_ams
1877
1878 #
1879 # Generate the configure.ac from configure.ad file(s)
1880 #
1881 ad_make_configure_ac ./configure.ad
1882
1883 #
1884 # Process all files with .ad suffix for distribution processing
1885 #
1886 ad_process_ads
1887
1888 #
1889 # Generate configure script
1890 #
1891 ad_make_configure
1892
1893 #
1894 # Generate Makefile.in files
1895 #
1896 ad_make_makefile_ins
1897
1898 #
1899 # Create autodist.dist
1900 #
1901 ad_debug "Creating autodist.dist"
1902 echo "dist:$distfile" > autodist.dist
1903 echo "ver:$dist_version" >> autodist.dist
1904 echo "distdir:$package-$dist_version" >> autodist.dist
1905
1906 #
1907 # Run post-hooks
1908 #
1909 ad_run_hooks "$post_hooks"
1910
1911 ad_log "Source tree processed successfully."
1912 ad_debug "Done, now run ./configure and make."
1913
1914 exit 0