-- DAWS_Package for DCSW 2.3.0

local MainVersion = "2"
local SubVersion = "2"
local Build = "2288"

print('Initializing DAWS GUI modification: starting...')

DAWSgf = {}

local base 		= _G
local require	= base.require
local lfs       = require "lfs" 
local io		= require "io"

local copyFile =  function(old, new)
	local i = io.open(old, "r")
	local o = io.open(new, "w")
	if i then
		o:write(i:read("*a"))
		o:close()
		i:close()
	end		
end

-- setup SSE callbacks and file movement
function DAWSgf.onMissionLoadBegin()
	local fname = DCS.getMissionFilename()
	if fname == nil then
		print('DAWS filename still nil')
	else
		print('DAWS mission filename: ' .. fname)
	end
	
	print("DAWS is checking mixpath.txt")
	local logpath = lfs.writedir() .. "Logs/mixpath.txt" -- logpath = lfs.currentdir() .. "AMVI/SaveMission/mixpath.txt"  --lfs.writedir() .. "Logs/dcs.log.old"		
	local i, erri = base.io.open(logpath, "w")
	if i == nil then
		print("DAWS failed to retrieve mixpath.txt, error: " .. erri)
		return
	else
		i:write(fname)
		print("DAWS is writing mixpath.txt")
	end
	i:close()			

	copyFile(lfs.currentdir() .. "Scripts/MissionScripting.lua", lfs.writedir() .. "AMVI/ManualOverwrite/Old_MissionScripting.lua")
	copyFile(lfs.writedir() .. "AMVI/ManualOverwrite/MissionScripting.lua", lfs.currentdir() .. "Scripts/MissionScripting.lua")
end

function DAWSgf.onSimulationStop()
	copyFile(lfs.writedir() .. "AMVI/ManualOverwrite/Old_MissionScripting.lua", lfs.currentdir() .. "Scripts/MissionScripting.lua")
	os.remove(lfs.writedir() .. "AMVI/ManualOverwrite/Old_MissionScripting.lua")
end

DCS.setUserCallbacks(DAWSgf)

print('Initializing DAWS GUI modification: done')