
function file_cut()

  -- get select dir
  selectdir = currentdir

  -- null cut/copy files arrays
  cut_files = {}
  copy_files = {}

  -- loop selected files
  for k,v in pairs(selected_files) do

    -- render message
    screen:clear()
    render_filelist()
    render_alert("Cutting item(s)...", white)

    -- populate cut files array
    table.insert(cut_files, v)
    
    -- flip screen
    screen.flip()
    screen.waitVblankStart(10)

  end

  -- null temp arrays
  selected_files = {}

end

function file_copy()

  -- get select dir
  selectdir = currentdir

  -- null cut/copy files arrays
  cut_files = {}
  copy_files = {}

  -- loop selected files
  for k,v in pairs(selected_files) do

    -- render message
    screen:clear()
    render_filelist()
    render_alert("Copying item(s)...", white)

    -- populate copy files array
    table.insert(copy_files, v)
    
    -- flip screen
    screen.flip()
    screen.waitVblankStart(10)

  end

  -- null temp arrays
  selected_files = {}

end

function file_paste()

  if not is_lowbatt() then

    if table.getn(cut_files) >= 1 then
      pastemsg = "Moving item(s)..."
      paste_files = cut_files
      delsrc = true
    elseif table.getn(copy_files) >= 1 then
      pastemsg = "Pasting item(s)..."
      paste_files = copy_files
      delsrc = false
    end

    -- display message
    render_filelist()
    render_alert(pastemsg, white)
    screen.flip()
    screen.waitVblankStart()

    -- loop selected files
    for k,v in pairs(paste_files) do

      -- is dir
      if is_dir(get_filepath(v), get_filename(v)) then

        -- scan directory
        scan_tmp = {}; scan_dirs = {}; scan_files = {}
        scan_dir(get_filepath(v), get_filename(v))    

        -- loop scanned dirs
        for k2,v2 in pairs(scan_dirs) do

          -- md dest dir
  	  md(remove_trailing_slash(currentdir) .. "/" .. string.sub(v2, (string.len(selectdir) + 2)))

          -- redraw screen
          render_filelist()
          render_alert(pastemsg, white)

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

        end

        -- loop scanned files
        for k2,v2 in pairs(scan_files) do

          local src = v2
          local dest = remove_trailing_slash(currentdir) .. "/" .. string.sub(v2, (string.len(selectdir) + 2))

          -- cp file
          cp(src, dest)

          -- rm src file
          if delsrc then
            rm(src)
          end

          -- redraw screen
          render_filelist()
          render_alert(pastemsg, white)

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

        end

        -- rm src dirs
        if delsrc then
          local i = table.getn(scan_dirs)
          while i > 0 do

            -- rm dir
            rm_dir(scan_dirs[i])
	  
            -- redraw screen
            render_filelist()
            render_alert(pastemsg, white)

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

          i = i - 1
          end
        end

      -- is file
      else

        local src = v
        local dest = remove_trailing_slash(currentdir) .. "/" .. get_filename(v)

        -- cp file
        cp(src, dest)

        -- rm src
        if delsrc then
          rm(src)
        end

      end

      -- update files
      menu_pointer = 1
      update_files()

      -- redraw screen
      render_filelist()
      render_alert(pastemsg, white)

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

    end

    -- null temp arrays
    cut_files = {}
    copy_files = {}
    paste_files = {}

  else

    -- redraw screen
    render_filelist()
    render_alert(lowbattmsg(), white)

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

  end

end

function file_delete()

  if not is_lowbatt() then

    -- display message
    render_filelist()
    render_alert("Deleting item(s)...", white)
    screen.flip()
    screen.waitVblankStart()

    -- loop selected files files
    for k,v in pairs(selected_files) do
    
      -- is dir
      if is_dir(get_filepath(v), get_filename(v)) then

        -- scan directory
        scan_tmp = {}; scan_dirs = {}; scan_files = {}
        scan_dir(get_filepath(v), get_filename(v))

        -- loop scanned files
        local i = table.getn(scan_files)
        while i > 0 do

  	  -- rm file
	  rm(scan_files[i])

          -- redraw screen
          render_filelist()
          render_alert("Deleting item(s)...", white)
    
          -- flip screen
          screen.flip()
          screen.waitVblankStart()


        i = i - 1
        end

        -- loop scanned dirs
        local i = table.getn(scan_dirs)
        while i > 0 do

          -- rm dir
          rm_dir(scan_dirs[i])

          -- redraw screen
          render_filelist()
          render_alert("Deleting item(s)...", white)
    
          -- flip screen
          screen.flip()
          screen.waitVblankStart()
	  
        i = i - 1
        end

      -- is file
      else

        -- rm file
        rm(v)

      end

      -- update files
      menu_pointer = 1
      update_files()

      -- redraw screen
      render_filelist()
      render_alert("Deleting item(s)...", white)
    
      -- flip screen
      screen.flip()
      screen.waitVblankStart(10)

    end

    -- null temp arrays
    selected_files = {}

  else

    -- redraw screen
    render_filelist()
    render_alert(lowbattmsg(), white)

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

  end

end

function file_select(path, name, selectmode)

  if selectmode == "all" or selectmode == "none" then

    menu_pointer = 1
    start_pointer = 1

    -- get filelsting
    local tmpfiles = System.listDirectory(path)
    table.sort(tmpfiles, filesort)

    -- loop files
    for k,v in pairs(tmpfiles) do

    -- concatenate path/file
    local path_file = remove_trailing_slash(path) .. "/" .. v.name

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

        -- select all
        if selectmode == "all" then

          -- if not in array add file to selected files array
          if not in_array(selected_files, path_file) then
            table.insert(selected_files, path_file)

    	    -- remove from cut files
	    for k2,v2 in pairs(cut_files) do
              if v2 == (path_file) then
                table.remove(cut_files, k2)
	      end
	    end

          end

          -- unselect all
          elseif selectmode == "none" then

            -- if in array remove files from selected files array
            if in_array(selected_files, path_file) then
	      for k2,v2 in pairs(selected_files) do
                if v2 == (path_file) then
                  table.remove(selected_files, k2)
	        end
	      end
	    end

	  end

        end

      -- highlight next file
      mp_nextfile()

      -- render files
      render_filelist()
      if selectmode == "all" then
        render_alert("Selecting item(s)...", white)
      elseif selectmode == "none" then
        render_alert("Unselecting item(s)...", white)
      end

      -- flip screen
      screen.flip()
      screen.waitVblankStart(1)
    
    end

  elseif selectmode == "toggle" then

    -- concatenate path/file
    local path_file = remove_trailing_slash(path) .. "/" .. name

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

      -- toggle selected file
      if in_array(selected_files, path_file) then
        for k,v in pairs(selected_files) do
          if v == (path_file) then
            table.remove(selected_files, k)
	  end
	end
      else
        table.insert(selected_files, path_file)
        -- remove from cut files
	for k,v in pairs(cut_files) do
          if v == (path_file) then
            table.remove(cut_files, k)
	  end
	end
      end

      -- highlight next file
      mp_nextfile()

      -- render files
      render_filelist()

    else

      -- render files
      render_filelist()
      render_alert("Cannot select . or ..", white)

    end

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

  end

end
