local mapSideToTurtleFunction = function(side, turtleFunction)
	if string.lower(side) == "top" then
		return turtle[turtleFunction.."Up"]
	elseif string.lower(side) == "bottom" then
		return turtle[turtleFunction.."Down"]
	elseif string.lower(side) == "front" then
		return turtle[turtleFunction]
	end
end

local isAboveLevel = function(jar, switchAmount)
	local aspects = jar.getAspects()
	if not aspects then return false end
	return aspects[1].quantity > switchAmount
end

local isBelowLevel = function(jar, switchAmount)
	local aspects = jar.getAspects()
	if not aspects then return false end
	return aspects[1].quantity < switchAmount
end

local Args = {...} -- jarToReplace newJars oldJars (<|>)switchAmount

local pickUpJar = mapSideToTurtleFunction(Args[1], "dig")
local placeJar = mapSideToTurtleFunction(Args[1], "place")
local jar = peripheral.wrap(Args[1])
local dropJar = mapSideToTurtleFunction(Args[2], "drop")
local suckJar = mapSideToTurtleFunction(Args[3], "suck")
local checkLevel = 1

if string.sub(Args[4], 1, 1) == "<" then
	checkLevel = isBelowLevel
elseif string.sub(Args[4], 1, 1) == ">" then
	checkLevel = isAboveLevel
end

local switchAmount = tonumber(string.sub(Args[4], 2))

while true do
	if checkLevel(jar, switchAmount) then
		while not pickUpJar() do sleep(2) end
		sleep(1)
		while not dropJar() do sleep(2) end
		sleep(1)
		while not suckJar() do sleep(2) end
		sleep(1)
		while not placeJar() do sleep(2) end
	end
	sleep(5)
end