obs           				= obslua
luafileTitle				= "Comment Removal"
gversion 					= 0.2
luafile						= ""
obsurl						= ""
p_settings 					= nil
icon="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAqCAYAAADS4VmSAAAInklEQVRYhcVYeWwcVxn/3ps3O7vey7ub+Nht4nWSmq5zOtRJnAYqVUL8UYFAKWoRLaqEhBBIIFIpVPQPkAIUCEHiEH8gFIQoAlFxSM0f4RBqlDSpQIHU1EnsOFkfcezsxvYe3pnZmdn30DfHeux47QRxfLI1xzu+6/d+3zdLzpz5AwwODvywo2Pz5+A/KJzz6YmJqWeHh0cuhUJBwhgDIcR9CsiNGzf7d+zYNlKr1UqSJF1mjEmWZRFJkgQuwDWUUmg0LEKpJAjBzQWhlOA4ASDuO04opbikIYTIKUogDQDGtWujfVevjk5GoxEcJwDgWYH3wEzTiuGNYZinrlz5+9c8KyWJ2srxGQ3gvAGEUCCEoDL72vTCfYdiWRbs3Jn7VDrd9RMACORy77moafqjk5PTaiwWIb4g2HeMc27gDaU0Wq0uARopSU64PB24yFEo3HtoXpfH7ciAYRggy6zdF+X0wMCe86qqvbdYLPJ4PE45TnQiINyoADQaDSrLDAKBAJFlRgIBmciy8+/cM9+9vMa4cw0EZNufFXkmZP/+/Xtfa2sLw9JSjRNf+NAaJxeMgSRJfqAQ/x4PCk9c7qXDL21toU8cOXLolCzLoGk692ygCCI3Am6em7r8kL0fvi0E1yNm1pJwuO3Y0NDgMVRhGAZ35zo5cBQ/sKPrRECAk+K1pb09fmpgYM8ziBXTNO0UOKGgZAWy/11rcAuywcp0uvv1Xbv6D9XrhmCEgC8F3MaB58zDKvfwoyhKcqO527f3vqlp2k7m2YtHD3O3CoQrSGMjoxzC4rCwsHguHo8dBQA8Ety3l3cNA0BnLBZ7iglXIwLQI521nHvQKCDt5vNTZ4vFe31IJU5guH3KkKQ0TYfHHnv0Y93dXb/mnOusFWIfRqlDWqQZBQRXoVD07UGaxKZpGqjqI7q7lDO0zp7i0uxaBWNVOp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goXAP+0qQZYn4WrxmTfCTKvrMfPW/2fWunulGBA3BVkoUCkU+Ozv3PTRWlgMUc2qapqWqKrZdxL/OL5hzBJ7Xh9p48EAIAJqq6sgHwvkoWW6/cS9/K+6A2RmwrMaKDlTTtPvq2fI+QqiqhosrbgqA4bFCCQTk9t7enihjzC3RwvdRwr3cuV2T07ggkThh5s1mBgGJnS/WDW8fVITvTBP7QMvq7Nzc6UYAMeBYGQ6HPzMwsOdFX08v1ugLxaruqNX4evPQm4ATD95gvnjJ7v//UAhQLwX/D7GLEXYz68nSUu1vqqq+65tSNgzjTc65Bcukcg4A7noTkPk0TZ/cyCfEC12LCf1SrS4dU1Xt294rTdNH79yZewZtA9eLmZk7z9Vq6gV3fHF0dPzDlUr1xEYGIIDpei2UowBSACS+/EwClNJOD6zuycD6bwPLsqzTXV0duxmTFgDAXG9vjB5t0YQ2RQiBpNAMk9PHEclb5rAnlSilzH3+U1dX50UhRKJWU3+2vnPk/nK8Wuww0eVTxLkwKZWwlVK8TfCPUprSNM3K5ycv1+v1twzDHCmVSr/b0IBGo7Hul5xlNRqEkLblRdCpqmoBQDBwfxHRNH1ekmiOc/HT9vZ4BICcCQaVPeVy5Wqj0TBa7W2DcGUtWGuSVdY07Xy9blzQ9fo5RVE6FEXpmZ2de9o0zRvV6tInk8lEQpKk6PT07S+kUonvB4PKD5LJxI9DoVB5fn7heKu98QA0a3QrSSaTL4RCoYiu699dWqqdsiwrn8l0/1lRFF4szr8UCATmksnEWcMw7yaTiWcjkciHvK1SqeQJSlsTDVI5jIxcOygeQvD3HU845zXfSvNh9kHJ5yefx7ZcWzcHq2TVr2NNbGBEH2YfFLvMF4v3RrZsyfw8HG572vc5/d8U9IBzzocXFxfP/gtqQwz9/J6x4gAAAABJRU5ErkJggg=="
desc	    				= [[
<hr/><center><h2>]] .. luafileTitle ..[[</h2>(Version: %s)</center>
<br><center><img width=32 height=42 src=']] .. icon .. [['/></center>
<br><center><a href="https://github.com/midnight-studios/obs-lua/blob/main/]] .. luafile ..[[">Find it on GitHub</a></center>
<br><p>Removes all comments in LUA file</p>
<p>
<ol>
<li><tt>`-- your comments`</tt></li>
<li><tt>`--[[ your comments ]`</tt></li>
</ol></p>
<p>Find help on the <a href="https://obsproject.com/forum/resources/]] .. obsurl ..[[">OBS Forum Thread</a>.</p>
<hr/>]]
script_settings = nil
script_props = nil
formats={
	["lua"]={
		name="Lua",
		single="%-%-[^%[%[\n\r%]%]]+",
		multi="%-%-%[%[.-%]%]" 
	},
}

