local function RemoveEntity( ent )

	if (ent:IsValid()) then
		ent:Remove()
	end

end

function TOOL:LeftClick( trace )

	if (!trace.Entity) then return false end
	if (!trace.Entity:IsValid()) then return false end
	if (trace.Entity:IsPlayer()) then return false end

	local effectdata = EffectData()
		effectdata:SetEntity( trace.Entity )
		effectdata:SetOrigin( trace.HitPos )
	util.Effect( "entity_remove", effectdata )
	
	// Client can bail out now.
	if ( CLIENT ) then
		return true 
	end
	
	timer.Simple( 1.0, RemoveEntity, trace.Entity )
	trace.Entity:SetSolid( SOLID_NONE )
	trace.Entity:SetMoveType( MOVETYPE_NONE )
	trace.Entity:SetNoDraw( true )
	
	print(self:GetOwner():Nick(),"Removed", trace.Entity:GetClass())
	
	return true
		
end

-- Alt fire deletes object and all objects constrained to it.
-- TODO make a modal confirmation box for this action.
--     Why?
function TOOL:RightClick( trace )

	if (!trace.Entity) then return false end
	if (!trace.Entity:IsValid()) then return false end
	if (trace.Entity:IsPlayer()) then return false end
	
	// Client can bail out now.
	if ( CLIENT ) then return true end
	
	// Loop through all the entities in the system
	for _,ent in pairs(duplicator.GetEnts(trace.Entity)) do
		if ent:IsValid()  &&
		  !ent:IsPlayer() &&
		   ent:EntIndex() != 0
		then
		
			local effectdata = EffectData()
				effectdata:SetEntity( ent )
				effectdata:SetOrigin( trace.HitPos )
			util.Effect( "entity_remove", effectdata, true, true )
			
			timer.Simple( 1.0, RemoveEntity, ent )
			ent:SetSolid( SOLID_NONE )
			ent:SetMoveType( MOVETYPE_NONE )
			ent:SetNoDraw( true )
	
			print(self:GetOwner():Nick(),"Removed", ent:GetClass())
			
		end
	end
	
	return true
end
