// Main function.
int main(int argc, char** argv) {
  cout.sync_with_stdio(false);
  std::cout << std::endl
            << EMPH_ON << "WriteIndexListsMain, version " << __DATE__ << " "
            << __TIME__ << EMPH_OFF << std::endl
            << std::endl;

  char* locale = setlocale(LC_CTYPE, "");
  cout << "Set locale LC_CTYPE to: " << locale << endl;

  std::locale loc;
  ad_utility::ReadableNumberFacet facet(1);
  std::locale locWithNumberGrouping(loc, &facet);
  ad_utility::Log::imbue(locWithNumberGrouping);

  string indexName = "";
  bool freebase = false;

  optind = 1;
  // Process command line arguments.
  while (true) {
    int c = getopt_long(argc, argv, "i:f", options, NULL);
    if (c == -1) break;
    switch (c) {
      case 'i':
        indexName = optarg;
        break;
      case 'f':
        freebase = true;
        break;
      default:
        cout << endl
             << "! ERROR in processing options (getopt returned '" << c
             << "' = 0x" << std::setbase(16) << c << ")" << endl
             << endl;
        exit(1);
    }
  }

  if (indexName.size() == 0) {
    cout << "Missing required argument --index (-i)..." << endl;
    exit(1);
  }

  try {
    Index index;
    index.createFromOnDiskIndex(indexName);
    index.addTextFromOnDiskIndex();

    vector<string> lists;
    lists.push_back("algo*");
    bool decodeGapsAndFrequency = true;
    index.dumpAsciiLists(lists, decodeGapsAndFrequency);

    Engine engine;
    QueryExecutionContext qec(index, engine);
    ParsedQuery q;
    if (!freebase) {
      q = SparqlParser::parse("SELECT ?x WHERE {?x <is-a> <Scientist>}");
    } else {
      q = SparqlParser::parse(
          "PREFIX fb: <http://rdf.freebase.com/ns/> SELECT ?p WHERE {?p "
          "fb:people.person.profession fb:m.06q2q}");
      q.expandPrefixes();
    }
    QueryPlanner queryPlanner(&qec);
    auto qet = queryPlanner.createExecutionTree(q);
    const auto res = qet.getResult();
    AD_CHECK(res->size() > 0);
    AD_CHECK(res->_data.cols() == 1);
    string personlistFile = indexName + ".list.scientists";
    std::ofstream f(personlistFile.c_str());
    const IdTable& ids = res->_data;
    for (size_t i = 0; i < ids.size(); ++i) {
      f << ids(i, 0) << ' ';
    }
    f.close();

  } catch (const std::exception& e) {
    cout << e.what() << std::endl;
  }

  return 0;
}