main: use boost::program_options and other cleanups

Change-Id: I5d20e78aaf782b6d0013175f9db9d2eb33156776
diff --git a/src/main.cpp b/src/main.cpp
index 800b8c7..6229759 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2018,  Regents of the University of California.
+/*
+ * Copyright (c) 2018-2019, 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.
@@ -18,82 +18,75 @@
  */
 
 #include "config.hpp"
+#include "extended-error-message.hpp"
 #include "repo.hpp"
 
-#include "extended-error-message.hpp"
+#include <string.h> // for strsignal()
+
+#include <boost/asio/io_service.hpp>
+#include <boost/asio/signal_set.hpp>
+#include <boost/program_options.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;
-    }
+  std::string configFile(DEFAULT_CONFIG_FILE);
+
+  namespace po = boost::program_options;
+  po::options_description optsDesc("Options");
+  optsDesc.add_options()
+    ("help,h",        "print this help message and exit")
+    ("config-file,c", po::value<std::string>(&configFile)->default_value(configFile),
+                      "path to configuration file")
+    ;
+
+  po::variables_map vm;
+  try {
+    po::store(po::parse_command_line(argc, argv, optsDesc), vm);
+    po::notify(vm);
+  }
+  catch (const po::error& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 2;
+  }
+  catch (const boost::bad_any_cast& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 2;
   }
 
+  if (vm.count("help") != 0) {
+    std::cout << "Usage: " << argv[0] << " [options]\n"
+              << "\n"
+              << optsDesc;
+    return 0;
+  }
+
+  boost::asio::io_service ioService;
+
+  /// \todo reload config file on SIGHUP
+  boost::asio::signal_set signalSet(ioService, SIGINT, SIGTERM);
+  signalSet.async_wait([&ioService] (const boost::system::error_code& error, int signalNo) {
+    if (!error) {
+      NDN_LOG_FATAL("Exiting on signal " << signalNo << "/" << strsignal(signalNo));
+      ioService.stop();
+    }
+  });
+
   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();
+    repo::Repo repo(ioService, repo::parseConfig(configFile));
+    repo.initializeStorage();
+    repo.enableValidation();
+    repo.enableListening();
 
     ioService.run();
   }
   catch (const std::exception& e) {
     NDN_LOG_FATAL(repo::getExtendedErrorMessage(e));
-    return 2;
+    return 1;
   }
 
   return 0;