# signature { license=0, seed=277031123, key="38f40d02b5dba7d015566fb3804c421a" }
































local paths =
{
	_COPYRIGHT   = "Copyright (C) 2017-2020 NEC";
	_DESCRIPTION = "Compatbility Module";
	_VERSION     = "paths 0.2.7";
}




local separator  = package.config:sub(1,1)
local package_path = package.path
local lib_path = package_path :gsub('([?].*)','')
local apps_path = lib_path:gsub('lib.$', '' )


paths.cwd  = apps_path


local maps = {}
maps[ '/apps/' ] = apps_path


if separator == '/' then
	paths.os = 'linux'
	maps[ '/config/' ] = '/etc/pyxis/config/'
else
	paths.os = 'nucleus'
	maps[ '/config/' ] = 'C:\\'
end



local function cd( appname )
	local cwd = apps_path .. appname .. separator
	paths.cwd = cwd
	package.path = cwd .. '?.lua;' .. package_path
end




local function map( path )
	if not path or type( path ) ~= 'string' or path:find('^%a:\\') then return path end
	if path:sub(1,1) == '/' then
		path = path:gsub( '^/%w+/', maps )
	else

		path = paths.cwd .. path
	end
	return path:gsub( '/', separator )
end
paths.map = map


local patched = {}






local function patch( module, fname, func )

	patched[ module ] = module
	local f = module[ fname ]
	local list = module[ paths ] or {}
	list[ fname ] = f
	module[ paths ] = list

	module[ fname ] = func  or  function( filename, ... ) return f( map( filename ), ...) end
end



os.uname = paths.os
patch( os, 'remove')
patch( os, 'license')


local os_rename = os.rename
patch( os, 'rename', function( old, new, ... ) return os_rename( map( old ) , map( new ), ... ) end )



local os_execute = os.execute
patch( os ,'execute',
	function( arg, ... )
		if type( arg ) == 'string' then
			local cmd, spc, args = arg:match( '(%S+)(%s?)(.*)' )
			arg = map( cmd ) .. ( spc or '' ) .. ( args or '' )
		end
		return os_execute( arg, ... )
	end
)



patch( io, 'open' )
patch( io, 'lines' )




local lfs = require 'lfs'




local lfs_dir = lfs.dir


local function linuxdir( pattern )
	pattern = map( pattern )
	local path, mask = pattern:match( '^(.-/)([^/]+)$' )

	mask = mask and '^'..mask:gsub('[.]','%%.'):gsub('*','.-')..'$' or '.-'

	local ok, dir, context = pcall( lfs_dir, path )
	local f = function()
		while ok do
			local name = dir( context )
			if not name then return nil end
			if name:find( mask ) then return name end
		end
	end
	return f
end

if paths.os == 'nucleus' then
	patch( lfs, 'dir' )
else
	patch( lfs, 'dir', linuxdir )
end

patch( lfs, 'mkdir' )
patch( lfs, 'rmdir ')
patch( lfs, 'attributes' )
patch( lfs, 'setattributes' )
patch( lfs, 'touch' )



patch( _G, 'loadfile' )



function paths.undo()

	for module in pairs( patched ) do
		local list = module[ paths ]
		if list then
			for fname, f in pairs( list ) do
				module[ fname] = f
			end
			module[ paths ] = nil
		end
	end
	package.path = package_path
	package.loaded.paths = nil
end



local mt =
{
	__call = function( _, appname )
		if appname then
			cd( appname )
		end

		return paths
	end;
	__gc = paths.undo;
}

return setmetatable( paths, mt )







