
defineProperty("position_saving", globalPropertyi("sim/custom/xap/position_saving")) -- position saving
defineProperty("livery", globalPropertyi("sim/aircraft/view/acf_livery_index")) -- livery index
defineProperty("flight_time", globalPropertyf("sim/time/total_flight_time_sec"))  -- local time since aircraft was loaded 
defineProperty("local_time", globalPropertyf("sim/time/local_time_sec")) -- local time
defineProperty("local_time_sec", globalPropertyf("sim/cockpit2/clock_timer/local_time_seconds")) -- local time sec
defineProperty("ground_speed", globalPropertyf("sim/flightmodel/position/groundspeed")) -- true airspeed

defineProperty("tailhook", globalPropertyf("sim/cockpit2/switches/tailhook_deploy")) -- tailhook switch
defineProperty("vinet", globalPropertyi("sim/custom/xap/post_effect"))  -- post effect

defineProperty("switch_1", globalPropertyi("sim/cockpit2/switches/custom_slider_on[0]"))  -- anim_menu
defineProperty("switch_20", globalPropertyi("sim/cockpit2/switches/custom_slider_on[1]")) -- switches menu
defineProperty("lang", globalPropertyi("sim/cockpit2/switches/custom_slider_on[2]")) -- language
defineProperty("switch_21", globalPropertyi("sim/cockpit2/switches/custom_slider_on[3]")) -- hide pilot
defineProperty("touchpanel", globalPropertyi("sim/cockpit2/switches/custom_slider_on[4]")) -- touch screen



defineProperty("paylweight", globalPropertyi("sim/flightmodel/weight/m_fixed")) -- payload weight


defineProperty("gears_status2", globalPropertyi("sim/cockpit2/controls/gear_handle_down"))
defineProperty("gears_status", globalPropertyi("sim/cockpit/switches/gear_handle_status"))
defineProperty("gears_dep_1", globalPropertyi("sim/aircraft/parts/acf_gear_deploy[0]"))
defineProperty("gears_dep_2", globalPropertyi("sim/aircraft/parts/acf_gear_deploy[1]"))


defineProperty("flaprat", globalPropertyf("sim/cockpit2/controls/flap_ratio"))
defineProperty("wingretract", globalPropertyf("sim/cockpit2/controls/wing_retraction_ratio"))


defineProperty("camera_type", globalPropertyi("sim/graphics/view/view_type"))

defineProperty("quater_1", globalPropertyf("sim/flightmodel/position/q[0]")) 
defineProperty("quater_2", globalPropertyf("sim/flightmodel/position/q[1]")) 
defineProperty("quater_3", globalPropertyf("sim/flightmodel/position/q[2]")) 
defineProperty("quater_4", globalPropertyf("sim/flightmodel/position/q[3]")) 

defineProperty("velocity_vx", globalPropertyf("sim/flightmodel/position/local_vx"))
defineProperty("velocity_vy", globalPropertyf("sim/flightmodel/position/local_vy"))
defineProperty("velocity_vz", globalPropertyf("sim/flightmodel/position/local_vz"))

defineProperty("park_brake", globalPropertyf("sim/flightmodel/controls/parkbrake")) -- parking brake
defineProperty("park_brake_handle", globalPropertyf("sim/cockpit2/controls/parking_brake_ratio")) -- parking brake handle


defineProperty("pos_x", globalPropertyf("sim/flightmodel/position/local_x"))
defineProperty("pos_y", globalPropertyf("sim/flightmodel/position/local_y"))
defineProperty("pos_z", globalPropertyf("sim/flightmodel/position/local_z"))

defineProperty("latitude", globalPropertyf("sim/flightmodel/position/latitude"))
defineProperty("longitude", globalPropertyf("sim/flightmodel/position/longitude"))
defineProperty("elevation", globalPropertyf("sim/flightmodel/position/elevation"))


defineProperty("engn_prop1", globalPropertyf("sim/flightmodel/engine/ENGN_prop[0]"))
defineProperty("engn_throt1", globalPropertyf("sim/cockpit2/engine/actuators/throttle_ratio[0]"))
defineProperty("proprot1", globalPropertyf("sim/flightmodel2/engines/prop_rotation_speed_rad_sec[0]"))
defineProperty("startup_running", globalPropertyi("sim/operation/prefs/startup_running"))
defineProperty("ignition1", globalPropertyi("sim/cockpit/engine/ignition_on[0]")) -- ignition 1 selector


defineProperty("fuelselector", globalPropertyi("sim/cockpit/engine/fuel_tank_selector")) -- fuel_selector

defineProperty("eng1mixt", globalPropertyf("sim/cockpit2/engine/actuators/mixture_ratio[0]")) -- engine 1 mixture for cut-off
defineProperty("eng2mixt", globalPropertyf("sim/cockpit2/engine/actuators/mixture_ratio[1]")) -- engine 2 mixture for cut-off

