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