dnl  Give the version number as the next/target release version.
dnl  Ie, do not include beta/rc numbers.
dnl  See README-developer.md for notes on marking releases.
dnl  The release date is generated from the date of the repository snapshot.
AC_INIT([unity], [1.1], [norman.gray@glasgow.ac.uk])

AC_PREFIX_DEFAULT([/usr/local])

AC_SUBST(LIBRARY_HOMEPAGE, [https://purl.org/nxg/dist/unity])
AC_SUBST(LIBRARY_REPOSITORY, [https://heptapod.host/nxg/unity])
AC_SUBST(LIBRARY_BUGPARADE, [https://heptapod.host/nxg/unity])
AC_SUBST(LIBRARY_DISTURL, [https://heptapod.host/nxg/unity/-/packages/])

dnl  This integer matches the package-version at the top,
dnl  and will be the same for (eg) all betas.
PACKAGE_VERSION_INTEGER=[`echo ${PACKAGE_VERSION}-0 | awk -F'[^0-9]*' '{printf "%d", $1*1000000 + $2*1000 + $3}'`]
AC_SUBST(PACKAGE_VERSION_INTEGER)
AC_DEFINE_UNQUOTED([PACKAGE_VERSION_INTEGER],
                   [$PACKAGE_VERSION_INTEGER],
                   [Version number as an integer: major*1e6+minor*1e3+release])

dnl  Ensure that all the parts of the tree list the same set of parsers
PARSERLIST="fits ogip cds vounits"
AC_SUBST(PARSERLIST)

AC_PROG_CC([cc clang gcc])
AC_PROG_INSTALL
AC_PROG_MKDIR_P  dnl don't want the fuss of install.sh at present

AC_CONFIG_HEADERS(src/c/config.h)

dnl  The calls to AC_PATH_PROG set the corresponding variables to full
dnl  paths.  If the tool isn't present, then we either (i) give a default
dnl  in the third argument, (ii) give no third argument, so that a
dnl  Makefile can detect that the tools is missing, or (iii) give a
dnl  flag third argument, for certain crucial tools, to let us produce
dnl  a 'this isn't going to go well' warning at the end of the script.

dnl  Yes, we should use Libtool....
AC_PATH_PROG(AR, ar, ar)
AC_PROG_RANLIB
dnl  DOXYGEN and DOT will be "" if not present
dnl  (and this is how their absence is detected in the Makefiles)
AC_PATH_PROG(DOXYGEN, doxygen)
AC_PATH_PROG(DOT, dot)
# Doxygen requires that DOT be the directory where the dot executable lives,
# not the path to the executable itself.
if test -n "$DOT"; then
   DOT=`dirname $DOT`
fi

dnl  java parser tools
AC_PATH_PROG(JAVA, java, NOJAVA)
AC_PATH_PROG(JAVAC, javac, NOJAVAC)
AC_PATH_PROG(JAR, jar, NOJAR)
AC_PATH_PROG(JAVADOC, javadoc, NOJAVADOC)

AC_PATH_PROG(BYACCJ, byaccj, NOBYACCJ)
dnl if test $BYACCJ != NOBYACCJ; then
dnl     YACC=$BYACCJ
dnl fi

dnl  C parser tools
AC_PROG_LEX(noyywrap)
dnl  The following is like AC_PROG_YACC, except that we default to
dnl  'NOYACC' instead of 'yacc'; this is so that we can substitute
dnl  BYACCJ in this case.  This means that we don't rely on bison
dnl  being present.
AC_CHECK_PROGS(YACC, ['bison -y' byacc yacc], NOYACC)
if test "$YACC" = NOYACC -a "$BYACCJ" != NOBYACCJ; then
   YACC=$BYACCJ
fi

AC_PATH_PROG(TAR, tar, tar)
AC_PATH_PROG(ZIP, zip, zip)


AC_SUBST(LIBCLASSPATH)
AC_SUBST(HAVE_JUNIT)
dnl  Use the presence of src/README.hacking (managed by ./Makefile.in)
dnl  to determine whether we're in a checkout or distribution tree.
dnl  If the file is present, we're in a distribution tree, and won't insist on junit.
AC_SUBST(IN_SOURCE_TREE)
AC_MSG_CHECKING([whether we're in a checkout or a distribution])
if test -f src/README.hacking; then
   IN_SOURCE_TREE=false
   AC_MSG_RESULT([distribution])
   dnl  We now check to see whether JUnit is in the classpath.
   dnl  This is a pretty basic test of this -- there are more
   dnl  sophisticated ways of doing this: see
   dnl  http://git.savannah.gnu.org/cgit/autoconf-archive.git/tree/m4/ax_prog_java.m4
   dnl  or http://ac-archive.sourceforge.net/ac-archive/ac_check_class.html
   cat >ConfTest.java <<EOD
   import org.junit.Test;
   public class ConfTest { @Test public void testTest() {}}
EOD
   AC_MSG_CHECKING([whether JUnit is present in CLASSPATH])
   if $JAVAC -cp $CLASSPATH ConfTest.java >/dev/null 2>&1; then
      LIBCLASSPATH=$CLASSPATH
      HAVE_JUNIT=:
   else
      HAVE_JUNIT=false
      LIBCLASSPATH=
   fi
   if $HAVE_JUNIT; then AC_MSG_RESULT(yes); else AC_MSG_RESULT(no); fi
   rm -f ConfTest.*
else
   IN_SOURCE_TREE=:
   AC_MSG_RESULT([checkout])
   dnl  Check to see if the classpath has right-looking jars in it
   dnl  (again, there's probably a more robust way of doing this)
   AC_MSG_CHECKING([whether library classpath looks sane])
   LIBCLASSPATH=`echo $PWD/lib/*.jar | sed s,\ ,:,g`
   classpathok=:
   expr "$LIBCLASSPATH" : '.*/jflex' >/dev/null || classpathok=false
   expr "$LIBCLASSPATH" : '.*/rdf4j' >/dev/null || classpathok=false
   expr "$LIBCLASSPATH" : '.*/junit' >/dev/null || classpathok=false
   if $classpathok; then
      AC_MSG_RESULT(yes)
   else
      AC_MSG_RESULT(no)
      echo "Library classpath seems to have missing jars: expected to see jflex, rdf4j and junit"
      echo '  ("mvn dependency:copy-dependencies" will fetch them; then run configure again)'
      echo "  Classpath: $LIBCLASSPATH"
   fi
   HAVE_JUNIT=:
fi

if $IN_SOURCE_TREE; then
   missing_tools=false
   for tool in JAVA JAVAC JAR JAVADOC YACC; do
       if eval test -n "$tool" -a \"\$$tool\" = NO$tool; then
          AC_MSG_WARN([can't find $tool])
          missing_tools=:
       fi
   done
   dnl  It would be good to check the presence of lex, but the
   dnl  AC_PROG_LEX seems to expand to ':' if this is missing, though the
   dnl  autoconf docs document it as expanding to 'lex'.  The
   dnl  inconsistency worries me, but I suspect there's little we can
   dnl  really do to help the user at this point.
   if $missing_tools; then
      AC_MSG_WARN([missing tools; you probably won't be able to build this library])
   fi
fi

AC_MSG_CHECKING([how to format stat .st_size])
saved_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -Wall -Werror"
have_st_size_printf=false
for spec in '"%d"' '"%zd"' '"%ld"' '"%lld"'; do
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#include <sys/stat.h>
#include <stdio.h>
void f(void) { extern struct stat S; printf($spec, S.st_size);}]])],
                   have_st_size_printf=:,
                   ST_SIZE_PRINTF=NO)
    if "$have_st_size_printf"; then
        AC_DEFINE_UNQUOTED(ST_SIZE_PRINTF, $spec, [How do we format stat st_size])
        AC_MSG_RESULT([$spec])
        break
    fi
done
if ! $have_st_size_printf; then
    AC_MSG_RESULT([don't know! set it to %zd and hope for the best])
    AC_DEFINE(ST_SIZE_PRINTF, "%zd", [How do we format stat st_size])
fi
CFLAGS="$saved_CFLAGS"

AC_CONFIG_FILES([Makefile
                 src/c/Makefile
                 src/c/test/Makefile
                 src/java/Makefile
                 src/java/test/Makefile])
dnl  The following files aren't distributed, and so appear only in the checked out source tree
if $IN_SOURCE_TREE; then
  AC_CONFIG_FILES([pom.xml
                   src/grammar/Makefile
                   src/buildtools/Makefile])
fi

AC_OUTPUT
