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