
local function printUsage()
	print( "Usages:" )
	print( "label get" )
	print( "label set <text>" )
	print( "label clear" )
	print( "label get <drive>" )
	print( "label set <drive> <text>" )
	print( "label clear <drive>" )
end

-- Get arguments
local tArgs = { ... }
if #tArgs == 0 then
	printUsage()
	return
end

local sCommand = nil
local sDrive = nil
if #tArgs == 1 then
	sCommand = tArgs[1]
	sDrive = nil
else
	sCommand = tArgs[1]
	local tSides = rs.getSides()
	for n,sSide in ipairs( tSides ) do
		if sSide == tArgs[2] then
			sDrive = sSide
			break
		end
	end
end

if sDrive ~= nil then
	-- Check the disk exists
	local bData = disk.hasData( sDrive )
	if not bData then
		print( "No disk in "..sDrive.." drive" )
		return
	end
end

-- Do commands
if sCommand == "get" then
	-- Get the label
	local sLabel
	if sDrive ~= nil then
		sLabel = disk.getLabel( sDrive )
		if sLabel then
			print( "Disk label is \""..sLabel.."\"" )
		else
			print( "No Disk label" )
		end
	else
		sLabel = os.getComputerLabel()
		if sLabel then
			print( "Computer label is \""..sLabel.."\"" )
		else
			print( "No Computer label" )
		end
	end
		
elseif sCommand == "set" or sCommand == "clear" then
	-- Set the label
	local sText = nil
	local nFirstArg = 2
	if sDrive ~= nil then
		nFirstArg = 3
	end
	
	if sCommand == "set" and #tArgs >= nFirstArg then
		-- Build the label from input
		sText = ""
		for n = nFirstArg, #tArgs do
			sText = sText..tArgs[n].." "
		end
	end
	
	local sLabel = nil
	if sDrive ~= nil then
		disk.setLabel( sDrive, sText )
		sLabel = disk.getLabel( sDrive )
		if sLabel then
			print( "Disk label set to \""..sLabel.."\"" )
		else
			print( "Disk label cleared" )
		end
	else
		os.setComputerLabel( sText )
		sLabel = os.getComputerLabel()
		if sLabel then
			print( "Computer label set to \""..sLabel.."\"" )
		else
			print( "Computer label cleared" )
		end
	end

else
	-- Unknown command
	printUsage()
	return
	
end
