/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
 * Copyright (c) 2018,  Regents of the University of California.
 *
 * This file is part of NDN repo-ng (Next generation of NDN repository).
 * See AUTHORS.md for complete list of repo-ng authors and contributors.
 *
 * repo-ng 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.
 *
 * repo-ng 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
 * repo-ng, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.hpp"
#include "repo.hpp"

#include "extended-error-message.hpp"

#include <ndn-cxx/util/logger.hpp>

NDN_LOG_INIT(repo.main);

static const std::string ndnRepoUsageMessage =
  /* argv[0] */ " - Next generation of NDN repository\n"
  "-h: show help message\n"
  "-c: set config file path\n"
  ;

void
terminate(boost::asio::io_service& ioService,
          const boost::system::error_code& error,
          int signalNo,
          boost::asio::signal_set& signalSet)
{
  if (error)
    return;

  if (signalNo == SIGINT || signalNo == SIGTERM) {
    ioService.stop();
    std::cout << "Caught signal '" << strsignal(signalNo) << "', exiting..." << std::endl;
  }
  else {
    /// \todo try to reload config file
    signalSet.async_wait(std::bind(&terminate, std::ref(ioService), _1, _2, std::ref(signalSet)));
  }
}

int
main(int argc, char** argv)
{
  std::string configPath = DEFAULT_CONFIG_FILE;
  int opt;
  while ((opt = getopt(argc, argv, "hc:")) != -1) {
    switch (opt) {
    case 'h':
      std::cout << argv[0] << ndnRepoUsageMessage << std::endl;
      return 1;
    case 'c':
      configPath = std::string(optarg);
      break;
    default:
      break;
    }
  }

  try {
    boost::asio::io_service ioService;
    repo::Repo repoInstance(ioService, repo::parseConfig(configPath));

    boost::asio::signal_set signalSet(ioService);
    signalSet.add(SIGINT);
    signalSet.add(SIGTERM);
    signalSet.add(SIGHUP);
    signalSet.add(SIGUSR1);
    signalSet.add(SIGUSR2);
    signalSet.async_wait(std::bind(&terminate, std::ref(ioService),
                                   std::placeholders::_1, std::placeholders::_2,
                                   std::ref(signalSet)));

    repoInstance.initializeStorage();

    repoInstance.enableValidation();

    repoInstance.enableListening();

    ioService.run();
  }
  catch (const std::exception& e) {
    NDN_LOG_FATAL(repo::getExtendedErrorMessage(e));
    return 2;
  }

  return 0;
}
