if get_option('rest_lib') == 'restc-cpp'
  restc_cpp_dep = dependency('restc-cpp', version: '>=0.1.1',
	fallback: ['restc-cpp', 'restc_cpp_dep'],
	default_options: [
	  'restc_cpp_with_unit_tests=false',
	  'restc_cpp_log_with_boost_log=false',
	],
  )
endif

curlcpp_dep = dependency('curlcpp', version: '>=1.4',
  fallback: ['curlcpp', 'curlcpp_dep'],
  default_options: [
	'default_library=static',
  ],
)

lzma_dep = dependency('liblzma', required: get_option('with_lzma'), version: '>=5.2.5')

simdjson_dep = dependency('simdjson', version: '>=0.5.0',
  fallback: ['simdjson', 'simdjson_dep'],
)

if not get_option('with_sqlite').disabled()
  sqlitecpp_dep = dependency('sqlitecpp', version: '>=3.0.0',
	fallback: ['SQLiteCpp', 'sqlitecpp_dep'],
	required: get_option('with_sqlite'),
  )
else
  sqlitecpp_dep = disabler()
endif

cpp_comp = meson.get_compiler('cpp')
ev_dep = dependency('libev', version: '>=4.31', required: false)
if not ev_dep.found()
  ev_dep = cpp_comp.find_library('libev', required: true, has_headers: 'ev++.h')
endif
threads_dep = dependency('threads')
boost_dep = dependency('boost')

base_url = get_option('base_url').strip()
if not base_url.endswith('/')
  base_url = base_url + '/'
endif

conf = configuration_data()
conf.set_quoted('BASE_URL', base_url)
conf.set_quoted('CONFIG_FILE_PATH', full_config_dir / meson.project_name() + '.conf')
conf.set_quoted('DEF_SQLITE_DB_NAME', get_option('def_sqlite_db_name'))
conf.set_quoted('PROJECT_NAME', meson.project_name())
if get_option('buildtype') == 'debug'
  conf.set_quoted('BUILD_PURPOSE', 'Debugging')
else
  conf.set_quoted('BUILD_PURPOSE', 'Production')
endif
version_arr = meson.project_version().split('.')
conf.set('PROJECT_VERSION_MAJOR', version_arr[0])
conf.set('PROJECT_VERSION_MINOR', version_arr[1])
conf.set('PROJECT_VERSION_PATCH', version_arr[2])
conf.set('OROTOOL_WITH_SQLITE', sqlitecpp_dep.found())
conf.set('OROTOOL_WITH_LZMA', lzma_dep.found())
conf.set('OROTOOL_WITH_NAP', get_option('rest_lib') == 'nap')
conf.set('OROTOOL_WITH_RESTCCPP', get_option('rest_lib') == 'restc-cpp')
project_config_file = configure_file(
	input: 'config.hpp.in',
	output: meson.project_name() + '_config.hpp',
	configuration: conf,
)

gnulib_conf = configuration_data()
gnulib_conf.set('_GL_ATTRIBUTE_CONST', '__attribute__ ((__const__))')
configure_file(output : 'config.h', configuration : gnulib_conf)

backend_libs = [
  sqlitecpp_dep,
]

backends_selected = false
foreach backend_lib : backend_libs
  backends_selected = backends_selected or backend_lib.found()
endforeach
if not backends_selected
  error('No backend was enabled or none of the enabled backend dependencies could be located')
endif

lib_deps = [
  ev_dep,
  threads_dep,
  boost_dep,
  curlcpp_dep,
  simdjson_dep,
  lzma_dep,
] + backend_libs

if get_option('rest_lib') == 'nap'
  optional_sources = [
	'nap/page_fetch.cpp',
	'nap/http_header_parse.cpp',
	'nap/quick_rest.cpp',
	'oro/api_nap.cpp',
  ]
elif get_option('rest_lib') == 'restc-cpp'
  optional_sources = ['oro/api_restccpp.cpp']
  lib_deps += [restc_cpp_dep]
endif

if lzma_dep.found()
  optional_sources += ['lzma.cpp']
endif

executable(meson.project_name(),
  'main.cpp',
  'ini_file.cpp',
  'oro/datatypes.cpp',
  'oro/private/dateconv.cpp',
  'oro/private/mime_split.cpp',
  'oro/items.cpp',
  'oro/shops.cpp',
  'evloop.cpp',
  'eventia/eventia.cpp',
  'eventia/timer.cpp',
  'timer_base.cpp',
  'oro/private/originsdb_sqlite.cpp',
  'gnulib/lib/base64.c',
  'base64.cpp',
  'app_config.cpp',
  'oro/private/tiger.c',
  'oro/private/tiger.cpp',
  'eventia/signal.cpp',
  'eventia/event.cpp',
  'timer_oro_api.cpp',
  'oro/originsdb.cpp',
  'oro/api.cpp',
  'oro/api_nap_exception.cpp',
  optional_sources,
  project_config_file,
  'eventia_thread_pool.cpp',
  install: true,
  dependencies: lib_deps,
  include_directories: [
	date_inc,
	duckhandy_inc,
	magic_enum_inc,
  ],
  cpp_args: ['-DEV_USE_STDEXCEPT'],
)
