   @options: null
   @run: (options,cb)->
 
+
     @options = options
     @tmp = options.out||'markem.out'
-    @source = options.source||'.'
+    @source = path.resolve options.source||'.'
+
+    await @_spawn 'ls',[],defer err
 
     # serving skeletons
     await fs.exists path.join('markem.conf','layout.jade'),defer exists

        branch='master'

      # make sure users dont put documents in their GithubPage branch
      await @_git "status",null,defer err,out
      curBranch=out.match(/on\s*branch\s*([^\s]*)/i)[1]
      if curBranch==branch
        console.err "You are in target branch '#{branch}'. Put your documents in another branch!!!"

  #     cb(err,stdout,stderr)
  @_git:(command,workTree,cb)->
    if workTree?
      args=[
        '--work-tree'
        workTree
        '--git-dir' 
        path.join workTree,'.git'
      ]
      args.push c for c in command
    else
      args=command
    await @_spawn 'git', args, defer err, stdout
    cb err, stdout

  @_spawn:(exec, args, cb)->
    console.log "> #{exec} #{args.join ' '}"
    proc = childprocess.spawn exec, args,
      cwd: process.cwd()
    proc.stdout.setEncoding 'utf8'
    proc.stderr.setEncoding 'utf8' 
    out = []
    self = @
    proc.stdout.on 'data',(data)->
      process.stdout.write data if self.options.verbose
      out.push data
    proc.stderr.on 'data',(data)->
      process.stderr.write data if self.options.verbose
      out.push data
    await proc.on 'close', defer err
    cb err, out.join ''
  @_generate: (layout,cb)->
    await fs.readdir @tmp,defer err, list
    for file in list
      continue if file in ['.git']
      await @_spawn 'rm', ['-Rf',path.join(@tmp,file)], defer err

    await fs.readdir path.join(@source,'markem.conf'),defer err, list
    for file in list
      continue if file  in ['layout.jade']
      await @_spawn 'cp', ['-R', path.join(@source,'markem.conf',file), @tmp], defer err

    await utils.scandir
      path: @source
      readFiles: false
      ignoreHiddenFiles: true
      next: defer err,list
    globals={}
    documents={}

    # initialize some basic stuff
    for relative,type of list
      if type=='file'&&!relative.match /node_modules/
        target=null
        if relative.match /\.markdown$/
          target=path.join @tmp,relative.replace /\.markdown$/,'.html'
        if relative.match /\.md$/
          target=path.join @tmp,relative.replace /\.md$/,'.html'
        if relative.match new RegExp("readme\\.md$",'i')
          target=path.join @tmp,relative.replace new RegExp("[^#{path.sep}]*$"),'index.html'
        if target?
          document=
            dirs:[]
            files:[]
            pathSource:'/'+relative
            pathFile:'/'+path.relative(@tmp,target)
            target:target
            globals:globals
          document.path=document.pathFile.replace(/\/index\.html$/,'/')
          document.root=path.relative document.path.replace(/\/[^\/]*$/,'/'),'\/'
          if !document.root.length
            document.root='.'
          documents[document.path]=document
          await fs.readFile path.join(@source, relative),'utf8',defer err,document.source


    # build relationships,content,title between documents
    for p,document of documents
      document.content=marked document.source
      document.title=(document.content.match(/<h1>([^<]*)<\/h1>/m)||[null,'untitled'])[1] 
      if p=='/'
        globals.root=document
      if p.match /\/$/
        document.parent=documents[p.replace(/[^\/]*\/$/,'')]
        document.type='dir'
      else
        document.parent=documents[p.replace(/[^\/]+$/,'')]
        document.type='file'
      if document.parent?
        if document.type=='dir'
          document.parent.dirs.push document
        else
          document.parent.files.push document

    try
      markemConf=require path.join process.cwd(),'markem.conf'
    catch e
    if markemConf? and markemConf.preRender?
      await markemConf.preRender globals,defer err

    # render each documents
    for p,document of documents
      
      console.log "rendering #{document.target}" if markem.options.verbose
      await mkdirp path.dirname(document.target),defer err
      try
        document.output=layout document
      catch e
        console.error e
        utils.rmdirDeep @tmp,->
        process.exit 1
        return
      await fs.writeFile document.target,document.output,'utf8',defer err
    cb()