auto Icarus::famicomManifest(const string& location) -> string {
  vector<uint8_t> buffer;
  concatenate(buffer, {location, "ines.rom"});
  concatenate(buffer, {location, "program.rom"});
  concatenate(buffer, {location, "character.rom"});
  return famicomManifest(buffer, location);
}

auto Icarus::famicomManifest(vector<uint8_t>& buffer, const string& location) -> string {
  FamicomCartridge cartridge{buffer.data(), buffer.size()};
  if(auto markup = cartridge.markup) {
    markup.append("\n");
    markup.append("information\n");
    markup.append("  sha256: ", Hash::SHA256(buffer.data(), buffer.size()).digest(), "\n");
    markup.append("  title:  ", prefixname(location), "\n");
    markup.append("  note:   ", "heuristically generated by icarus\n");
    return markup;
  }
  return "";
}

auto Icarus::famicomImport(vector<uint8_t>& buffer, const string& location) -> bool {
  auto name = prefixname(location);
  auto source = pathname(location);
  string target{settings.libraryPath, "Famicom/", name, ".fc/"};
//if(directory::exists(target)) return failure("game already exists");

  string markup;

//if(settings.useHeuristics && !markup) {
    FamicomCartridge cartridge{buffer.data(), buffer.size()};
    if(markup = cartridge.markup) {
      markup.append("\n");
      markup.append("information\n");
      markup.append("  title: ", name, "\n");
      markup.append("  note:  heuristically generated by icarus\n");
    }
//}

  if(!markup) return failure("failed to parse ROM image");
  if(!directory::create(target)) return failure("library path unwritable");

  if(settings.createManifests) file::write({target, "manifest.bml"}, markup);
  file::write({target, "ines.rom"}, buffer.data(), 16);
  file::write({target, "program.rom"}, buffer.data() + 16, cartridge.prgrom);
  if(!cartridge.chrrom) return success();
  file::write({target, "character.rom"}, buffer.data() + 16 + cartridge.prgrom, cartridge.chrrom);
  return success();
}
