#! /usr/bin/env python
# encoding: utf-8
# mittorn, 2018

from waflib import Logs
import os

top = '.'

def options(opt):
	grp = opt.add_option_group('Engine options')
	grp.add_option(
		'--enable-bsp2', action = 'store_true', dest = 'SUPPORT_BSP2_FORMAT', default = False,
		help = 'build engine with BSP2 map support(recommended for Quake, breaks compatibility!)')

	grp.add_option(
		'--stdin-input', action = 'store_true', dest = 'USE_SELECT', default = None,
		help = 'enable console input from stdin (default for dedicated)')

	opt.load('sdl2')

def configure(conf):
	# check for dedicated server build
	if conf.options.DEDICATED:
		if(conf.env.DEST_OS == 'linux'):
			conf.check_cc( lib='rt' )
		conf.env.append_unique('DEFINES', 'XASH_DEDICATED')
	elif conf.env.DEST_OS2 == 'android': # Android doesn't need SDL2
		conf.check_cc(lib='android')
		conf.check_cc(lib='log')
		conf.check_cc(lib='EGL')
	else:
		conf.load('sdl2')
		if not conf.env.HAVE_SDL2:
			conf.fatal('SDL2 not availiable! If you want to build dedicated server, specify --dedicated')
		conf.env.append_unique('DEFINES', 'XASH_SDL')

	if conf.env.SINGLE_BINARY:
		conf.env.append_unique('DEFINES', 'SINGLE_BINARY')

	if conf.options.USE_SELECT == None:
		conf.options.USE_SELECT = conf.options.DEDICATED
	if conf.options.USE_SELECT:
		conf.env.append_unique('DEFINES', 'XASH_USE_SELECT')

	if conf.options.SUPPORT_BSP2_FORMAT:
		conf.env.append_unique('DEFINES', 'SUPPORT_BSP2_FORMAT')

	if conf.env.DEST_OS == 'win32':
		conf.env.append_unique('DEFINES', 'DBGHELP')
		conf.env.append_unique('DEFINES', 'PSAPI_VERSION=1')

	if conf.env.DEST_SIZEOF_VOID_P != 4:
		conf.env.append_unique('DEFINES', 'XASH_64BIT')

def build(bld):
	libs = [ 'public' ]
	source = bld.path.ant_glob([
		'common/*.c',
		'common/imagelib/*.c',
		'common/soundlib/*.c',
		'common/soundlib/libmpg/*.c',
		'server/*.c'])

	# basic build: dedicated only, no dependencies
	if bld.env.DEST_OS != 'win32':
		libs += [ 'DL' , 'M', 'PTHREAD' ]
		source += bld.path.ant_glob(['platform/posix/*.c'])
	else:
		libs += ['USER32', 'SHELL32', 'GDI32', 'ADVAPI32', 'DBGHELP', 'PSAPI', 'WS2_32' ]
		source += bld.path.ant_glob(['platform/win32/*.c'])

	if bld.env.DEST_OS == 'linux':
		source += bld.path.ant_glob(['platform/linux/*.c'])

	if bld.env.HAVE_SDL2:
		libs.append('SDL2')
		source += bld.path.ant_glob(['platform/sdl/*.c'])

	if bld.env.DEST_OS2 == 'android':
		libs += ['LOG', 'EGL', 'ANDROID']
		source += bld.path.ant_glob(['platform/android/*.cpp', 'platform/android/*.c'])

	# add client files
	if not bld.env.DEDICATED:
		source += bld.path.ant_glob([
			'client/*.c',
			'client/vgui/*.c',
			'client/avi/*.c'])
	else:
		if bld.env.DEST_OS == 'linux':
			libs.append('RT')

	# HACK: public headers must be put before common, so we don't get wrong mathlib included
	includes = ['common', 'server', 'client', 'client/vgui', '.', '../public', '../common', '../pm_shared' ]

	if bld.env.SINGLE_BINARY:
		install_path = bld.env.BINDIR
		features = 'c cprogram'
	else:
		install_path = bld.env.LIBDIR
		features = 'c cshlib'

	bld(
		source   = source,
		target   = 'xash',
		features = features,
		includes = includes,
		use      = libs,
		install_path = install_path,
		subsystem = bld.env.MSVC_SUBSYSTEM
	)
