// file: create_mixtures.cc
//
// this program does mixture splitting
//

// isip include files
//
#include "create_mixtures.h"
#include "create_mixtures_constants.h"

// main program
//
int main(int_4 argc, char_1** argv) {

  // local variables
  //
  int_4 target_mixtures = (int_4)1;
  int_4 num_states = (int_4)0;
  int_4 num_features = (int_4)0;
  
  // variables to hold commandline parameters
  //
  char_1* input_file = new char_1[ISIP_MAX_STRING_LENGTH];
  char_1* output_file = new char_1[ISIP_MAX_STRING_LENGTH];

  // read the command line arguments
  //
  read_cmdline_cc(argc, argv, input_file, output_file, target_mixtures);

  // read the input states file
  //
  FILE* fp_in = fopen((char*)input_file, "rb");
  if (fp_in == (FILE*)NULL) {
    fprintf(stdout, "Error : cannot open input file %s\n", input_file);
    exit(ISIP_PROTO_ERROR);
  }
  
  Train_State** states = read_states_cc(num_states, num_features, fp_in);
  delete [] input_file;
  fclose(fp_in);

  // create states to hold new values after mixture creation
  //
  Train_State** new_states = new Train_State*[num_states];

  // split mixtures
  //
  compute_cc(states, new_states, num_states, num_features, target_mixtures);

  // write the states to the output file
  //
  FILE* fp_out = fopen((char*)output_file, "w");
  if (fp_out == (FILE*)NULL) {
    fprintf(stdout, "Error : cannot open output file %s\n", output_file);
    exit(ISIP_PROTO_ERROR);
  }
  
  print_state_cc(fp_out, new_states, num_states, num_features);
  fclose(fp_out);

  // clean up memory
  //
  for (int_4 i = 0; i < num_states; i++) {
    delete states[i];
  }

  // clean up memory
  //
  for (int_4 i = 1; i < num_states; i++) {
    delete new_states[i];
  }

  delete [] states;
  delete [] new_states; 
  delete [] output_file;
  // exit gracefully
  //
  exit(ISIP_NO_ERROR);
}
