#!/bin/sh
#$Header: /home/amb/cxref/cpp/RCS/config-cpp 1.3 1996/11/16 16:03:01 amb Exp $
#
# C Cross Referencing & Documentation tool. Version 1.3
#
# Automatic cxref-cpp Configuration program.
#
# Written by Andrew M. Bishop
#
# This file Copyright 1995,96 Andrew M. Bishop
# It may be distributed under the GNU Public License, version 2, or
# any higher version.  See section COPYING of the GNU Public license
# for conditions under which this file may be redistributed.
#

# Create the Makefile.cpp header.

cat > Makefile.cpp << End-Of-Header
#
# C Cross Referencing & Documentation tool. Version 1.3
#
# CPP Configuration Makefile.
#
# Generated by config-cpp
#
# This file Copyright 1995,96 Andrew M. Bishop
# It may be distributed under the GNU Public License, version 2, or
# any higher version.  See section COPYING of the GNU Public license
# for conditions under which this file may be redistributed.
#
End-Of-Header


# Check that the C compiler is gcc

ok_gcc=yes

[ $CC ] || CC=gcc

echo Checking C compiler CC = $CC for compatability

if hash $CC 2> /dev/null ; then
    $CC -E -v - < /dev/null 2> /tmp/config-cpp.$$ > /dev/null
    ident=`egrep '^GNU CPP' /tmp/config-cpp.$$`

    if [ "$ident" ]; then
        echo '        ' Yes ... $CC looks like GNU C \'$ident\'
    else
        echo '        ' No ... $CC does not look like GNU C
        ok_gcc=no
    fi
else
    echo '        ' Error ... Cannot find command $CC
    ok_gcc=no
fi


# Work out the gcc predefined definitions to compile in.

echo Working out C compiler built in predefines

cat >> Makefile.cpp << End-Of-Predefines

# The predefined macros and answers to questions
# e.g PREDEFINES = -D__GNUC__=2 -D__GNUC_MINOR__=6 -Dunix -D__unix__ -D__unix -Asystem(unix)

End-Of-Predefines

if [ $ok_gcc = yes ]; then
    echo PREDEFINES = `egrep -e -lang-c /tmp/config-cpp.$$ | tr ' ' '\012' | egrep -e '^-[AD]' | tr '\012' ' '` >> Makefile.cpp
else
    echo PREDEFINES = '' >> Makefile.cpp
fi


# Work out the gcc predefined include directories to compile in.

echo Working out C compiler built in include paths

cat >> Makefile.cpp << End-Of-Search-Includes

# The predefined include search directories

End-Of-Search-Includes

if [ $ok_gcc = yes ]; then
    awk '/include <.+> search/,/End of search/ {if(i>0 && i<4)printf("INCLUDE_DIR%d = %s\n",i,$1);i++}' /tmp/config-cpp.$$ >> Makefile.cpp
else
    awk 'BEGIN{for(i=1;i<4;i++)printf("INCLUDE_DIR%d = \n",i);i++}' /dev/null >> Makefile.cpp
fi


# A check of whether we need SVR4 string functions.

echo Checking if the system has the bzero function

svr4_strings=no

cat > test-svr4.c << End-Of-SVR4-String-Test-Program

int main()
{
 bzero(0,0);
}

End-Of-SVR4-String-Test-Program

$CC test-svr4.c -o test-svr4 2> /dev/null || svr4_strings=yes

rm -f test-svr4.c test-svr4

cat >> Makefile.cpp << End-Of-SVR4-Strings

# Set if replacements for bzero, bcmp, bcopy and index, rindex are needed

End-Of-SVR4-Strings

if [ $svr4_strings = yes ]; then
    echo '        ' No ... the bzero function is not present
    echo SVR4_STRINGS = 1 >> Makefile.cpp
else
    echo '        ' Yes ... the bzero function is present
    echo SVR4_STRINGS = 0 >> Makefile.cpp
fi

# A flag to indicate the the configuration status.

cat >> Makefile.cpp << End-Of-Configuration-Status

# The status of the configuration
# Set CONFIG_STATUS to a null value if cxref-cpp is to be built.

End-Of-Configuration-Status

if [ $ok_gcc = yes ]; then
    echo CONFIG_STATUS = >> Makefile.cpp
else
    echo CONFIG_STATUS = \'Configuration Failed\' >> Makefile.cpp
fi


# Tidy up at the end.

if [ $ok_gcc = yes ]; then
    rm /tmp/config-cpp.$$
else
    echo ''
    echo You will need to configure cxref-cpp by hand if you want to use it.
    echo Read cpp/README file for help and edit cpp/Makefile.cpp yourself.
    echo Meanwhile the build of cxref can continue without cxref-cpp.
    exit 1
fi
