local fx_name = ""
local preset_name = ""
local inipath = reaper.GetResourcePath()
local  pathfull =inipath  .. "\\ini\\dj fx.ini"
num1, num2 = reaper.BR_Win32_GetPrivateProfileString("info", "val", "", pathfull)
numm, fx_name  = reaper.BR_Win32_GetPrivateProfileString(num2, "f7", "", pathfull)
numm, preset_name   = reaper.BR_Win32_GetPrivateProfileString(num2, "p7", "", pathfull)
if (fx_name == "") then
return
end
status=0
 num_of_tracks = reaper.CountTracks(0)
    if num_of_tracks  == 0 then  return end;
local path = reaper.GetResourcePath()
V1, V2 = reaper.BR_Win32_GetPrivateProfileString("info", "master", "nil", path .. "\\ini\\dj fx.ini")
if (tonumber(V2) == 1) then
track = reaper.GetMasterTrack(0)
else
track = reaper.GetLastTouchedTrack();
end
    count_FX = reaper.TrackFX_GetCount(track)    
if (    count_FX >0) then
for i=1, count_FX do
			retval, fx_name2 = reaper.TrackFX_GetFXName(track, i-1, "")
			fx_name2 = fx_name2:gsub("DX: ", "")
			fx_name2 = fx_name2:gsub("DXi: ", "")
			fx_name2 = fx_name2:gsub("VST: ", "")
			fx_name2 = fx_name2:gsub("VSTi: ", "")
			fx_name2 = fx_name2:gsub("VST3: ", "")
			fx_name2 = fx_name2:gsub("VST3i: ", "")
			fx_name2 = fx_name2:gsub("JS: ", "")
			fx_name2 = fx_name2:gsub("ReWire: ", "")
if (fx_name2 == fx_name) then
status=1

if (status ==1) then
    count_FX = reaper.TrackFX_GetCount(track)    
for i=1, count_FX do
			retval, fx_name2 = reaper.TrackFX_GetFXName(track, i-1, "")
			fx_name2 = fx_name2:gsub("DX: ", "")
			fx_name2 = fx_name2:gsub("DXi: ", "")
			fx_name2 = fx_name2:gsub("VST: ", "")
			fx_name2 = fx_name2:gsub("VSTi: ", "")
			fx_name2 = fx_name2:gsub("VST3: ", "")
			fx_name2 = fx_name2:gsub("VST3i: ", "")
			fx_name2 = fx_name2:gsub("JS: ", "")
			fx_name2 = fx_name2:gsub("ReWire: ", "")
if (fx_name2 == fx_name) then
reaper.TrackFX_Delete(track, i-1)
end
end
end
return
end
end
end

if (status ==0) then
if not fx_name:match';' then reaper.TrackFX_AddByName(track, fx_name, 0, -1)
else
  for name in fx_name:gmatch'(.-);' do reaper.TrackFX_AddByName(track, name, 0, -1) end
  if fx_name:sub(-1) ~= ';' then reaper.TrackFX_AddByName(track, fx_name:match'.*;(.*)', 0, -1) end
end

local timer2 =  reaper.NamedCommandLookup("_RS6af3821bf11e0869caf8985bc479dd0d80b39d4d")
		reaper.Main_OnCommand(timer2, 0)
if not (preset_name == "") then
    count_FX = reaper.TrackFX_GetCount(track)    
for i=1, count_FX do
			retval, fx_name2 = reaper.TrackFX_GetFXName(track, i-1, "")
			fx_name2 = fx_name2:gsub("DX: ", "")
			fx_name2 = fx_name2:gsub("DXi: ", "")
			fx_name2 = fx_name2:gsub("VST: ", "")
			fx_name2 = fx_name2:gsub("VSTi: ", "")
			fx_name2 = fx_name2:gsub("VST3: ", "")
			fx_name2 = fx_name2:gsub("VST3i: ", "")
			fx_name2 = fx_name2:gsub("JS: ", "")
			fx_name2 = fx_name2:gsub("ReWire: ", "")
if (fx_name2 == fx_name) then
  reaper.TrackFX_SetPreset(track, i-1, preset_name)
return
end
end
end
end
