/**
 * \fn main (void)
 * \brief Configuration creation and deletion. 
 * \return EXIT_SUCCESS status code: ok. 
 * \return EXIT_FAILURE status code: error.
 */
int main (void){ // char *arge[] permet d'utiliser les redirections avec la fonction popen
  /* BOUCLES */
  int i=0;
  
  /* CONFIGURATION */
  int nbConfigurations                = 0;
  ConfigurationName *tabConfigurations = NULL;
  int configurationChoisie            = 0;
  int nb_circles                      = 0;
  char racinePython[ROOT_SIZE]        = "";
  MACaddress *tabMAC                  = NULL;
  char **counters_names               = NULL; // Le nom de chaque compteurs est stocké dans un tableau de chaine de caractères 
  
  /* MENU */
  int choixMenu                       = 0;
  
  printf("Management of the plugwise configurations :\n");
  printf("\n");
  
  // MENU CONFIGURATIONS
  printf("What do you want to do?\n");
  printf("1: Create a new configuration\n");
  printf("2: Delete a configuration\n");
  
  printf("Your choice: ");
  scanf("%d",&choixMenu);
  printf("\n");
  
  if ((choixMenu != 1) && (choixMenu !=2))
    {
      perror("Error : your choice is not possible !\n");
      return EXIT_FAILURE;
    } 
  
  // Because the source code was make for another program
  choixMenu++;
  
  /// CHOIX 1 : CREATION D'UNE NOUVELLE CONFIGURATION :
  if(choixMenu==2)
    {
      allocation_configurations_names(1, &tabConfigurations);
      write_static_data(choixMenu, 1, tabConfigurations, racinePython, &nb_circles);      
      
      mac_adress_dynamic_allocation(nb_circles,&tabMAC);
      counters_names_dynamic_allocation(nb_circles,&counters_names); 
      write_dynamic_data(choixMenu, nb_circles, tabMAC, counters_names);
      
      free(tabConfigurations);
      deallocation(nb_circles, &tabMAC, &counters_names);
    }
  
  /// CHOIX 2 : SUPPRIMER UNE CONFIGURATION :
  else
    { 
      /// 1. On récupère le nombre et les noms des configurations
      nbConfigurations = nb_configurations();
      allocation_configurations_names(nbConfigurations, &tabConfigurations);
      save_configurations_names(nbConfigurations, tabConfigurations);
      
      /// 2. On demande à l'utilisateur de choisir la configuration à supprimer
      configurationChoisie = configuration_choice(choixMenu, nbConfigurations, tabConfigurations);
      
      /// 3. On récupère les données de la configuration
      system("touch configurations.tmp");
      i=1;
      while(i <= nbConfigurations){
	if(i != configurationChoisie){
	  
	  // Récupération des données
	  static_data_recovery(choixMenu, i, tabConfigurations, racinePython,&nb_circles);
	  mac_adress_dynamic_allocation(nb_circles,&tabMAC);
	  counters_names_dynamic_allocation(nb_circles,&counters_names); 
	  dynamic_data_recovery(i, nb_circles, tabMAC, counters_names);
	  
	  // Permutation fichiers
	  system("mv configurations.txt configurations.txt.tmp");
	  system("mv configurations.tmp configurations.txt");
	  
	  // Ecriture des données
	  write_static_data(choixMenu, i, tabConfigurations, racinePython, &nb_circles);      
	  write_dynamic_data(choixMenu, nb_circles, tabMAC, counters_names);
	  
	  system("mv configurations.txt configurations.tmp");
	  system("mv configurations.txt.tmp configurations.txt");
	}
	i++;
      }
      
      /// 4. On libère la mémoire et renomme le fichier temporaire en configurations.txt
      free(tabConfigurations);
      deallocation(nb_circles, &tabMAC, &counters_names);
      system("rm configurations.txt");
      system("mv configurations.tmp configurations.txt");
    } 

  return EXIT_SUCCESS;
}
/**
 * \fn main (void)
 * \brief Configuration creation and deletion. 
 * \return EXIT_SUCCESS status code: ok. 
 * \return EXIT_FAILURE status code: error.
 */
