/* ***********************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.
 **************************************************************************************/
#ifndef __CONFIG_STATIC

#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include "Conf.h"
#include "main.h"
#include "Debug.h"
#include "Gconf.h"
#include "Debug.h"
#include "Declaration_Constante.h"
#include "Macro.h"


gboolean Copie_Fichier_Config (gchar * Source, gchar * Destination)
{
	FILE     *fsource = NULL, *fdestination = NULL;
	gchar     buffer[255];

	debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Copy file (%s)",Destination);

	fsource = fopen (Source, "r");
	if (fsource == NULL) {
		debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Reading file: %s: %s", Source, strerror (errno));
		return FALSE;
	}

	fdestination = fopen (Destination, "w");
	if (fdestination == NULL) {
		debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Creating file: %s: %s", Destination, strerror (errno));
		fclose (fsource);
		return FALSE;
	}
	while (fgets (buffer, sizeof (buffer), fsource) != NULL) {
		if (fputs (buffer, fdestination) == EOF)
			break;
	}
	fclose (fsource);
	fclose (fdestination);
	return TRUE;
}


void Copie_Fichier_Configuration (void)
{
	DIR      *dir = NULL;
	gchar    *Chemin = NULL;


	Chemin = g_strdup_printf ("%s/.autoscan-network", Info_User->pw_dir);

	dir = opendir (Chemin);
	if (dir == NULL) {
	
	
		if (mkdir (Chemin, 0700) != 0) {
			debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Creating folder: %s: %s", Chemin, strerror (errno));
			exit (1);
		}
		dir = opendir (Chemin);
		if (dir == NULL) {
			printf("ERROR : %s not found", Chemin);
			exit (-1);
		}
	}
	closedir (dir);
	MYFREE (Chemin);


	Chemin = g_strdup_printf ("%s/.autoscan-network/config", Info_User->pw_dir);
	dir = opendir (Chemin);
	if (dir == NULL) {
	
		if (mkdir (Chemin, 0700) != 0) {
			debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Creating folder: %s: %s", Chemin, strerror (errno));
			exit (1);
		}
		dir = opendir (Chemin);
		if (dir == NULL) {
			debug_printf (INTERFACE, D_WARN_USER, __FILE__, __PRETTY_FUNCTION__, __LINE__, "%s not found", Chemin);
			exit (-1);
		}
	}
	closedir (dir);
	MYFREE (Chemin);

	Chemin = g_strdup_printf ("%s/.autoscan-network/config/Port.xml", Info_User->pw_dir);
	if (!g_file_test(Chemin,G_FILE_TEST_EXISTS))

		if (Copie_Fichier_Config (PATH_CONF "Port.xml", Chemin) == FALSE)
			exit (1);
	MYFREE (Chemin);

	Chemin = g_strdup_printf ("%s/.autoscan-network/config/Subnet.xml", Info_User->pw_dir);
	if (!g_file_test(Chemin,G_FILE_TEST_EXISTS))

		if (Copie_Fichier_Config (PATH_CONF "Subnet.xml", Chemin) == FALSE)
			exit (1);
	MYFREE (Chemin);

	
	Chemin = g_strdup_printf ("%s/.autoscan-network/config/" FILE_XML_FINGER_PRINTING , Info_User->pw_dir);
	if (!g_file_test(Chemin,G_FILE_TEST_EXISTS))
		if (Copie_Fichier_Config (PATH_CONF "" FILE_XML_FINGER_PRINTING, Chemin) == FALSE)
			exit (1);


	MYFREE (Chemin);

#ifdef         __SCRIPT_ENABLE
	Chemin = g_strdup_printf ("%s/.autoscan-network/config/Script.xml", Info_User->pw_dir);
	if (!g_file_test(Chemin,G_FILE_TEST_EXISTS))

		if (Copie_Fichier_Config (PATH_CONF "Script.xml", Chemin) == FALSE)
			exit (1);
	MYFREE (Chemin);	
#endif

	Chemin = g_strdup_printf ("%s/.autoscan-network/config/Restore.xml", Info_User->pw_dir);
	if (!g_file_test(Chemin,G_FILE_TEST_EXISTS))

		if (Copie_Fichier_Config (PATH_CONF"Restore.xml", Chemin) == FALSE)
			exit (1);
	MYFREE (Chemin);
}
#endif
