
-- Setup paths
local sPath = ".:/rom/programs"
if term.isColor() then
	sPath = sPath..":/rom/programs/advanced"
end
if turtle then
	sPath = sPath..":/rom/programs/turtle"
else
    sPath = sPath..":/rom/programs/rednet:/rom/programs/fun"
    if term.isColor() then
    	sPath = sPath..":/rom/programs/fun/advanced"
    end
end
if pocket then
    sPath = sPath..":/rom/programs/pocket"
end
if commands then
    sPath = sPath..":/rom/programs/command"
end
if http then
	sPath = sPath..":/rom/programs/http"
end
shell.setPath( sPath )
help.setPath( "/rom/help" )

-- Setup aliases
shell.setAlias( "ls", "list" )
shell.setAlias( "dir", "list" )
shell.setAlias( "cp", "copy" )
shell.setAlias( "mv", "move" )
shell.setAlias( "rm", "delete" )
shell.setAlias( "clr", "clear" )
shell.setAlias( "rs", "redstone" )
shell.setAlias( "sh", "shell" )
if term.isColor() then
    shell.setAlias( "background", "bg" )
    shell.setAlias( "foreground", "fg" )
end

-- Setup completion functions
local function completeFile( shell, nIndex, sText )
    if nIndex == 1 then
        return fs.complete( sText, shell.dir(), true, false )
    end
end
local function completeDir( shell, nIndex, sText )
    if nIndex == 1 then
        return fs.complete( sText, shell.dir(), false, true )
    end
end
local function completeEither( shell, nIndex, sText )
    if nIndex == 1 then
        return fs.complete( sText, shell.dir(), true, true )
    end
end
local function completeEitherEither( shell, nIndex, sText )
    if nIndex == 1 then
        local tResults = fs.complete( sText, shell.dir(), true, true )
        for n=1,#tResults do
            local sResult = tResults[n]
            if string.sub( sResult, #sResult, #sResult ) ~= "/" then
                tResults[n] = sResult .. " "
            end
        end
        return tResults
    elseif nIndex == 2 then
        return fs.complete( sText, shell.dir(), true, true )
    end
end
local function completeProgram( shell, nIndex, sText )
    if nIndex == 1 then
        return shell.completeProgram( sText )
    end
end
local function completeHelp( shell, nIndex, sText )
    if nIndex == 1 then
        return help.completeTopic( sText )
    end
end
shell.setCompletionFunction( "rom/programs/cd", completeDir )
shell.setCompletionFunction( "rom/programs/copy", completeEitherEither )
shell.setCompletionFunction( "rom/programs/delete", completeEither )
shell.setCompletionFunction( "rom/programs/edit", completeFile )
shell.setCompletionFunction( "rom/programs/help", completeHelp )
shell.setCompletionFunction( "rom/programs/list", completeDir )
shell.setCompletionFunction( "rom/programs/move", completeEitherEither )
shell.setCompletionFunction( "rom/programs/rename", completeEitherEither )
shell.setCompletionFunction( "rom/programs/advanced/bg", completeProgram )
shell.setCompletionFunction( "rom/programs/advanced/fg", completeProgram )
shell.setCompletionFunction( "rom/programs/fun/advanced/paint", completeFile )

-- Run autorun files
if fs.exists( "/rom/autorun" ) and fs.isDir( "/rom/autorun" ) then
	local tFiles = fs.list( "/rom/autorun" )
	table.sort( tFiles )
	for n, sFile in ipairs( tFiles ) do
		if string.sub( sFile, 1, 1 ) ~= "." then
			local sPath = "/rom/autorun/"..sFile
			if not fs.isDir( sPath ) then
				shell.run( sPath )
			end
		end
	end
end

-- Run the user created startup, either from disk drives or the root
local sUserStartup = shell.resolveProgram( "/startup" )
local bEnableDiskStartup = (commands == nil)
if bEnableDiskStartup then
    for n,sName in pairs( peripheral.getNames() ) do
        if disk.isPresent( sName ) and disk.hasData( sName ) then
            local sDiskStartup = shell.resolveProgram( "/" .. disk.getMountPath( sName ) .. "/startup" )
            if sDiskStartup then
                sUserStartup = sDiskStartup
                break
            end
        end
    end
end
if sUserStartup then
    shell.run( sUserStartup )
end