defineProperty("bat1", globalPropertyi("sim/cockpit2/electrical/battery_on[0]")) -- battery 1 on/off
defineProperty("generator", globalPropertyi("sim/cockpit2/electrical/generator_on[0]")) -- generator on/off
defineProperty("bat2", globalPropertyi("sim/cockpit2/electrical/battery_on[1]")) -- battery 2 on/off

defineProperty("avionics_power", globalPropertyf("sim/cockpit2/switches/avionics_power_on")) -- avionics power switch
defineProperty("autopilot", globalPropertyf("sim/cockpit/autopilot/autopilot_mode")) -- autopilot mode


defineProperty("taxi_light", globalPropertyf("sim/cockpit2/switches/taxi_light_on")) -- taxi light
defineProperty("land_light", globalPropertyf("sim/cockpit2/switches/landing_lights_on")) -- land light
defineProperty("beacon", globalPropertyi("sim/cockpit/electrical/beacon_lights_on")) -- beacon light on/off
defineProperty("nav_lights", globalPropertyi("sim/cockpit/electrical/nav_lights_on")) -- nav lights on/off
defineProperty("strobe_lights", globalPropertyi("sim/cockpit/electrical/strobe_lights_on")) -- strobe lights on/off

defineProperty("fueltank1", globalPropertyf("sim/flightmodel/weight/m_fuel[0]"))
defineProperty("fueltank2", globalPropertyf("sim/flightmodel/weight/m_fuel[1]"))
defineProperty("fueltank3", globalPropertyf("sim/flightmodel/weight/m_fuel[2]"))
defineProperty("fueltank4", globalPropertyf("sim/flightmodel/weight/m_fuel[3]"))

defineProperty("hydropump", globalPropertyi("sim/cockpit2/switches/electric_hydraulic_pump_on")) -- hydraulic pump


starter_1 = findCommand("sim/starters/engage_starter_1")  -- simulator command for starter 1
starter_2 = findCommand("sim/starters/engage_starter_2")  -- simulator command for starter 2

local quater_1_last = get(quater_1)
local quater_2_last = get(quater_2)
local quater_3_last = get(quater_3)
local quater_4_last = get(quater_4)
local park_brake_last = 0
local position_saving_last = get(position_saving)
local livery_last = get(livery)
local camera_last = get(camera_type)
local elevation_last = get(elevation)
local longitude_last = get(longitude)
local latitude_last = get(latitude)
local gears_dep_1_last = get(gears_dep_1)
local gears_dep_2_last = get(gears_dep_2)
local vinet_last = get(vinet)

local anim_menu = get(switch_1)
local switch_menu = get(switch_20)
local hide_pilot = get(switch_21)

set(fuelselector, 4)
set(hydropump, 1)

local tab = {}
-- read file
local filename = panelDir .. "/sh2.ini"
local file = io.open(filename, "r")
print(file)
if file then
 local lines = file:read("*a")
 for k, v in string.gmatch(lines, "(%w+) = (%C+)") do
  tab[k] = v
 end
 file:close()

end

set(vinet, tab["vinet"])
set(position_saving, tab["positionsaving"])
set(lang, tab["language"])
set(touchpanel, tab["touchpanel"])

local rpm1 = tonumber(tab["proprot1"])


function update()

if get(position_saving) > 0.5 then
if get(flight_time) > 0 and get(flight_time) < 0.5 then
set(park_brake_handle, 0)
end
end

-- Position saving functions

local x, y, z = worldToLocal(tab["latitude"], tab["longitude"], tab["elevation"])

if get(flight_time) > 0.5 and get(flight_time) < 1 then

	set(park_brake_handle, tab["parkbrake"])
	set(park_brake, tab["parkbrake"])

end


 if get(position_saving) > 0.5 then
   if get(flight_time) > 0 then
	if get(flight_time) < 1.5 then
	set(velocity_vx, tab["velocityvx"])
	set(velocity_vy, tab["velocityvy"])
	set(velocity_vz, tab["velocityvz"])
	set(engn_prop1, tab["engnprop1"])
	set(engn_throt1, tab["engnthrot1"])
	set(ignition1, tab["ignition1"])
	set(eng1mixt, tab["eng1mixt"])
	set(bat1, tab["bat1"])
	set(paylweight, tab["paylweight"])
	end
    end
 end

if get(position_saving) > 0.5 then
 if get(startup_running) < 0.5 then
    if rpm1 > 30 then
       if get(flight_time) > 0 then
        if get(flight_time) < 5 then
--	    if get(ground_speed) < 40 then
	       commandOnce(starter_1)
--	    end
	 end
       end
     end
   end
