local u = dofile('common.lua')
local default = function(var,def)
	local v = os.getenv(var)
	if v then return v else return def end
end
local posixes = {
	linux = true; osx = true;
	android = true; haiku = true;
}
local conf = {
	pkg = {};
	os        = default('parsav_target_os', 'linux');
	dist      = default('parsav_dist', os.getenv('NIX_PATH') and 'nixos');
	tgttrip   = default('parsav_arch_triple'); -- target triple, used in xcomp
	tgtcpu    = default('parsav_arch_cpu'); -- target cpu, used in xcomp
	tgthf     = u.tobool(default('parsav_arch_armhf',true)); 
}
conf.posix = posixes[conf.os]
conf.exe   = u.tobool(default('parsav_link',not conf.tgttrip)); -- turn off for partial builds

local fallback = dofile('pkgdata.lua')
local libfind do local mkf = function(p)
		return function(l) return string.format(p,l) end
	end
	local unx = function(p)
		return function(l)
			if string.sub(l,1,3) == 'lib'
				then return string.format('%s.%s',l,p)
				else return string.format('lib%s.%s',l,p)
			end
		end
	end

	libfind = {
		linux = {
			static = unx 'a';
			dynamic = unx 'so';
		};
		osx = { dynamic = mkf '%s.dylib'; };
		win = { dynamic = mkf '%s.dll'; };
	}
end
local pkg = function(name)
	local fbo = fallback[name] and fallback[name].osvars
		and fallback[name].osvars[conf.os .. '_' .. conf.dist]
	local fbv = fallback[name] and fallback[name].vars
	local pkgenv = function(e)
		return os.getenv(string.format('parsav_pkg_%s_%s',name,e))
	end
	name = pkgenv('override') or name
	local nul = function() end
	local pkc, pkv = nul, nul
	local cnfvar = function(v,e)
		local eval = function(e)
			if type(e) == 'function'
				then return e(conf)
				else return e
			end
		end
		return pkgenv(v) or pkv(e or v) or (fbo and eval(fbo[v])) or (fbv and eval(fbv[v]))
	end
	if conf.posix then
		pkc  = function(...) return u.exec { 'pkg-config'; name; ...  } end
		local pkcv = function(...) return u.exec({ 'pkg-config'; name; ...  }, true) end
		if pkcv('--exists') == 0 then
			 pkv = function(v)
				return pkc('--variable', v)
			end
		else pkc = nul end
	else
		print '(warn) configuring on non-POSIX OS, all relevant paths must be specified manually in environment variables or build will fail!'
	end
	local locdep = u.ping('./lib/' .. name)
	local prefix
	if locdep then
		prefix = './lib/' .. name
	end
	prefix = prefix or cnfvar('prefix')
	local libdir = cnfvar('libdir')
	if not libdir then
		if locdep
			then libdir = prefix .. (cnfvar('builddir') or cnfvar('libbuilddir')) or ''
			else libdir = prefix .. '/lib'
		end
	end
	local incdir = cnfvar('incdir','includedir') or (prefix .. '/include')
	local libstr = pkc '--libs-only-l' -- (--static is not reliable)
	local libs = fallback[name] and fallback[name].libs or {}
	local linkstatic = locdep
	if (not locdep) and libstr then
		libs = {}
		for m in string.gmatch(libstr, '-l(%g+)') do
			libs[#libs + 1] = m
		end
	else
		if #libs == 0 then libs = { name } end
	end

	conf.pkg[name] = {
		prefix = prefix;
		libdir = libdir;
		incdir = incdir;
		dylibs = {}, statlibs = {};
	}
	local me = conf.pkg[name]
	
	local lf = libfind[conf.os]
	if lf.dynamic then me.dylibs   = u.map(libs, lf.dynamic) end
	if lf.static  then me.statlibs = u.map(libs, lf.static)  end

	if linkstatic then
		local ldf = {}
		for _,v in pairs(me.statlibs) do
			local fl = libdir .. '/' .. v
			if u.ping(fl) then ldf[#ldf+1] = fl end
		end
		me.linkargs = ldf
	else
		local arg = name
		if string.sub(arg,1,3) == 'lib' then arg = string.sub(arg,4) end
		local linkline = libstr or string.format('-l%s', arg)
		me.linkargs = {
			string.format('-L%s', me.libdir);
		}
		for m in string.gmatch(linkline, '(%g+)') do
			me.linkargs[#me.linkargs+1] = m
		end

		-- siiiiigh
		for _,l in pairs(libs) do
			local sw = string.format('-l%s', l) 
			local found = false
			for _,a in pairs(me.linkargs) do
				if a == sw then found = true break end
			end
			if not found then
				me.linkargs[#me.linkargs+1] = sw
			end
		end
	end
end

pkg('mbedtls')
pkg('libhttp')
pkg('json-c')

return conf
