/*
compile with
gcc -Wall -g -O2 `pkg-config --cflags "glib-2.0 gio-2.0"`   -c -o giodebug.o giodebug.c
gcc  `pkg-config --libs "glib-2.0 gio-2.0"`  -o giodebug giodebug.o
*/
#include <stdlib.h>
#include <glib.h>
#include <gio/gio.h>

static void load_contents_async_lcb(GObject *source_object,GAsyncResult *res,gpointer user_data) {
	gchar *contents=NULL, *etag_out=NULL;
	gsize length=0;
	GError *error=NULL;
	
	GFile *file=user_data;
	g_print("load_contents_async_lcb, started\n");
	if (g_file_load_contents_finish(file,res,&contents,&length,&etag_out,&error)) {
		g_print("%s",contents);
	} else if (error) {
		g_print("got error %d: %s\n",error->code,error->message);
	}
	g_free(contents);
	g_free(etag_out);
	g_object_unref(file);
	g_print("load_contents_async_lcb, done, you might as well hit <ctrl><c> now\n");
}

int main(int argc, char **argv) {
	GMainLoop *mainloop;
	GFile *file;
	g_type_init();
	if (argc <= 1) {
		g_print("pass a URL as parameter\n");
		exit(0);
	}
	
	mainloop = g_main_loop_new(NULL,TRUE);
	g_print("parsing %s\n",argv[1]);
	file = g_file_new_for_commandline_arg(argv[1]);
	g_file_load_contents_async(file,NULL,load_contents_async_lcb,file);

	
	g_main_loop_run(mainloop);
	return 0;
}
