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