1#!/bin/sh
2#                        a u t o g e n . s h
3#
4# Copyright (c) 2005-2010 United States Government as represented by
5# the U.S. Army Research Laboratory.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10#
11# 1. Redistributions of source code must retain the above copyright
12# notice, this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above
15# copyright notice, this list of conditions and the following
16# disclaimer in the documentation and/or other materials provided
17# with the distribution.
18#
19# 3. The name of the author may not be used to endorse or promote
20# products derived from this software without specific prior written
21# permission.
22#
23# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35###
36#
37# Script for automatically preparing the sources for compilation by
38# performing the myriad of necessary steps.  The script attempts to
39# detect proper version support, and outputs warnings about particular
40# systems that have autotool peculiarities.
41#
42# Basically, if everything is set up and installed correctly, the
43# script will validate that minimum versions of the GNU Build System
44# tools are installed, account for several common configuration
45# issues, and then simply run autoreconf for you.
46#
47# If autoreconf fails, which can happen for many valid configurations,
48# this script proceeds to run manual preparation steps effectively
49# providing a POSIX shell script (mostly complete) reimplementation of
50# autoreconf.
51#
52# The AUTORECONF, AUTOCONF, AUTOMAKE, LIBTOOLIZE, ACLOCAL, AUTOHEADER
53# environment variables and corresponding _OPTIONS variables (e.g.
54# AUTORECONF_OPTIONS) may be used to override the default automatic
55# detection behaviors.  Similarly the _VERSION variables will override
56# the minimum required version numbers.
57#
58# Examples:
59#
60#   To obtain help on usage:
61#     ./autogen.sh --help
62#
63#   To obtain verbose output:
64#     ./autogen.sh --verbose
65#
66#   To skip autoreconf and prepare manually:
67#     AUTORECONF=false ./autogen.sh
68#
69#   To verbosely try running with an older (unsupported) autoconf:
70#     AUTOCONF_VERSION=2.50 ./autogen.sh --verbose
71#
72# Author:
73#   Christopher Sean Morrison <morrison@brlcad.org>
74#
75# Patches:
76#   Sebastian Pipping <sebastian@pipping.org>
77#   Tom Browder <tbrowder2@users.sourceforge.net>
78#
79######################################################################
80
81# set to minimum acceptable version of autoconf
82if [ "x$AUTOCONF_VERSION" = "x" ] ; then
83    AUTOCONF_VERSION=2.58
84fi
85# set to minimum acceptable version of automake
86if [ "x$AUTOMAKE_VERSION" = "x" ] ; then
87    AUTOMAKE_VERSION=1.6.0
88fi
89# set to minimum acceptable version of libtool
90if [ "x$LIBTOOL_VERSION" = "x" ] ; then
91    LIBTOOL_VERSION=1.4.2
92fi
93
94
95##################
96# ident function #
97##################
98ident ( ) {
99    # extract copyright from header
100    __copyright="`grep Copyright $AUTOGEN_SH | head -${HEAD_N}1 | awk '{print $4}'`"
101    if [ "x$__copyright" = "x" ] ; then
102	__copyright="`date +%Y`"
103    fi
104
105    # extract version from CVS Id string
106    __id="$Id: autogen.sh 38795 2010-04-26 19:34:05Z brlcad $"
107    __version="`echo $__id | sed 's/.*\([0-9][0-9][0-9][0-9]\)[-\/]\([0-9][0-9]\)[-\/]\([0-9][0-9]\).*/\1\2\3/'`"
108    if [ "x$__version" = "x" ] ; then
109	__version=""
110    fi
111
112    echo "autogen.sh build preparation script by Christopher Sean Morrison"
113    echo "  + config.guess download patch by Sebastian Pipping (2008-12-03)"
114    echo "revised 3-clause BSD-style license, copyright (c) $__copyright"
115    echo "script version $__version, ISO/IEC 9945 POSIX shell script"
116}
117
118
119##################
120# USAGE FUNCTION #
121##################
122usage ( ) {
123    echo "Usage: $AUTOGEN_SH [-h|--help] [-v|--verbose] [-q|--quiet] [-d|--download] [--version]"
124    echo "    --help      Help on $NAME_OF_AUTOGEN usage"
125    echo "    --verbose   Verbose progress output"
126    echo "    --quiet     Quiet suppressed progress output"
127    echo "    --download  Download the latest config.guess from gnulib"
128    echo "    --version   Only perform GNU Build System version checks"
129    echo
130    echo "Description: This script will validate that minimum versions of the"
131    echo "GNU Build System tools are installed and then run autoreconf for you."
132    echo "Should autoreconf fail, manual preparation steps will be run"
133    echo "potentially accounting for several common preparation issues.  The"
134
135    echo "AUTORECONF, AUTOCONF, AUTOMAKE, LIBTOOLIZE, ACLOCAL, AUTOHEADER,"
136    echo "PROJECT, & CONFIGURE environment variables and corresponding _OPTIONS"
137    echo "variables (e.g. AUTORECONF_OPTIONS) may be used to override the"
138    echo "default automatic detection behavior."
139    echo
140
141    ident
142
143    return 0
144}
145
146
147##########################
148# VERSION_ERROR FUNCTION #
149##########################
150version_error ( ) {
151    if [ "x$1" = "x" ] ; then
152	echo "INTERNAL ERROR: version_error was not provided a version"
153	exit 1
154    fi
155    if [ "x$2" = "x" ] ; then
156	echo "INTERNAL ERROR: version_error was not provided an application name"
157	exit 1
158    fi
159    $ECHO
160    $ECHO "ERROR:  To prepare the ${PROJECT} build system from scratch,"
161    $ECHO "        at least version $1 of $2 must be installed."
162    $ECHO
163    $ECHO "$NAME_OF_AUTOGEN does not need to be run on the same machine that will"
164    $ECHO "run configure or make.  Either the GNU Autotools will need to be installed"
165    $ECHO "or upgraded on this system, or $NAME_OF_AUTOGEN must be run on the source"
166    $ECHO "code on another system and then transferred to here. -- Cheers!"
167    $ECHO
168}
169
170##########################
171# VERSION_CHECK FUNCTION #
172##########################
173version_check ( ) {
174    if [ "x$1" = "x" ] ; then
175	echo "INTERNAL ERROR: version_check was not provided a minimum version"
176	exit 1
177    fi
178    _min="$1"
179    if [ "x$2" = "x" ] ; then
180	echo "INTERNAL ERROR: version check was not provided a comparison version"
181	exit 1
182    fi
183    _cur="$2"
184
185    # needed to handle versions like 1.10 and 1.4-p6
186    _min="`echo ${_min}. | sed 's/[^0-9]/./g' | sed 's/\.\././g'`"
187    _cur="`echo ${_cur}. | sed 's/[^0-9]/./g' | sed 's/\.\././g'`"
188
189    _min_major="`echo $_min | cut -d. -f1`"
190    _min_minor="`echo $_min | cut -d. -f2`"
191    _min_patch="`echo $_min | cut -d. -f3`"
192
193    _cur_major="`echo $_cur | cut -d. -f1`"
194    _cur_minor="`echo $_cur | cut -d. -f2`"
195    _cur_patch="`echo $_cur | cut -d. -f3`"
196
197    if [ "x$_min_major" = "x" ] ; then
198	_min_major=0
199    fi
200    if [ "x$_min_minor" = "x" ] ; then
201	_min_minor=0
202    fi
203    if [ "x$_min_patch" = "x" ] ; then
204	_min_patch=0
205    fi
206    if [ "x$_cur_minor" = "x" ] ; then
207	_cur_major=0
208    fi
209    if [ "x$_cur_minor" = "x" ] ; then
210	_cur_minor=0
211    fi
212    if [ "x$_cur_patch" = "x" ] ; then
213	_cur_patch=0
214    fi
215
216    $VERBOSE_ECHO "Checking if ${_cur_major}.${_cur_minor}.${_cur_patch} is greater than ${_min_major}.${_min_minor}.${_min_patch}"
217
218    if [ $_min_major -lt $_cur_major ] ; then
219	return 0
220    elif [ $_min_major -eq $_cur_major ] ; then
221	if [ $_min_minor -lt $_cur_minor ] ; then
222	    return 0
223	elif [ $_min_minor -eq $_cur_minor ] ; then
224	    if [ $_min_patch -lt $_cur_patch ] ; then
225		return 0
226	    elif [ $_min_patch -eq $_cur_patch ] ; then
227		return 0
228	    fi
229	fi
230    fi
231    return 1
232}
233
234
235######################################
236# LOCATE_CONFIGURE_TEMPLATE FUNCTION #
237######################################
238locate_configure_template ( ) {
239    _pwd="`pwd`"
240    if test -f "./configure.ac" ; then
241	echo "./configure.ac"
242    elif test -f "./configure.in" ; then
243	echo "./configure.in"
244    elif test -f "$_pwd/configure.ac" ; then
245	echo "$_pwd/configure.ac"
246    elif test -f "$_pwd/configure.in" ; then
247	echo "$_pwd/configure.in"
248    elif test -f "$PATH_TO_AUTOGEN/configure.ac" ; then
249	echo "$PATH_TO_AUTOGEN/configure.ac"
250    elif test -f "$PATH_TO_AUTOGEN/configure.in" ; then
251	echo "$PATH_TO_AUTOGEN/configure.in"
252    fi
253}
254
255
256##################
257# argument check #
258##################
259ARGS="$*"
260PATH_TO_AUTOGEN="`dirname $0`"
261NAME_OF_AUTOGEN="`basename $0`"
262AUTOGEN_SH="$PATH_TO_AUTOGEN/$NAME_OF_AUTOGEN"
263
264LIBTOOL_M4="${PATH_TO_AUTOGEN}/misc/libtool.m4"
265
266if [ "x$HELP" = "x" ] ; then
267    HELP=no
268fi
269if [ "x$QUIET" = "x" ] ; then
270    QUIET=no
271fi
272if [ "x$VERBOSE" = "x" ] ; then
273    VERBOSE=no
274fi
275if [ "x$VERSION_ONLY" = "x" ] ; then
276    VERSION_ONLY=no
277fi
278if [ "x$DOWNLOAD" = "x" ] ; then
279    DOWNLOAD=no
280fi
281if [ "x$AUTORECONF_OPTIONS" = "x" ] ; then
282    AUTORECONF_OPTIONS="-i -f"
283fi
284if [ "x$AUTOCONF_OPTIONS" = "x" ] ; then
285    AUTOCONF_OPTIONS="-f"
286fi
287if [ "x$AUTOMAKE_OPTIONS" = "x" ] ; then
288    AUTOMAKE_OPTIONS="-a -c -f"
289fi
290ALT_AUTOMAKE_OPTIONS="-a -c"
291if [ "x$LIBTOOLIZE_OPTIONS" = "x" ] ; then
292    LIBTOOLIZE_OPTIONS="--automake -c -f"
293fi
294ALT_LIBTOOLIZE_OPTIONS="--automake --copy --force"
295if [ "x$ACLOCAL_OPTIONS" = "x" ] ; then
296    ACLOCAL_OPTIONS=""
297fi
298if [ "x$AUTOHEADER_OPTIONS" = "x" ] ; then
299    AUTOHEADER_OPTIONS=""
300fi
301if [ "x$CONFIG_GUESS_URL" = "x" ] ; then
302    CONFIG_GUESS_URL="http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob_plain;f=build-aux/config.guess;hb=HEAD"
303fi
304for arg in $ARGS ; do
305    case "x$arg" in
306	x--help) HELP=yes ;;
307	x-[hH]) HELP=yes ;;
308	x--quiet) QUIET=yes ;;
309	x-[qQ]) QUIET=yes ;;
310	x--verbose) VERBOSE=yes ;;
311	x-[dD]) DOWNLOAD=yes ;;
312	x--download) DOWNLOAD=yes ;;
313	x-[vV]) VERBOSE=yes ;;
314	x--version) VERSION_ONLY=yes ;;
315	*)
316	    echo "Unknown option: $arg"
317	    echo
318	    usage
319	    exit 1
320	    ;;
321    esac
322done
323
324
325#####################
326# environment check #
327#####################
328
329# sanity check before recursions potentially begin
330if [ ! -f "$AUTOGEN_SH" ] ; then
331    if test -f ./autogen.sh ; then
332	PATH_TO_AUTOGEN="."
333	NAME_OF_AUTOGEN="autogen.sh"
334	AUTOGEN_SH="$PATH_TO_AUTOGEN/$NAME_OF_AUTOGEN"
335    else
336	echo "INTERNAL ERROR: $AUTOGEN_SH does not exist"
337	exit 1
338    fi
339fi
340
341# force locale setting to C so things like date output as expected
342LC_ALL=C
343
344# commands that this script expects
345for __cmd in echo head tail pwd ; do
346    echo "test" > /dev/null 2>&1 | $__cmd > /dev/null 2>&1
347    if [ $? != 0 ] ; then
348	echo "INTERNAL ERROR: '${__cmd}' command is required"
349	exit 2
350    fi
351done
352echo "test" | grep "test" > /dev/null 2>&1
353if test ! x$? = x0 ; then
354    echo "INTERNAL ERROR: grep command is required"
355    exit 1
356fi
357echo "test" | sed "s/test/test/" > /dev/null 2>&1
358if test ! x$? = x0 ; then
359    echo "INTERNAL ERROR: sed command is required"
360    exit 1
361fi
362
363
364# determine the behavior of echo
365case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in
366    *c*,-n*) ECHO_N= ECHO_C='
367' ECHO_T='	' ;;
368    *c*,*  ) ECHO_N=-n ECHO_C= ECHO_T= ;;
369    *)       ECHO_N= ECHO_C='\c' ECHO_T= ;;
370esac
371
372# determine the behavior of head
373case "x`echo 'head' | head -n 1 2>&1`" in
374    *xhead*) HEAD_N="n " ;;
375    *) HEAD_N="" ;;
376esac
377
378# determine the behavior of tail
379case "x`echo 'tail' | tail -n 1 2>&1`" in
380    *xtail*) TAIL_N="n " ;;
381    *) TAIL_N="" ;;
382esac
383
384VERBOSE_ECHO=:
385ECHO=:
386if [ "x$QUIET" = "xyes" ] ; then
387    if [ "x$VERBOSE" = "xyes" ] ; then
388	echo "Verbose output quelled by quiet option.  Further output disabled."
389    fi
390else
391    ECHO=echo
392    if [ "x$VERBOSE" = "xyes" ] ; then
393	echo "Verbose output enabled"
394	VERBOSE_ECHO=echo
395    fi
396fi
397
398
399# allow a recursive run to disable further recursions
400if [ "x$RUN_RECURSIVE" = "x" ] ; then
401    RUN_RECURSIVE=yes
402fi
403
404
405################################################
406# check for help arg and bypass version checks #
407################################################
408if [ "x`echo $ARGS | sed 's/.*[hH][eE][lL][pP].*/help/'`" = "xhelp" ] ; then
409    HELP=yes
410fi
411if [ "x$HELP" = "xyes" ] ; then
412    usage
413    $ECHO "---"
414    $ECHO "Help was requested.  No preparation or configuration will be performed."
415    exit 0
416fi
417
418
419#######################
420# set up signal traps #
421#######################
422untrap_abnormal ( ) {
423    for sig in 1 2 13 15; do
424	trap - $sig
425    done
426}
427
428# do this cleanup whenever we exit.
429trap '
430    # start from the root
431    if test -d "$START_PATH" ; then
432	cd "$START_PATH"
433    fi
434
435    # restore/delete backup files
436    if test "x$PFC_INIT" = "x1" ; then
437	recursive_restore
438    fi
439' 0
440
441# trap SIGHUP (1), SIGINT (2), SIGPIPE (13), SIGTERM (15)
442for sig in 1 2 13 15; do
443    trap '
444	$ECHO ""
445	$ECHO "Aborting $NAME_OF_AUTOGEN: caught signal '$sig'"
446
447	# start from the root
448	if test -d "$START_PATH" ; then
449	    cd "$START_PATH"
450	fi
451
452	# clean up on abnormal exit
453	$VERBOSE_ECHO "rm -rf autom4te.cache"
454	rm -rf autom4te.cache
455
456	if test -f "acinclude.m4.$$.backup" ; then
457	    $VERBOSE_ECHO "cat acinclude.m4.$$.backup > acinclude.m4"
458	    chmod u+w acinclude.m4
459	    cat acinclude.m4.$$.backup > acinclude.m4
460
461	    $VERBOSE_ECHO "rm -f acinclude.m4.$$.backup"
462	    rm -f acinclude.m4.$$.backup
463        fi
464
465	{ (exit 1); exit 1; }
466' $sig
467done
468
469
470#############################
471# look for a configure file #
472#############################
473if [ "x$CONFIGURE" = "x" ] ; then
474    CONFIGURE="`locate_configure_template`"
475    if [ ! "x$CONFIGURE" = "x" ] ; then
476	$VERBOSE_ECHO "Found a configure template: $CONFIGURE"
477    fi
478else
479    $ECHO "Using CONFIGURE environment variable override: $CONFIGURE"
480fi
481if [ "x$CONFIGURE" = "x" ] ; then
482    if [ "x$VERSION_ONLY" = "xyes" ] ; then
483	CONFIGURE=/dev/null
484    else
485	$ECHO
486	$ECHO "A configure.ac or configure.in file could not be located implying"
487	$ECHO "that the GNU Build System is at least not used in this directory.  In"
488	$ECHO "any case, there is nothing to do here without one of those files."
489	$ECHO
490	$ECHO "ERROR: No configure.in or configure.ac file found in `pwd`"
491	exit 1
492    fi
493fi
494
495####################
496# get project name #
497####################
498if [ "x$PROJECT" = "x" ] ; then
499    PROJECT="`grep AC_INIT $CONFIGURE | grep -v '.*#.*AC_INIT' | tail -${TAIL_N}1 | sed 's/^[ 	]*AC_INIT(\([^,)]*\).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`"
500    if [ "x$PROJECT" = "xAC_INIT" ] ; then
501	# projects might be using the older/deprecated arg-less AC_INIT .. look for AM_INIT_AUTOMAKE instead
502	PROJECT="`grep AM_INIT_AUTOMAKE $CONFIGURE | grep -v '.*#.*AM_INIT_AUTOMAKE' | tail -${TAIL_N}1 | sed 's/^[ 	]*AM_INIT_AUTOMAKE(\([^,)]*\).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`"
503    fi
504    if [ "x$PROJECT" = "xAM_INIT_AUTOMAKE" ] ; then
505	PROJECT="project"
506    fi
507    if [ "x$PROJECT" = "x" ] ; then
508	PROJECT="project"
509    fi
510else
511    $ECHO "Using PROJECT environment variable override: $PROJECT"
512fi
513$ECHO "Preparing the $PROJECT build system...please wait"
514$ECHO
515
516
517########################
518# check for autoreconf #
519########################
520HAVE_AUTORECONF=no
521if [ "x$AUTORECONF" = "x" ] ; then
522    for AUTORECONF in autoreconf ; do
523	$VERBOSE_ECHO "Checking autoreconf version: $AUTORECONF --version"
524	$AUTORECONF --version > /dev/null 2>&1
525	if [ $? = 0 ] ; then
526	    HAVE_AUTORECONF=yes
527	    break
528	fi
529    done
530else
531    HAVE_AUTORECONF=yes
532    $ECHO "Using AUTORECONF environment variable override: $AUTORECONF"
533fi
534
535
536##########################
537# autoconf version check #
538##########################
539_acfound=no
540if [ "x$AUTOCONF" = "x" ] ; then
541    for AUTOCONF in autoconf ; do
542	$VERBOSE_ECHO "Checking autoconf version: $AUTOCONF --version"
543	$AUTOCONF --version > /dev/null 2>&1
544	if [ $? = 0 ] ; then
545	    _acfound=yes
546	    break
547	fi
548    done
549else
550    _acfound=yes
551    $ECHO "Using AUTOCONF environment variable override: $AUTOCONF"
552fi
553
554_report_error=no
555if [ ! "x$_acfound" = "xyes" ] ; then
556    $ECHO "ERROR:  Unable to locate GNU Autoconf."
557    _report_error=yes
558else
559    _version="`$AUTOCONF --version | head -${HEAD_N}1 | sed 's/[^0-9]*\([0-9\.][0-9\.]*\)/\1/'`"
560    if [ "x$_version" = "x" ] ; then
561	_version="0.0.0"
562    fi
563    $ECHO "Found GNU Autoconf version $_version"
564    version_check "$AUTOCONF_VERSION" "$_version"
565    if [ $? -ne 0 ] ; then
566	_report_error=yes
567    fi
568fi
569if [ "x$_report_error" = "xyes" ] ; then
570    version_error "$AUTOCONF_VERSION" "GNU Autoconf"
571    exit 1
572fi
573
574
575##########################
576# automake version check #
577##########################
578_amfound=no
579if [ "x$AUTOMAKE" = "x" ] ; then
580    for AUTOMAKE in automake ; do
581	$VERBOSE_ECHO "Checking automake version: $AUTOMAKE --version"
582	$AUTOMAKE --version > /dev/null 2>&1
583	if [ $? = 0 ] ; then
584	    _amfound=yes
585	    break
586	fi
587    done
588else
589    _amfound=yes
590    $ECHO "Using AUTOMAKE environment variable override: $AUTOMAKE"
591fi
592
593
594_report_error=no
595if [ ! "x$_amfound" = "xyes" ] ; then
596    $ECHO
597    $ECHO "ERROR: Unable to locate GNU Automake."
598    _report_error=yes
599else
600    _version="`$AUTOMAKE --version | head -${HEAD_N}1 | sed 's/[^0-9]*\([0-9\.][0-9\.]*\)/\1/'`"
601    if [ "x$_version" = "x" ] ; then
602	_version="0.0.0"
603    fi
604    $ECHO "Found GNU Automake version $_version"
605    version_check "$AUTOMAKE_VERSION" "$_version"
606    if [ $? -ne 0 ] ; then
607	_report_error=yes
608    fi
609fi
610if [ "x$_report_error" = "xyes" ] ; then
611    version_error "$AUTOMAKE_VERSION" "GNU Automake"
612    exit 1
613fi
614
615
616########################
617# check for libtoolize #
618########################
619HAVE_LIBTOOLIZE=yes
620HAVE_ALT_LIBTOOLIZE=no
621_ltfound=no
622if [ "x$LIBTOOLIZE" = "x" ] ; then
623    LIBTOOLIZE=libtoolize
624    $VERBOSE_ECHO "Checking libtoolize version: $LIBTOOLIZE --version"
625    $LIBTOOLIZE --version > /dev/null 2>&1
626    if [ ! $? = 0 ] ; then
627	HAVE_LIBTOOLIZE=no
628	$ECHO
629	if [ "x$HAVE_AUTORECONF" = "xno" ] ; then
630	    $ECHO "Warning:  libtoolize does not appear to be available."
631	else
632	    $ECHO "Warning:  libtoolize does not appear to be available.  This means that"
633	    $ECHO "the automatic build preparation via autoreconf will probably not work."
634	    $ECHO "Preparing the build by running each step individually, however, should"
635	    $ECHO "work and will be done automatically for you if autoreconf fails."
636	fi
637
638	# look for some alternates
639	for tool in glibtoolize libtoolize15 libtoolize14 libtoolize13 ; do
640	    $VERBOSE_ECHO "Checking libtoolize alternate: $tool --version"
641	    _glibtoolize="`$tool --version > /dev/null 2>&1`"
642	    if [ $? = 0 ] ; then
643		$VERBOSE_ECHO "Found $tool --version"
644		_glti="`which $tool`"
645		if [ "x$_glti" = "x" ] ; then
646		    $VERBOSE_ECHO "Cannot find $tool with which"
647		    continue;
648		fi
649		if test ! -f "$_glti" ; then
650		    $VERBOSE_ECHO "Cannot use $tool, $_glti is not a file"
651		    continue;
652		fi
653		_gltidir="`dirname $_glti`"
654		if [ "x$_gltidir" = "x" ] ; then
655		    $VERBOSE_ECHO "Cannot find $tool path with dirname of $_glti"
656		    continue;
657		fi
658		if test ! -d "$_gltidir" ; then
659		    $VERBOSE_ECHO "Cannot use $tool, $_gltidir is not a directory"
660		    continue;
661		fi
662		HAVE_ALT_LIBTOOLIZE=yes
663		LIBTOOLIZE="$tool"
664		$ECHO
665		$ECHO "Fortunately, $tool was found which means that your system may simply"
666		$ECHO "have a non-standard or incomplete GNU Autotools install.  If you have"
667		$ECHO "sufficient system access, it may be possible to quell this warning by"
668		$ECHO "running:"
669		$ECHO
670		sudo -V > /dev/null 2>&1
671		if [ $? = 0 ] ; then
672		    $ECHO "   sudo ln -s $_glti $_gltidir/libtoolize"
673		    $ECHO
674		else
675		    $ECHO "   ln -s $_glti $_gltidir/libtoolize"
676		    $ECHO
677		    $ECHO "Run that as root or with proper permissions to the $_gltidir directory"
678		    $ECHO
679		fi
680		_ltfound=yes
681		break
682	    fi
683	done
684    else
685	_ltfound=yes
686    fi
687else
688    _ltfound=yes
689    $ECHO "Using LIBTOOLIZE environment variable override: $LIBTOOLIZE"
690fi
691
692
693############################
694# libtoolize version check #
695############################
696_report_error=no
697if [ ! "x$_ltfound" = "xyes" ] ; then
698    $ECHO
699    $ECHO "ERROR: Unable to locate GNU Libtool."
700    _report_error=yes
701else
702    _version="`$LIBTOOLIZE --version | head -${HEAD_N}1 | sed 's/[^0-9]*\([0-9\.][0-9\.]*\)/\1/'`"
703    if [ "x$_version" = "x" ] ; then
704	_version="0.0.0"
705    fi
706    $ECHO "Found GNU Libtool version $_version"
707    version_check "$LIBTOOL_VERSION" "$_version"
708    if [ $? -ne 0 ] ; then
709	_report_error=yes
710    fi
711fi
712if [ "x$_report_error" = "xyes" ] ; then
713    version_error "$LIBTOOL_VERSION" "GNU Libtool"
714    exit 1
715fi
716
717
718#####################
719# check for aclocal #
720#####################
721if [ "x$ACLOCAL" = "x" ] ; then
722    for ACLOCAL in aclocal ; do
723	$VERBOSE_ECHO "Checking aclocal version: $ACLOCAL --version"
724	$ACLOCAL --version > /dev/null 2>&1
725	if [ $? = 0 ] ; then
726	    break
727	fi
728    done
729else
730    $ECHO "Using ACLOCAL environment variable override: $ACLOCAL"
731fi
732
733
734########################
735# check for autoheader #
736########################
737if [ "x$AUTOHEADER" = "x" ] ; then
738    for AUTOHEADER in autoheader ; do
739	$VERBOSE_ECHO "Checking autoheader version: $AUTOHEADER --version"
740	$AUTOHEADER --version > /dev/null 2>&1
741	if [ $? = 0 ] ; then
742	    break
743	fi
744    done
745else
746    $ECHO "Using AUTOHEADER environment variable override: $AUTOHEADER"
747fi
748
749
750#########################
751# check if version only #
752#########################
753$VERBOSE_ECHO "Checking whether to only output version information"
754if [ "x$VERSION_ONLY" = "xyes" ] ; then
755    $ECHO
756    ident
757    $ECHO "---"
758    $ECHO "Version requested.  No preparation or configuration will be performed."
759    exit 0
760fi
761
762
763#################################
764# PROTECT_FROM_CLOBBER FUNCTION #
765#################################
766protect_from_clobber ( ) {
767    PFC_INIT=1
768
769    # protect COPYING & INSTALL from overwrite by automake.  the
770    # automake force option will (inappropriately) ignore the existing
771    # contents of a COPYING and/or INSTALL files (depending on the
772    # version) instead of just forcing *missing* files like it does
773    # for AUTHORS, NEWS, and README. this is broken but extremely
774    # prevalent behavior, so we protect against it by keeping a backup
775    # of the file that can later be restored.
776
777    for file in COPYING INSTALL ; do
778	if test -f ${file} ; then
779	    if test -f ${file}.$$.protect_from_automake.backup ; then
780		$VERBOSE_ECHO "Already backed up ${file} in `pwd`"
781	    else
782		$VERBOSE_ECHO "Backing up ${file} in `pwd`"
783		$VERBOSE_ECHO "cp -p ${file} ${file}.$$.protect_from_automake.backup"
784		cp -p ${file} ${file}.$$.protect_from_automake.backup
785	    fi
786	fi
787    done
788}
789
790
791##############################
792# RECURSIVE_PROTECT FUNCTION #
793##############################
794recursive_protect ( ) {
795
796    # for projects using recursive configure, run the build
797    # preparation steps for the subdirectories.  this function assumes
798    # START_PATH was set to pwd before recursion begins so that
799    # relative paths work.
800
801    # git 'r done, protect COPYING and INSTALL from being clobbered
802    protect_from_clobber
803
804    if test -d autom4te.cache ; then
805	$VERBOSE_ECHO "Found an autom4te.cache directory, deleting it"
806	$VERBOSE_ECHO "rm -rf autom4te.cache"
807	rm -rf autom4te.cache
808    fi
809
810    # find configure template
811    _configure="`locate_configure_template`"
812    if [ "x$_configure" = "x" ] ; then
813	return
814    fi
815    # $VERBOSE_ECHO "Looking for configure template found `pwd`/$_configure"
816
817    # look for subdirs
818    # $VERBOSE_ECHO "Looking for subdirs in `pwd`"
819    _det_config_subdirs="`grep AC_CONFIG_SUBDIRS $_configure | grep -v '.*#.*AC_CONFIG_SUBDIRS' | sed 's/^[ 	]*AC_CONFIG_SUBDIRS(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`"
820    CHECK_DIRS=""
821    for dir in $_det_config_subdirs ; do
822	if test -d "`pwd`/$dir" ; then
823	    CHECK_DIRS="$CHECK_DIRS \"`pwd`/$dir\""
824	fi
825    done
826
827    # process subdirs
828    if [ ! "x$CHECK_DIRS" = "x" ] ; then
829	$VERBOSE_ECHO "Recursively scanning the following directories:"
830	$VERBOSE_ECHO "  $CHECK_DIRS"
831	for dir in $CHECK_DIRS ; do
832	    $VERBOSE_ECHO "Protecting files from automake in $dir"
833	    cd "$START_PATH"
834	    eval "cd $dir"
835
836	    # recursively git 'r done
837	    recursive_protect
838	done
839    fi
840} # end of recursive_protect
841
842
843#############################
844# RESTORE_CLOBBERED FUNCION #
845#############################
846restore_clobbered ( ) {
847
848    # The automake (and autoreconf by extension) -f/--force-missing
849    # option may overwrite COPYING and INSTALL even if they do exist.
850    # Here we restore the files if necessary.
851
852    spacer=no
853
854    for file in COPYING INSTALL ; do
855	if test -f ${file}.$$.protect_from_automake.backup ; then
856	    if test -f ${file} ; then
857	    # compare entire content, restore if needed
858	    if test "x`cat ${file}`" != "x`cat ${file}.$$.protect_from_automake.backup`" ; then
859		if test "x$spacer" = "xno" ; then
860		    $VERBOSE_ECHO
861		    spacer=yes
862		fi
863		# restore the backup
864		$VERBOSE_ECHO "Restoring ${file} from backup (automake -f likely clobbered it)"
865		$VERBOSE_ECHO "rm -f ${file}"
866		rm -f ${file}
867		$VERBOSE_ECHO "mv ${file}.$$.protect_from_automake.backup ${file}"
868		mv ${file}.$$.protect_from_automake.backup ${file}
869	    fi # check contents
870	    elif test -f ${file}.$$.protect_from_automake.backup ; then
871		$VERBOSE_ECHO "mv ${file}.$$.protect_from_automake.backup ${file}"
872		mv ${file}.$$.protect_from_automake.backup ${file}
873	    fi # -f ${file}
874
875	    # just in case
876	    $VERBOSE_ECHO "rm -f ${file}.$$.protect_from_automake.backup"
877	    rm -f ${file}.$$.protect_from_automake.backup
878	fi # -f ${file}.$$.protect_from_automake.backup
879    done
880
881    CONFIGURE="`locate_configure_template`"
882    if [ "x$CONFIGURE" = "x" ] ; then
883	return
884    fi
885
886    _aux_dir="`grep AC_CONFIG_AUX_DIR $CONFIGURE | grep -v '.*#.*AC_CONFIG_AUX_DIR' | tail -${TAIL_N}1 | sed 's/^[ 	]*AC_CONFIG_AUX_DIR(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`"
887    if test ! -d "$_aux_dir" ; then
888	_aux_dir=.
889    fi
890
891    for file in config.guess config.sub ltmain.sh ; do
892	if test -f "${_aux_dir}/${file}" ; then
893	    $VERBOSE_ECHO "rm -f \"${_aux_dir}/${file}.backup\""
894	    rm -f "${_aux_dir}/${file}.backup"
895	fi
896    done
897} # end of restore_clobbered
898
899
900##############################
901# RECURSIVE_RESTORE FUNCTION #
902##############################
903recursive_restore ( ) {
904
905    # restore COPYING and INSTALL from backup if they were clobbered
906    # for each directory recursively.
907
908    # git 'r undone
909    restore_clobbered
910
911    # find configure template
912    _configure="`locate_configure_template`"
913    if [ "x$_configure" = "x" ] ; then
914	return
915    fi
916
917    # look for subdirs
918    _det_config_subdirs="`grep AC_CONFIG_SUBDIRS $_configure | grep -v '.*#.*AC_CONFIG_SUBDIRS' | sed 's/^[ 	]*AC_CONFIG_SUBDIRS(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`"
919    CHECK_DIRS=""
920    for dir in $_det_config_subdirs ; do
921	if test -d "`pwd`/$dir" ; then
922	    CHECK_DIRS="$CHECK_DIRS \"`pwd`/$dir\""
923	fi
924    done
925
926    # process subdirs
927    if [ ! "x$CHECK_DIRS" = "x" ] ; then
928	$VERBOSE_ECHO "Recursively scanning the following directories:"
929	$VERBOSE_ECHO "  $CHECK_DIRS"
930	for dir in $CHECK_DIRS ; do
931	    $VERBOSE_ECHO "Checking files for automake damage in $dir"
932	    cd "$START_PATH"
933	    eval "cd $dir"
934
935	    # recursively git 'r undone
936	    recursive_restore
937	done
938    fi
939} # end of recursive_restore
940
941
942#######################
943# INITIALIZE FUNCTION #
944#######################
945initialize ( ) {
946
947    # this routine performs a variety of directory-specific
948    # initializations.  some are sanity checks, some are preventive,
949    # and some are necessary setup detection.
950    #
951    # this function sets:
952    #   CONFIGURE
953    #   SEARCH_DIRS
954    #   CONFIG_SUBDIRS
955
956    ##################################
957    # check for a configure template #
958    ##################################
959    CONFIGURE="`locate_configure_template`"
960    if [ "x$CONFIGURE" = "x" ] ; then
961	$ECHO
962	$ECHO "A configure.ac or configure.in file could not be located implying"
963	$ECHO "that the GNU Build System is at least not used in this directory.  In"
964	$ECHO "any case, there is nothing to do here without one of those files."
965	$ECHO
966	$ECHO "ERROR: No configure.in or configure.ac file found in `pwd`"
967	exit 1
968    fi
969
970    #####################
971    # detect an aux dir #
972    #####################
973    _aux_dir="`grep AC_CONFIG_AUX_DIR $CONFIGURE | grep -v '.*#.*AC_CONFIG_AUX_DIR' | tail -${TAIL_N}1 | sed 's/^[ 	]*AC_CONFIG_AUX_DIR(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`"
974    if test ! -d "$_aux_dir" ; then
975	_aux_dir=.
976    else
977	$VERBOSE_ECHO "Detected auxillary directory: $_aux_dir"
978    fi
979
980    ################################
981    # detect a recursive configure #
982    ################################
983    CONFIG_SUBDIRS=""
984    _det_config_subdirs="`grep AC_CONFIG_SUBDIRS $CONFIGURE | grep -v '.*#.*AC_CONFIG_SUBDIRS' | sed 's/^[ 	]*AC_CONFIG_SUBDIRS(\(.*\)).*/\1/' | sed 's/.*\[\(.*\)\].*/\1/'`"
985    for dir in $_det_config_subdirs ; do
986	if test -d "`pwd`/$dir" ; then
987	    $VERBOSE_ECHO "Detected recursive configure directory: `pwd`/$dir"
988	    CONFIG_SUBDIRS="$CONFIG_SUBDIRS `pwd`/$dir"
989	fi
990    done
991
992    ###########################################################
993    # make sure certain required files exist for GNU projects #
994    ###########################################################
995    _marker_found=""
996    _marker_found_message_intro='Detected non-GNU marker "'
997    _marker_found_message_mid='" in '
998    for marker in foreign cygnus ; do
999	_marker_found_message=${_marker_found_message_intro}${marker}${_marker_found_message_mid}
1000	_marker_found="`grep 'AM_INIT_AUTOMAKE.*'${marker} $CONFIGURE`"
1001	if [ ! "x$_marker_found" = "x" ] ; then
1002	    $VERBOSE_ECHO "${_marker_found_message}`basename \"$CONFIGURE\"`"
1003	    break
1004	fi
1005	if test -f "`dirname \"$CONFIGURE\"/Makefile.am`" ; then
1006	    _marker_found="`grep 'AUTOMAKE_OPTIONS.*'${marker} Makefile.am`"
1007	    if [ ! "x$_marker_found" = "x" ] ; then
1008		$VERBOSE_ECHO "${_marker_found_message}Makefile.am"
1009		break
1010	    fi
1011	fi
1012    done
1013    if [ "x${_marker_found}" = "x" ] ; then
1014	_suggest_foreign=no
1015	for file in AUTHORS COPYING ChangeLog INSTALL NEWS README ; do
1016	    if [ ! -f $file ] ; then
1017		$VERBOSE_ECHO "Touching ${file} since it does not exist"
1018		_suggest_foreign=yes
1019		touch $file
1020	    fi
1021	done
1022
1023	if [ "x${_suggest_foreign}" = "xyes" ] ; then
1024	    $ECHO
1025	    $ECHO "Warning: Several files expected of projects that conform to the GNU"
1026	    $ECHO "coding standards were not found.  The files were automatically added"
1027	    $ECHO "for you since you do not have a 'foreign' declaration specified."
1028	    $ECHO
1029	    $ECHO "Considered adding 'foreign' to AM_INIT_AUTOMAKE in `basename \"$CONFIGURE\"`"
1030	    if test -f "`dirname \"$CONFIGURE\"/Makefile.am`" ; then
1031		$ECHO "or to AUTOMAKE_OPTIONS in your top-level Makefile.am file."
1032	    fi
1033	    $ECHO
1034	fi
1035    fi
1036
1037    ##################################################
1038    # make sure certain generated files do not exist #
1039    ##################################################
1040    for file in config.guess config.sub ltmain.sh ; do
1041	if test -f "${_aux_dir}/${file}" ; then
1042	    $VERBOSE_ECHO "mv -f \"${_aux_dir}/${file}\" \"${_aux_dir}/${file}.backup\""
1043	    mv -f "${_aux_dir}/${file}" "${_aux_dir}/${file}.backup"
1044	fi
1045    done
1046
1047    ############################
1048    # search alternate m4 dirs #
1049    ############################
1050    SEARCH_DIRS=""
1051    for dir in m4 ; do
1052	if [ -d $dir ] ; then
1053	    $VERBOSE_ECHO "Found extra aclocal search directory: $dir"
1054	    SEARCH_DIRS="$SEARCH_DIRS -I `pwd`/$dir"
1055	fi
1056    done
1057
1058    ######################################
1059    # remove any previous build products #
1060    ######################################
1061    if test -d autom4te.cache ; then
1062	$VERBOSE_ECHO "Found an autom4te.cache directory, deleting it"
1063	$VERBOSE_ECHO "rm -rf autom4te.cache"
1064	rm -rf autom4te.cache
1065    fi
1066# tcl/tk (and probably others) have a customized aclocal.m4, so can't delete it
1067#     if test -f aclocal.m4 ; then
1068# 	$VERBOSE_ECHO "Found an aclocal.m4 file, deleting it"
1069# 	$VERBOSE_ECHO "rm -f aclocal.m4"
1070# 	rm -f aclocal.m4
1071#     fi
1072
1073} # end of initialize()
1074
1075
1076##############
1077# initialize #
1078##############
1079
1080# stash path
1081START_PATH="`pwd`"
1082
1083# Before running autoreconf or manual steps, some prep detection work
1084# is necessary or useful.  Only needs to occur once per directory, but
1085# does need to traverse the entire subconfigure hierarchy to protect
1086# files from being clobbered even by autoreconf.
1087recursive_protect
1088
1089# start from where we started
1090cd "$START_PATH"
1091
1092# get ready to process
1093initialize
1094
1095
1096#########################################
1097# DOWNLOAD_GNULIB_CONFIG_GUESS FUNCTION #
1098#########################################
1099
1100# TODO - should make sure wget/curl exist and/or work before trying to
1101# use them.
1102
1103download_gnulib_config_guess () {
1104    # abuse gitweb to download gnulib's latest config.guess via HTTP
1105    config_guess_temp="config.guess.$$.download"
1106    ret=1
1107    for __cmd in wget curl fetch ; do
1108	$VERBOSE_ECHO "Checking for command ${__cmd}"
1109	${__cmd} --version > /dev/null 2>&1
1110	ret=$?
1111	if [ ! $ret = 0 ] ; then
1112	    continue
1113        fi
1114
1115	__cmd_version=`${__cmd} --version | head -n 1 | sed -e 's/^[^0-9]\+//' -e 's/ .*//'`
1116	$VERBOSE_ECHO "Found ${__cmd} ${__cmd_version}"
1117
1118	opts=""
1119	case ${__cmd} in
1120	    wget)
1121		opts="-O"
1122		;;
1123	    curl)
1124		opts="-o"
1125		;;
1126	    fetch)
1127		opts="-t 5 -f"
1128		;;
1129	esac
1130
1131	$VERBOSE_ECHO "Running $__cmd \"${CONFIG_GUESS_URL}\" $opts \"${config_guess_temp}\""
1132	eval "$__cmd \"${CONFIG_GUESS_URL}\" $opts \"${config_guess_temp}\"" > /dev/null 2>&1
1133	if [ $? = 0 ] ; then
1134	    mv -f "${config_guess_temp}" ${_aux_dir}/config.guess
1135	    ret=0
1136	    break
1137	fi
1138    done
1139
1140    if [ ! $ret = 0 ] ; then
1141	$ECHO "Warning: config.guess download failed from: $CONFIG_GUESS_URL"
1142	rm -f "${config_guess_temp}"
1143    fi
1144}
1145
1146
1147##############################
1148# LIBTOOLIZE_NEEDED FUNCTION #
1149##############################
1150libtoolize_needed () {
1151    ret=1 # means no, don't need libtoolize
1152    for feature in AC_PROG_LIBTOOL AM_PROG_LIBTOOL LT_INIT ; do
1153	$VERBOSE_ECHO "Searching for $feature in $CONFIGURE"
1154	found="`grep \"^$feature.*\" $CONFIGURE`"
1155	if [ ! "x$found" = "x" ] ; then
1156	    ret=0 # means yes, need to run libtoolize
1157	    break
1158	fi
1159    done
1160    return ${ret}
1161}
1162
1163
1164
1165############################################
1166# prepare build via autoreconf or manually #
1167############################################
1168reconfigure_manually=no
1169if [ "x$HAVE_AUTORECONF" = "xyes" ] ; then
1170    $ECHO
1171    $ECHO $ECHO_N "Automatically preparing build ... $ECHO_C"
1172
1173    $VERBOSE_ECHO "$AUTORECONF $SEARCH_DIRS $AUTORECONF_OPTIONS"
1174    autoreconf_output="`$AUTORECONF $SEARCH_DIRS $AUTORECONF_OPTIONS 2>&1`"
1175    ret=$?
1176    $VERBOSE_ECHO "$autoreconf_output"
1177
1178    if [ ! $ret = 0 ] ; then
1179	if [ "x$HAVE_ALT_LIBTOOLIZE" = "xyes" ] ; then
1180	    if [ ! "x`echo \"$autoreconf_output\" | grep libtoolize | grep \"No such file or directory\"`" = "x" ] ; then
1181		$ECHO
1182		$ECHO "Warning: autoreconf failed but due to what is usually a common libtool"
1183		$ECHO "misconfiguration issue.  This problem is encountered on systems that"
1184		$ECHO "have installed libtoolize under a different name without providing a"
1185		$ECHO "symbolic link or without setting the LIBTOOLIZE environment variable."
1186		$ECHO
1187		$ECHO "Restarting the preparation steps with LIBTOOLIZE set to $LIBTOOLIZE"
1188
1189		export LIBTOOLIZE
1190		RUN_RECURSIVE=no
1191		export RUN_RECURSIVE
1192		untrap_abnormal
1193
1194		$VERBOSE_ECHO sh $AUTOGEN_SH "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
1195		sh "$AUTOGEN_SH" "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
1196		exit $?
1197	    fi
1198	fi
1199
1200	$ECHO "Warning: $AUTORECONF failed"
1201
1202	if test -f ltmain.sh ; then
1203	    $ECHO "libtoolize being run by autoreconf is not creating ltmain.sh in the auxillary directory like it should"
1204	fi
1205
1206	$ECHO "Attempting to run the preparation steps individually"
1207	reconfigure_manually=yes
1208    else
1209	if [ "x$DOWNLOAD" = "xyes" ] ; then
1210	    if libtoolize_needed ; then
1211		download_gnulib_config_guess
1212	    fi
1213	fi
1214    fi
1215else
1216    reconfigure_manually=yes
1217fi
1218
1219
1220############################
1221# LIBTOOL_FAILURE FUNCTION #
1222############################
1223libtool_failure ( ) {
1224
1225    # libtool is rather error-prone in comparison to the other
1226    # autotools and this routine attempts to compensate for some
1227    # common failures.  the output after a libtoolize failure is
1228    # parsed for an error related to AC_PROG_LIBTOOL and if found, we
1229    # attempt to inject a project-provided libtool.m4 file.
1230
1231    _autoconf_output="$1"
1232
1233    if [ "x$RUN_RECURSIVE" = "xno" ] ; then
1234	# we already tried the libtool.m4, don't try again
1235	return 1
1236    fi
1237
1238    if test -f "$LIBTOOL_M4" ; then
1239	found_libtool="`$ECHO $_autoconf_output | grep AC_PROG_LIBTOOL`"
1240	if test ! "x$found_libtool" = "x" ; then
1241	    if test -f acinclude.m4 ; then
1242		rm -f acinclude.m4.$$.backup
1243		$VERBOSE_ECHO "cat acinclude.m4 > acinclude.m4.$$.backup"
1244		cat acinclude.m4 > acinclude.m4.$$.backup
1245	    fi
1246	    $VERBOSE_ECHO "cat \"$LIBTOOL_M4\" >> acinclude.m4"
1247	    chmod u+w acinclude.m4
1248	    cat "$LIBTOOL_M4" >> acinclude.m4
1249
1250	    # don't keep doing this
1251	    RUN_RECURSIVE=no
1252	    export RUN_RECURSIVE
1253	    untrap_abnormal
1254
1255	    $ECHO
1256	    $ECHO "Restarting the preparation steps with libtool macros in acinclude.m4"
1257	    $VERBOSE_ECHO sh $AUTOGEN_SH "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
1258	    sh "$AUTOGEN_SH" "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
1259	    exit $?
1260	fi
1261    fi
1262}
1263
1264
1265###########################
1266# MANUAL_AUTOGEN FUNCTION #
1267###########################
1268manual_autogen ( ) {
1269
1270    ##################################################
1271    # Manual preparation steps taken are as follows: #
1272    #   aclocal [-I m4]                              #
1273    #   libtoolize --automake -c -f                  #
1274    #   aclocal [-I m4]                              #
1275    #   autoconf -f                                  #
1276    #   autoheader                                   #
1277    #   automake -a -c -f                            #
1278    ##################################################
1279
1280    ###########
1281    # aclocal #
1282    ###########
1283    $VERBOSE_ECHO "$ACLOCAL $SEARCH_DIRS $ACLOCAL_OPTIONS"
1284    aclocal_output="`$ACLOCAL $SEARCH_DIRS $ACLOCAL_OPTIONS 2>&1`"
1285    ret=$?
1286    $VERBOSE_ECHO "$aclocal_output"
1287    if [ ! $ret = 0 ] ; then $ECHO "ERROR: $ACLOCAL failed" && exit 2 ; fi
1288
1289    ##############
1290    # libtoolize #
1291    ##############
1292    if libtoolize_needed ; then
1293	if [ "x$HAVE_LIBTOOLIZE" = "xyes" ] ; then
1294	    $VERBOSE_ECHO "$LIBTOOLIZE $LIBTOOLIZE_OPTIONS"
1295	    libtoolize_output="`$LIBTOOLIZE $LIBTOOLIZE_OPTIONS 2>&1`"
1296	    ret=$?
1297	    $VERBOSE_ECHO "$libtoolize_output"
1298
1299	    if [ ! $ret = 0 ] ; then $ECHO "ERROR: $LIBTOOLIZE failed" && exit 2 ; fi
1300	else
1301	    if [ "x$HAVE_ALT_LIBTOOLIZE" = "xyes" ] ; then
1302		$VERBOSE_ECHO "$LIBTOOLIZE $ALT_LIBTOOLIZE_OPTIONS"
1303		libtoolize_output="`$LIBTOOLIZE $ALT_LIBTOOLIZE_OPTIONS 2>&1`"
1304		ret=$?
1305		$VERBOSE_ECHO "$libtoolize_output"
1306
1307		if [ ! $ret = 0 ] ; then $ECHO "ERROR: $LIBTOOLIZE failed" && exit 2 ; fi
1308	    fi
1309	fi
1310
1311	###########
1312	# aclocal #
1313	###########
1314	# re-run again as instructed by libtoolize
1315	$VERBOSE_ECHO "$ACLOCAL $SEARCH_DIRS $ACLOCAL_OPTIONS"
1316	aclocal_output="`$ACLOCAL $SEARCH_DIRS $ACLOCAL_OPTIONS 2>&1`"
1317	ret=$?
1318	$VERBOSE_ECHO "$aclocal_output"
1319
1320	# libtoolize might put ltmain.sh in the wrong place
1321	if test -f ltmain.sh ; then
1322	    if test ! -f "${_aux_dir}/ltmain.sh" ; then
1323		$ECHO
1324		$ECHO "Warning:  $LIBTOOLIZE is creating ltmain.sh in the wrong directory"
1325		$ECHO
1326		$ECHO "Fortunately, the problem can be worked around by simply copying the"
1327		$ECHO "file to the appropriate location (${_aux_dir}/).  This has been done for you."
1328		$ECHO
1329		$VERBOSE_ECHO "cp -p ltmain.sh \"${_aux_dir}/ltmain.sh\""
1330		cp -p ltmain.sh "${_aux_dir}/ltmain.sh"
1331		$ECHO $ECHO_N "Continuing build preparation ... $ECHO_C"
1332	    fi
1333	fi # ltmain.sh
1334
1335	if [ "x$DOWNLOAD" = "xyes" ] ; then
1336	    download_gnulib_config_guess
1337	fi
1338    fi # libtoolize_needed
1339
1340    ############
1341    # autoconf #
1342    ############
1343    $VERBOSE_ECHO
1344    $VERBOSE_ECHO "$AUTOCONF $AUTOCONF_OPTIONS"
1345    autoconf_output="`$AUTOCONF $AUTOCONF_OPTIONS 2>&1`"
1346    ret=$?
1347    $VERBOSE_ECHO "$autoconf_output"
1348
1349    if [ ! $ret = 0 ] ; then
1350	# retry without the -f and check for usage of macros that are too new
1351	ac2_65_macros="AT_CHECK_EUNIT AC_PROG_OBJCXX AC_PROG_OBJCXXCPP"
1352	ac2_64_macros="AT_CHECK_UNQUOTED AT_FAIL_IF AT_SKIP_IF AC_ERLANG_SUBST_ERTS_VER"
1353	ac2_62_macros="AC_AUTOCONF_VERSION AC_OPENMP AC_PATH_PROGS_FEATURE_CHECK"
1354	ac2_60_macros="AC_C_FLEXIBLE_ARRAY_MEMBER AC_C_VARARRAYS"
1355	ac2_59_macros="AC_C_RESTRICT AC_INCLUDES_DEFAULT AC_LANG_ASSERT AC_LANG_WERROR AS_SET_CATFILE AC_PROG_SED AC_PROG_GREP AC_REQUIRE_AUX_FILE AC_CHECK_TARGET_TOOL AC_PATH_TARGET_TOOL AC_CHECK_TARGET_TOOLS AC_CHECK_ALIGNOF AC_PROG_OBJC AC_PROG_OBJCPP AC_ERLANG_SUBST_INSTALL_LIB_DIR AC_ERLANG_SUBST_INSTALL_LIB_SUBDIR AC_ERLANG_PATH_ERLC AC_ERLANG_NEED_ERLC AC_ERLANG_PATH_ERL AC_ERLANG_NEED_ERL AC_ERLANG_CHECK_LIB AC_ERLANG_SUBST_ROOT_DIR AC_ERLANG_SUBST_LIB_DIR AT_COPYRIGHT AS_BOURNE_COMPATIBLE AS_SHELL_SANITIZE AS_CASE AH_HEADER AC_USE_SYSTEM_EXTENSIONS AC_TYPE_INT8_T AC_TYPE_INT16_T AC_TYPE_INT32_T AC_TYPE_INT64_T AC_TYPE_INTMAX_T AC_TYPE_INTPTR_T AC_TYPE_LONG_LONG_INT AC_TYPE_SSIZE_T AC_TYPE_UINT8_T AC_TYPE_UINT16_T AC_TYPE_UINT32_T AC_TYPE_UINT64_T AC_TYPE_UINTMAX_T AC_TYPE_UINTPTR_T AC_TYPE_UNSIGNED_LONG_LONG_INT AC_TYPE_LONG_DOUBLE AC_TYPE_LONG_DOUBLE_WIDER AC_STRUCT_DIRENT_D_INO AC_STRUCT_DIRENT_D_TYPE AC_PROG_CC_C89 AC_PROG_CC_C99 AC_PRESERVE_HELP_ORDER AC_HEADER_ASSERT AC_FUNC_STRTOLD AC_C_TYPEOF AC_PROG_MKDIR_P AC_PROG_CXX_C_O"
1356	ac2_55_macros="AC_COMPILER_IFELSE AC_FUNC_MBRTOWC AC_HEADER_STDBOOL AC_LANG_CONFTEST AC_LANG_SOURCE AC_LANG_PROGRAM AC_LANG_CALL AC_LANG_FUNC_TRY_LINK AC_MSG_FAILURE AC_PREPROC_IFELSE"
1357	ac2_54_macros="AC_C_BACKSLASH_A AC_CONFIG_LIBOBJ_DIR AC_GNU_SOURCE AC_PROG_EGREP AC_PROG_FGREP AC_REPLACE_FNMATCH AC_FUNC_FNMATCH_GNU AC_FUNC_REALLOC AC_TYPE_MBSTATE_T"
1358
1359	macros_to_search=""
1360	ac_major="`echo ${AUTOCONF_VERSION}. | cut -d. -f1 | sed 's/[^0-9]//g'`"
1361	ac_minor="`echo ${AUTOCONF_VERSION}. | cut -d. -f2 | sed 's/[^0-9]//g'`"
1362
1363	if [ $ac_major -lt 2 ] ; then
1364	    macros_to_search="$ac2_65 $ac2_64 $ac2_62 $ac2_60 $ac2_59 $ac2_55 $ac2_54"
1365	else
1366	    if [ $ac_minor -lt 54 ] ; then
1367		macros_to_search="$ac2_65 $ac2_64 $ac2_62 $ac2_60 $ac2_59 $ac2_55 $ac2_54"
1368	    elif [ $ac_minor -lt 55 ] ; then
1369		macros_to_search="$ac2_65 $ac2_64 $ac2_62 $ac2_60 $ac2_59 $ac2_55"
1370	    elif [ $ac_minor -lt 59 ] ; then
1371		macros_to_search="$ac2_65 $ac2_64 $ac2_62 $ac2_60 $ac2_59"
1372	    elif [ $ac_minor -lt 60 ] ; then
1373		macros_to_search="$ac2_65 $ac2_64 $ac2_62 $ac2_60"
1374	    elif [ $ac_minor -lt 62 ] ; then
1375		macros_to_search="$ac2_65 $ac2_64 $ac2_62"
1376	    elif [ $ac_minor -lt 64 ] ; then
1377		macros_to_search="$ac2_65 $ac2_64"
1378	    elif [ $ac_minor -lt 65 ] ; then
1379		macros_to_search="$ac2_65"
1380	    fi
1381	fi
1382
1383	configure_ac_macros=__none__
1384	for feature in $macros_to_search ; do
1385	    $VERBOSE_ECHO "Searching for $feature in $CONFIGURE"
1386	    found="`grep \"^$feature.*\" $CONFIGURE`"
1387	    if [ ! "x$found" = "x" ] ; then
1388		if [ "x$configure_ac_macros" = "x__none__" ] ; then
1389		    configure_ac_macros="$feature"
1390		else
1391		    configure_ac_macros="$feature $configure_ac_macros"
1392		fi
1393	    fi
1394	done
1395	if [ ! "x$configure_ac_macros" = "x__none__" ] ; then
1396	    $ECHO
1397	    $ECHO "Warning:  Unsupported macros were found in $CONFIGURE"
1398	    $ECHO
1399	    $ECHO "The `basename \"$CONFIGURE\"` file was scanned in order to determine if any"
1400	    $ECHO "unsupported macros are used that exceed the minimum version"
1401	    $ECHO "settings specified within this file.  As such, the following macros"
1402	    $ECHO "should be removed from configure.ac or the version numbers in this"
1403	    $ECHO "file should be increased:"
1404	    $ECHO
1405	    $ECHO "$configure_ac_macros"
1406	    $ECHO
1407	    $ECHO $ECHO_N "Ignorantly continuing build preparation ... $ECHO_C"
1408	fi
1409
1410	###################
1411	# autoconf, retry #
1412	###################
1413	$VERBOSE_ECHO
1414	$VERBOSE_ECHO "$AUTOCONF"
1415	autoconf_output="`$AUTOCONF 2>&1`"
1416	ret=$?
1417	$VERBOSE_ECHO "$autoconf_output"
1418
1419	if [ ! $ret = 0 ] ; then
1420	    # test if libtool is busted
1421	    libtool_failure "$autoconf_output"
1422
1423	    # let the user know what went wrong
1424	    cat <<EOF
1425$autoconf_output
1426EOF
1427	    $ECHO "ERROR: $AUTOCONF failed"
1428	    exit 2
1429	else
1430	    # autoconf sans -f and possibly sans unsupported options succeed so warn verbosely
1431	    $ECHO
1432	    $ECHO "Warning: autoconf seems to have succeeded by removing the following options:"
1433	    $ECHO "	AUTOCONF_OPTIONS=\"$AUTOCONF_OPTIONS\""
1434	    $ECHO
1435	    $ECHO "Removing those options should not be necessary and indicate some other"
1436	    $ECHO "problem with the build system.  The build preparation is highly suspect"
1437	    $ECHO "and may result in configuration or compilation errors.  Consider"
1438	    if [ "x$VERBOSE_ECHO" = "x:" ] ; then
1439		$ECHO "rerunning the build preparation with verbose output enabled."
1440		$ECHO "	$AUTOGEN_SH --verbose"
1441	    else
1442		$ECHO "reviewing the minimum GNU Autotools version settings contained in"
1443		$ECHO "this script along with the macros being used in your `basename \"$CONFIGURE\"` file."
1444	    fi
1445	    $ECHO
1446	    $ECHO $ECHO_N "Continuing build preparation ... $ECHO_C"
1447	fi # autoconf ret = 0
1448    fi # autoconf ret = 0
1449
1450    ##############
1451    # autoheader #
1452    ##############
1453    need_autoheader=no
1454    for feature in AM_CONFIG_HEADER AC_CONFIG_HEADER ; do
1455	$VERBOSE_ECHO "Searching for $feature in $CONFIGURE"
1456	found="`grep \"^$feature.*\" $CONFIGURE`"
1457	if [ ! "x$found" = "x" ] ; then
1458	    need_autoheader=yes
1459	    break
1460	fi
1461    done
1462    if [ "x$need_autoheader" = "xyes" ] ; then
1463	$VERBOSE_ECHO "$AUTOHEADER $AUTOHEADER_OPTIONS"
1464	autoheader_output="`$AUTOHEADER $AUTOHEADER_OPTIONS 2>&1`"
1465	ret=$?
1466	$VERBOSE_ECHO "$autoheader_output"
1467	if [ ! $ret = 0 ] ; then $ECHO "ERROR: $AUTOHEADER failed" && exit 2 ; fi
1468    fi # need_autoheader
1469
1470    ############
1471    # automake #
1472    ############
1473    need_automake=no
1474    for feature in AM_INIT_AUTOMAKE ; do
1475	$VERBOSE_ECHO "Searching for $feature in $CONFIGURE"
1476	found="`grep \"^$feature.*\" $CONFIGURE`"
1477	if [ ! "x$found" = "x" ] ; then
1478	    need_automake=yes
1479	    break
1480	fi
1481    done
1482
1483    if [ "x$need_automake" = "xyes" ] ; then
1484	$VERBOSE_ECHO "$AUTOMAKE $AUTOMAKE_OPTIONS"
1485	automake_output="`$AUTOMAKE $AUTOMAKE_OPTIONS 2>&1`"
1486	ret=$?
1487	$VERBOSE_ECHO "$automake_output"
1488
1489	if [ ! $ret = 0 ] ; then
1490
1491	    ###################
1492	    # automake, retry #
1493	    ###################
1494	    $VERBOSE_ECHO
1495	    $VERBOSE_ECHO "$AUTOMAKE $ALT_AUTOMAKE_OPTIONS"
1496	    # retry without the -f
1497	    automake_output="`$AUTOMAKE $ALT_AUTOMAKE_OPTIONS 2>&1`"
1498	    ret=$?
1499	    $VERBOSE_ECHO "$automake_output"
1500
1501	    if [ ! $ret = 0 ] ; then
1502	 	# test if libtool is busted
1503		libtool_failure "$automake_output"
1504
1505		# let the user know what went wrong
1506		cat <<EOF
1507$automake_output
1508EOF
1509		$ECHO "ERROR: $AUTOMAKE failed"
1510		exit 2
1511	    fi # automake retry
1512	fi # automake ret = 0
1513    fi # need_automake
1514} # end of manual_autogen
1515
1516
1517#####################################
1518# RECURSIVE_MANUAL_AUTOGEN FUNCTION #
1519#####################################
1520recursive_manual_autogen ( ) {
1521
1522    # run the build preparation steps manually for this directory
1523    manual_autogen
1524
1525    # for projects using recursive configure, run the build
1526    # preparation steps for the subdirectories.
1527    if [ ! "x$CONFIG_SUBDIRS" = "x" ] ; then
1528	$VERBOSE_ECHO "Recursively configuring the following directories:"
1529	$VERBOSE_ECHO "  $CONFIG_SUBDIRS"
1530	for dir in $CONFIG_SUBDIRS ; do
1531	    $VERBOSE_ECHO "Processing recursive configure in $dir"
1532	    cd "$START_PATH"
1533	    cd "$dir"
1534
1535	    # new directory, prepare
1536	    initialize
1537
1538	    # run manual steps for the subdir and any others below
1539	    recursive_manual_autogen
1540	done
1541    fi
1542}
1543
1544
1545################################
1546# run manual preparation steps #
1547################################
1548if [ "x$reconfigure_manually" = "xyes" ] ; then
1549    $ECHO
1550    $ECHO $ECHO_N "Preparing build ... $ECHO_C"
1551
1552    recursive_manual_autogen
1553fi
1554
1555
1556#########################
1557# restore and summarize #
1558#########################
1559cd "$START_PATH"
1560
1561# restore COPYING and INSTALL from backup if necessary
1562recursive_restore
1563
1564# make sure we end up with a configure script
1565config_ac="`locate_configure_template`"
1566config="`echo $config_ac | sed 's/\.ac$//' | sed 's/\.in$//'`"
1567if [ "x$config" = "x" ] ; then
1568    $VERBOSE_ECHO "Could not locate the configure template (from `pwd`)"
1569fi
1570
1571# summarize
1572$ECHO "done"
1573$ECHO
1574if test "x$config" = "x" -o ! -f "$config" ; then
1575    $ECHO "WARNING: The $PROJECT build system should now be prepared but there"
1576    $ECHO "does not seem to be a resulting configure file.  This is unexpected"
1577    $ECHO "and likely the result of an error.  You should run $NAME_OF_AUTOGEN"
1578    $ECHO "with the --verbose option to get more details on a potential"
1579    $ECHO "misconfiguration."
1580else
1581    $ECHO "The $PROJECT build system is now prepared.  To build here, run:"
1582    $ECHO "  $config"
1583    $ECHO "  make"
1584fi
1585
1586# finish up where we started
1587cd "$START_PATH"
1588
1589# Local Variables:
1590# mode: sh
1591# tab-width: 8
1592# sh-basic-offset: 4
1593# sh-indentation: 4
1594# indent-tabs-mode: t
1595# End:
1596# ex: shiftwidth=4 tabstop=8
1597