/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * 17-04-2008 20h51 by Vamps (vamps@alterinet.org)
 * 
 * Compilation : gcc -Wall -O3 -o entry entry.c `pkg-config --cflags --libs gtk+-2.0`
 * */

#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define MAX_FILE_LENGHT 255

int file_exist (char *file);
void delete_event (GtkWidget * widget, GdkEvent * event, gpointer * data);
void execute (GtkWidget * widget, gpointer * data);
int arg_split (char *file, char *arg[]);

GtkWidget *entry;

int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *ok;
  GtkWidget *box;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_window_set_title (GTK_WINDOW (window), "Entry");

  gtk_signal_connect (GTK_OBJECT (window), "delete_event",
		      GTK_SIGNAL_FUNC (delete_event), NULL);

  gtk_container_border_width (GTK_CONTAINER (window), 1);

  box = gtk_hbox_new (FALSE, 0);

  gtk_container_add (GTK_CONTAINER (window), box);

  ok = gtk_button_new_with_label ("ok");
  entry = gtk_entry_new ();

  gtk_signal_connect (GTK_OBJECT (ok), "clicked",
		      GTK_SIGNAL_FUNC (execute), NULL);

  gtk_box_pack_start (GTK_BOX (box), entry, TRUE, TRUE, 0);

  GTK_WIDGET_SET_FLAGS (ok, GTK_CAN_DEFAULT);
  gtk_box_pack_start (GTK_BOX (box), ok, TRUE, TRUE, 0);

  gtk_widget_grab_default (ok);
  gtk_widget_show (ok);

  gtk_widget_grab_focus (entry);
  gtk_widget_show (entry);

  gtk_widget_show (box);
  gtk_widget_show (window);
  gtk_main ();

  return 0;
}

void
execute (GtkWidget * widget, gpointer * data)
{
  char *path;
  int i, a;
  char file[MAX_FILE_LENGHT];
  char temp[MAX_FILE_LENGHT];
  int end = 0;
  char *arg[MAX_FILE_LENGHT];
  int nb_arg = 0;

  path = getenv ("PATH");
  memset (temp, 0, MAX_FILE_LENGHT);
  strcpy (temp, gtk_entry_get_text (GTK_ENTRY (entry)));
  a = 0;

  if (temp[0] == '\0')
    {
      gtk_main_quit ();
      exit (1);
    }

  nb_arg = arg_split (temp, arg);

  if (temp[0] == '/')
    {
      if (nb_arg <= 1)
	execlp (arg[0], NULL, NULL);
      else
	execvp (arg[0], arg);
    }
  else
    {
      while (end == 0)
	{
	  memset (file, 0, MAX_FILE_LENGHT);
	  for (i = 0; path[a] != ':'; i++, a++)
	    {
	      if (path[a] == '\0')
		{
		  end = 1;
		  break;
		}
	      file[i] = path[a];
	    }
	  a++;
	  sprintf (file, "%s/%s", file, arg[0]);
	  if (file_exist (file) == 1)
	    {
	      gtk_main_quit ();
	      if (nb_arg <= 1)
		execlp (file, NULL, NULL);
	      else
		execvp (file, arg);
	    }
	}
    }

  for (a = 0; a < nb_arg; a++)
    free (arg[a]);

  gtk_main_quit ();
}

void
delete_event (GtkWidget * widget, GdkEvent * event, gpointer * data)
{
  gtk_main_quit ();
}

int
file_exist (char *file)
{
  FILE *test;

  test = fopen (file, "r");
  if (test != NULL)
    {
      fclose (test);
      return 1;
    }
  else
    return 0;

}

int
arg_split (char *file, char *arg[])
{
  int a;
  int end = 0;
  char byte[MAX_FILE_LENGHT];
  int nb_arg;
  int save = 0;

  nb_arg = 0;

  while (end == 0)
    {
      memset (byte, 0, MAX_FILE_LENGHT);
      for (a = 0; file[save] != ' '; a++)
	{
	  if (file[save] == '\0')
	    {
	      end = 1;
	      break;
	    }

	  byte[a] = file[save];
	  save++;
	}
      save++;
      if (file[save] != ' ')	// au cas ou des espaces trainent
	{
	  arg[nb_arg] = NULL;
	  arg[nb_arg] =
	    malloc (sizeof (char) * MAX_FILE_LENGHT);
	  memset (arg[nb_arg], 0, MAX_FILE_LENGHT);
	  if (arg[nb_arg] == NULL)
	    {
	      g_print ("Erreur d'allocation mémoire !\n");
	      gtk_main_quit ();
	      exit (1);
	    }
	  strcpy (arg[nb_arg], byte);
	  nb_arg++;

	}
    }
  arg[(nb_arg + 1)] = (char *) NULL;
/*	for(a = 0; a < nb_arg; a++)
		printf("arg[%d] = %s\n", a, arg[a]);*/

  return nb_arg;
}
