def build():
  # ============================
  # Basic stuff
  # ============================
    sed = Sed()
    PH.replacer(sed)

    # Get the remoty shims first
    remoty = Yak.links["airstrip"]["url"].split('/')
    remoty.pop()
    remoty = '/'.join(remoty)
    yam = json.loads(http.get(Yak.links["airstrip"]["url"], verify=False).text)[Yak.links["airstrip"]["version"]]

    # Allow to test separate loaders as well
    # print yam
    # for i in yam['yahoo']:
    #   if (i.find('trunk') == -1) or istrunk:
    #     sed.add('{SPIT-YAHOO}', remoty + '/' + i)#.replace('.css', '-min.css'))
    # print yam
    # for i in yam['head']:
    #   if (i.find('trunk') == -1) or istrunk:
    #     sed.add('{SPIT-HEAD}', remoty + '/' + i)#.replace('.css', '-min.css'))


  # ============================
  # Get the external shims
  # ============================
    allshims = FileList("src/burnscars", filter="*.js", exclude="*xxx*")
    for (k, elem) in {
      'json3': 'json3',
      'xmlhttprequest': 'xmlhttprequest',
      'es5-shim': 'es5',
      'es5-sham': 'es5',
      'console': 'console'
    }.items():
      candidate = ''
      for i in yam[elem]:
        if (Yak.istrunk and i.find('trunk') != -1) or ((not Yak.istrunk) and i.find('stable') != -1) and (i.find(k) != -1):
          candidate = remoty + '/' + i
          allshims.merge([candidate])
          break
      combine(['src/strict.js', candidate], '%s/burnscars/%s.js' % (Yak.build_root, k), replace=sed)
      sed.add('{SPIT-%s}' % k.upper(), k)

  # ============================
  # Get local shims and vanilla flavors
  # ============================
    deepcopy(FileList("src/burnscars", filter="*.js,*.html", exclude="*xxx*"), Yak.build_root + '/burnscars', replace=sed)
    deepcopy("src/shimer.js", Yak.build_root, replace=sed)
    deepcopy("src/loader.js", Yak.build_root, replace=sed)
    deepcopy("src/gulliver.js", Yak.build_root, replace=sed)
    combine(["src/loader.js", "src/shimer.js"], Yak.build_root + '/spitfire.js', replace=sed)

  # ============================
  # Prep-up the manifest
  # ============================
    shortversion = Yak.package['version'].split('-').pop(0).split('.')
    shortversion = shortversion[0] + "." + shortversion[1]
    spitroot = Yak.package['name'] + "/" + shortversion

    # All in one shim
    combine(allshims, Yak.build_root + '/burnscars.js', replace=sed)

  # ============================
  # Build tainted loaders
  # ============================
    for elem in ['lab', 'head', 'require']:
      candidate = ''
      for i in yam[elem]:
        if (Yak.istrunk and i.find('trunk') != -1) or ((not Yak.istrunk) and i.find('stable') != -1):
          candidate = remoty + '/' + i
          break
      combine(['src/strict.js', candidate, 'src/loader.js'], '%s/loader-%s.js' % (Yak.build_root, elem), replace=sed)

    # YepNope and Yahoo are crap and don't support strict mode
    candidate = ''
    elem = 'yahoo'
    for i in yam[elem]:
      if (Yak.istrunk and i.find('trunk') != -1) or ((not Yak.istrunk) and i.find('stable') != -1):
        candidate = remoty + '/' + i
        break
    combine([candidate, 'src/loader.js'], '%s/loader-%s.js' % (Yak.build_root, elem), replace=sed)

    candidate = ''
    elem = 'yepnope'
    for i in yam[elem]:
      if (Yak.istrunk and i.find('trunk') != -1) or ((not Yak.istrunk) and i.find('stable') != -1):
        candidate = remoty + '/' + i
        break
    combine([candidate, 'src/loader.js'], '%s/loader-%s.js' % (Yak.build_root, elem), replace=sed)


  # ============================
  # Build all-in-one
  # ============================
    for elem in ['lab', 'head', 'require', 'yepnope', 'yahoo']:
      combine(['%s/loader-%s.js' % (Yak.build_root, elem), '%s/shimer.js' % Yak.build_root], '%s/spitfire-%s.js' % (Yak.build_root, elem), replace=sed)

  # ============================
  # Build manifest itself
  # ============================
    description = {}
    # Separate components
    description["shimer"] = '%s/shimer.js' % spitroot;
    description["gulliver"] = '%s/gulliver.js' % spitroot;
    description["loader"] = '%s/loader.js' % spitroot;
    # Tainted loaders
    for elem in ['lab', 'head', 'require', 'yahoo', 'yepnope']:
      description["loader-%s" % elem] = "%s/loader-%s.js" % (spitroot, elem)

    # Standalone shims that can be used bundled by build systems
    description["json"] = '%s/burnscars/json3.js' % spitroot;
    description["xhr"] = '%s/burnscars/xmlhttprequest.js' % spitroot;
    description["es5"] = '%s/burnscars/es5-shim.js' % spitroot;
    description["console"] = '%s/burnscars/console.js' % spitroot;
    # All-in-one shim
    description["burnscars"] = '%s/burnscars.js' % spitroot;

    # The actual spitfire (loader + shimer)
    description["spitfire"] = '%s/spitfire.js' % spitroot;
    # And the tainted versions
    for elem in ['lab', 'head', 'require', 'yahoo', 'yepnope']:
      description["spitfire-%s" % elem] = "%s/spitfire-%s.js" % (spitroot, elem)


    PH.describe(shortversion, "spitfire", description)
