

function delete_node(pos, direction_from)
    if( not is_wire(pos)) then 
	 return
    end


    local node = minetest.get_node(pos)
    local name = node.name
    local param2 = node.param2

    if( (name == "mesecons_insulated:insulated_off")  or  (name == "mesecons_insulated:insulated_on")  ) then
	-- wire
	if( (param2 == 0 ) or (param2 == 2) ) then
	    if( direction_from == "u" ) then
		--nothing
		return
	    elseif( direction_from == "d" ) then
		--nothing
		return
	    elseif( direction_from == "r" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = { x = pos.x - 1, y = pos.y, z = pos.z }
		delete_node(next_pos,"r") 
		return
	    elseif( direction_from == "l" ) then	
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x+1,y=pos.y,z=pos.z}
		delete_node(next_pos,"l")
		return
	    end
	end
	if( (param2 == 1 ) or (param2 == 3) ) then
	    if( direction_from == "l" ) then
		--nothing
		return
	    elseif( direction_from == "r" ) then
		--nothing
		return
	    elseif( direction_from == "u" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = { x = pos.x , y = pos.y, z = pos.z -1}
		delete_node(next_pos,"u") 
		return
	    elseif( direction_from == "d" ) then	
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x,y=pos.y,z=pos.z+1}
		delete_node(next_pos,"d")
		return
	    end
	end



	
    elseif (name == "mesecons_extrawires:corner_off") or (name == "mesecons_extrawires:corner_on")  then
	-- corner
	if( param2 == 0 ) then
	    if( direction_from == "u" ) then
		--nothing
		return
	    elseif(direction_from == "r" ) then
		--nothing
		return
	    elseif(direction_from == "l" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x,y=pos.y,z=pos.z-1}
		delete_node(next_pos,"u")
		return
	    elseif(direction_from == "d" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x-1,y=pos.y,z=pos.z}
		delete_node(next_pos,"r")
		return
	    end
	elseif (param2 == 1) then
	    if( direction_from == "d" ) then
		--nothing
		return
	    elseif(direction_from == "r" ) then
		--nothing
		return
	    elseif(direction_from == "l" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x,y=pos.y,z=pos.z+1}
		delete_node(next_pos,"d")
		return
	    elseif(direction_from == "u" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x-1,y=pos.y,z=pos.z}
		delete_node(next_pos,"r")
		return
	    end
	elseif( param2 == 2 ) then
	    if( direction_from == "l" ) then
		--nothing
		return
	    elseif(direction_from == "d" ) then
		--nothing
		return
	    elseif(direction_from == "u" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x+1,y=pos.y,z=pos.z}
		delete_node(next_pos,"l")
		return
	    elseif(direction_from == "r" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x,y=pos.y,z=pos.z+1}
		delete_node(next_pos,"d")
		return
	    end
	elseif( param2 == 3 ) then
	    if( direction_from == "l" ) then
		--nothing
		return
	    elseif(direction_from == "u" ) then
		--nothing
		return
	    elseif(direction_from == "d" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x+1,y=pos.y,z=pos.z}
		delete_node(next_pos,"l")
		return
	    elseif(direction_from == "r" ) then
		minetest.set_node(pos,{name="air"})
		local next_pos = {x=pos.x,y=pos.y,z=pos.z-1}
		delete_node(next_pos,"u")
		return
	    end
	end
    elseif( name=="mesecons_extrawires:crossover_off") then
	    if( direction_from == "l" ) then
		local next_pos = {x=pos.x+1,y=pos.y,z=pos.z}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_off", param2=1})
		delete_node(next_pos,"l")
		return
	    elseif(direction_from == "u" ) then
		local next_pos = {x=pos.x,y=pos.y,z=pos.z-1}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_off", param2=0})
		delete_node(next_pos,"u")
		return
	    elseif(direction_from == "d" ) then
		local next_pos = {x=pos.x,y=pos.y,z=pos.z+1}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_off", param2=0})
		delete_node(next_pos,"d")
		return
	    elseif(direction_from == "r" ) then
		local next_pos = {x=pos.x-1,y=pos.y,z=pos.z}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_off", param2=1})
		delete_node(next_pos,"r")
		return
	    end

    elseif (name=="mesecons_extrawires:crossover_10") then
	    if( direction_from == "l" ) then
		local next_pos = {x=pos.x+1,y=pos.y,z=pos.z}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_on", param2=1})
		delete_node(next_pos,"l")
		return
	    elseif(direction_from == "u" ) then
		local next_pos = {x=pos.x,y=pos.y,z=pos.z-1}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_off", param2=0})
		delete_node(next_pos,"u")
		return
	    elseif(direction_from == "d" ) then
		local next_pos = {x=pos.x,y=pos.y,z=pos.z+1}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_off", param2=0})
		delete_node(next_pos,"d")
		return
	    elseif(direction_from == "r" ) then
		local next_pos = {x=pos.x-1,y=pos.y,z=pos.z}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_on", param2=1})
		delete_node(next_pos,"r")
		return
	    end

    elseif (name=="mesecons_extrawires:crossover_01") then
	    if( direction_from == "l" ) then
		local next_pos = {x=pos.x+1,y=pos.y,z=pos.z}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_off", param2=1})
		delete_node(next_pos,"l")
		return
	    elseif(direction_from == "u" ) then
		local next_pos = {x=pos.x,y=pos.y,z=pos.z-1}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_on", param2=0})
		delete_node(next_pos,"u")
		return
	    elseif(direction_from == "d" ) then
		local next_pos = {x=pos.x,y=pos.y,z=pos.z+1}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_on", param2=0})
		delete_node(next_pos,"d")
		return
	    elseif(direction_from == "r" ) then
		local next_pos = {x=pos.x-1,y=pos.y,z=pos.z}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_off", param2=1})
		delete_node(next_pos,"r")
		return
	    end

    elseif (name=="mesecons_extrawires:crossover_on")  then
	    if( direction_from == "l" ) then
		local next_pos = {x=pos.x+1,y=pos.y,z=pos.z}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_on", param2=1})
		delete_node(next_pos,"l")
		return
	    elseif(direction_from == "u" ) then
		local next_pos = {x=pos.x,y=pos.y,z=pos.z-1}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_on", param2=0})
		delete_node(next_pos,"u")
		return
	    elseif(direction_from == "d" ) then
		local next_pos = {x=pos.x,y=pos.y,z=pos.z+1}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_on", param2=0})
		delete_node(next_pos,"d")
		return
	    elseif(direction_from == "r" ) then
		local next_pos = {x=pos.x-1,y=pos.y,z=pos.z}
		minetest.set_node(pos,{name="mesecons_insulated:insulated_on", param2=1})
		delete_node(next_pos,"r")
		return
	    end

    elseif (name=="mesecons_morewires:xjunction_off")  then
	    if( direction_from == "l" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:tjunction_off", param2=3})
		return
	    elseif(direction_from == "u" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:tjunction_off", param2=0})
		return
	    elseif(direction_from == "d" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:tjunction_off", param2=2})
		return
	    elseif(direction_from == "r" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:tjunction_off", param2=1})
		return
	    end

    elseif (name=="mesecons_morewires:xjunction_on")  then
	    if( direction_from == "l" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:tjunction_on", param2=3})
		return
	    elseif(direction_from == "u" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:tjunction_on", param2=0})
		return
	    elseif(direction_from == "d" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:tjunction_on", param2=2})
		return
	    elseif(direction_from == "r" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:tjunction_on", param2=1})
		return
	    end
	
    elseif( name == "mesecons_extrawires:tjunction_off"  ) or ( name == "mesecons_extrawires:tjunction_on"  )then
	local state = "on"
	if ( name == "mesecons_extrawires:tjunction_off" ) then
	    state = "off"
	end
	if( param2 == 0 ) then
	    if( direction_from == "l" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:corner_"..state, param2 = 3})
		return
	    elseif(direction_from == "u" ) then
		--nothing
		return
	    elseif(direction_from == "d" ) then
		minetest.set_node(pos,{name="mesecons_insulated:insulated_"..state, param2 = 0})
		return
	    elseif(direction_from == "r" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:corner_"..state, param2 = 0})
		return
	    end

	elseif( param2 == 1 ) then
	    if( direction_from == "u" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:corner_"..state, param2 = 0})
		return
	    elseif(direction_from == "r" ) then
		--nothing
		return
	    elseif(direction_from == "l" ) then
		minetest.set_node(pos,{name="mesecons_insulated:insulated_"..state, param2 = 1})
		return
	    elseif(direction_from == "d" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:corner_"..state, param2 = 1})
		return
	    end

	elseif( param2 == 2 ) then
	    if( direction_from == "l" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:corner_"..state, param2 = 2})
		return
	    elseif(direction_from == "d" ) then
		--nothing
		return
	    elseif(direction_from == "u" ) then
		minetest.set_node(pos,{name="mesecons_insulated:insulated_"..state, param2 = 0})
		return
	    elseif(direction_from == "r" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:corner_"..state, param2 = 1})
		return
	    end

	elseif( param2 == 3) then
	    if( direction_from == "u" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:corner_"..state, param2 = 3})
		return
	    elseif(direction_from == "l" ) then
		--nothing
		return
	    elseif(direction_from == "r" ) then
		minetest.set_node(pos,{name="mesecons_insulated:insulated_"..state, param2 = 1})
		return
	    elseif(direction_from == "d" ) then
		minetest.set_node(pos,{name="mesecons_extrawires:corner_"..state, param2 = 2})
		return
	    end

	end
    end
end



function delete(pos)
--    local p = get_pins(pos)
    local p = get_pins_sticky(pos)
	if(p.u==1) then	
	    delete_node(up_node(pos),"d")
	end
	if(p.d==1) then
	    delete_node(down_node(pos),"u")
	end
	if(p.l==1) then
	    delete_node(left_node(pos),"r")
	end
	if(p.r==1) then
	    delete_node(right_node(pos),"l")
	end
    
    minetest.set_node(pos,{ name = "air" } )
	
end

local function delete_all_selection(pos1,pos2)
        local xf = math.min(pos1.x,pos2.x)
        local xt = math.max(pos1.x,pos2.x)
        local zf = math.min(pos1.z,pos2.z)
        local zt = math.max(pos1.z,pos2.z)
        
        for i=xf,xt do
                for k = zf,zt do
                        local cur = { x=i,z=k,y=pos1.y}
                        minetest.set_node(cur,{name="air"})
                end
        end
        
        
end


minetest.register_craftitem("mesecons_autowire:delete", {
    description = "Tool Autodelete",
    inventory_image = "tool_autodelete.png",
    stack_max = 1,
    groups = { tool =1  },
    
    on_place = function(itemstack, user, pointed_thing)
	local pos = pointed_thing.under
        if pointed_thing.type == "node" then
                if( is_wire(pos) ) then
                        delete(pos)
                elseif( is_sticky(pos) ) then
                        delete(pos)
                end
        end
        
    end,
    on_secondary_use = function(itemstack, user, pointed_thing)
            if pointed_thing.type == "nothing" then
                    if check_selection(mesecons_automove.pos1,mesecons_automove.pos2) then
                            delete_all_selection(mesecons_automove.pos1,mesecons_automove.pos2)
                    end
                    
            end
            
    end
    
    

})
