#include <string.h>
#include <glib.h>

int
main (int argc, char **argv)
{
  gint i;

  if (argc < 2)
    {
      g_printerr ("Must give at least one filename to process\n");
      return 1;
    }

  for (i = 1; i < argc; i++)
    {
      const gchar *filename = argv[i];
      GError *error = NULL;
      gchar *in, *out;

      if (!g_file_get_contents (filename, &in, NULL, &error))
        {
          g_printerr ("%s: %s\n", filename, error->message);
          g_error_free (error);
          continue;
        }

      out = g_utf8_normalize (in, -1, G_NORMALIZE_ALL_COMPOSE);

      if (strcmp (in, out) != 0)
        {
          if (g_file_set_contents (filename, out, -1, &error))
            {
              g_print ("%s: reduced by %d bytes\n", filename,
                       (int) (strlen (in) - strlen (out)));
            }
          else
            {
              g_printerr ("%s: %s\n", filename, error->message);
              g_error_free (error);
            }
        }
      else
        g_print ("%s: no changes\n", filename);

      g_free (out);
      g_free (in);
    }

  return 0;
}
