---
-- @package   eg4-crank-ui
-- @file      file-utilities.lua
-- @brief     Various file/folder utilities
--
-- @copyright Copyright 2019 Hanover Displays Limited.
-- @license   This program is the confidential and proprietary product of
--            Hanover Displays Limited. Any unauthorised use, reproduction or
--            transfer of this program is strictly prohibited. (Subject to
--            limited distribution and restricted disclosure only.) All
--            rights reserved.
--
--

fileinfo = {}

---
--- @brief  Check if file or directory exists at this path
---
function fileinfo:exists(path)
  return fileinfo:isfile(path) or fileinfo:isfolder(path)
end

---
--- @brief  Check if a folder exists in this path
---
function fileinfo:isfolder(path)
  local list = fileinfo:execute('ls -a "'..path..'/." 2>/dev/null')
  if #list == 0 then return false end
  return true
end

---
--- @brief  Check if a file exists in this path
---
function fileinfo:isfile(path)
  if type(path)~="string" then return false end
  local f = io.open(path, "r")
  if f then
    f:close()
    return true
  end
  return false
end

---
--- @brief  Executes a system command and returns the console output
---
function fileinfo:execute(command)
  local t = {}
  local pfile = io.popen(command)
  for line in pfile:lines() do
    table.insert(t, line)
  end
  pfile:close()
  return t
end

---
--- @brief  Fetches list of files and folders in a dir.
---
function fileinfo:scandir(path)  
  return fileinfo:execute('ls "'..path..'" 2>/dev/null')
end


-- EOF --
