 normalize = path.normalize
 fs = require('fs')
 stripExtension= require('./strip_extension')
-metafileSignature= /\.meta\.(js|coffee|json)$/
-moduleSignature= /\.(js|coffee|json)$/
-skipThisFileSignature= /^\.|^_default\.meta\.(js|coffee|json)$/
-stripMetaExtension= /(.*)(?:\.meta\.(js|coffee|json))$/
-stripDotMeta= /(.*?)(?:_default)?(?:\.meta)$/
 moduleExtensions= ['.js','.coffee','.json']
-
 
 isFileSync= (name)->
   try

    return c
  descend(name)

rawmetas= {}
load= (name,superMeta)->
  raw= require(name)
  rawmetas[name.replace(stripDotMeta,'$1')]= raw
  extend(superMeta, raw, name)

loadTree= (rootdir,superMeta,supersites) ->
  modules= []
  loadDir= (dirname,superMeta,visited) ->   #recursive
    defaultModName = path.join(dirname,'_default.meta')
    meta= if isModuleSync(defaultModName)
      metacache[dirname+'/']= load(defaultModName,superMeta)
    else
      superMeta
    for filename in fs.readdirSync(dirname)
      unless skipThisFileSignature.test(filename)
        filename= path.join(dirname,filename)
        stat = fs.lstatSync(filename)
        if stat.isDirectory()
          unless visited[stat.ino]  # do not follow circular symbolic link
            newvisited= {}
            newvisited[ino] = true for ino, val of visited
            newvisited[stat.ino]= true
            loadDir(filename,meta,newvisited)
        else
          if moduleSignature.test(filename)
            stripped= stripExtension(filename)
            if metafileSignature.test(filename)
              metacache[filename.replace(stripMetaExtension,'$1')]= load(stripped,meta)
            else
              modules.unshift(stripped)
    return meta
  visited= {}
  visited[fs.lstatSync(rootdir).ino]= true
  metacache[rootdir+'/']= superMeta  # this will be overridden if _default_meta found
  meta= loadDir(rootdir,superMeta,visited)
  preload(modules,meta,supersites)
  return meta

siteLookupRoot= null
sites= {}
sitesModuleSignature= /_sites$/
preload= (modules,superMeta,supersites)->
  for modname in modules
    m= require(modname)
    if m?.meta
      rawmetas[modname]= m.meta
      itsMeta= meta_lookup(modname)
      metacache[modname]= extend(itsMeta,m.meta,modname)
    if sitesModuleSignature.test(modname)
      siteroot= path.dirname(modname)
      siteLookupRoot ?= siteroot
      sites[siteroot]= m
      (xsupersites= supersites.slice(0)).push(siteroot)
      for sitepath in m.paths
        loadTree(normalize(sitepath),superMeta,xsupersites)

module.exports=
  init: (baseSiteStack,options={})->
    dirs = baseSiteStack  # note: the stack is maintained in reverse order
    userPath= dirs[0]
    defaultPath= dirs[1]
    inter= loadTree(defaultPath,{},[])
    inter= extend(inter,options,'(options)')
    loadTree(userPath,inter,[defaultPath])
  meta_lookup: meta_lookup
  site_lookup: (req,res,next)->
    stack= []
    siteLookup= (sitename)=>

isModuleSync= (name)->
  return true for ext in moduleExtensions when isFileSync(name+ext)
  false

metacache= {}