function script_description()
	return string.format( desc, tostring( gversion ) )
end

function clean_content( txt, patt )
	if patt.single then
		txt=txt:gsub( patt.single, "" ) 
	end
	if patt.multi then
		txt=txt:gsub( patt.multi, "" )
	end
	txt=txt:gsub( "(\r?\n)%s*\r?\n","\n" )
	txt=txt:gsub( "^\n","" )
	txt=txt:gsub( "\n$","" )
	return txt
end

function file_to_clean( fileName )
	if fileName ~= nil then
		local f = io.open( fileName, "r" )
		local txt = f:read( "*a" )
		-- closes the open file
		f:close()	
		local extension = fileName:match( "%.(.-)$" )
		if not extension then
			return nil
		end
		extension=extension:lower()
		if formats[extension] then
			return clean_content( txt, formats[extension] )
		else
			return nil
		end
	else
		return nil
	end
end

function get_filenames( path )
	local filenames = {}
	local dir = obs.os_opendir( path )
	local entry
	repeat
	  entry = obs.os_readdir(dir)
	  if entry then
		local ext = obs.os_get_path_extension( entry.d_name )
		if ext == ".lua" then
			local  filename = string.gsub( entry.d_name, ext, "" )
			table.insert( filenames, filename )
	  	end
	  end
	until not entry
	obs.os_closedir( dir )
	return filenames
end

function clean_file( )
	path = obs.obs_data_get_string( script_settings, "path" )
	file = obs.obs_data_get_string( script_settings, "file" )
	local unique_stamp = string.format('%s%s' .. os.date("%d.%m.%Y.%H.%M.%S") .. '.%s%s',' no-comments ', '(', string.sub(math.floor( (obs.os_gettime_ns()/100000000) ), 5, 7), ')')
	local inputfile = path .. "/" .. file .. ".lua"
	local outputfile = path .. "/" .. file .. unique_stamp .. ".lua"
	if obs.os_file_exists(inputfile) then
		f = file_to_clean(inputfile)
	else
		print('File does not exist! (' .. inputfile .. ')')
	end
	if f ~= nil then	
			newfile = io.open(outputfile, "w")
			newfile:write(f)
			newfile:close()	
	end
end

function action_button_clicked( props, p )
	on_action( true )
	return false
end

function on_action( pressed )
	if not pressed then
		return
	end
	clean_file()
end

function property_onchange( props, property, settings )
	local file = obs.obs_data_get_string( settings, "file" )
	local path = obs.obs_data_get_string( settings, "path" )
	local file_prop = obs.obs_properties_get( props, "file" )
	local notice_prop = obs.obs_properties_get( props, "notice" )
	local filenames = get_filenames( path )
	local has_file_list = (table.getn( filenames ) > 0)
	local button = obs.obs_properties_get( props, "action_button" )
	local inputfile = path .. "/" .. file .. ".lua"
	local has_file = obs.os_file_exists(inputfile)
	obs.obs_property_set_visible( obs.obs_properties_get( props, "action_button" ), false )
	obs.obs_property_set_visible( obs.obs_properties_get( props, "action_button" ), has_file )
	obs.obs_property_list_clear( file_prop )
	obs.obs_property_list_add_string( file_prop, 'Select', 'select' )
	if path ~= "" then
		if has_file_list then
			obs.obs_data_set_string( settings, "notice", "Folder selected, please select the file from the dropdown list.")
		else
			obs.obs_data_set_string( settings, "notice", "The selected folder does not contain any valid lua files.\nPlease select a valid directory.") 
		end		
	else
		obs.obs_data_set_string( settings, "notice", "Please select a folder containing your lua files.")
	end	
	if file ~= "select" then
		if has_file then
			obs.obs_data_set_string( settings, "notice", "File ready for processing, please click the 'Execute' button.")
		else
			obs.obs_data_set_string( settings, "notice", "Cannot find the file you selected. \nPlease check that the file exist.")
		end	
	end
	if has_file_list then
  		for i,v in pairs( filenames ) do 
			obs.obs_property_list_add_string( file_prop, v, v ) 
		end
	end
  return true
end	

function script_properties()
	local props = obs.obs_properties_create()
    local notice = obs.obs_properties_add_text(props, "notice", "Notice:", obs.OBS_TEXT_MULTILINE)	
	local p_a = obslua.obs_properties_add_path(props, "path", "Select Folder", obs.OBS_PATH_DIRECTORY,
    nil, nil)
	local p_b = obs.obs_properties_add_list( props, "file", "<i>Select file</i>", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING )
	obs.obs_property_list_add_string( p_b, 'Select', 'select' )
	obs.obs_property_set_long_description( p_b, "\nSelect the file to remove the comments.\n" )
	local filenames = get_filenames( path )
	if table.getn( filenames ) > 0 then
  		for i,v in pairs( filenames ) do 
			obs.obs_property_list_add_string( p_b, v, v )
		end
	end
	local p_c = obs.obs_properties_add_button( props, "action_button", "Execute", action_button_clicked )
	obs.obs_property_set_visible( p_c, false )
    obs.obs_property_set_enabled(notice, false)
  	obs.obs_property_set_modified_callback(p_a, property_onchange )
  	obs.obs_property_set_modified_callback(p_b, property_onchange )
	script_props = props
	return props
end	

function script_update( settings )
	script_settings = settings
end

function script_defaults( settings )
	obs.obs_data_set_default_string( settings, "path", "" )
	obs.obs_data_set_default_string( settings, "file", "Select" )
	obs.obs_data_set_default_string( settings, "notice", "To get started, please select the file folder." )
end