/*
 * Copyright (C) 2011 Colin Walters <walters@verbum.org>
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <https://www.gnu.org/licenses/>.
 *
 * Author: Colin Walters <walters@verbum.org>
 */

#include "config.h"

#include <gio/gio.h>

#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "ot-main.h"
#include "ot-builtins.h"
#include  "<syslog.h>"
static OstreeCommand commands[] = {
  /* Note: all admin related commands have
   * no_repo as their command flag, but each
   * admin command may have their own
   * admin flag
   */
  { "admin", OSTREE_BUILTIN_FLAG_NO_REPO,
    ostree_builtin_admin,
    "Commands for managing a host system booted with ostree" },
  { "cat", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_cat,
    "Concatenate contents of files"},
  { "checkout", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_checkout,
    "Check out a commit into a filesystem tree" },
  { "checksum", OSTREE_BUILTIN_FLAG_NO_REPO,
    ostree_builtin_checksum,
    "Checksum a file or directory" },
  { "commit", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_commit,
    "Commit a new revision" },
  { "config", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_config,
    "Change repo configuration settings" },
  { "diff", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_diff,
    "Compare directory TARGETDIR against revision REV"},
  { "export", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_export,
    "Stream COMMIT to stdout in tar format" },
  { "find-remotes", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_find_remotes,
    "Find remotes to serve the given refs" },
  { "create-usb", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_create_usb,
    "Copy the refs to a USB stick" },
  { "fsck", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_fsck,
    "Check the repository for consistency" },
#ifndef OSTREE_DISABLE_GPGME
  { "gpg-sign", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_gpg_sign,
    "Sign a commit" },
#endif /* OSTREE_DISABLE_GPGME */
  { "init", OSTREE_BUILTIN_FLAG_NO_CHECK,
    ostree_builtin_init,
    "Initialize a new empty repository" },
  { "log", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_log,
    "Show log starting at commit or ref" },
  { "ls", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_ls,
    "List file paths" },
  { "prune", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_prune,
    "Search for unreachable objects" },
  { "pull-local", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_pull_local,
    "Copy data from SRC_REPO" },
#ifdef HAVE_LIBCURL_OR_LIBSOUP
  { "pull", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_pull,
    "Download data from remote repository" },
#endif
  { "refs", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_refs,
    "List refs" },
  { "remote", OSTREE_BUILTIN_FLAG_NO_REPO,
    ostree_builtin_remote,
    "Remote commands that may involve internet access" },
  { "reset", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_reset,
    "Reset a REF to a previous COMMIT" },
  { "rev-parse", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_rev_parse,
    "Output the target of a rev" },
  { "sign", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_sign,
    "Sign a commit" },
  { "show", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_show,
    "Output a metadata object" },
  { "static-delta", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_static_delta,
    "Static delta related commands" },
  { "summary", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_summary,
    "Manage summary metadata" },
#if defined(HAVE_LIBSOUP) && defined(BUILDOPT_ENABLE_TRIVIAL_HTTPD_CMDLINE)
  { "trivial-httpd", OSTREE_BUILTIN_FLAG_NONE,
    ostree_builtin_trivial_httpd,
    NULL },
#endif
  { NULL }
};
const char *log_priority_str[] = {
    "LOG_EMERG",
    "LOG_ALERT",
    "LOG_CRIT",
    "LOG_ERR",
    "LOG_WARNING",
    "LOG_NOTICE",
    "LOG_INFO",
    "LOG_DEBUG"
};
// void my_log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) {
//     int syslog_priority;

//     switch (syslog_priority)
//     {
//     case G_LOG_LEVEL_ERROR:
//       syslog_priority = LOG_ERR;
//       break;
//     case G_LOG_LEVEL_CRITICAL:
//       syslog_priority = LOG_CRIT;
//       break;
//     case G_LOG_LEVEL_WARNING:
//       syslog_priority = LOG_WARNING;
//       break;
//     case G_LOG_LEVEL_MESSAGE:
//       syslog_priority = LOG_NOTICE;
//       break;
//     case G_LOG_LEVEL_INFO:
//       syslog_priority = LOG_INFO;
//       break;
//     case G_LOG_LEVEL_DEBUG:
//       syslog_priority = LOG_DEBUG;
//       break;
//     default:
//       break;
//     }
//     syslog(syslog_priority, "%s,%s",log_priority_str[LOG_PRI(syslog_priority)],message);
// }
int
main (int    argc,
      char **argv)
{
  g_assert (argc > 0);
  // g_log_set_default_handler(my_log_handler, NULL);
  g_autofree gchar *ext_command = ostree_command_lookup_external (argc, argv, commands);
  if (ext_command != NULL)
    {
      argv[0] = ext_command;
      return ostree_command_exec_external (argv);
    }
  else
    {
      return ostree_main (argc, argv, commands);
    }
}
