#include <glib.h>

static void test (GRegex *old, GRegex *re, const gchar *s, gboolean always_print)
{
        GMatchInfo *old_mi = NULL;
        GMatchInfo *mi = NULL;
        gchar *old_name, *old_section, *old_hash;
        gchar *name, *section, *hash;
        gboolean not_same;

        old_name = old_section = old_hash = NULL;
        name = section = hash = NULL;

        if (g_regex_match (old, s, 0, &old_mi)) {
                old_name = g_match_info_fetch (old_mi, 1);
                old_section = g_match_info_fetch (old_mi, 3);
                if (!old_section || old_section[0] == '\0')
                        old_section = g_match_info_fetch (old_mi, 4);
                old_hash = g_match_info_fetch (old_mi, 6);
        }

        if (g_regex_match (re, s, 0, &mi)) {
                name = g_match_info_fetch (mi, 1);
                section = g_match_info_fetch (mi, 3);
                if (!section || section[0] == '\0')
                        section = g_match_info_fetch (mi, 4);
                hash = g_match_info_fetch (mi, 6);
        }

        not_same = g_strcmp0 (name, old_name) != 0 ||
                   g_strcmp0 (section, old_section) != 0 ||
                   g_strcmp0 (hash, old_hash) != 0;

        if (not_same) {
                g_print ("%s -> old: %s %s %s    new: %s %s %s\n",
                         s,
                         old_name, old_section, old_hash,
                         name, section, hash);
        }
        else if (always_print) {
                g_print ("%s -> %s %s %s\n",
                         s,
                         name, section, hash);
        }
}

int
main (int argc, char *argv[])
{
        GRegex *old;
        GRegex *re;
        GError *error = NULL;

        old = g_regex_new ("man:([^ /.()#]+)"
                          "(\\(([0-9A-Za-z]+)\\)|\\.([0-9A-Za-z]+)|)"
                          "(#([^/ ()]+))?",
                          0, 0, &error);
        if (!old) {
                g_error ("g_regex_new failed: %s", error->message);
                g_error_free (error);
                return 1;
        }

        re = g_regex_new ("man:((?:[^ /.()#]|\\.(?=[^0-9]))+)"
                          "(\\(([0-9A-Za-z]+)\\)|\\.([0-9A-Za-z]+)|)"
                          "(#([^/ ()]+))?",
                          0, 0, &error);

        if (!re) {
                g_error ("g_regex_new failed: %s", error->message);
                g_error_free (error);
                return 1;
        }

        if (argc > 1) {
                gint i;
                for (i = 1; i < argc; i++) {
                        test (old, re, argv[i], TRUE);
                }
        }
        else {
                test (old, re, "man:printf", FALSE);
                test (old, re, "man:printf.1", FALSE);
                test (old, re, "man:printf(1)", FALSE);
                test (old, re, "man:printf(1)#foo", FALSE);
                test (old, re, "man:yum-groups-manager.1", FALSE);
                test (old, re, "man:xml2po", FALSE);
                test (old, re, "man:whois.jwhois", FALSE);
                test (old, re, "man:whois.jwhois.1", FALSE);
                test (old, re, "man:whois.jwhois(1)", FALSE);
                test (old, re, "man:vbox_file.5", FALSE);
                test (old, re, "man:wpa_supplicant.conf.5", FALSE);
                test (old, re, "man:x509v3_config.5ssl", FALSE);
        }

        return 0;
}