int main (void){ 
  /* BOUCLES */
  int i=0;
  
  /* CONFIGURATION */
  int nbConfigurations                = 0;
  ConfigurationName *tabConfigurations = NULL;
  int chosenConfiguration             = 0;
  int nb_circles                      = 0;
  char pythonRoot[ROOT_SIZE]        = "";
  MACaddress *tabMAC                  = NULL;
  char **counters_names               = NULL; // the name of each counters is saved in a table of strings
  
  /* MENU */
  int menuChoice                       = 0;
  
  printf("Management of the plugwise configurations :\n");
  printf("\n");
  
  // MENU CONFIGURATIONS
  printf("What do you want to do?\n");
  printf("1: Create a new configuration\n");
  printf("2: Delete a configuration\n");
  
  printf("Your choice: ");
  scanf("%d",&menuChoice);
  printf("\n");
  
  if ((menuChoice != 1) && (menuChoice !=2))
    {
      perror("Error : your choice is not possible !\n");
      return EXIT_FAILURE;
    } 
  
  // Because the source code was made for another program (TO CHANGE)
  menuChoice++;
  
  /// <h2>CHOICE 1 : CREATION OF A NEW CONFIGURATION :</h2>
  if(menuChoice==2)
    {
      allocation_configurations_names(1, &tabConfigurations);
      write_static_data(menuChoice, 1, tabConfigurations, pythonRoot, &nb_circles);      
      
      mac_adress_dynamic_allocation(nb_circles,&tabMAC);
      counters_names_dynamic_allocation(nb_circles,&counters_names); 
      write_dynamic_data(menuChoice, nb_circles, tabMAC, counters_names);
      
      free(tabConfigurations);
      deallocation(nb_circles, &tabMAC, &counters_names);
    }
  
  /// <h2>CHOICE 2 : DELETE A CONFIGURATION:</h2>
  else
    { 
      /// <h3>1. We save the number and the name of the configurations</h3>
      nbConfigurations = nb_configurations();
      allocation_configurations_names(nbConfigurations, &tabConfigurations);
      save_configurations_names(nbConfigurations, tabConfigurations);
      
      /// <h3>2. We ask the user to chose the configuration to delete</h3>
      chosenConfiguration = configuration_choice(menuChoice, nbConfigurations, tabConfigurations);
      
      /// <h3>3. We save the configuration data</h3>
      system("touch configurations.tmp");
      i=1;
      while(i <= nbConfigurations){
	if(i != chosenConfiguration){
	  
	  // Data recovery
	  static_data_recovery(menuChoice, i, tabConfigurations, pythonRoot,&nb_circles);
	  mac_adress_dynamic_allocation(nb_circles,&tabMAC);
	  counters_names_dynamic_allocation(nb_circles,&counters_names); 
	  dynamic_data_recovery(i, nb_circles, tabMAC, counters_names);
	  
	  // Files permutation
	  system("mv configurations.txt configurations.txt.tmp");
	  system("mv configurations.tmp configurations.txt");
	  
	  // Data writing
	  write_static_data(menuChoice, i, tabConfigurations, pythonRoot, &nb_circles);      
	  write_dynamic_data(menuChoice, nb_circles, tabMAC, counters_names);
	  
	  system("mv configurations.txt configurations.tmp");
	  system("mv configurations.txt.tmp configurations.txt");
	}
	i++;
      }
      
      /// <h3>4. We release the memory and rename the temporary file in configurations.txt</h3>
      free(tabConfigurations);
      deallocation(nb_circles, &tabMAC, &counters_names);
      system("rm configurations.txt");
      system("mv configurations.tmp configurations.txt");
    } 
  
  return EXIT_SUCCESS;
}