--[[
	cLinux : Lore of the Day!
	Made by Piorjade, daelvn

	NAME:        /bin/rm
	CATEGORY:    Binary
	SET:         Core Binary
	VERSION:     01:alpha0
	DESCRIPTION:
		This program deletes a specified file in the specified path / in the
		current directory.
]]--

local function del(d)
	local r = false
	local currentDir = shell.dir()
	local falseDir = false
	local inhome = true
	local currentUsr = lib.perm.permission.getCurrentUser()
	if currentUsr ~= "root" then
		r = false
	else
		r = true
	end
	local a, b = string.find(d, "//")
	local i, j = string.find(d, "~")
	if a or i ~= nil and i == j then
		falseDir = true
	end
	local a, b = string.find(currentDir, "/usr/"..currentUsr.."/home/")
	if r == false and a == nil then
		inhome = false
	end

	if falseDir == false then

		local path = currentDir..d
		local a, b = string.find(d, "/")
		if a == 1 then
			path = d
		end
		local a, b = string.find(d, "[..]/")
		if a then
			--d = string.gsub(d, "/", "", 1)
			local col = term.getTextColor()
			term.setTextColor(colors.red)
			print("Invalid Path.")
			term.setTextColor(col)
			return
		end
		if not fs.exists(path) then
			local col = term.getTextColor()
			term.setTextColor(colors.red)
			print("Folder/File does not exist.")
			term.setTextColor(col)
			return
		end
		local a = fs.delete(path)
		if a == "noPermission" then
			local c = term.getTextColor()
			term.setTextColor(colors.red)
			print("You have no privileges, try 'sudo'.")
			term.setTextColor(c)
		else
			return true
		end
	elseif falseDir == true then
		local col = term.getTextColor()
		term.setTextColor(colors.red)
		print("Invalid path!")
		term.setTextColor(col)
		return false
	else
		print("User not found.")
		return false
	end
end
local tArgs = {...}

if #tArgs < 1 or #tArgs > 1 then
	print("Usage:")
	print("		rm <path>")
else
	del(tArgs[1])
end