Added Autodist.
[silc.git] / autodist
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
35 # XXXXXXXXXXXXXXX
36 #
37 # TODO
38 #
39 # --makedist
40 # "pre-dist-hooks"
41 # "post-dist-hooks"
42 # "include"
43 # "exclude"
44 # "option"
45 # default conffile in -i
46 # default distro in -i
47 #
48 # NOTE: inherit inherits only distdefs, undefs, hooks, options, includes
49 # and excludes.  It must not inherit others.
50 #
51 # XXXXXXXXXXXXXXX
52
53 # Scripts have the following variables in their disposal
54 #
55 # distribution      distribution name
56 # dist_version      distribution version
57 # package           the package name of the distribution
58
59 # Allowed structure for distdefs in non-source files
60
61 #ifdef SILC_DIST_DEFINE
62 #endif SILC_DIST_DEFINE
63
64 #ifndef SILC_DIST_DEFINE
65 #endif SILC_DIST_DEFINE
66
67 #ifdef SILC_DIST_DEFINE
68 #else !SILC_DIST_DEFINE
69 #endif SILC_DIST_DEFINE
70
71 #ifndef SILC_DIST_DEFINE
72 #else SILC_DIST_DEFINE
73 #endif SILC_DIST_DEFINE
74
75 # Allowed structure for distdefs in source files
76
77 #ifdef SILC_DIST_DEFINE
78 #endif /* SILC_DIST_DEFINE */
79
80 #ifndef SILC_DIST_DEFINE
81 #endif /* SILC_DIST_DEFINE */
82
83 #ifdef SILC_DIST_DEFINE
84 #else /* !SILC_DIST_DEFINE */
85 #endif /* SILC_DIST_DEFINE */
86
87 #ifndef SILC_DIST_DEFINE
88 #else /* SILC_DIST_DEFINE */
89 #endif /* SILC_DIST_DEFINE */
90
91 ###############################################################################
92 # Global variables
93
94 # Distribution subdirectory
95 distdir="distdir"
96
97 # This current distribution
98 distribution=default
99 dist_version=0.0
100 package=
101
102 # All inherited distributions in this distribution
103 inherits=
104
105 # All distribution defines for this distribution
106 distdefs=
107
108 # All distribution undefines for this distribution
109 undistdefs=
110
111 # All distribution options
112 options=
113
114 # All includes
115 includes=
116
117 # All excludes
118 excludes=
119
120 # All pre, post pre-dist and post-dist hooks
121 pre_hooks=
122 post_hooks=
123 pre_dist_hooks=
124 post_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 ver="1.0"
133 makedist=false
134
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     echo autodist: $1
158   fi
159 }
160
161 #
162 # Prints out error message and exits the script.
163 #
164 # Arguments: ad_fatal <error message>
165 #
166 ad_fatal()
167 {
168   echo autodist: error: $1
169   exit 1
170 }
171
172 #
173 # Initializes the Autodist environment, creates default distribution
174 # directory, and default distribution.
175 #
176 # Arguments: ad_initialize
177 #
178 ad_initialize()
179 {
180   ad_debug "-->$FUNCNAME()"
181
182   # Create default distdir
183   if test '!' -f $distdir; then
184     mkdir -p $distdir
185   fi
186
187   # Create default distribution
188   # XXX
189
190   ad_debug "<--$FUNCNAME()"
191 }
192
193 #
194 # Creates the distdefs header file including defined distdefs
195 #
196 # Arguments: ad_create_distdefs_h
197 #
198 ad_create_distdefs_h()
199 {
200   ad_debug "-->$FUNCNAME()"
201
202   fname=$DISTDEFS
203   rm -f $fname
204   echo "/*" > $fname
205   echo "  Automatically generated by Autodist $ver.  Do not edit." >> $fname
206   echo >> $fname
207   echo "  Generated: `date` by `whoami`" >> $fname
208   echo "  Distribution: $distribution" >> $fname
209   echo "  License: $license" >> $fname
210   echo "*/" >> $fname
211   echo >> $fname
212   echo "#ifndef _"$DP"_DISTDEFS_H" >> $fname
213   echo "#define _"$DP"_DISTDEFS_H" >> $fname
214   echo >> $fname
215
216   for i in $distdefs
217   do
218     echo "#define $i 1" >>$fname
219   done
220
221   echo >> $fname
222   echo "#endif /* _"$DP"_DISTDEFS_H */" >> $fname
223
224   ad_debug "<--$FUNCNAME()"
225 }
226
227 #
228 # Creates the main configure script for the distribution.  This runs
229 # the aclocal, autoheader and autoconf tools.
230 #
231 # Arguments: ad_make_configure
232 #
233 ad_make_configure()
234 {
235   ad_debug "-->$FUNCNAME()"
236   local run_autoconf=false
237
238   rm -f configure
239
240   if test "$ACLOCAL"; then
241     ad_debug "Running aclocal"
242     $ACLOCAL 1>/dev/null 2>/dev/null
243     if test $? != 0; then
244       ad_fatal "aclocal failed"
245     fi
246   fi
247
248   if test "$AUTOCONF"; then
249     ad_debug "Running autoconf"
250     $AUTOCONF
251     if test $? != 0; then
252       ad_fatal "autoconf failed"
253     fi
254     run_autoconf=true
255   fi
256
257   if test "$AUTOHEADER"; then
258     ad_debug "Running autoheader"
259     $AUTOHEADER
260     if test $? != 0; then
261       ad_fatal "autoheader failed"
262     fi
263   fi
264
265   if test "$LIBTOOLIZE"; then
266     ad_debug "Running libtoolize"
267     $LIBTOOLIZE
268     if test $? != 0; then
269       ad_fatal "libtoolize failed"
270     fi
271   fi
272
273   if test x$run_autoconf = xtrue; then
274     if test '!' -f configure; then
275       ad_fatal "creating configure script failed"
276     fi 
277   fi
278
279   ad_debug "<--$FUNCNAME()"
280 }
281
282 #
283 # Creates the configure.ac script from the configure.ad fragments in
284 # the source tree.  Takes the source configure file as argument which
285 # is used to create the actual configure.ac.
286 #
287 # Arguments: ad_make_configure_ac <configure_ac_source>
288 #
289 ad_make_configure_ac()
290 {
291   ad_debug "-->$FUNCNAME(): $1"
292
293   if test '!' -f $1; then
294     ad_fatal "The configure file '$1' does not exist"
295   fi
296
297   rm -f configure.ac configure.ad.cfs
298
299   cfs=`find . -name configure\*\.ad`
300   for i in $cfs
301   do
302     if test "x$i" = "x$1"; then
303       continue
304     fi
305     ad_debug "including $i"
306     cat $i >> configure.ad.cfs
307   done
308
309   # Header for configure.ac
310   fname="configure.tmp.ac"
311   echo "# Automatically generated by Autodist $ver.  Do not edit." > $fname
312   echo "# To make changes edit the $i file in the source tree." >> $fname
313   echo >> $fname
314   echo "# Source: $i" >> $fname
315   echo "# Generated: `date` by `whoami`" >> $fname
316   echo "# Distribution: $distribution" >> $fname
317   echo "# License: $license" >> $fname
318   echo >> $fname
319
320   ad_debug "creating configure.ac"
321   if test -f configure.ad.cfs; then
322     sed '/AD_INCLUDE_CONFIGURE/ r configure.ad.cfs' $1 > $fname.tmp
323     sed -e "/AD_INCLUDE_CONFIGURE/s///" $fname.tmp >> $fname
324     rm -f configure.ad.cfs $fname.tmp
325   else
326     cat $1 >> $fname
327   fi
328
329   # Process AD_INIT
330   sed -e "/AD_INIT/s//AC_INIT($distribution, $dist_version, [], $package)/" $fname > $fname.tmp
331
332   # Process for distribution
333   ad_process_file $fname.tmp configure.ac
334   rm -f $fname $fname.tmp
335
336   ad_debug "<--$FUNCNAME(): $1"
337 }
338
339 #
340 # Creates the Makefile.in files by running the automake tool.
341 #
342 # Arguments: ad_make_makefile_ins
343 #
344 ad_make_makefile_ins()
345 {
346   ad_debug "-->$FUNCNAME()"
347
348   if test "$AUTOMAKE"; then
349     ad_debug "Running automake"
350     $AUTOMAKE
351     if test $? != 0; then
352       ad_fatal "automake failed"
353     fi
354   fi
355
356   ad_debug "<--$FUNCNAME()"
357 }
358
359 #
360 # Creates the Makefile.am files from the Makefile.ad files in the
361 # source tree.  This runs the distribution specific processing for the
362 # Makefile.ad files.
363 #
364 # Arguments: ad_make_makefile_ams
365 #
366 ad_make_makefile_ams()
367 {
368   ad_debug "-->$FUNCNAME()"
369
370   files=`find . -name Makefile\.ad`
371   for i in $files
372   do
373     fname=`echo $i | sed s/\.ad//`
374
375     # Header for the Makefile.am
376     echo "# Automatically generated by Autodist $ver from Makefile.ad.  Do not edit." > $fname.am
377     echo "# To make changes edit the $i file in the source tree." >> $fname.am
378     echo >> $fname.am
379     echo "# Source: $i" >> $fname.am
380     echo "# Generated: `date` by `whoami`" >> $fname.am
381     echo "# Distribution: $distribution" >> $fname.am
382     echo "# License: $license" >> $fname.am
383     echo >> $fname.am
384
385     # Run the distribution processing for this Makefile.ad
386     ad_debug "Processing $i to be $fname.am"
387     ad_process_file $i $fname.am
388   done
389
390   ad_debug "<--$FUNCNAME()"
391 }
392
393 #
394 # Processes all files with .ad suffix, with exception of configure*.ad
395 # and Makefile.ad files, for distribution from the source tree.
396 #
397 # Arguments: ad_process_ads
398 #
399 ad_process_ads()
400 {
401   ad_debug "-->$FUNCNAME()"
402
403   files=`find . -name \*\.ad \! -name configure\*\.ad \! -name Makefile\.ad`
404   for i in $files
405   do
406     fname=`echo $i | sed s/\.ad//`
407
408     # Header
409     echo "# Automatically generated by Autodist $ver.  Do not edit." > $fname
410     echo "# To make changes edit the $i file in the source tree." >> $fname
411     echo >> $fname
412     echo "# Source: $i" >> $fname
413     echo "# Generated: `date` by `whoami`" >> $fname
414     echo "# Distribution: $distribution" >> $fname
415     echo "# License: $license" >> $fname
416     echo >> $fname
417
418     # Run the distribution processing for this file
419     ad_debug "Processing $i to be $fname"
420     ad_process_file $i $fname
421   done
422
423   ad_debug "<--$FUNCNAME()"
424 }
425
426 #
427 # Processes the entire source tree for distribution.  This inspects files
428 # in the source tree, with exception of any file with .ad suffix, and
429 # performs distribution processing for the file.  The original file is
430 # replaced with the processed file.  This function is used when creating
431 # the distribution for packaging.
432 #
433 # Arguments: ad_process_source_tree <directory>
434 #
435 ad_process_source_tree()
436 {
437   ad_debug "-->$FUNCNAME(): $1"
438
439   ad_debug "<--$FUNCNAME(): $1"
440 }
441
442 #
443 # Creates distribution of the source tree.  All files in the distribution
444 # will be processed and the distribution will be packaged.
445 #
446 ad_makedist()
447 {
448   ad_debug "-->$FUNCNAME()"
449
450   ad_debug "<--$FUNCNAME()"
451 }
452
453 #
454 # Parses the distribution.  Gets all distribution defines from the
455 # distribution.  This also expands all inherited distributions recursively
456 # to get all inherited distribution defines.  From inherited distributions
457 # their name and package name is not inherited.
458 #
459 # Arguments: parse_distribution <distribution name> <inherit>
460 #
461 ad_parse_distribution()
462 {
463   ad_debug "-->$FUNCNAME(): $1"
464
465   if test '!' -f $distdir/$1; then
466     ad_fatal "Distribution '$1' is not declared"
467   fi
468
469   # Get inherited
470   local inhs=`cat $distdir/$1 | grep -v "#" \
471     | grep "inherit " | cut -d' ' -f2 | sort | uniq`
472
473   # Get distdefs
474   local defs=`cat $distdir/$1 | grep -v "#" \
475    | grep "define " | cut -d' ' -f2 | sort | uniq`
476
477   if test "$inhs" = "" && test "$defs" = ""; then
478     ad_fatal "Distribution '$1' does not define anything"
479   fi
480
481   # Get undefined distdefs
482   local undefs=`cat $distdir/$1 | grep -v "#" \
483    | grep "undef " | cut -d' ' -f2`
484
485   # Get options
486   local opts=`cat $distdir/$1 | grep -v "#" \
487    | grep "option " | cut -d' ' -f2`
488
489   # Get includes
490   local incs=`cat $distdir/$1 | grep -v "#" \
491    | grep "include " | cut -d' ' -f2`
492
493   # Get excludes
494   local excs=`cat $distdir/$1 | grep -v "#" \
495    | grep "exclude " | cut -d' ' -f2`
496
497   ad_debug "inherits=$inhs"
498   ad_debug "distdefs=$defs"
499   ad_debug "includes=$incs"
500   ad_debug "excludes=$excs"
501   ad_debug "undistdefs=$undefs"
502   ad_debug "options=$opts"
503
504   # Expand distdefs from inherited distributions
505   for i in $inhs
506   do
507     if test x$1 = x$i; then
508       ad_fatal "Infinite recursion detected.  Fix the '$distdir/$1' \
509             distribution to not have 'inherit $i' declared."
510     fi
511
512     if test '!' -f $distdir/$i; then
513       ad_fatal "Distribution '$i' is not declared (inherited from '$1')"
514     fi
515
516     ad_parse_distribution $i true
517   done
518
519   # Get license
520   license=`cat $distdir/$1 | grep -v "#" \
521    | grep "license " | cut -d' ' -f2`
522   licenseh=`cat $distdir/$1 | grep -v "#" \
523    | grep "license-header " | cut -d' ' -f2`
524
525   ad_debug "license=$license"
526   ad_debug "licenseh=$licenseh"
527
528   if test x$2 = xfalse; then
529     # Get distribution name
530     local dname=`cat $distdir/$1 | grep -v "#" | grep "name "`
531
532     if test "$dname"; then
533       distribution=$dname
534     fi
535
536     # Get distribution package name (optional)
537     local dpname=`cat $distdir/$1 | grep -v "#" \
538      | grep "package " | cut -d' ' -f2`
539
540     if test "$dpname"; then
541       package=$dpname
542     else
543       package=$distribution
544     fi
545
546     ad_debug "distribution=$distribution"
547     ad_debug "package=$package"
548   fi
549
550   # Get hooks (optional)
551   local prh=`cat $distdir/$1 | grep -v "#" \
552    | grep "pre-hook " | cut -d' ' -f2`
553   local poh=`cat $distdir/$1 | grep -v "#" \
554    | grep "post-hook " | cut -d' ' -f2`
555   local prdh=`cat $distdir/$1 | grep -v "#" \
556    | grep "pre-dist-hook " | cut -d' ' -f2`
557   local podh=`cat $distdir/$1 | grep -v "#" \
558    | grep "post-dist-hook " | cut -d' ' -f2`
559
560   # Return to caller
561   inherits="$inherits $inhs"
562   distdefs="$distdefs $defs"
563   includes="$includes $incs"
564   excludes="$excludes $incs"
565   undistdefs="$undistdefs $undefs"
566   options="$options $opts"
567   pre_hooks="$pre_hooks $prh"
568   post_hooks="$post_hooks $poh"
569   pre_dist_hooks="$pre_dist_hooks $prdh"
570   post_dist_hooks="$post_dist_hooks $podh"
571
572   ad_debug "<--$FUNCNAME(): $1"
573 }
574
575 #
576 # Processes parsed distdefs.  Removes duplicates, and undefined distdefs
577 # from the distdefs.
578 #
579 # Arguments: ad_process_distdefs
580 #
581 ad_process_distdefs()
582 {
583   ad_debug "-->$FUNCNAME()"
584
585   # Remove all undefined distribution defines
586   for i in $undistdefs
587   do
588     ad_debug "undefining $i distdef"
589     distdefs=`echo $distdefs | sed s/$i//`
590   done
591
592   rm -f autodist.tmp.defs autodist.pre.hooks autodist.post.hooks 
593   rm -f autodist.pre.dist.hooks autodist.post.dist.hooks
594
595   # Remove duplicate distdefs
596   for i in $distdefs
597   do
598     echo $i >>autodist.tmp.defs
599   done
600   distdefs=`cat autodist.tmp.defs | sort | uniq`
601   distdefs=`echo $distdefs`
602   rm -f autodist.tmp.defs
603
604   ad_debug "distdefs=$distdefs"
605
606   # Remove duplicate pre-hooks
607   for i in $pre_hooks
608   do
609     echo ". $i" >>autodist.tmp.defs
610   done
611   if test -f autodist.tmp.defs; then
612     cat autodist.tmp.defs | sort | uniq > autodist.pre.hooks
613     rm -f autodist.tmp.defs
614   fi
615
616   # Remove duplicate post-hooks
617   for i in $post_hooks
618   do
619     echo ". $i" >>autodist.tmp.defs
620   done
621   if test -f autodist.tmp.defs; then
622     cat autodist.tmp.defs | sort | uniq > autodist.post.hooks
623     rm -f autodist.tmp.defs
624   fi
625
626   # Remove duplicate pre-dist-hooks
627   for i in $pre_dist_hooks
628   do
629     echo ". $i" >>autodist.tmp.defs
630   done
631   if test -f autodist.tmp.defs; then
632     cat autodist.tmp.defs | sort | uniq > autodist.pre.dist.hooks
633     rm -f autodist.tmp.defs
634   fi
635
636   # Remove duplicate post-dist-hooks
637   for i in $post_dist_hooks
638   do
639     echo ". $i" >>autodist.tmp.defs
640   done
641   if test -f autodist.tmp.defs; then
642     cat autodist.tmp.defs | sort | uniq > autodist.post.dist.hooks
643     rm -f autodist.tmp.defs
644   fi
645
646   ad_debug "<--$FUNCNAME()"
647 }
648
649 #
650 # Process a file given as argument for the distribution.
651 #
652 # Arguments: process_file <filepath> <dest_filepath>
653 #
654 ad_process_file()
655 {
656   local found=false
657
658   ad_debug "-->$FUNCNAME(): $1 $2"
659
660   # Process only regular files
661   if test '!' -f $1; then
662     ad_debug "<--$FUNCNAME(): $1 $2"
663     return
664   fi
665
666   ad_debug "Getting #ifdef's and #ifndef's"
667
668   # Get defined distribution defines
669   local defs=`awk "/#ifdef "$DP"_DIST_|#else "$DP"_DIST_/ { print; }" \
670     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
671
672   # Get explicitly not-defined distribution defines
673   local ndefs=`awk "/#ifndef "$DP"_DIST_|#else !"$DP"_DIST_/ { print; }" \
674     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
675
676   ad_debug "defs=$defs"
677   ad_debug "ndefs=$ndefs"
678
679   # Create the script to include and exclude stuff in the file according
680   # to the distribution defines
681   local f=autodist.tmp.script
682   rm -f $f
683
684   # ifdefs
685   ad_debug "processing ifdefs"
686   for d in $defs
687   do
688     found=false
689     for i in $distdefs
690     do
691       if test x$d = x$i; then
692         found=true
693         break
694       fi
695     done
696
697     # If distribution define was not found exclude those lines from the file.
698     # This also handles the #ifdef's #else (ie. #ifndef) branch.
699     if test x$found = xfalse; then
700       ad_debug "ifdef $d will be excluded (it is NOT defined)"
701       echo "/^#ifdef $d/,/^#else !$d|^#endif $d/ { next; }" >> $f
702     else
703       echo "/^#else !$d/,/^#endif $d/ { next; }" >> $f
704     fi
705   done
706
707   # ifndefs
708   ad_debug "processing ifndefs"
709   for d in $ndefs
710   do
711     found=false
712     for i in $distdefs
713     do
714       if test x$d = x$i; then
715         found=true
716         break
717       fi
718     done
719
720     # If distribution define was found exclude those lines from the file.
721     # This also handles the #ifndef's #else (ie. #ifdef) branch.
722     if test x$found = xtrue; then
723       ad_debug "ifndef $d will be excluded (it IS defined)"
724       echo "/^#ifndef $d/,/^#else $d|^#endif $d/ { next; }" >> $f
725     else
726       echo "/^#else $d/,/^#endif $d/ { next; }" >> $f
727     fi
728   done
729
730   # Now process the file with the script
731   if test -f $f; then
732
733     # Those distdef lines that remain in the file are removed to make
734     # the appearance prettier
735     echo "/^#ifdef "$DP"_DIST_|^#endif "$DP"_DIST_|^#else "$DP"_DIST_|^#else !"$DP"_DIST_|^#ifndef "$DP"_DIST_/ { next; }" >> $f
736     echo "{ print; }" >> $f
737
738     # Execute the script
739     awk -f $f $1 >> $2
740   fi
741
742   rm -f $f
743
744   ad_debug "<--$FUNCNAME(): $1 $2"
745 }
746
747
748 #
749 # Process a source file given as argument for the distribution.
750 #
751 # Arguments: process_source_file <filepath> <dest_filepath>
752 #
753 ad_process_source_file()
754 {
755   local found=false
756
757   ad_debug "-->$FUNCNAME(): $1 $2"
758
759   # Process only regular files
760   if test '!' -f $1; then
761     ad_debug "<--$FUNCNAME(): $1 $2"
762     return
763   fi
764
765   ad_debug "Getting #ifdef's and #ifndef's"
766
767   # Get defined distribution defines
768   local defs=`awk '/#ifdef SILC_DIST_|#else \/\* SILC_DIST_/ { print; }' \
769     $1 |cut -d'*' -f2 |cut -d' ' -f2 | sort | uniq`
770
771   # Get explicitly not-defined distribution defines
772   local ndefs=`awk '/#ifndef SILC_DIST_|#else \/\* \!SILC_DIST_/ { print; }' \
773     $1 |cut -d'*' -f2 |cut -d' ' -f2 | cut -d'!' -f2 | sort | uniq`
774
775   ad_debug "defs=$defs"
776   ad_debug "ndefs=$ndefs"
777
778   # Create the script to include and exclude stuff in the file according
779   # to the distribution defines
780   local f=autodist.tmp.script
781   rm -f $f
782
783   # ifdefs
784   ad_debug "processing ifdefs"
785   for d in $defs
786   do
787     found=false
788     for i in $distdefs
789     do
790       if test x$d = x$i; then
791         found=true
792         break
793       fi
794     done
795
796     # If distribution define was not found exclude those lines from the file.
797     # This also handles the #ifdef's #else (ie. #ifndef) branch.
798     if test x$found = xfalse; then
799       ad_debug "ifdef $d will be excluded (it is NOT defined)"
800       echo "/^#ifdef $d/,/^#else \/\* \!$d|^#endif \/\* $d/ { next; }" >> $f
801     else
802       echo "/^#else \/\* \!$d/,/^#endif \/\* $d/ { next; }" >> $f
803     fi
804   done
805
806   # ifndefs
807   ad_debug "processing ifndefs"
808   for d in $ndefs
809   do
810     found=false
811     for i in $distdefs
812     do
813       if test x$d = x$i; then
814         found=true
815         break
816       fi
817     done
818
819     # If distribution define was found exclude those lines from the file.
820     # This also handles the #ifndef's #else (ie. #ifdef) branch.
821     if test x$found = xtrue; then
822       ad_debug "ifndef $d will be excluded (it IS defined)"
823       echo "/^#ifndef $d/,/^#else \/\* $d|^#endif \/\* $d/ { next; }" >> $f
824     else
825       echo "/^#else \/\* $d/,/^#endif \/\* $d/ { next; }" >> $f
826     fi
827   done
828
829   # Now process the file with the script
830   if test -f $f; then
831
832     # Those distdef lines that remain in the file are removed to make
833     # the appearance prettier
834     echo "/^#ifdef SILC_DIST_|^#endif \/\* SILC_DIST_|^#else \/\* SILC_DIST_|^#else \/\* \!SILC_DIST_|^#ifndef SILC_DIST_/ { next; }" >> $f
835     echo "{ print; }" >> $f
836
837     # Execute the script
838     awk -f $f $1 >> $2
839   fi
840
841   rm -f $f
842
843   ad_debug "<--$FUNCNAME(): $1 $2"
844 }
845
846
847 ###############################################################################
848 # Autodist code
849
850 usage="Usage: autodist [options] [distribution] [version]"
851 help="\
852 Autodist prepares source tree for configuration, compilation and 
853 distribution.  Prepares the source tree from the \`autodist.ad' 
854 configuration file.  Generates Automake.am files from Automake.ad
855 files, configure.ac file from configure.ad file(s), generates the 
856 configure script by running Autoconf tool, and generates Makefile.in 
857 files by running Automake tool.
858
859 Operation modes:
860   -h, --help                print this help, then exit
861   -V, --version             print version number, then exit
862   -v, --verbose             verbosely report processing
863   -d, --distdir <dir>       search distributions from <dir>
864   -s, --distdefs [<dist>]   print distribution defines of <dist>, then exit
865   -m, --makedist            create and package distribution
866   -i, --init                initialize Autodist environment, create default
867                             distribution directory and distribution, then exit"
868
869 #
870 # Process command line arguments
871 #
872 while test $# -gt 0; do
873   case "${1}" in
874
875   -d |--distdir)
876     shift;
877     test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; }
878     distdir="${1}";
879     shift;;
880
881   --list)
882     exit 0;;
883
884   -s | --distdefs)
885     shift;
886     if test $# -eq 0; then
887       ad_parse_distribution $distribution false
888       echo "Distribution: ${distribution}" 1>&2;
889     else
890       ad_parse_distribution $1 false
891       echo "Distribution: ${1}" 1>&2;
892     fi
893     ad_process_distdefs
894     echo "Distdefs:" 1>&2;
895     echo "${distdefs}";
896     exit 0;;
897
898   -i | --init)
899     ad_initialize;
900     exit 0;;
901
902   -m | --makedist)
903     makdist=true
904     shift;;
905
906   -v | --verbose)
907     debug=true
908     shift;;
909
910   -h | --help | --h*)
911     echo "${usage}" 1>&2;
912     echo 1>&2;
913     echo "${help}" 1>&2;
914     echo 1>&2;
915     exit 0;;
916
917   -V | --version)
918     echo "autodist (SILC Autodist) $ver" 1>&2;
919     echo "Written by Pekka Riikonen" 1>&2;
920     echo 1>&2;
921     echo "Copyright (C) 2004 - 2005 SILC Project" 1>&2;
922     echo "\
923 This is free software; see the source for copying conditions.  There is NO
924 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " 1>&2;
925     exit 0;;
926
927   --)
928     shift;
929     break;;
930
931   -*)
932     echo "${usage}" 1>&2;
933     exit 1;;
934
935   *)
936     break;;
937
938   esac
939 done
940
941 #
942 # Parse the requested distribution
943 #
944 if test $# != 0; then
945   distribution="${1}";
946   shift
947 fi
948 ad_parse_distribution $distribution false
949 ad_process_distdefs
950
951 if test $# != 0; then
952   dist_version="${1}";
953 fi
954 ad_debug "Preparing source tree for configuration and compilation..."
955 ad_debug "Preparing $distribution distribution version $dist_version"
956
957 #
958 # Create the distribution defines header file
959 #
960 if test "$DISTDEFS"; then
961   ad_create_distdefs_h
962 else
963   ad_fatal "DISTDEFS not defined in $distdir/autodist.conf"
964 fi
965
966 #
967 # Run pre-hooks
968 #
969 if test -f autodist.pre.hooks; then
970   . autodist.pre.hooks
971   rm -f autodist.pre.hooks
972 fi
973
974 #
975 # Generate the Makefile.am files from Makefile.ad files
976 #
977 ad_make_makefile_ams
978
979 #
980 # Generate the configure.ac from configure.ad file(s)
981 #
982 ad_make_configure_ac ./configure.ad
983
984 #
985 # Generate configure script
986 #
987 ad_make_configure
988
989 #
990 # Generate Makefile.in files
991 #
992 ad_make_makefile_ins
993
994 #
995 # Process all files with .ad suffix for distribution processing
996 #
997 ad_process_ads
998
999 #
1000 # Run post-hooks
1001 #
1002 if test -f autodist.post.hooks; then
1003   . autodist.post.hooks
1004   rm -f autodist.post.hooks
1005 fi
1006
1007 #
1008 # Generate distribution, if requested
1009 #
1010 if test x$makedist = xtrue; then
1011  ad_debug "Creating distribution"
1012
1013  exit 0
1014 fi
1015
1016 ad_debug "Done, now run ./configure and make."
1017 exit 0