input_side = 'top'
output_side = 'bottom'
delay = 10.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 >= 9 then
            ran = true
            count = math.floor(stack.qty/9)
            print('Packing '..tostring(count*9)..' '..stack.name..' from slot '..tostring(i))
            input.pushItemIntoSlot('down', i, count, 1)
            input.pushItemIntoSlot('down', i, count, 2)
            input.pushItemIntoSlot('down', i, count, 3)
            
            input.pushItemIntoSlot('down', i, count, 5)
            input.pushItemIntoSlot('down', i, count, 6)
            input.pushItemIntoSlot('down', i, count, 7)
            
            input.pushItemIntoSlot('down', i, count, 9)
            input.pushItemIntoSlot('down', i, count, 10)
            input.pushItemIntoSlot('down', i, count, 11)
            
            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()

for i=1,16 do if turtle.getItemCount(i)>0 then turtle.select(i) turtle.dropDown() end end

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