/* Sankar P - GPL V3 or Later */

#include <mail/em-popup.h>
#include <gtk/gtk.h>

#define d(x) (x)

void org_gnome_url_operations_apply_patch (void *ep, EMPopupTargetURI *t);

void
org_gnome_url_operations_apply_patch (void *ep, EMPopupTargetURI *t)
{
	printf ("\n\aGetting URI : [%s] \n\a", t->uri);
	GtkWidget *dialog;
	gchar *command;
	gchar *url;

	url = g_strdup ("http://bugzilla.gnome.org/attachment.cgi?id=105067&action=view");


	dialog = gtk_file_chooser_dialog_new ("Apply patch in directory",
			NULL,
			GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
			GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
			NULL);

	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	{
		char *filename;

		filename = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (dialog));
		d(printf ("\n\aFolder selected is : [%s] \n\a", filename ));

		d(printf ("\n\a############################################# \n\a"));
		command = g_strdup_printf ("wget -O ~/Desktop/evo-plugin-patch \"%s\"", url);
		system (command);

		d(printf ("\n\a############################################# \n\a"));

		command = g_strdup_printf ("patch -s -d %s -p0 --dry-run < ~/Desktop/evo-plugin-patch", filename);
		system (command);

		g_free (filename);
	}
	gtk_widget_destroy (dialog);


}
