MAXREPEATS = 32

----------------------------

function main()
    status("Setting up...")
    placeBlocks()
    
    status("Configuring your Miner...")
    os.sleep(0.5)
    Miner = peripheral.wrap("top")
    configMiner(Miner)

    status("Mining...")
    os.sleep(0.5)
    Miner.start()
    
    os.sleep(0.5)
    while Miner.getToMine() > 0 do
        local toMine = Miner.getToMine()
        local secondsLeft = toMine * 0.385
        
        term.setCursorPos(1, 2)
        print(string.format("To mine: %d, ETA: %s                    ", toMine, getTime(secondsLeft))) -- Print the ETA (extra spaces for overwrite)

        os.sleep(0.39)
    end

    status("Cleaning up...")
    os.sleep(0.5)
    breakBlocks()

    print("Finished!")
end

-- Places the blocks for the Miner setup, and returns to the starting position
function placeBlocks()
    turtle.select(1) -- The Digiminer
    turtle.placeUp()
    turtle.turnRight()
    turtle.forward()
    turtle.forward()
    turtle.turnLeft()
    turtle.select(2) -- The Energy Source
    turtle.placeUp()
    turtle.forward()
    turtle.forward()
    turtle.turnLeft()
    turtle.forward()
    turtle.forward()
    turtle.up()
    turtle.turnRight()
    turtle.select(3) -- The Inventory Block
    turtle.placeUp()
    turtle.down()
    turtle.back()
    turtle.back()
end

-- Sets up the configs of the Digiminer
function configMiner(Miner)
    Miner.reset()
    Miner.setRadius(Miner.getMaxRadius())
    Miner.setMaxY(128)
    Miner.setMinY(0)
    Miner.setAutoEject(true)
    Miner.setSilkTouch(false)
end

-- Returns the ETA
function getTime(seconds)
    local hours = math.floor(seconds / 3600)
    local minutes = math.floor((seconds % 3600) / 60)
    local remaining_seconds = seconds % 60
    
    return string.format("%02d:%02d:%02d", hours, minutes, remaining_seconds)
end

function breakBlocks()
    turtle.select(1) -- The Digiminer
    turtle.digUp()
    turtle.turnRight()
    turtle.forward()
    turtle.forward()
    turtle.turnLeft()
    turtle.select(2) -- The Energy Source
    turtle.digUp()
    turtle.forward()
    turtle.forward()
    turtle.turnLeft()
    turtle.forward()
    turtle.forward()
    turtle.up()
    turtle.turnRight()
    turtle.select(3) -- The Inventory Block
    turtle.digUp()
    turtle.down()
    turtle.back()
    turtle.back()
end

function status(message)
    term.clear()
    term.setCursorPos(1, 1)
    print(message)
end

function moveForward()
    -- Collect the player for chunkloading
    turtle.down()
    turtle.down()
    turtle.down()
    turtle.back()
    turtle.back()
    turtle.up()
    turtle.up()

    -- Now that we have the player, move forward twice the distance of the maximum radius, to avoid any overlap
    for i = 1, 64, 1 do
        turtle.forward()
    end

    -- And finally, get into position for mining
    turtle.down()
    turtle.down()
    turtle.forward()
    turtle.forward()
    turtle.up()
    turtle.up()
    turtle.up()
end

status("Get in position!")
--os.sleep(5)

-- Move the player a little to ensure they are in the right spot
status("Initializing...")
for i = 1, 5, 1 do
    print(i)
    turtle.forward()
end
turtle.down()
turtle.down()
turtle.forward()
turtle.forward()
turtle.up()
turtle.up()
turtle.up()

main()

for i = 1, MAXREPEATS, 1 do
    INDEX = i
    
    moveForward()

    main()
end
