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

from waflib import Utils
import os

def options(opt):
	grp = opt.add_option_group('Client options')

	grp.add_option('--disable-goldsrc-support', action = 'store_false', dest = 'GOLDSOURCE_SUPPORT',
		default=True, help = 'disable GoldSource compatibility on i386 Win/Mac/Linux')

def configure(conf):
	if conf.env.DEST_OS in ['win32', 'linux', 'darwin'] and conf.env.DEST_CPU == 'x86':
		conf.env.GOLDSOURCE_SUPPORT = conf.options.GOLDSOURCE_SUPPORT
	else:
		conf.env.GOLDSOURCE_SUPPORT = False

	if conf.env.GOLDSOURCE_SUPPORT and not conf.env.DEST_OS == 'win32':
		conf.check_cc(lib='dl')

def build(bld):
	libs = ['vcs_info']
	defines = ['CLIENT_DLL']
	defines += bld.env.EXPORT_DEFINES_LIST

	# the idea is to always include original vgui headers set (because nobody is gonna port their code to freevgui headers)
	# but compile freevgui as a shared library here so we can link against it, but skip it from install
	includes = ['.',
		'../dlls',
		'../common',
		'../engine',
		'../pm_shared',
		'../game_shared',
		'../public',
		'../particleman',
		'../3rdparty/MiniUTL',
		'../utils/vgui/include']
	excluded_files = ['game_shared/voice_vgui_tweakdlg.cpp',
		'game_shared/voice_gamemgr.cpp',
		'game_shared/voice_status.cpp']

	libs += ['vgui']

	source = bld.path.ant_glob('**/*.cpp', excl=excluded_files)
	source += bld.path.parent.ant_glob('game_shared/*.cpp', excl=excluded_files)
	source += bld.path.parent.ant_glob([
		'public/safe_snprintf.c',
		'pm_shared/*.cpp',
		'particleman/*.cpp',
		'3rdparty/MiniUTL/*.cpp',
		'dlls/dod_shared.cpp',
		'dlls/wpn_shared/*.cpp'
	])


	if bld.env.DEST_OS == 'win32':
		libs += ['USER32']

	if bld.env.GOLDSOURCE_SUPPORT:
		defines += ['GOLDSOURCE_SUPPORT']
		includes += ['../external/']

		if bld.env.DEST_OS == 'win32':
			libs += ["WINMM"]
		else:
			libs += ['DL']

	if 'HAVE_STRLCPY=1' not in defines:
		source += bld.path.parent.ant_glob('external/openbsd/strlcpy.c')

	if 'HAVE_STRLCAT=1' not in defines:
		source += bld.path.parent.ant_glob('external/openbsd/strlcat.c')

	if bld.env.USE_STATIC_FREEVGUI:
		defines += ['INTERNAL_VGUI_SUPPORT']

	if bld.env.DEST_OS != 'dos' and not bld.env.ANDROID_APK:
		install_path = os.path.join(bld.env.GAMEDIR, bld.env.CLIENT_INSTALL_DIR)
	else:
		install_path = bld.env.PREFIX

	bld.shlib(
		source   = source,
		target   = 'client' + bld.env.POSTFIX,
		name     = 'client',
		features = 'c cxx fix_freevgui_link',
		includes = includes,
		defines  = defines,
		use      = libs,
		install_path = install_path,
		idx = bld.get_taskgen_count()
	)
