//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Fri Mar 29 14:36:58 PDT 2013
// Last Modified: Fri Mar 29 14:37:00 PDT 2013
// Filename:      ...museinfo/examples/all/marksim.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/marksim.cpp
// Syntax:        C++; museinfo
//
// Description:   calculates the minimum timebase which is the least common
//                multiple of all rhythms in the file.
//
// Todo: some things to clean up in rhythmic representation due to conversions
// from ints to RationalNumber class for rhythms/durations...

#include "humdrum.h"


///////////////////////////////////////////////////////////////////////////

// function declarations
void      checkOptions(Options& opts, int argc, char* argv[]);
void      example(void);
void      usage(const char* command);
void      loadInputData(const char* filename, HumdrumFileSet& controlset, 
                              HumdrumFileSet& testset);
void      loadInputData(istream& input, HumdrumFileSet& controlset,
                              HumdrumFileSet& testset);

// global variables
Options   options;           // database for command-line arguments
int       debugQ = 0;

///////////////////////////////////////////////////////////////////////////


int main(int argc, char** argv) {
   checkOptions(options, argc, argv);
   HumdrumFileSet controlset;
   HumdrumFileSet testset;
   if (options.getArgCount() == 0) {
      loadInputData(cin, controlset, testset);
   } else {
      loadInputData(options.getArg(1), controlset, testset);
   }

   cout << "The test set contains: "    << testset.getCount()    << endl;
   cout << "The control set contains: " << controlset.getCount() << endl;

}



///////////////////////////////////////////////////////////////////////////


//////////////////////////////
//
// loadInputData --
//

void loadInputData(const char* filename, HumdrumFileSet& controlset, 
      HumdrumFileSet& testset) {
   ifstream input;
   input.open(filename);
   if (!input.is_open()) {
      cerr << "Error: Cannot read input file: " << filename << endl;
      exit(1);
   }
   loadInputData(input, controlset, testset);
}


void loadInputData(istream& input, HumdrumFileSet& controlset,
      HumdrumFileSet& testset) {
   #define READ_CONTROL_SET 0
   #define READ_TEST_SET    1
   int state = READ_CONTROL_SET;
   char line[123123] = {0};
   input.getline(line, 123123);
   PerlRegularExpression pre;
   while (!input.eof()) {
      if (pre.search(line, "^\\s*#.*control", "i")) {
         state = READ_CONTROL_SET;
         input.getline(line, 123123);
         continue;
      }
      if (pre.search(line, "^\\s*#.*test", "i")) {
         state = READ_TEST_SET;
         input.getline(line, 123123);
         continue;
      }
      if (pre.search(line, "^\\s*#")) {
         // ignore comment line.
         input.getline(line, 123123);
         continue;
      }
      if (pre.search(line, "^\\s*$")) {
         // ignore blank lines
         input.getline(line, 123123);
         continue;
      }
      if (state == READ_CONTROL_SET) {
         controlset.readAppend(line);
      } else {
         testset.readAppend(line);
      }
      input.getline(line, 123123);
   }
}


//////////////////////////////
//
// checkOptions -- validate and process command-line options.
//

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("debug=b");              // determine bad input line num
   opts.define("author=b");             // author of program
   opts.define("version=b");            // compilation info
   opts.define("example=b");            // example usages
   opts.define("h|help=b");             // short description
   opts.process(argc, argv);
   
   // handle basic options:
   if (opts.getBoolean("author")) {
      cout << "Written by Craig Stuart Sapp, "
           << "craig@ccrma.stanford.edu, April 2011" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 22 April 2010" << endl;
      cout << "compiled: " << __DATE__ << endl;
      cout << MUSEINFO_VERSION << endl;
      exit(0);
   } else if (opts.getBoolean("help")) {
      usage(opts.getCommand());
      exit(0);
   } else if (opts.getBoolean("example")) {
      example();
      exit(0);
   }

   debugQ    = opts.getBoolean("debug");
}



//////////////////////////////
//
// example -- example usage of the program
//

void example(void) {
   cout <<
   "                                                                         \n"
   << endl;
}



//////////////////////////////
//
// usage -- gives the usage statement for the program
//

void usage(const char* command) {
   cout <<
   "                                                                         \n"
   << endl;
}


// md5sum: 5508455aacf1a63b1fd2370a561645ca marksim.cpp [20130404]