/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 *
 * Copyright (C) 2011 Colin Walters <walters@verbum.org>
 *
 * 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <ostree.h>
#include <stdlib.h>

int main (int    argc,
	  char **argv)
{
  GFile *path;
  OstreeRepo *repo;
  const char *commit;
  GError *local_error = NULL;
  GError **error = &local_error;
  GCancellable *cancellable = NULL;
  GHashTable *objects;
  GHashTableIter hiter;
  gpointer key, value;
  char *rev;
  guint n_metadata = 0;
  guint n_content = 0;
  guint n_commits = 0;
  guint64 total_metadata_bytes = 0;
  guint64 total_content_bytes = 0;
  int depth = 0;

  path = g_file_new_for_path (argv[1]);
  commit = argv[2];
  if (argc >= 3)
    depth = atoi (argv[3]);

  repo = ostree_repo_new (path);
  if (!ostree_repo_open (repo, cancellable, error))
    goto out;
  if (!ostree_repo_resolve_rev (repo, commit, FALSE, &rev, error))
    goto out;
  if (!ostree_repo_traverse_commit (repo, rev, depth, &objects,
                                    cancellable, error))
    goto out;

  g_hash_table_iter_init (&hiter, objects);
  while (g_hash_table_iter_next (&hiter, &key, &value))
    {
      GVariant *obj = key;
      const char *checksum;
      OstreeObjectType objtype;
      guint64 objsize;

      ostree_object_name_deserialize (obj, &checksum, &objtype);
      
      if (!ostree_repo_query_object_storage_size (repo, objtype, checksum,
                                                  &objsize, cancellable, error))
        goto out;
      if (OSTREE_OBJECT_TYPE_IS_META (objtype))
        {
          n_metadata++;
          if (objtype == OSTREE_OBJECT_TYPE_COMMIT)
            n_commits++;
          total_metadata_bytes += objsize;
        }
      else
        {
          n_content++;
          total_content_bytes += objsize;
        }
    }
  g_print ("%u objects in %u commits\n", g_hash_table_size (objects), n_commits);
  g_print ("%u metadata objects occupying %llu bytes\n", n_metadata, total_metadata_bytes);
  g_print ("%u content objects occupying %llu bytes\n", n_content, total_content_bytes);
  g_print ("%02f metadata overhead\n", ((double) total_metadata_bytes) / total_content_bytes);

 out:
  if (local_error)
    {
      g_printerr ("%s\n", local_error->message);
      return 1;
    }
  return 0;
}
