#! /bin/sh -e
# $Id: setup,v 1.24 2004/07/31 17:39:53 dm Exp $
#
# This script builds the files necessary to configure this package for
# development.  Once you run this script, you will need GNU make,
# autoconf, and automake to build the software.  You must run "make
# dist" if you the wish to create a source tree that can be built
# without these tools.
#
# You should generally only need to run this script if you just
# checked sfs out of a CVS repository, or else somehow badly mangled
# the Makefile.

opt_c=
opt_f=

for arg in "$@"; do
case "$arg" in
    -cf|-fc)
	    opt_c='--copy'
	    opt_f='--force'
	    ;;
    -c)
	    opt_c='--copy'
	    ;;
    -f)
	    opt_f='--force'
	    rm -f depcomp install-sh missing mkinstalldirs
	    rm -f libtool ltconfig ltmain.sh
	    rm -f config.guess config.sub 
	    ;;
    *)
	    echo setup: unknown option "$arg"
	    ;;
esac
done

if test -z "${AUTOCONF+set}"; then
	AUTOCONF=autoconf
	export AUTOCONF
fi
if test -z "${AUTOCONF_VERSION+set}"; then
    for acv in 2.59 2.58 2.57 2.54 2.52 2.13; do
	for dir in /usr/local/bin /usr/bin; do
	if test -x ${dir}/autoconf-${acv}; then
	    #AUTOCONF=${dir}/autoconf-${acv}
	    #export AUTOCONF
	    AUTOCONF_VERSION=$acv
	    export AUTOCONF_VERSION
	    echo "Using AUTOCONF_VERSION $AUTOCONF_VERSION"
	    break 2
	fi
	done
    done
else
    acv="$AUTOCONF_VERSION"
    for dir in /usr/local/bin /usr/bin; do
	if test -x ${dir}/autoconf-${acv}; then
	    AUTOCONF=${dir}/autoconf-${acv}
	    export AUTOCONF
	    break
	fi
    done
fi

if test -z "${SETUP_TOP_PID}"; then
    SETUP_TOP_PID=$$
    export SETUP_TOP_PID
fi

if test ! -f configure.in; then
	echo Must run this from source directory >&2
	exit 1
fi

M4=gm4
$M4 --version < /dev/null 2>&1 | grep GNU >/dev/null 2>&1 || M4=gnum4
$M4 --version < /dev/null 2>&1 | grep GNU >/dev/null 2>&1 || M4=m4
$M4 --version < /dev/null 2>&1 | grep GNU >/dev/null 2>&1 \
    || (echo Cannot locate GNU m4 >&2; exit 1)

if test "${LIBTOOLIZE+set}" != "set"; then
    LIBTOOLIZE=libtoolize
    if test ! -x /usr/local/bin/libtoolize \
		-a ! -x /usr/bin/libtoolize \
		-a -x /usr/bin/glibtoolize; then
	LIBTOOLIZE=glibtoolize
    fi
fi

for file in acinclude.m4; do
    if test ! -f $file -a -f ../$file; then
	echo "+ ln -f -s ../$file $file"
	ln -f -s ../$file $file 
    fi
done

for file in Makefile.am.m4 */Makefile.am.m4; do
    if test -f $file; then
	out=`echo $file | sed -e 's/\.m4$//'`
	echo "+ $M4 $file > $out"
	rm -f $out~
	$M4 $file > $out~
	mv -f $out~ $out
    fi
done

for dir in *; do
    if test -r ${dir}/setup; then
	echo "+ ${dir}/setup $*";
	(cd ${dir} && chmod +x ./setup && ./setup "$@")
    fi
done

if test -z "${AM_ARGS:+set}"; then
    AM_ARGS="--add-missing $opt_c $opt_f"
fi
if test -z "${LTI_ARGS:+set}"; then
    LTI_ARGS="$opt_c $opt_f"
fi

set -x
chmod +x setup
$LIBTOOLIZE $LTI_ARGS
aclocal
autoheader
automake $AM_ARGS
$AUTOCONF
set +x

if test -f config.status; then
    set -x
    sh config.status
fi
set +x

if test "$SETUP_TOP_PID" = "$$"; then
    echo ""
    echo "	    *** * * * * * * * * * * * * * * * * ***"
    echo "	    ***         setup succeeded         ***"
    echo "	    *** * * * * * * * * * * * * * * * * ***"
    echo ""
fi
