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