#! /bin/sh

outfile=settings.make
verbose=0

#
# This subroutine prints standard usage message
#
usage()
{
    exit_code=$1
    cat <<EOF
Usage: $0 [-v] </absolute/path/to/babel-config> | --help

Generates a small makefile fragment, called "settings.make"
suitable for inclusion into Makefiles that build Babelized libraries.
Instead of probing the system (like autoconf would), this script
simply gets the information from the babel-config script that 
was generated with the normal Babel distribution.
The absolute path to the babel-config script is the only requirement.
EOF
    exit $exit_code
}

config_var()
{
    varname=$1
    shift
    value="$@"
    if test $verbose -ne 0; then
	echo $varname = $value
    fi
    echo $varname="$value" >> $outfile
}

# if wrong number of arguments, it is an error
if test $# -eq 0; then
    usage 1
fi

if test $1 = "-v"; then
    verbose=1
    shift
fi

if test $# -ne 1; then
    usage 1
fi

# allow various types of help flags (not an error)
if test \( "$1" = "--help" \) -o \( "$1" = "-help" \) -o \( "$1" = "-h" \) ; then
    usage 0
fi

# make sure the argument is an executable
if test -f  "$1"; then
    bc="$1"
else
    echo "$0: error: '$1' is not an executable file" >&2
    exit 1
fi 

result=`$bc --query-var=PACKAGE 2>/dev/null`
if test "$result" != "babel" ; then
    echo "$0: error: '$1' does not act like a valid babel-config file" >&2
    echo "           ('--query-var=PACKAGE' does not return 'babel')" >&2
    exit 1
fi

cat <<EOF > $outfile
# Generated by mini-configure

#
# All other settings are determined based where this
# babel-config script is
#

EOF
config_var BABEL_CONFIG "$bc"

cat <<EOF > $outfile

###############################################################3
# 
# There should be no reason to edit anything below this line.
# Only settings from an installed Babel distro are carried over
# in this file.
#
EOF
TOPDIR=`pwd`
config_var TOPDIR "$TOPDIR"
echo >> $outfile

config_var BABEL_VERSION `$bc --version`
config_var BABEL_PREFIX `$bc --prefix`
config_var BABEL_EPREFIX `$bc --exec-prefix`
config_var BABEL_INCLDIR `$bc --includedir`
config_var BABEL_BINDIR `$bc --bindir`
config_var BABEL_LIBDIR `$bc --libdir`
config_var BABEL `$bc --bindir`/babel
config_var BABEL_LIBTOOL `$bc --bindir`/babel-libtool

cat <<EOF >> $outfile

# These are the kinds of things that are determined
# at configure time.  Again, babel-config is helpful, 
# but it is not the final word.  Babel uses libtool
# which does some argument processing of its own.
# Therefore these args are pushed into babel-libool.
EOF

config_var CPP gcc -E --traditional-cpp -P -x c
config_var INCLUDES `$bc --includes`
echo >> $outfile

config_var CC `$bc --query-var=CC`
config_var CFLAGS `$bc --flags-c`
config_var CLIBS `$bc --libs-c`
echo >> $outfile

config_var CXX `$bc --query-var=CXX`
config_var CXXFLAGS `$bc --flags-c++`
config_var CXXLIBS `$bc --libs-c++`
echo >> $outfile

config_var FC `$bc --query-var=FC`
config_var FCFLAGS `$bc --flags-f90`
config_var FCLIBS `$bc --libs-f90`
echo >> $outfile

config_var PYTHON `$bc --which-var=PYTHON`
PYTHONINC=`$bc --query-var=PYTHONINC`
PYTHON_SUBDIR=`basename $PYTHONINC`
BABEL_INCLDIR=`$bc --includedir`
BABEL_LIBDIR=`$bc --libdir`
config_var PYTHON_SUBDIR $PYTHON_SUBDIR
config_var BABEL_PYEXTENSION_INCLDIR ${BABEL_INCLDIR}/${PYTHON_SUBDIR}/babel
config_var BABEL_PYEXTENSION_LIBDIR ${BABEL_LIBDIR}/${PYTHON_SUBDIR}/site-packages
echo >> $outfile

cat <<EOF >> $outfile
# suffix rules
.SUFFIXES:
.SUFFIXES: .cc .c .F90 .lo .o

.cc.lo:
	\$(BABEL_LIBTOOL) --mode=compile \$(CXX) -c -o \$@ \$(CPPFLAGS) \$(CXXFLAGS) \$(INCLUDES) \$< 

.c.lo:
	\$(BABEL_LIBTOOL) --mode=compile \$(CC) -c -o \$@ \$(CPPFLAGS) \$(CFLAGS) \$(INCLUDES) \$< 

.c.o:
	\$(CC) -c -o \$@ \$(CPPFLAGS) \$(CFLAGS) \$(INCLUDES) \$< 

.F90.o: 
	\$(CPP) \$< \$(INCLUDES) -o \$(@:.o=.f90) \$<
	\$(F90) \$(F90FLAGS) \$(INCLUDES) -c -o \$@ \$(@:.o=.f90)
	rm -f \$(@:.o=.f90)

EOF

