--- SConscript.orig	2016-07-27 02:21:28 UTC
+++ SConscript
@@ -41,6 +41,7 @@ def AddSconsOption(name, default, hasArg
 AddSconsOption('win', False, False, "Target Windows.")
 AddSconsOption('lin', False, False, "Target Linux.")
 AddSconsOption('mac', False, False, "Target Mac OS X.")
+AddSconsOption('bsd', False, False, "Target FreeBSD.")
 AddSconsOption('msvc', False, False, "Use the Microsoft Visual Studio compiler.")
 AddSconsOption("tool", False, True, "Tool prefix appended before gcc/g++.")
 
@@ -79,7 +80,7 @@ AddSconsOption("output", False, True, "E
 
 #detect platform automatically, but it can be overrided
 tool = GetOption('tool')
-isX86 = platform.machine() in ["AMD64", "i386", "i686", "x86", "x86_64"]
+isX86 = platform.machine() in ["amd64", "AMD64", "i386", "i686", "x86", "x86_64"]
 platform = compilePlatform = platform.system()
 if GetOption('win'):
 	platform = "Windows"
@@ -87,7 +88,9 @@ elif GetOption('lin'):
 	platform = "Linux"
 elif GetOption('mac'):
 	platform = "Darwin"
-elif compilePlatform not in ["Linux", "Windows", "Darwin"]:
+elif GetOption('bsd'):
+	platform = "FreeBSD"
+elif compilePlatform not in ["Linux", "Windows", "Darwin", "FreeBSD"]:
 	FatalError("Unknown platform: {0}".format(platform))
 
 msvc = GetOption('msvc')
@@ -100,7 +103,7 @@ if GetOption('msvc'):
 elif platform == "Windows" and not GetOption('msvc'):
 	env = Environment(tools=['mingw'], ENV={'PATH' : os.environ['PATH']})
 else:
-	env = Environment(tools=['default'], ENV={'PATH' : os.environ['PATH']})
+	env = Environment(tools=['default'], ENV=os.environ)
 
 #attempt to automatically find cross compiler
 if not tool and compilePlatform == "Linux" and compilePlatform != platform:
@@ -242,7 +245,7 @@ def findLibs(env, conf):
 
 	if not GetOption('renderer'):
 		#Look for SDL
-		runSdlConfig = platform == "Linux" or compilePlatform == "Linux"
+		runSdlConfig = platform == "Linux" or compilePlatform == "Linux" or platform == "FreeBSD"
 		if platform == "Darwin" and conf.CheckFramework("SDL"):
 			runSdlConfig = False
 		elif not conf.CheckLib("SDL"):
@@ -267,7 +270,7 @@ def findLibs(env, conf):
 
 	if not GetOption('nolua') and not GetOption('renderer'):
 		#Look for Lua
-		luaver = "lua5.1"
+		luaver = "lua-51"
 		if GetOption('luajit'):
 			if not conf.CheckLib(['luajit-5.1', 'luajit5.1', 'luajit', 'libluajit']):
 				FatalError("luajit development library not found or not installed")
@@ -277,13 +280,13 @@ def findLibs(env, conf):
 			if not conf.CheckLib(['lua5.2', 'lua-5.2', 'lua52', 'lua']):
 				FatalError("lua5.2 development library not found or not installed")
 			env.Append(CPPDEFINES=["LUA_COMPAT_ALL"])
-			luaver = "lua5.2"
+			luaver = "lua-52"
 		else:
 			if not conf.CheckLib(['lua5.1', 'lua-5.1', 'lua51', 'lua']):
 				if platform != "Darwin" or not conf.CheckFramework("Lua"):
 					FatalError("lua5.1 development library not found or not installed")
 		foundpkg = False
-		if platform == "Linux":
+		if platform == "Linux" or platform == "FreeBSD":
 			try:
 				env.ParseConfig("pkg-config --cflags {0}".format(luaver))
 				env.ParseConfig("pkg-config --libs {0}".format(luaver))
@@ -297,9 +300,9 @@ def findLibs(env, conf):
 			if GetOption('luajit'):
 				foundheader = conf.CheckCHeader('luajit-2.0/lua.h')
 			elif GetOption('lua52'):
-				foundheader = conf.CheckCHeader('lua5.2/lua.h')
+				foundheader = conf.CheckCHeader('lua52/lua.h')
 			else:
-				foundheader = conf.CheckCHeader('lua5.1/lua.h')
+				foundheader = conf.CheckCHeader('lua51/lua.h')
 			if not foundheader:
 				if conf.CheckCHeader('lua.h'):
 					env.Append(CPPDEFINES=["LUA_R_INCL"])
@@ -340,7 +343,7 @@ def findLibs(env, conf):
 
 	#Look for OpenGL libraries
 	if GetOption('opengl'):
-		if platform == "Linux":
+		if platform == "Linux" or platform == "FreeBSD":
 			if not conf.CheckLib('GL'):
 				FatalError("libGL not found or not installed")
 			try:
@@ -357,7 +360,7 @@ def findLibs(env, conf):
 			if not conf.CheckFramework("OpenGL"):
 				FatalError("OpenGL framework not found or not installed")
 
-	if platform == "Linux":
+	if platform == "Linux" or platform == "FreeBSD":
 		if not conf.CheckLib('X11'):
 			FatalError("X11 development library not found or not installed")
 
@@ -411,7 +414,7 @@ if platform == "Windows":
 			env.Append(LINKFLAGS=['/NODEFAULTLIB:msvcrt.lib'])
 	else:
 		env.Append(LINKFLAGS=['-mwindows'])
-elif platform == "Linux":
+elif platform == "Linux" or platform == "FreeBSD":
 	env.Append(CPPDEFINES=['LIN'])
 elif platform == "Darwin":
 	env.Append(CPPDEFINES=['MACOSX'])
@@ -464,9 +467,7 @@ elif GetOption('release'):
 		else:
 			env.Append(CCFLAGS=['/MD'])
 	else:
-		env.Append(CCFLAGS=['-O3', '-ftree-vectorize', '-funsafe-math-optimizations', '-ffast-math', '-fomit-frame-pointer'])
-		if platform != "Darwin":
-			env.Append(CCFLAGS=['-funsafe-loop-optimizations'])
+		pass
 
 if GetOption('static'):
 	if platform == "Windows":