def build(buildonly = False):
  # Crossdomain
  sed = Sed()
  sed.add("<\!--.*-->\s*", "")
  combine("src/crossdomain.xml", Yak.build_root + "/crossdomain.xml", replace = sed)

  # Robots
  sed = Sed()
  # XXX partially f****d-up
  sed.add("(?:^|\n+)(?:#[^\n]*\n*)+", "")
  combine("src/robots.txt", Yak.build_root + "/robots.txt", replace = sed)

  # Deepcopy other stuff
  sed = Sed()
  PH.replacer(sed)
  list = FileList("src/", exclude="*robots.txt,*crossdomain.xml,*index.html")
  deepcopy(list, Yak.build_root, replace=sed)


  # Process the remote leaves
  description = {}

  # Yak.collection.items()
  colls = PH.getyanks()
  # print Yak.collection
  # for name in Yak.collection:
  #   print name
  for name in colls:
    packinfo = colls[name]
    # Temporary and build output directories definitions
    tmpdir = FileSystem.join(Yak.tmp_root, "lib", packinfo["Destination"], name)
    builddir = FileSystem.join(Yak.build_root, "lib", packinfo["Destination"], name)

    desclist = []
    marker = 'lib/%s/' % packinfo["Destination"]
    for(localname, url) in packinfo["Source"].items():
      # Do the fetch of 
      PH.fetchone(url, tmpdir, localname)
      # Copy files that "exists" to build directory
      f = FileSystem.join(tmpdir, localname)
      if FileSystem.exists(f):
        d = FileSystem.join(builddir, localname)
        # if not FileSystem.exists(FileSystem.dirname(d)):
        #   FileSystem.makedir(FileSystem.dirname(d));
        FileSystem.copyfile(f, d)
        # Augment desclist with provided localname
        desclist += [FileSystem.join(marker, name, localname)]

    if "Build" in packinfo:
      buildinfo = packinfo["Build"]
      production = buildinfo["production"]
      tmpdir = FileSystem.join(tmpdir, buildinfo["dir"])
      extra = ''
      if 'args' in buildinfo:
        extra = buildinfo["args"]
      if not buildonly or buildonly == name:
        PH.make(tmpdir, buildinfo["type"], extra)

      # Copy production to build dir
      for(local, builded) in production.items():
        f = FileSystem.join(tmpdir, builded)
        d = FileSystem.join(builddir, local)
        desclist += [FileSystem.join(marker, name, local)]
        if FileSystem.isfile(f):
          FileSystem.copyfile(f, d)
        elif FileSystem.isdir(f):
          deepcopy(FileList(f), d)

      # ["coin%s" % key for key in ['item1', 'item2']]


      # map((lambda item: "%s%s" % (name, item)), ['item1', 'item2'])
      # # Augment description list with build result
      # bitch = production.keys();

      # for x in bitch:
      #   bitch[x] = FileSystem.join(name, bitch[x]);

      # print bitch
      # raise "toto"

      # desclist = desclist + production.keys()

    description[name] = desclist
    # description[name] = "%s%s" % (name, marker, ('",\n"%s' % marker).join(desclist)))

    # miam += """
    #   %s:
    #     ["%s%s"]
    # """ % (name, marker, ('", "%s' % marker).join(desclist))
  # FileSystem.writefile(FileSystem.join(Yak.build_root, "airstrip.yaml"), yaml.dump(yaml.load('\n'.join(description))))


    # print json.dumps(description)
    # raise "toto"

  shortversion = Yak.package['version'].split('-').pop(0).split('.')
  shortversion = shortversion[0] + "." + shortversion[1]
  PH.describe(shortversion, "airstrip", description)
  # Write description file
  # FileSystem.writefile(FileSystem.join(Yak.build_root, "airstrip.json"), '{%s}' % ',\n'.join(description))

  # Build-up the description file
  file = "src/index.html"
  sed.add("{PUKE-LIST}", json.dumps(description, indent=4))
  deepcopy(file, Yak.build_root, replace=sed)