end




 if get(position_saving) > 0.5 then
    if get(flight_time) > 0 then
	if get(flight_time) < 5 then
		set(quater_1, tab["quater1"])
		set(quater_2, tab["quater2"])
		set(quater_3, tab["quater3"])
		set(quater_4, tab["quater4"])
	end
    end
 end


 if get(position_saving) > 0.5 then
    if get(flight_time) > 0 then
	if get(flight_time) < 1 then

		set(pos_x, x)
		set(pos_y, y)
		set(pos_z, z)

		set(livery, tab["livery"])
		set(switch_1, tab["animationmenu"])
		set(switch_20, tab["switchesmenu"])
	        set(switch_21, tab["hidepilot"])
		set(camera_type, tab["cameratype"])
		set(gears_status2, tab["gearsdep1"])
		set(gears_status, tab["gearsdep1"])
		set(gears_dep_1, tab["gearsdep1"])
		set(gears_dep_2, tab["gearsdep2"])
		set(livery, tab["livery"])
		set(autopilot, tab["autopilot"])
		set(fueltank1, tab["fueltank1"])
		set(fueltank2, tab["fueltank2"])
		set(fueltank3, tab["fueltank3"])
		set(fueltank4, tab["fueltank4"])
		set(flaprat, tab["flaprat"])
	end
    end
 end



-- save ini file


local tab = {}
tab["animationmenu"] = get(switch_1)
tab["switchesmenu"] = get(switch_20)
tab["cameratype"] = get(camera_type)
tab["elevation"] = get(elevation)
tab["longitude"] = get(longitude)
tab["latitude"] = get(latitude)
tab["gearsdep1"] = get(gears_dep_1)
tab["gearsdep2"] = get(gears_dep_2)
tab["positionsaving"] = get(position_saving)
tab["vinet"] = get(vinet)
tab["parkbrake"] = get(park_brake)
tab["quater1"] = get(quater_1)
tab["quater2"] = get(quater_2)
tab["quater3"] = get(quater_3)
tab["quater4"] = get(quater_4)
tab["livery"] = get(livery)
tab["velocityvx"] =  get(velocity_vx)
tab["velocityvy"] =  get(velocity_vy)
tab["velocityvz"] =  get(velocity_vz)
tab["engnprop1"] = get(engn_prop1)
tab["engnthrot1"] = get(engn_throt1)
tab["ignition1"] = get(ignition1)
tab["eng1mixt"] = get(eng1mixt)
tab["bat1"] = get(bat1)
tab["autopilot"] = get(autopilot)
tab["fueltank1"] = get(fueltank1)
tab["fueltank2"] = get(fueltank2)
tab["fueltank3"] = get(fueltank3)
tab["fueltank4"] = get(fueltank4)
tab["proprot1"] = get(proprot1)
tab["flaprat"] = get(flaprat)
tab["hidepilot"] = get(switch_21)
tab["strobelights"] = get(strobe_lights) 
tab["paylweight"] = get(paylweight) 
tab["touchpanel"] = get(touchpanel) 
tab["language"] = get(lang)

if get(flight_time) > 5 then
if get(local_time_sec) == 29 or get(local_time_sec) == 44 or get(local_time_sec) == 59 or get(local_time_sec) == 14 or position_saving_last ~= get(position_saving) or park_brake_last ~= get(park_brake) or camera_last ~= get(camera_type) then
  if vinet_last ~= get(vinet) or anim_menu ~= get(switch_1) or switch_menu ~= get(switch_20) or hide_pilot ~= get(switch_21) or elevation_last ~= get(elevation) or longitude_last ~= get(longitude) or latitude_last ~= get(latitude) or gears_dep_1_last ~= get(gears_dep_1) or gears_dep_2_last ~= get(gears_dep_2) or quater_1_last ~= get(quater_1) or quater_2_last ~= get(quater_2) or quater_3_last ~= get(quater_3) or quater_4_last ~= get(quater_4) or livery_last ~= get(livery) then
	local filename = panelDir .. "/sh2.ini"
	local savefile = io.open(filename, "w")
	for k, v in pairs(tab) do savefile:write(k," = ",v,"\n") end
	savefile:close()

	 vinet_last = get(vinet)
	 anim_menu = get(switch_1)
	 switch_menu = get(switch_20)
	 hide_pilot = get(switch_21)
	 camera_last = get(camera_type)
	 elevation_last = get(elevation)
	 longitude_last = get(longitude)
	 latitude_last = get(latitude)
	 gears_dep_1_last = get(gears_dep_1)
	 gears_dep_2_last = get(gears_dep_2)
	 quater_1_last = get(quater_1)
	 quater_2_last = get(quater_2)
	 quater_3_last = get(quater_3)
	 quater_4_last = get(quater_4)
	 park_brake_last = get(park_brake)
	 position_saving_last = get(position_saving)
	 livery_last = get(livery)
	end
      end
   end
end