#
# Default values for the configuration variables
#

#
# Name of the config file
#
CONFIG_FILE = $(file $(ROOT)/.config)

#
# Figure it out where ocaml is installed
#
CAMLLIB_DEFAULT = $(shell ocamlc -where)

#
# Default installation path: check the environment (and the registry on Windows).
# On Unix, use /usr/local by default.
#
PREFIX =
    getenv(PREFIX)
        get-registry(HKEY_CURRENT_USER, Software\MetaPRL\OMake, OMAKEDIR)
            get-registry(HKEY_LOCAL_MACHINE, Software\MetaPRL\OMake, OMAKEDIR)
                get-registry(HKEY_LOCAL_MACHINE, Software\Microsoft\Windows\CurrentVersion, ProgramFilesDir)
                    value $(if $(equal $(OSTYPE), Win32), $'C:\OMake', /usr/local)

PREFIX = $(dir $"$(PREFIX)")

BINDIR = $(dir $"$(getenv BINDIR, $(PREFIX)/bin)")
LIBDIR = $(dir $"$(getenv LIBDIR, $(PREFIX)/lib)")
MANDIR = $(dir $"$(getenv MANDIR, $(PREFIX)/man)")

#
# Profiling is disabled by default
#
NATIVE_PROFILE = false

#
# C compiler and flags - check the environment
#
CC = $(getenv CC, $(CC))
CFLAGS = $(getenv CFLAGS, $(EMPTY))

if $(file-exists $(CONFIG_FILE))
    include $(CONFIG_FILE)
    export

if $(file-exists $(CONFIG_FILE).local)
    include $(CONFIG_FILE).local
    export

#
# Configure
#
include configure/fam
include configure/readline

FAM_ENABLED = $(FAM_AVAILABLE)
READLINE_ENABLED = $(READLINE_AVAILABLE)

include mk/make_config

if $(file-exists $(CONFIG_FILE))
    include $(CONFIG_FILE)
    export

PREFIX = $(dir $(PREFIX))
BINDIR = $(dir $(BINDIR))
LIBDIR = $(dir $(LIBDIR))
MANDIR = $(dir $(MANDIR))

#
# XXX This is a big hack.
#
section
   PREFIX = $(dir $(PREFIX))
   INSTALL_ROOT = $(getenv INSTALL_ROOT, $(EMPTY))
   INSTALL_PREFIX = $(dir $(getenv PREFIX, $(PREFIX)))

   if $(INSTALL_ROOT)
      INSTALL_BINDIR=$(dir $(INSTALL_ROOT)$(absname $(BINDIR)))
      INSTALL_LIBDIR=$(dir $(INSTALL_ROOT)$(absname $(LIBDIR)))
      INSTALL_MANDIR=$(dir $(INSTALL_ROOT)$(absname $(MANDIR)))
      export
   elseif $(not $(equal $(INSTALL_PREFIX), $(PREFIX)))
      eprintln($"""
!!! WARNING !!!
   Your environment variable "PREFIX" points to $(absname $(INSTALL_PREFIX))
   but the PREFIX variable in .config points to $(absname $(PREFIX)).
   I am guessing you are using a packaging system (like GODI) that means the two
   to be different, want the installation to happen in $(absname $(INSTALL_PREFIX))
   and will then relocate files to $(absname $(PREFIX)). If this is wrong, please
   make sure your PREFIX variables agree or remove the PREFIX variable from your
   environment.
""")
      INSTALL_BINDIR=$(dir $(getenv BINDIR, $(INSTALL_PREFIX)/bin))
      INSTALL_LIBDIR=$(dir $(getenv LIBDIR, $(INSTALL_PREFIX)/lib))
      INSTALL_MANDIR=$(dir $(getenv MANDIR, $(INSTALL_PREFIX)/man))
      export
   else
      INSTALL_BINDIR=$(dir $(getenv BINDIR, $(BINDIR)))
      INSTALL_LIBDIR=$(dir $(getenv LIBDIR, $(LIBDIR)))
      INSTALL_MANDIR=$(dir $(getenv MANDIR, $(MANDIR)))
      export
   
   export INSTALL_BINDIR INSTALL_LIBDIR INSTALL_MANDIR

if $(not $(file-exists $(CONFIG_FILE)))
    eprintln($(CONFIG_FILE) does not exist)
    MakeConfig($(CONFIG_FILE))
    eprintln($"""!!!!! A new config file '$(CONFIG_FILE)' was created for you.
!!!!! In that file, we filled in a number of default values for you.
!!!!! You may want to edit the '$(CONFIG_FILE)' file before continuing.
!!!!!
!!!!! After you are done, run omake again to continue the build process.""")
    exit(0)

.INCLUDE: $(CONFIG_FILE): mk/make_config
    MakeConfig($(CONFIG_FILE))
