/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  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; either 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 Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#ifdef         __SCRIPT_ENABLE


#include "Script.h"
#include "Debug.h"
#include "Com/Communication.h"
#include "Com/Commun.h"
#include "main.h"
#include "Interface/Script_Interface.h"
#include "Animation.h"
#include "Modules.h"
#include "Objet.h"

void Retour_Script (gpointer * Donnee)
{
	struct struct_Retour_Script *Message = (struct struct_Retour_Script *) Donnee;
	struct Struct_Equipement *Equipement = NULL;
	struct Struct_Modele_Objet *Objet = NULL;
	GtkTextBuffer *buffer = NULL;
	GtkTextIter iter;

	ERROR_if (Donnee == NULL);
	Objet = Recherche_Objet_Equipement (Message->ID_Equipement, Message->ID_Reseau);
	if (Objet == NULL) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "(Equipment unknown)");
		return;
	}
	Equipement = (struct Struct_Equipement *) Objet->Pointeur_Attacher;
	if ((Modules_Actuel == MODULES_SCRIPTS) && (Equipement == Equipement_Selectionner)) {
		buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview_Script));
		gtk_text_buffer_get_end_iter (buffer, &iter);
		gtk_text_buffer_insert (buffer, &iter, (const gchar *) Message->Retour_Console, -1);
		gdk_threads_enter ();
		refresh ();
		gdk_threads_leave ();
		gtk_text_buffer_get_iter_at_offset (buffer, &iter, -1);
		gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (textview_Script), &iter, 0, FALSE, 0, 0);
		gdk_threads_enter ();
		refresh ();	
		gdk_threads_leave ();
	}
}


void Retour_Script_Terminer (gpointer * Donnee)
{

#define Message_Fin "**********************************************************"
	struct struct_Retour_Script_Terminer *Message = (struct struct_Retour_Script_Terminer *) Donnee;
	struct Struct_Equipement *Equipement = NULL;
	struct Struct_Modele_Objet *Objet = NULL;
	GtkTextBuffer *buffer = NULL;
	GtkTextIter iter;

	ERROR_if (Donnee == NULL);
	Objet = Recherche_Objet_Equipement (Message->ID_Equipement, Message->ID_Reseau);
	if (Objet == NULL) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "(Equipment unknown)");
		return;
	}
	Equipement = (struct Struct_Equipement *) Objet->Pointeur_Attacher;
	
	if ((Modules_Actuel == MODULES_SCRIPTS) && (Equipement == Equipement_Selectionner)) {
		buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview_Script));
		gtk_text_buffer_get_end_iter (buffer, &iter);
		gtk_text_buffer_insert (buffer, &iter, (const gchar *) Message_Fin, -1);
		gdk_threads_enter ();
		refresh ();	
		gdk_threads_leave ();
		gtk_text_buffer_get_iter_at_offset (buffer, &iter, -1);
		gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (textview_Script), &iter, 0, FALSE, 0, 0);
		gdk_threads_enter ();
		refresh ();	
		gdk_threads_leave ();
	}

}


void Executer_Script (gchar * script, struct Struct_Equipement *Equipement)
{

	GtkTextBuffer *buffer = NULL;
	gchar     ligne[255];
	GtkTextIter iter;
	gint      Taille, pid_fils,ID_Reseau;
	unsigned char ID_Equipement[MAC_ADDR_LEN + IP_ADDR_LEN + 1];

	ERROR_if (script == NULL);
	ERROR_if (Equipement == NULL);
	Taille = index (script, ' ') - script;
	strncpy (ligne, script, Taille);
	ligne[Taille] = '\0';

	if (!g_file_test(ligne,G_FILE_TEST_EXISTS)){
		debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Error loading executable : [%s]", ligne);
		return;
	}
	sprintf (ligne, "\n*******  Run : [%s] *****************************\n", script);
	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview_Script));
	gtk_text_buffer_get_end_iter (buffer, &iter);
	gtk_text_buffer_insert (buffer, &iter, (const gchar *) ligne, -1);
	refresh ();
	gtk_text_buffer_get_iter_at_offset (buffer, &iter, -1);
	gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (textview_Script), &iter, 0, FALSE, 0, 0);
	refresh ();
	memcpy (ID_Equipement, Equipement->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
    ID_Reseau = ID_Reseau_En_Cours;
	pid_fils = fork ();
	if (pid_fils == 0) {
		struct struct_Retour_Script Message_Retour;
		struct struct_Retour_Script_Terminer Message_Retour_Terminer;
		FILE     *sortie = NULL;
		gchar     command[255], Ncommand[255], *pVariable = NULL;

#define VARIABLE_IP "_IP_"
#define VARIABLE_MAC "_MAC_"

		strcpy (command, script);
		if ((pVariable = strstr (command, VARIABLE_IP)) != 0) {
			strncpy (Ncommand, command, pVariable - command);
			Ncommand[pVariable - command] = '\0';
			strcat (Ncommand, Equipement->Adresse_Ip_Char);
			strcat (Ncommand, pVariable + strlen (VARIABLE_IP));
			strcpy (command, Ncommand);
		}

		if ((pVariable = strstr (command, VARIABLE_MAC)) != 0) {
			strncpy (Ncommand, command, pVariable - command);
			Ncommand[pVariable - command] = '\0';
			strcat (Ncommand, Equipement->Adresse_MAC_Char);
			strcat (Ncommand, pVariable + strlen (VARIABLE_MAC));
			strcpy (command, Ncommand);
		}

		if ((sortie = popen (command, "r")) == NULL) {
			printf ("popen  : %s", strerror (errno));
			exit (127);
		}

		while (fgets (ligne, 254, sortie) != NULL) {
			bzero (&Message_Retour, sizeof (struct struct_Retour_Script));
			Taille = strlen (ligne);
			strncpy (Message_Retour.Retour_Console, ligne, Taille);
			Message_Retour.Retour_Console[Taille] = 0;
			memcpy (Message_Retour.ID_Equipement, ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
			Message_Retour.ID_Reseau = ID_Reseau;
			Communication_Envoie_Donnee (Pid_AutoScan, RETOUR_SCRIPT, (gpointer *) & Message_Retour, sizeof (Message_Retour), NULL);
		}
		pclose (sortie);

		bzero (&Message_Retour_Terminer, sizeof (struct struct_Retour_Script_Terminer));
		memcpy (Message_Retour_Terminer.ID_Equipement, ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
		Message_Retour_Terminer.ID_Reseau = ID_Reseau;
		Communication_Envoie_Donnee (Pid_AutoScan, RETOUR_SCRIPT_TERMINER, (gpointer *) & Message_Retour_Terminer, sizeof (Message_Retour_Terminer), NULL);
		_exit (0);
	}
}





	/*struct struct_Demande_Lancement_Script Message_Demande;
	 * 
	 * bzero (&Message_Demande, sizeof (struct struct_Demande_Lancement_Script));
	 * Message_Demande.ID_Reseau = Reseau_Selectionner->Id_Reseau;
	 * strcpy (Message_Demande.Script, script);
	 * memcpy (Message_Demande.ID_Equipement, Equipement->ID_Equipement, MAC_ADDR_LEN + IP_ADDR_LEN + 1);
	 * Communication_Envoie_Donnee (Reseau_Selectionner->Pid_Agent, DEMANDE_LANCEMENT_SCRIPT, (gpointer *) & Message_Demande, sizeof (Message_Demande), Reseau_Selectionner);
	 * 
	 * }
	 */



#endif
