dnl
dnl
dnl    Pixie configure.in file
dnl
dnl
AC_INIT(src/common/algebra.cpp)
AM_INIT_AUTOMAKE(Pixie,1.6.3)
AM_CONFIG_HEADER(config.h)
AC_PREFIX_DEFAULT(/opt/pixie)


dnl ---------------------------------------------------
dnl Check the compiler/programs
dnl
AC_PROG_CC
AC_PROG_CPP
AC_PROG_CXX
AM_DISABLE_STATIC
AM_PROG_LIBTOOL
AC_PROG_INSTALL
AC_PROG_MAKE_SET

dnl ---------------------------------------------------
dnl Check standard libraries
dnl
AC_CHECK_LIB(dl, dlopen,,[AC_MSG_ERROR([dl library is not found])],)
AC_CHECK_LIB(m, sin,,[AC_MSG_ERROR([math library is not found])],)
AC_CHECK_LIB(pthread, pthread_create,,[AC_MSG_ERROR([pthread library is not found])],)

dnl ---------------------------------------------------
dnl Find X
dnl
AC_PATH_X
AC_PATH_XTRA
X_LDFLAGS="$X_LDFLAGS -lX11 -lXext"

saved_CFLAGS="$CFLAGS"
saved_LDFLAGS="$LDFLAGS"
saved_LIBS="$LIBS"

CFLAGS="$CFLAGS $X_CFLAGS"
LDFLAGS="$X_LDFLAGS $X_LIBS $LDFLAGS"

AC_CHECK_LIB(X11, XOpenDisplay,,[AC_MSG_ERROR([X11 is not found, it is required by framebuffer])],)
AC_CHECK_LIB(Xext, XShmAttach,,[AC_MSG_ERROR([Xext is not found, it is required by framebuffer])],)

CFLAGS="$saved_CFLAGS"
LDFLAGS="$saved_LDFLAGS"
LIBS="$saved_LIBS"

dnl ---------------------------------------------------
dnl Find FLTK
dnl

have_fltk=false
AC_CHECK_LIB(fltk,main,LIBFLTK="-lfltk")
AC_CHECK_LIB(fltk_gl,main,[LIBFLTKGL="-lfltk_gl -lGL" have_fltk=true])
AM_CONDITIONAL(BUILD_SHOW, test x$have_fltk = xtrue)

dnl ---------------------------------------------------
dnl Find libtiff
dnl

saved_LIBS="$LIBS"

AC_CHECK_HEADER(tiffio.h,,AC_MSG_ERROR([tiffio.h not found]))
AC_CHECK_LIB(tiff,TIFFOpen,,[AC_MSG_ERROR([libtiff not found])],)

TIFF_LIBS="$LIBS"
LIBS="$saved_LIBS"

dnl ---------------------------------------------------
dnl Find OpenEXR
dnl

AM_PATH_OPENEXR
AM_CONDITIONAL(BUILD_OPENEXR, test "x$no_openexr" = "x")

dnl ---------------------------------------------------
dnl Check system/headers
dnl
AC_HEADER_STDC
AC_CHECK_HEADERS(fcntl.h limits.h malloc.h sys/time.h unistd.h)

AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T
AC_HEADER_TIME

AC_FUNC_ALLOCA
AC_FUNC_VPRINTF
AC_CHECK_FUNCS(ftime gethostname gettimeofday mkdir rmdir socket strdup strstr strtod strtol)

dnl ---------------------------------------------------
dnl Do variable substitution
dnl
AC_SUBST(LIBFLTK)
AC_SUBST(LIBFLTKGL)
AC_SUBST(X_LIBS)
AC_SUBST(X_CFLAGS)
AC_SUBST(X_LDFLAGS)
AC_SUBST(TIFF_LIBS)
AC_SUBST(BUILD_SHOW)

dnl ---------------------------------------------------
dnl Set custom directories for shaders, models, procedurals, and displays
dnl

AC_ARG_WITH(docdir,
	AS_HELP_STRING([--with-docdir=DIR],[directory where we install documentation (Default: DATADIR/doc/Pixie)]),
	docdir="${withval}",
	docdir="${datadir}/doc/Pixie")

AC_ARG_WITH(shaderdir,
	AS_HELP_STRING([--with-shaderdir=DIR],[directory where we store shaders (Default: DATADIR/Pixie/shaders)]),
	shaderdir="${withval}",
	shaderdir="${datadir}/Pixie/shaders")

