AC_INIT(unity, 1.0, norman@astro.gla.ac.uk)

dnl  See README.developer for the procedure for making a release
RELEASE_DATE='2014 May 13'
dnl RELEASE_DATE='2013 December 24'

AC_SUBST(RELEASE_DATE)
AC_DEFINE_UNQUOTED(RELEASE_DATE, ["$RELEASE_DATE"], [Date the package was released])

AC_PREFIX_DEFAULT([/usr/local])

AC_SUBST(LIBRARY_HOMEPAGE, [http://www.astro.gla.ac.uk/users/norman/ivoa/unity/])

PACKAGE_VERSION_INTEGER=[`echo ${PACKAGE_VERSION}-0 | awk -F'[^0-9]*' '{printf "%d 1000000* %d 1000* %d++p", $1,$2,$3}' | dc`]
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
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(BYACCJ, byaccj, byaccj)

dnl  C parser tools
AC_PROG_LEX
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 -n "$BYACCJ"; 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" : '.*/mrg' >/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, mrg 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
   if test "$JAVA" = NOJAVA; then
      AC_MSG_WARN([can't find java])
      missing_tools=:
   fi
   if test "$JAVAC" = NOJAVAC; then
      AC_MSG_WARN([can't find javac])
      missing_tools=:
   fi
   if test "$YACC" = NOYACC; then
      AC_MSG_WARN([can't find any yacc-variant])
      missing_tools=:
   fi
   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


# NODIST must expand to empty -- it's used in the various foo.in files for creating distribution versions
NODIST=
AC_SUBST(NODIST)

AC_CONFIG_FILES([Makefile
                 src/c/Makefile
                 src/c/test/Makefile
                 src/java/Makefile
                 src/java/test/Makefile
                 src/java/uk/me/nxg/unity/Version.java
                 src/package-info.txt])
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
