/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2014-2018,  Regents of the University of California.
 *
 * This file is part of ndn-tools (Named Data Networking Essential Tools).
 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
 *
 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * Copyright (c) 2011-2014, Regents of the University of California,
 *
 * This file is part of ndndump, the packet capture and analysis tool for Named Data
 * Networking (NDN).
 *
 * ndndump is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * ndndump is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ndndump, e.g., in COPYING file.  If not, see <http://www.gnu.org/licenses/>.
 **/

#include "ndndump.hpp"
#include "core/version.hpp"

#include <sstream>

namespace ndn {
namespace dump {

namespace po = boost::program_options;

static void
usage(std::ostream& os, const std::string& appName, const po::options_description& options)
{
  os << "Usage: " << appName << " [options] [pcap-filter]\n"
     << "\n"
     << "Default pcap-filter:\n"
     << "  '" << NdnDump::getDefaultPcapFilter() << "'\n"
     << "\n"
     << options;
}

static int
main(int argc, char* argv[])
{
  NdnDump instance;
  std::string nameFilter;
  std::vector<std::string> pcapFilter;

  po::options_description visibleOptions("Options");
  visibleOptions.add_options()
    ("help,h",      "print this help message and exit")
    ("interface,i", po::value<std::string>(&instance.interface),
                    "capture packets from the specified interface; if unspecified, the first "
                    "non-loopback interface will be used; on Linux, the special value \"any\" "
                    "can be used to capture from all interfaces")
    ("read,r",      po::value<std::string>(&instance.inputFile),
                    "read packets from the specified file; use \"-\" to read from standard input")
    ("filter,f",    po::value<std::string>(&nameFilter),
                    "print packet only if name matches this regular expression")
    ("no-promiscuous-mode,p", po::bool_switch(), "do not put the interface into promiscuous mode")
    ("no-timestamp,t",        po::bool_switch(), "do not print a timestamp for each packet")
    ("verbose,v",   po::bool_switch(&instance.wantVerbose),
                    "print more detailed information about each packet")
    ("version,V",   "print program version and exit")
    ;

  po::options_description hiddenOptions;
  hiddenOptions.add_options()
    ("pcap-filter", po::value<std::vector<std::string>>(&pcapFilter))
    ;

  po::positional_options_description posOptions;
  posOptions.add("pcap-filter", -1);

  po::options_description allOptions;
  allOptions.add(visibleOptions).add(hiddenOptions);

  po::variables_map vm;
  try {
    po::store(po::command_line_parser(argc, argv).options(allOptions).positional(posOptions).run(), vm);
    po::notify(vm);
  }
  catch (const po::error& e) {
    std::cerr << "ERROR: " << e.what() << "\n\n";
    usage(std::cerr, argv[0], visibleOptions);
    return 2;
  }
  catch (const boost::bad_any_cast& e) {
    std::cerr << "ERROR: " << e.what() << "\n\n";
    usage(std::cerr, argv[0], visibleOptions);
    return 2;
  }

  if (vm.count("help") > 0) {
    usage(std::cout, argv[0], visibleOptions);
    return 0;
  }

  if (vm.count("version") > 0) {
    std::cout << "ndndump " << tools::VERSION << "\n"
              << pcap_lib_version() << std::endl;
    return 0;
  }

  if (vm.count("interface") > 0 && vm.count("read") > 0) {
    std::cerr << "ERROR: conflicting '--interface' and '--read' options specified\n\n";
    usage(std::cerr, argv[0], visibleOptions);
    return 2;
  }

  if (vm.count("filter") > 0) {
    try {
      instance.nameFilter = std::regex(nameFilter);
    }
    catch (const std::regex_error& e) {
      std::cerr << "ERROR: invalid filter regex: " << e.what() << std::endl;
      return 2;
    }
  }

  if (vm.count("pcap-filter") > 0) {
    std::ostringstream os;
    std::copy(pcapFilter.begin(), pcapFilter.end(), make_ostream_joiner(os, " "));
    instance.pcapFilter = os.str();
  }

  instance.wantPromisc = !vm["no-promiscuous-mode"].as<bool>();
  instance.wantTimestamp = !vm["no-timestamp"].as<bool>();

  try {
    instance.run();
  }
  catch (const std::exception& e) {
    std::cerr << "ERROR: " << e.what() << std::endl;
    return 1;
  }
  return 0;
}

} // namespace dump
} // namespace ndn

int
main(int argc, char** argv)
{
  return ndn::dump::main(argc, argv);
}