AC_ARG_WITH(modeldir,
	AS_HELP_STRING([--with-modeldir=DIR],[directory where we store models (Default: DATADIR/Pixie/models)]),
	modeldir="${withval}",
	modeldir="${datadir}/Pixie/models")

AC_ARG_WITH(texturedir,
	AS_HELP_STRING([--with-modeldir=DIR],[directory where we store models (Default: DATADIR/Pixie/textures)]),
	texturedir="${withval}",
	texturedir="${datadir}/Pixie/textures")

AC_ARG_WITH(proceduraldir,
	AS_HELP_STRING([--with-shaderdir=DIR],[directory to store shaders (Default: LIBDIR/Pixie/procedurals)]),
	proceduraldir="${withval}",
	proceduraldir="${libdir}/Pixie/procedurals")

AC_ARG_WITH(displaysdir,
	AS_HELP_STRING([--with-displaysdir=DIR],[directory to store displays (Default: LIBDIR/Pixie/displays)]),
	displaysdir="${withval}",
	displaysdir="${libdir}/Pixie/displays")

dnl ---------------------------------------------------
dnl Are we building for self contained distribution
dnl

AC_ARG_ENABLE(selfcontained,
AS_HELP_STRING([--enable-selfcontained],[Build for a selfcontained setup (overrides custom directory settings)]),
[case "${enableval}" in
	yes)	docdir="${prefix}/doc"
		shaderdir="${prefix}/shaders"
		modeldir="${prefix}/models"
		texturedir="${prefix}/textures"
		displaysdir="${prefix}/displays"
		proceduraldir="${prefix}/procedurals"
		;;
	no) ;;
	*) AC_MSG_ERROR(bad value ${enableval} for --enable-selfcontained) ;;
esac])

dnl ---------------------------------------------------
dnl Now tell the c preprocessor and the Makefiles where stuff goes
dnl 

AC_DEFUN([AC_DEFINE_DIR], [
  prefix_NONE=
  exec_prefix_NONE=
  test "x$prefix" = xNONE && prefix_NONE=yes && prefix=$ac_default_prefix
  test "x$exec_prefix" = xNONE && exec_prefix_NONE=yes && exec_prefix=$prefix
  eval ac_define_dir="\"[$]$2\""
  AC_SUBST($1, "$ac_define_dir")
  AC_SUBST($2, "$ac_define_dir")
  AC_DEFINE_UNQUOTED($1, "$ac_define_dir", [$3])
  test "$prefix_NONE" && prefix=NONE
  test "$exec_prefix_NONE" && exec_prefix=NONE
])

AC_DEFINE_DIR(PIXIE_DOCS,docdir,[Directory where we store documentation])
AC_DEFINE_DIR(PIXIE_SHADERS,shaderdir,[Directory where we store shaders])
AC_DEFINE_DIR(PIXIE_MODELS,modeldir,[Directory where we store models])
AC_DEFINE_DIR(PIXIE_TEXTURES,texturedir,[Directory where we store textures])
AC_DEFINE_DIR(PIXIE_DISPLAYS,displaysdir,[Directory where we store displays])
AC_DEFINE_DIR(PIXIE_PROCEDURALS,proceduraldir,[Directory where we store procedurals])

dnl ---------------------------------------------------
dnl Write the output
dnl
AC_OUTPUT(Makefile src/Makefile src/common/Makefile src/file/Makefile src/framebuffer/Makefile src/openexr/Makefile src/opengl/Makefile src/precomp/Makefile src/rgbe/Makefile src/sdr/Makefile src/sdrc/Makefile src/ri/Makefile src/rndr/Makefile src/texmake/Makefile src/sdrinfo/Makefile src/show/Makefile)




if test "have_fltk = FALSE"; then
echo "------------------------------------------------"
echo "--->            FLTK not found."
echo "---> You can download it from www.fltk.org"
echo "---> show will not be built."
fi

if test "no_openexr = TRUE"; then
echo "------------------------------------------------"
echo "--->            OpenEXR not found."
echo "---> You can download it from www.openexr.org"
echo "---> openexr display driver will not be built."
fi
 
echo "------------------------------------------------"
echo " Installation info:"
echo "     PIXIEHOME: " $prefix
echo " Documentation: " $docdir
echo "       Shaders: " $shaderdir
echo "        Models: " $modeldir
echo "      Textures: " $texturedir
echo "      Displays: " $displaysdir
echo "   Procedurals: " $proceduraldir
echo "------------------------------------------------"
echo "To build Pixie:"
echo ">make"
echo ">make -install"
echo "              Terminus Est"
echo








