dnl Process this file with autoconf to produce a configure script.
AC_INIT(cxref-cpp,1/6)

dnl Specify the autoconf file.
AC_CONFIG_HEADER(autoconfig.h)

dnl Check if we are forced to use the cxref-cpp
WITH_CXREF_CPP=test
AC_ARG_WITH(cxref-cpp,
[  --with-cxref-cpp       use the supplied cpp instead of trying to use gcc],
[if test "$withval" = "yes"; then WITH_CXREF_CPP=yes; fi
 if test "$withval" = "no";  then WITH_CXREF_CPP=no;  fi])

dnl Checks for programs.
AC_PROG_CC
AC_PROG_YACC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_EGREP

dnl Checks for libraries.

dnl Checks for header files.
AC_HEADER_STDC
AC_HEADER_TIME
AC_CHECK_HEADERS(fcntl.h locale.h stdlib.h string.h strings.h time.h unistd.h)
AC_CHECK_HEADERS(sys/param.h sys/stat.h sys/time.h sys/wait.h)

dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
AC_STRUCT_TM
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long)
AC_CHECK_SIZEOF(long long)

dnl Checks for library functions.
AC_CHECK_FUNCS(bcmp bcopy bzero index rindex strerror strsignal)

dnl Check the gcc version

if test ! "x$GCC" = "x" ; then

   AC_MSG_CHECKING([if installed gcc is new enough to use instead of cxref-cpp])

   AC_TRY_RUN([
   int main()
   {
   #if defined(__GNUC__) && ( ( __GNUC__==2 && __GNUC_MINOR__>=8 ) || __GNUC__>=3 )
    exit(0);
   #else
    exit(1);
   #endif
   }
   ],
   [newGCC=yes])

   if test "$WITH_CXREF_CPP" = "no" ; then

      if test "x$newGCC" = "x" ; then
         AC_MSG_RESULT([no])
         AC_MSG_ERROR([the '--without-cxref-cpp' option was used but gcc is not suitable])
      else
         AC_MSG_RESULT([yes, ignoring further checks due to '--without-cxref-cpp' option])
      fi

   elif test "x$newGCC" = "x" || test "$WITH_CXREF_CPP" = "yes" ; then

      if test "x$newGCC" = "x" ; then
         AC_MSG_RESULT([no, using cxref-cpp])
      else
         AC_MSG_RESULT([yes, but '--with-cxref-cpp' option overrides this])
      fi

      CXREF_CPP=cxref-cpp

   else

      AC_MSG_RESULT(yes)

      AC_MSG_CHECKING([if installed gcc preserves comments after define statements])

      echo "#define foo bar /* FIND-ME */" | $CC -E -C - > ./conftest.gcc

      if $EGREP -e FIND-ME ./conftest.gcc > /dev/null 2>&1 ; then
         AC_MSG_RESULT(yes)
      else
         AC_MSG_RESULT([no, using cxref-cpp])

         CXREF_CPP=cxref-cpp
      fi

      rm ./conftest.gcc

      AC_MSG_CHECKING([if installed gcc preserves comments after include statements])

      touch conftest.h

      echo "#include \"conftest.h\" /* FIND-ME */" | $CC -E -C - > ./conftest.gcc

      if $EGREP -e FIND-ME ./conftest.gcc > /dev/null 2>&1 ; then
         AC_MSG_RESULT(yes)
      else
         AC_MSG_RESULT([no, using cxref-cpp])

         CXREF_CPP=cxref-cpp
      fi

      rm ./conftest.h ./conftest.gcc

   fi

else

   AC_MSG_WARN(-----------------------------------------------------)
   AC_MSG_WARN(The C compiler is not gcc, you may have problems!    )
   AC_MSG_WARN(read the file cpp/README and cpp/cxref-cpp-configure.)
   AC_MSG_WARN(-----------------------------------------------------)

   CXREF_CPP=cxref-cpp

fi

AC_SUBST(CXREF_CPP)
AC_SUBST(GCC)

dnl Create the output files.
AC_OUTPUT(Makefile)
AC_OUTPUT(cxref-cpp-configure)

chmod 755 cxref-cpp-configure