def build():
    
    sed = Sed()
    PH.replacer(sed)

    spitman = PH.getmanifest('spitfire')

    spitbase = spitman['spitfire'].split('/')
    spitbase.pop()
    spitbase = '/'.join(spitbase)

    sed.add('{SPIT-BASE}', spitbase.encode('latin-1'))
    # Our own internal boot path to resolve modules against... - XXX unlikely to work properly due to deploy root variations?
    # sed.add('{SPIT-BOOT}', Yak.links['self']['url'] + "/"+ Yak.package['name'] + "/" + Yak.package['version'])
    # Tricky!
    sed.add("'{SPIT-STATICS}'", json.dumps(PH.getstaticmanifest('*')))

    # ================================
    # Tests
    # ================================

    list = FileList("tests", filter="*.js,*.html")
    deepcopy(list, Yak.build_root + '/tests', replace=sed)

    # ================================
    # Spitfire loader, in many variants
    # ================================

    # Mini-loader
    combine([spitman['gulliver'], 'src/onegateisopening/b.js'], Yak.build_root + "/t.i.o.j.js", replace=sed)

    # Bootstrappers - we use lab by default
    spitfireList = [
      spitman['spitfire-lab'],
      'src/onegateisopening/boot.js',
      'src/gister/packman.js'
    ]
    combine(spitfireList, Yak.build_root + "/there.is.only.jsboot.js", replace=sed)

    # spitfireList = [
    #   spitman['spitfire'],
    #   'src/onegateisopening/boot.js',
    # ]
    # combine(spitfireList, Yak.build_root + "/there.is.only.jsboot.vanilla.js", replace=sed)

    # spitfireList = [
    #   spitman['spitfire-require'],
    #   'src/onegateisopening/boot.js',
    # ]
    # # combine(spitfireList, Yak.build_root + "/there.is.only.jsboot.require.js", replace=sed)
    # combine(spitfireList, Yak.build_root + "/there.is.only.jsboot.require.js", replace=sed)

    # spitfireList = [
    #   spitman['spitfire-head'],
    #   'src/onegateisopening/boot.js',
    # ]
    # combine(spitfireList, Yak.build_root + "/there.is.only.jsboot.head.js", replace=sed)

    # # These two don't support strict mode - the hell with them!
    # spitfireList = [
    #   spitman['spitfire-yahoo'],
    #   'src/onegateisopening/boot.js',
    # ]
    # combine(spitfireList, Yak.build_root + "/there.is.only.jsboot.yahoo.js", replace=sed)

    # spitfireList = [
    #   spitman['spitfire-yepnope'],
    #   'src/onegateisopening/boot.js',
    # ]
    # combine(spitfireList, Yak.build_root + "/there.is.only.jsboot.yepnope.js", replace=sed)



    # XXX Have unified XHR bundled to be safe (?)
    # spitman['xhr'],

    # ================================
    # Monolithic ember stack test
    # ================================
    # f = [
    #   PH.getstaticmanifest('jquery'),
    #   PH.getstaticmanifest('handlebars'),
    #   PH.getstaticmanifest('ember'),
    #   PH.getstaticmanifest('i18n')
    # ]
    # combine(f, Yak.build_root + "/stack.ember.js", replace=sed)


    # ================================
    # Css normalizer
    # ================================
    cssnorm = PH.getstaticmanifest('normalize', Yak.istrunk)
    combine([cssnorm], Yak.build_root + "/there.is.only.jsboot.css", replace=sed)

    # ================================
    # Build-up the gate framy
    # ================================
    # XXX this must die and be replaced by a proper postmessage shim
    postmessageshim = PH.getstaticmanifest('postmessage', Yak.istrunk)
    postmessageshim = 'src/mingus/postmessage.js';

    gateList = [
      spitman['loader-lab'],
      spitman['spitfire'],
      postmessageshim,
      'src/onegateisopening/gate.js'
    ]

    combine(gateList, Yak.build_root + '/toobsj.ylno.si.ereht.js', replace=sed)
    # FileSystem.copyfile(Yak.build_root + '/gates/gate-frame.js', Yak.build_root + '/gates/gate-frame-min.js')

    shortversion = Yak.package['version'].split('-').pop(0).split('.')
    shortversion = shortversion[0] + "." + shortversion[1]

    # Build-up the frame to deploy connect to
    # XXX whether this works is undefined... because of shortversion and selectable versioned deploy
    sed.add('{PUKE-GATE-OPENER}', Yak.links['self']['url'] + "/"+ Yak.package['name'] + "/" + shortversion + "/toobsj.ylno.si.ereht.js")
    combine('src/onegateisopening/gate.html', Yak.build_root + '/gate.html', replace=sed)



    # ================================
    # Standalone mingus
    # ================================

    mingusList = [
      "src/strict.js",
      # Have postmessage shit
      postmessageshim,
    # Better safe than sorry - always include that
      # "src/lib/com/wiu/mingus/shim-plus/console.js",
      # "src/lib/com/wiu/mingus/shim-plus/cookies.js",
      # "src/lib/com/wiu/mingus/shim-plus/string.js",
      # "src/lib/com/wiu/mingus/shim-plus/iegetset.js",
#      "src/lib/com/wiu/mingus/shim/postmessage.js",
    # Mingus NS
      "src/mingus/namespace.js",
    # Base dep
      "src/mingus/grammar/ABNF.js",
      # "src/lib/com/wiu/mingus/converters/entity.js",
      # "src/lib/com/wiu/mingus/converters/bencoder.js",
      # "src/lib/com/wiu/mingus/converters/wikimedia.js",
    # And parsers / grammars
      "src/mingus/grammar/IMF.js",
      "src/mingus/grammar/IRI.js",
      "src/mingus/grammar/HTTP.js",

    # Then md5 dep
      "src/mingus/crypto/md5.js",


    # Not necessary per-se, but darn useful
      "src/mingus/converters/entity.js",

    # Then bases
      "src/mingus/xhr/ungate.js",
      "src/mingus/xhr/appkey.js",
      "src/mingus/xhr/digest.js",
    # Then the final XHR
      "src/mingus/xhr/appkeydigestxhr.js"
    ]

    # Shimmy yeah
    combine(mingusList, Yak.build_root + "/mingus.js", replace=sed)


    # ================================
    # jsBoot modules
    # ================================

    list = FileList('src/jsboot/debug', filter = '*.js', exclude = '*xxx*');
    combine(list, Yak.build_root + "/debug.js", replace=sed)

    # list = FileList('src/jsboot/gister', filter = '*.js', exclude = '*xxx*')
    list = FileList('src/jsboot/core', filter = '*.js', exclude = '*xxx*');
    # list.merge(FileList('src/jsboot/gister', filter = '*.js', exclude = '*xxx*'));
    list.merge(['src/jsboot/types/eventdispatcher.js'])

    # Not exactly "core" per-se
    list.merge(['src/jsboot/types/mutable.js'])
    list.merge(['src/jsboot/controllers/idle.js'])
    list.merge(['src/jsboot/controllers/singleapp.js'])
    list.merge(['src/jsboot/utils/storage.js'])
    list.merge(['src/jsboot/utils/tweener.js'])


    combine(list, Yak.build_root + "/core.js", replace=sed)

    list = [
      'src/jsboot/service/errors.js',
      'src/jsboot/service/client.js',
      'src/jsboot/service/core.js',
      'src/jsboot/service/flaves/account.js',
      'src/jsboot/controllers/application.js'
    ]

    combine(list, Yak.build_root + "/service.js", replace=sed)

    list = FileList('src/jsboot/ui', filter = '*.js', exclude = '*xxx*');
    combine(list, Yak.build_root + "/ui.js", replace=sed)




    spitroot = Yak.package['name'] + "/" + shortversion

    description = {}
    description["mingus"] = "%s/mingus.js" % spitroot
    description["jsbootstrap"] = "%s/there.is.only.jsboot.js" % spitroot
    description["gate"] = "%s/gate.html" % spitroot
    description["cssbootstrap"] = "%s/there.is.only.jsboot.css" % spitroot

    # Link the yaml stuff

    # yamu = FileSystem.join(Yak.deploy_root, "jsboot.yaml")
    # description = yaml.load('\n'.join(description))
    # if FileSystem.exists(yamu):
    #   mama = yaml.load(FileSystem.readfile(yamu))
    #   mama[Yak.package['version']] = description
    # else:
    #   mama = {Yak.package['version']: description}

    # # Straight to service root instead - kind of hackish...
    # FileSystem.writefile(yamu, yaml.dump(mama))



    PH.describe(shortversion, "jsboot", description)