input_side = 'top'
output_side = 'bottom'
delay = 11.1


function list(side)
    local ml = peripheral.getMethods(side)
    for k,v in pairs(ml) do
        print(k..': '..v)
    end
end

function list_amd(side)
    local pl = peripheral.wrap(side)
    ml = pl.getAdvancedMethodsData()
    for k,v in pairs(ml) do
        print(k..': '..textutils.serialize(v))
    end
end

function show_in(side, slot)
    local pl = peripheral.wrap(side)
    local stack = pl.getStackInSlot(slot)
    print(textutils.serialize(stack))
end
 
function info(side)
    if not peripheral.isPresent(side) then
        print('No peripheral on side '..side)
    end
    print(peripheral.getType(side))
    local ml = peripheral.getMethods(side)
    local p = peripheral.wrap(side)
    for i,v in pairs(ml) do
        if v == 'listMethods' or v == 'getAdvancedMethodsData' then else
            print(v)
        end
    end
end

function pack()
    input.condenseItems()
    local ran = false
    for i=1,input_size do
        stack = input.getStackInSlot(i)
        if stack ~= nil and stack.qty >= 4 then
            ran = true
            count = math.floor(stack.qty/4)
            print('Packing '..tostring(count*4)..' '..stack.name..' from slot '..tostring(i))
            input.pushItemIntoSlot('down', i, count, 1)
            input.pushItemIntoSlot('down', i, count, 2)
            
            input.pushItemIntoSlot('down', i, count, 5)
            input.pushItemIntoSlot('down', i, count, 6)
            
            turtle.craft()
            turtle.dropDown()
        end
    end
    return ran
end

--info('top')
--show_in('top', 1)
--show_in('top', 2)

input = peripheral.wrap(input_side)
output = peripheral.wrap(output_side)
input_size = input.getInventorySize()

while true do
    if pack() then
        print('Waiting for new stuff to pack...')
    else
        os.sleep(delay)
    end
end
