/* ***********************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.
 **************************************************************************************/
#include <gtk/gtk.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include "Execute.h"
#include "Com/Initialisation_Com.h"
#include "Debug.h"
#include "main.h"
#include "Macro.h"
#include <sys/stat.h>

int Test_Droit_Execution(const char *executable){
static struct stat _stat;

	if(stat(executable,&_stat)) return FALSE;
	if((_stat.st_mode&(S_IXGRP|S_IXUSR|S_IXOTH|S_ISUID|S_ISGID)) 
	 	&&S_ISREG(_stat.st_mode)) return TRUE;
	return FALSE;
}

char *Cherche_Chemin_Executable(char *commande)
{
	char *path = NULL,*chemin_commande = NULL,*s = NULL,*saveptr = NULL;

	path = getenv("PATH");

	if(!path) return NULL; 
	path = g_strdup(path);
	if(!path) return NULL;
	s = strtok_r(path,":", &saveptr);// on recupere la première repertoire dans le path
	while(s) {
		chemin_commande = g_strdup_printf ("%s/%s",s,commande);
	
		if(Test_Droit_Execution(chemin_commande)) { 
			MYFREE(path);
		
			return chemin_commande;
		}
		MYFREE (chemin_commande);
		s = strtok_r(0,":", &saveptr);//cherche dans le prochain chemin dans path...
	}
	MYFREE (path);

	return NULL;
}

#define LISTE_NAVIGATEUR "firefox,mozilla,epiphany,konqueror,iceweasel,opera,dillo" 
gchar *Recherche_Navigateur(){
gchar *liste = NULL,*s = NULL,*navigateur = NULL,*saveptr = NULL;
	liste = g_strdup(LISTE_NAVIGATEUR);
	s = strtok_r(liste,",", &saveptr);// on recupere la premier navigateur
	while(s) {
	
		if ((navigateur = Cherche_Chemin_Executable(s))!=NULL){
			MYFREE (liste);
		
			return navigateur;
		}
		s = strtok_r(0,",", &saveptr);
	}
	MYFREE (liste);
	return NULL;
}

void Affiche_Page_Html(gchar * url){
gchar *navigateur = NULL;
	
	if ((navigateur = Recherche_Navigateur())!=NULL){
		Execute_Commande(navigateur,url);
		MYFREE (navigateur);
	}
}


void Execute_Commande(gchar * command, gchar * param){
int       pid_fils;	

	pid_fils = fork ();	
	if (pid_fils == 0) {   
	
		execlp (command, command, param, NULL);
		_exit (0);
	}
}


#ifdef         __SCRIPT_ENABLE
void Execute_Programme_Externe (gchar * path, gchar * param, struct Struct_Equipement *Equipement)
{
	int       pid_fils;

	ERROR_if (path == NULL);
	ERROR_if (param == NULL);
	ERROR_if (Equipement == NULL);

	if (!g_file_test(path,G_FILE_TEST_EXISTS)){
		debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Can't find the '%s' program!", path);
		return;
	}
	pid_fils = fork ();
	if (pid_fils == 0) {
#define VARIABLE_IP "_IP_"
#define VARIABLE_MAC "_MAC_"
		gchar     Command[255], NCommand[255], *pVariable;

	
		strcpy (Command, path);
		strcat (Command, " ");
		strcat (Command, param);
		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);
		}
	
		system (Command);
		_exit (0);
	}
}
#endif




	




/*void Kill_Process_AutoScan_Agent ()
{

	if ((Pid_Agent_Initialisation == -1) || (Pid_Agent_Initialisation == 0))
		return;
	debug_printf (INTERFACE, D_INFO, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Arret du processus : %d", Pid_Agent_Initialisation);
	kill (Pid_Agent_Initialisation, SIGINT);
}*/
