-- luafiler (c) 2006 mallchin, http://www.mallchin.com/luafm/
-- You are free to distribute this file without modifications.

function filesort(a, b)
  if case_sorting == 0 then
    a_name = string.lower(a.name)
    b_name = string.lower(b.name)
  else
    a_name = a.name
    b_name = b.name
  end
  if a.directory == b.directory then
    return a_name < b_name
  else
    return a.directory
  end
end

function mp_nextfile()

  -- reset filename scroller
  reset_filename_scroller()

  -- increment menu pointer
  if menu_pointer + 1 <= num_files then
    menu_pointer = menu_pointer + 1
  else
    menu_pointer = 1
    start_pointer = 1
  end
  if (num_files > num_listings) and (menu_pointer == (start_pointer + num_listings)) then
    start_pointer = start_pointer + 1
  end

end

function mp_prevfile()

  -- reset filename scroller
  reset_filename_scroller()

  -- decrement menu pointer
  if (num_files > num_listings) and (menu_pointer == start_pointer) then
    start_pointer = start_pointer - 1
  end
  if menu_pointer - 1 >= 1 then
    menu_pointer = menu_pointer - 1
  else
    menu_pointer = num_files
    if (num_files > num_listings) then
      start_pointer = ((num_files - num_listings) + 1)
    else
      start_pointer = 1
    end      
  end

end

function test_filetype(filename, extension)
--  if string.lower(string.sub(filename, -3)) == extension then return true end
  local s,v,r = string.find(filename, "\.(%w*)$")
  if string.lower(r) == extension then return true else return false end
end

function update_files()

  -- get current working directory
  currentdir = remove_trailing_slash(System.currentDirectory())

  -- get directory listing
  files = System.listDirectory()

  -- add actions
  for i,file in files do
    file.action_cross = "file_action(\"" .. add_trailing_slash(currentdir) .. "\", \"" .. file.name .. "\", \"execute\")"
    file.action_square = "file_action(\"" .. add_trailing_slash(currentdir) .. "\", \"" .. file.name .. "\", \"operation\")"
    file.action_triangle = "file_select(\"" .. add_trailing_slash(currentdir) .. "\", \"" .. file.name .. "\", \"toggle\")"
  end

  -- ugly hack to remove ./.. from flash0/1
  if currentdir == "flash0:" or currentdir == "flash1:" then
    table.remove(files, 1)
    table.remove(files, 1)
  end

  -- insert fake rootdir items
  if is_rootdir() then

    -- .
    local fakefile = {}
    fakefile.name = "."
    fakefile.directory = true
    fakefile.size = 1024
    fakefile.action_cross = ""
    fakefile.action_square = "file_action(\"" .. add_trailing_slash(currentdir) .. "\", \"" .. fakefile.name .. "\", \"operation\")"
    fakefile.action_triangle = "file_select(\"" .. add_trailing_slash(currentdir) .. "\", \"" .. fakefile.name .. "\", \"toggle\")"
    table.insert(files, 1, fakefile)

    -- ..
    local fakefile = {}
    fakefile.name = ".."
    fakefile.directory = true
    fakefile.size = 1024
    fakefile.action_cross = "render_drives()"
    fakefile.action_square = "file_action(\"" .. add_trailing_slash(currentdir) .. "\", \"" .. fakefile.name .. "\", \"operation\")"
    fakefile.action_triangle = "file_select(\"" .. add_trailing_slash(currentdir) .. "\", \"" .. fakefile.name .. "\", \"toggle\")"
    table.insert(files, 2, fakefile)

  end

  -- sort file array
  table.sort(files, filesort)

  -- count files
  num_files = table.getn(files)

  -- update menu pointer
  if menu_pointer >= (num_files + 1) then
    menu_pointer = 1
  end

  -- reset start pointer
  start_pointer = 1

  -- null selected files array
  selected_files = {}

end

function is_dir(path, name)
  local tmpfiles = System.listDirectory(path)
  local found_needle = false
  for k,v in pairs(tmpfiles) do
    if v.name == name and v.directory then found_needle = true end
  end
  return found_needle
end

function get_filename(path_name)
  return string.gsub(path_name, ".*/(.*[^\/])", "%1")
end

function get_filepath(path_name)
  return string.gsub(path_name, "^(.+)/.*$", "%1")
end

function scan_dir(path, name)

  -- set path
  local path_name = remove_trailing_slash(path) .. "/" .. name

  -- add dir to scan array
  table.insert(scan_dirs, path_name)

  -- get listing
  tmplist = System.listDirectory(path_name)
  table.sort(tmplist, filesort)
  scan_tmp[path_name] = tmplist

  -- loop listing
  for k,v in pairs(scan_tmp[path_name]) do

    -- skip ./..
    if v.name ~= "." and v.name ~= ".." then

      -- is dir
      if is_dir(path_name, v.name) then

	-- scan dir
	scan_dir(path_name, v.name)

      -- is file
      else

        -- add file to scan array
	table.insert(scan_files, path_name .. "/" .. v.name)

      end

    end

  end

end

function confirm_end_app()

  skip_finish_file_operations = true

  -- confirm file delete
  confirm_action = "end_app()"
  render_confirm("Really Quit?", white, false)

  -- flip screen
  screen.flip()
  screen.waitVblankStart()

end

function end_app()
  if quittype == 1 then
    break_app = true
  elseif quittype == 2 then
    System.Quit() 
  end
end

function is_rootdir()
  if in_array(drives, (add_trailing_slash(currentdir))) then
    return true
  else
    return false
  end
end

function change_drive(name)

  -- ugly hack to mount umd
  if name == "disc0:/" then System.initUMD() end

  -- set current directory
  System.currentDirectory(name)

  -- update files
  update_files()

  -- render files
  render_filelist()

  -- flip screen
  screen.flip()
  screen.waitVblankStart()

end

function init_files(path)

  if path == "/" then
    render_drives()
  else
    System.currentDirectory(path)
    update_files()
  end

end

function refresh_filelist()

  -- clear screen
  screen:clear()

  -- update files
--  update_files()

  -- render filelist
  render_filelist()

  -- flip screen
  screen.flip()
  screen.waitVblankStart()

  -- render filelist
  render_filelist()

  -- flip screen
  screen.flip()
  screen.waitVblankStart()

end
