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