AC_INIT([Theme-D], [1.4.1], [tommi.hoynalanmaa at iki.fi], [theme-d], [http://www.iki.fi/tohoyn/theme-d/])

AM_INIT_AUTOMAKE

AC_PROG_CC

AC_CONFIG_MACRO_DIR([m4])

AC_LIBTOOL_DLOPEN
AC_PROG_LIBTOOL

maint_root_dir=$(pwd)
maint_translator_dir=$maint_root_dir/theme-d/translator
maint_runtime_dir=$maint_root_dir/theme-d/runtime
maint_lib_dir=$maint_root_dir/theme-d-code
maint_examples_dir=$maint_root_dir/theme-d-code/examples
maint_tests_dir=$maint_root_dir/theme-d-code/tests
maint_tools_dir=$maint_root_dir/tools
maint_compiler_path=$maint_translator_dir/theme-d-compile.scm
maint_linker_path=$maint_translator_dir/theme-d-link.scm
maint_run_path=$maint_runtime_dir/run-theme-d-program.scm
theme_d_native_classes_size=1000
theme_d_classes_size=1000

AC_SUBST(maint_root_dir)
AC_SUBST(maint_translator_dir)
AC_SUBST(maint_runtime_dir)
AC_SUBST(maint_lib_dir)
AC_SUBST(maint_examples_dir)
AC_SUBST(maint_tests_dir)
AC_SUBST(maint_tools_dir)
AC_SUBST(maint_compiler_path)
AC_SUBST(maint_linker_path)
AC_SUBST(maint_run_path)
AC_SUBST(theme_d_native_classes_size)
AC_SUBST(theme_d_classes_size)

AC_ARG_WITH([guile],
  [AS_HELP_STRING([--with-guile],
    [Guile version to be used])],
  [guile_spec=$withval],
  [guile_spec=no])

CHECK_GUILE_VERSION

AC_SUBST(MY_GUILE_VERSION)

GUILE_PACKAGE=guile-$MY_GUILE_VERSION

AC_SUBST(LIBGUILE_CPPFLAGS, $(pkg-config --cflags $GUILE_PACKAGE))

AC_ARG_WITH([target-platform],
  [AS_HELP_STRING([--with-target-platform],
    [Target platform to be used])],
  [TARGET_PLATFORM=$withval],
  [TARGET_PLATFORM=$GUILE_PACKAGE])

if test "$TARGET_PLATFORM" != "guile-2.0" -a \
   "$TARGET_PLATFORM" != "guile-2.2" -a \
   "$TARGET_PLATFORM" != "guile-3.0" -a \
   "$TARGET_PLATFORM" != "racket" ; then
   echo "Invalid target platform." ;
   exit 1 ;
fi

AC_SUBST(TARGET_PLATFORM)

AC_ARG_WITH([support-library],
  [AS_HELP_STRING([--with-support-library],
    [Use the Theme-D support library (libthemedsupport)])],
  [support_lib_spec=$withval],
  [support_lib_spec=default])

if test "$support_lib_spec" = "yes" ; then
   support_lib=yes
elif test "$support_lib_spec" = "no" ; then
   support_lib=no
elif test "$support_lib_spec" = "default" ; then
  if test "$TARGET_PLATFORM" = "racket" ; then
     support_lib=no
  else
     support_lib=yes
  fi
fi

if test "$support_lib" = "yes" -a "$TARGET_PLATFORM" = "racket" ; then
   echo "The support library cannot be used with Racket." ;
   exit 1;
fi

dnl Having a # in SUPPORT_LIB caused problems in makefiles.
if test "$support_lib" = yes; then
  SUPPORT_LIB=t
else
  SUPPORT_LIB=f
fi

AC_SUBST(SUPPORT_LIB)

AM_CONDITIONAL([C_SUPPORT_LIB], [test "$support_lib" = yes])

platform_body_link=$(pwd)/theme-d-code/standard-library/platform-specific-impl.thb

if test \( -e $platform_body_link -o -h $platform_body_link \) ; then
  rm $platform_body_link ;
fi

if test "$TARGET_PLATFORM" = "racket" ; then
  platform_body=$(pwd)/theme-d-code/standard-library/platform-specific-impl-racket.thb ;
else
  platform_body=$(pwd)/theme-d-code/standard-library/platform-specific-impl-guile.thb ;
fi

ln -s $platform_body $platform_body_link

AC_SUBST(MODULE_DIR, $(${PKG_CONFIG} --variable=sitedir $GUILE_PACKAGE))

GUILE_LIB_DIR=$(${PKG_CONFIG} --variable=libdir $GUILE_PACKAGE)
COMP_MODULE_DIR=$GUILE_LIB_DIR/guile/$MY_GUILE_VERSION/site-ccache

AC_SUBST(COMP_MODULE_DIR)

AC_ARG_ENABLE([extra-math],
  [AS_HELP_STRING([--enable-extra-math],
    [Enable some mathematical functions specified by the C standard])],
  [extra_math_spec=$enableval],
  [extra_math_spec=default])

if test "$extra_math_spec" = "default" ; then
   if test "$TARGET_PLATFORM" != "racket" ; then
      extra_math=yes ;
   else
      extra_math=no ;
   fi
elif test "$extra_math_spec" = "yes" -o "$extra_math_spec" = "no" ; then
  extra_math=$extra_math_spec
else
  echo "Syntax error with option extra-math" ;
  exit 1 ;
fi

if test \( "$extra_math" = "yes" \) -a \( "$support_lib" = "no" \) ; then
   echo "Error: Extra math procedures can't be used if the support library is not used." ;
   exit 1;
fi

AM_CONDITIONAL([EXTRA_MATH], [test "$extra_math" = "yes"])

if test "$extra_math" = "yes" ; then
   STANDARD_MATH_FLAG=-DSTANDARD_MATH=1
else
   STANDARD_MATH_FLAG=
fi

AC_SUBST(STANDARD_MATH_FLAG)

AC_ARG_ENABLE([posix-math],
  [AS_HELP_STRING([--enable-posix-math],
    [Enable some mathematical functions specified by the POSIX C standard])],
  [posix_math_spec=$enableval],
  [posix_math_spec=default])

if test "$posix_math_spec" = "default" ; then
   if test "$TARGET_PLATFORM" != "racket" ; then
      posix_math=yes ;
   else
      posix_math=no ;
   fi
elif test "$posix_math_spec" = "yes" -o "$posix_math_spec" = "no" ; then
  posix_math=$posix_math_spec
else
  echo "Syntax error with option posix-math" ;
  exit 1 ;
fi

if test \( "$posix_math" = "yes" \) -a \( "$support_lib" = "no" \) ; then
   echo "Error: POSIX math procedures can't be used if the support library is not used." ;
   exit 1;
fi

AM_CONDITIONAL([POSIX_MATH], [test "$posix_math" = "yes"])

if test "$posix_math" = "yes" ; then
   POSIX_MATH_FLAG=-DPOSIX_MATH=1
else
   POSIX_MATH_FLAG=
fi

AC_SUBST(POSIX_MATH_FLAG)

EXTENSION_DIR=$(pkg-config --variable=extensiondir $GUILE_PACKAGE)

if test "$EXTENSION_DIR" = "" ; then
  EXTENSION_DIR=$libdir
fi

AC_SUBST(EXTENSION_DIR)

if test "$support_lib" = "yes" ; then
   if test "$extra_math" = "yes" -a "$posix_math" = "yes" ; then
      support_module=theme-d-support-all.scm ;
   elif test "$extra_math" = "yes" -a "$posix_math" = "no" ; then
      support_module=theme-d-support-no-posix.scm ;
   elif test "$extra_math" = "no" -a "$posix_math" = "yes" ; then
      echo "Posix math cannot be used without extra math." ;
      exit 1 ;
   elif test "$extra_math" = "no" -a "$posix_math" = "no" ; then
     support_module=theme-d-support-no-extra.scm ;
   else
      echo "Error" ;
      exit 1 ;
   fi
else
   support_module=theme-d-alt-support.scm
fi

if test "$support_module" = "" ; then
   echo "Error"
   exit 1
fi

ln -s $(pwd)/theme-d/runtime/${support_module} \
   $(pwd)/theme-d/runtime/theme-d-support.scm
   
THEME_D_TRANSLATOR_DIR=$COMP_MODULE_DIR/theme-d/translator
THEME_D_RUNTIME_DIR=$COMP_MODULE_DIR/theme-d/runtime
THEME_D_SCRIPT_DIR=$datadir/theme-d/scripts
THEME_D_LIB_DIR=$datadir/theme-d/lib
THEME_D_EXAMPLES_DIR=$docdir/examples
THEME_D_TESTS_DIR=$datadir/theme-d/tests
THEME_D_TOOLS_DIR=$datadir/theme-d/tools
THEME_D_COMPILER_PATH=$THEME_D_SCRIPT_DIR/theme-d-compile.scm
THEME_D_LINKER_PATH=$THEME_D_SCRIPT_DIR/theme-d-link.scm
THEME_D_RUN_PATH=$THEME_D_SCRIPT_DIR/run-theme-d-program.scm

AC_SUBST(THEME_D_TRANSLATOR_DIR)
AC_SUBST(THEME_D_RUNTIME_DIR)
AC_SUBST(THEME_D_SCRIPT_DIR)
AC_SUBST(THEME_D_LIB_DIR)
AC_SUBST(THEME_D_EXAMPLES_DIR)
AC_SUBST(THEME_D_TESTS_DIR)
AC_SUBST(THEME_D_TOOLS_DIR)
AC_SUBST(THEME_D_COMPILER_PATH)
AC_SUBST(THEME_D_LINKER_PATH)
AC_SUBST(THEME_D_RUN_PATH)

AC_CONFIG_FILES([Makefile
                 doc/Makefile
		 th-scheme-utilities/Makefile
		 libthemedsupport/Makefile
                 theme-d/Makefile
		 theme-d/common/Makefile
		 theme-d/translator/Makefile
                 theme-d/runtime/Makefile
                 theme-d/runtime/params.scm
		 theme-d/testing/Makefile
		 theme-d-racket/runtime/params.rkt
		 tools/Makefile
		 theme-d-code/Makefile
                 theme-d-code/standard-library/Makefile
		 theme-d-code/tests/Makefile
		 theme-d-code/examples/Makefile
		 theme-d-config-maint
])
AC_OUTPUT
