
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "wimplib.h"

#include "InstallPC.h"
#include "misc.h"

static void update_config_parameter(char *old_line, char *old_config, int old_config_len, char *new_line)
{
  /* The procedure is:
  **
  **    * get the parameter name into param[];
  **    * find its old value from the old Config file;
  **    * check our 'special' processing functions which cope with PCRAM
  **         changing from Kb to Mb, etc.;
  **    * if none of these catch it, the setting is passed through unchanged.
  **
  */
  int pcram_value, c=0;
  char param[64], old_value[128];

  if (old_line[0] < 32 || old_line[0] == '#' ||
      strncmp(old_line, "HD0-", 4) != 0 || strncmp(old_line, "HD1-", 4) != 0)
    { strcpy(new_line, old_line); return; }

  sscanf(old_line, "%s ", param);
  /*fprintf(stderr, "param = %s\n", param);*/

  while ( (c != old_config_len) &&
          (strncmp(old_config+c, param, strlen(param)) != 0)
        ) c++;
  if (c == old_config_len)
    {
      strcpy(new_line, old_line);
      return;
    }
  sscanf(old_config+c, "%s %s", param, old_value);
  /*fprintf(stderr, "old_value = %s\n", old_value);*/

  if (strcmp(param, "PCRAM") == 0)
  {
    sscanf(old_value, "%d", &pcram_value);
    if (pcram_value > 1024) /* Make sure it's in Mb not Kb */
      pcram_value = pcram_value >> 10;
    if ( (pcram_value & 3) != 0) /* Make sure it's a multiple of 4 */
      pcram_value = (pcram_value + 4) & 0xFC;
    sprintf(new_line, "PCRAM %d\n", pcram_value);
    return;
  }

  sprintf(new_line, "%s %s\n", param, old_value);
  return;
}

static void make_new_config(char *old_config_filename, char *new_config_filename, char *template_filename)
{
  FILE *fh, *fh_out;
  char *old_config;
  char line[256], new_line[256];
  int old_config_len;

  old_config_len = file_length(old_config_filename);
  old_config = malloc(old_config_len);
  /*fprintf(stderr, "length: %d\n", old_config_len);*/
  /*old_config[file_length("<Diva$Folder>._!PC.Config")+1] = 0;*/
  /*fprintf(stderr, "old_config: %08d %d\n", (int)old_config, old_config_len);*/
  file_load_to(old_config_filename, old_config);

  fh = fopen(template_filename, "rb");
  fh_out = fopen(new_config_filename, "wb");
  if (fh == NULL || fh_out == NULL)
    exit(1);
  while (!feof(fh))
  {
    fgets(line, 256, fh);
    /*fprintf(stderr, "old line: %s", line);*/
    update_config_parameter(line, old_config, old_config_len, new_line);
    /*fprintf(stderr, "new line: %s", new_line);*/
    fputs(new_line, fh_out);
  }
  free(old_config); fclose(fh); fclose(fh_out);
  sprintf(line, "SetType %s FFF", new_config_filename);
  system(line);
}


static void convert_configs(char *config_dir_in, char *config_dir_out, char *template_filename)
{
  int  count=0, x;
  char line[256], temp1[256], temp2[256];

  sprintf(line, "Cdir %s", config_dir_out);
  /*fprintf(stderr, line);*/
  FERR(wimp_start_task(line, &child_task_handle));

  while (count > -1)
  {
    count = read_entry(config_dir_in, line, count, NULL);

    sprintf(temp1, "%s.%s", config_dir_in, line);
    sprintf(temp2, "%s.%s", config_dir_out, line);
    make_new_config(temp1, temp2, template_filename);
    x = read_entry(config_dir_in, line, count, NULL);
    if (x == -1) count = -1;
  }
}

