/* gcc -g -Wall gfile-resolve-dir.c $(pkg-config --cflags --libs gio-2.0 glib-2.0) -o gfile-resolve-dir */
#include <stdio.h>
#include <glib.h>
#include <gio/gio.h>

int
resolve_link (const char *source)
{
	GFile *file, *rfile, *dir;
	gchar *link, *rlink;
	GError *error = NULL;

	printf ("resolving %s\n", source);
	file = g_file_new_for_path (source);

	dir = g_file_get_parent (file);
	rlink = g_file_get_path (dir);
	printf ("  parent=%s\n", rlink);
	g_free (rlink);

	link = g_file_read_link (source, &error);
	if (error)
	{
		fprintf (stderr, "g_file_read_link() failed: %s\n", error->message);
		g_error_free (error);
		return 1;
	}
	printf ("  link=%s\n", link);
	rfile = g_file_resolve_relative_path(dir, link);
	rlink = g_file_get_path (rfile);
	printf ("  resolved=%s\n", rlink);
	g_free (rlink);

	g_free (link);
	return 0;
}

int
main()
{
	g_type_init ();

	resolve_link ("/usr/lib64/browser-plugins/opensc-signer.so");
	resolve_link ("/usr/lib64/browser-plugins/npwrapper.so");
	return 0;
}
