--This module itnerfaces with the os, eg commands gets returned as strings--
module("command", package.seeall)

function split(c,str)
	a 			=	string.find(str, c)
	str 		= 	string.gsub(str, c, "", 1)
	aCount	= 	0
	start 	= 	1
	array 	= 	{}
	last		= 	0
	
	while a do
		array[aCount] = string.sub(str, start, a - 1)
		start = a
		a = string.find(str, c)
	
		str = string.gsub(str, c, "", 1)
		aCount = aCount + 1
	end
	
	return array
end

------------------------------
-- Modified 20 March 2013 --
-- sam@kses.net           --
--------------------------------------------------------------
-- Check if a file exists and return true/false accordingly --
--------------------------------------------------------------
-- Open Named File If It Exist.
function file_exists(name)

	-- This function opens a file, in the mode specified in the string mode. 
	-- It returns a new file handle, or, in case of errors, nil plus an error message. 
	-- The mode string can be any of the following: 
	-- "r":  read mode (the default);
	--	"w":  write mode;
	--	"a":  append mode;
	--	"r+": update mode, all previous data is preserved;
	--	"w+": update mode, all previous data is erased;
	--	"a+": append update mode, previous data is preserved, writing is only allowed at the end of file.
	-- The mode string can also have a ‘b‘ at the end, which is needed in some systems to open the file in binary mode. 
	-- This string is exactly what is used in the standard C function fopen.
   local f = io.open(name,"r")
   
   if f~=nil then 
   
   -- Equivalent to file:close(). Without a file, closes the default output file.
		io.close(f) 
   	return true 
   else 
	   return false 
   end
end


--Function to execute terminal commands to run a separate program and returns value as string
function ex(command)

	-- Starts program program in a separated process and returns a file handle 
	-- that you can use to read data from this program (if mode is "r", the default) 
	-- or to write data to this program (if mode is "w").
	local f = io.popen(command)	
	
	-- Reads the file, according to the given formats, which specify what to read. For each format, 
	-- the function returns a string (or a number) with the characters read, or nil if it cannot read 
	-- data with the specified format.
	-- "*n" reads a number; this is the only format that returns a number instead of a string.
	-- "*a" reads the whole file, starting at the current position. On end of file, it returns the empty string	
	-- "*|" reads the next line (skipping the end of line), returning nil on end of file. This is the default format.
	-- Number reads a string with up to this number of characters, returning nil on end of file. 
	-- If number is zero, it reads nothing and returns an empty string, or nil on end of file.
	local value = f:read("*a") or "none"
	
	
	-- Closes file. Note that files are automatically closed when their handles are garbage collected, but 
	-- that takes an unpredictable amount of time to happen.
	local status,reason,code = f:close()
	
	return value,status,reason,code
end


--Function to write value string to a fileaddress file
function writefile(fileaddres,value)
	local f = io.open(fileaddres,"w+")
	f:write(value)
	f:close()
	return true
end

--this function return the content in file as a string
function returnFileString(filename)

	local returnString 	=	""

	-- This function opens a file, in the mode specified in the string mode. 
	-- It returns a new file handle, or, in case of errors, nil plus an error message. 
	-- The mode string can be any of the following: 
	-- "r":  read mode (the default);
	--	"w":  write mode;
	--	"a":  append mode;
	--	"r+": update mode, all previous data is preserved;
	--	"w+": update mode, all previous data is erased;
	--	"a+": append update mode, previous data is preserved, writing is only allowed at the end of file.
	-- The mode string can also have a ‘b‘ at the end, which is needed in some systems to open the file in binary mode. 
	-- This string is exactly what is used in the standard C function fopen.	
	f = io.open(filename,"r")
	
	
	-- Returns an iterator function that, each time it is called, returns a new line from the file. Therefore, the construction
	-- (for line in file:lines() do body end) will iterate over all lines of the file. (Unlike io.lines, 
	-- this function does not close the file when the loop ends.)
	for line in f:lines() do
		returnString = returnString .. line .. "\n"
	end
	
	-- Closes file. Note that files are automatically closed when their handles are garbage collected, but 
	-- that takes an unpredictable amount of time to happen.	
	f:close()
	return returnString
end

--this function returns a table of all the xml files in the directory
function getFilesInTable(directorypath)

	local mods = {}
	local tel  = 0

	--The io library provides another way to iterate over lines of a text file

	--local file = assert(io.popen('/bin/ls -la', 'r'))
	--local file = assert(io.popen('/var/www/lighttpd/httpd/modsxml/ls -la', 'r'))
	--	for i in file:lines() do
	--	for i in io.popen("ls /var/www/lighttpd/http/modsxml/"):lines() do	

	for i in io.popen("ls "..directorypath):lines() do
		if string.find(i,"%.xml$") then  
			tel 			= 	tel +1
			mods[tel] 	=	i
		end
	end
	
	return mods
end

function log(m)
   ex([[logger -t cgi "]]..m..[["]])   
end
