//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Fri Jun 18 15:26:27 PDT 2010
// Last Modified: Fri Jun 18 15:26:31 PDT 2010
// Filename:      ...sig/examples/all/museset.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/museset.cpp
// Syntax:        C++; museinfo
//
// Description:   Test program for handling multiple MuseData part files.
//

#include "MuseDataSet.h"
#include "Options.h"
#include "museinfo.h"

#ifndef OLDCPP
   using namespace std;
#endif
   
// function declarations
void      checkOptions(Options& opts, int argc, char* argv[]);
void      example(void);
void      usage(const char* command);
void      printData(MuseData& infile);
void      cleanData(MuseDataSet& infile, const char* cleanstring);

// global variables
Options   options;             // database for command-line arguments
int       countQ      = 0;     // used with -c option
int       part        = -1;    // used with -p option
int       partQ       = 0;     // used with -p option
int       instrumentQ = 0;     // used with -i option
int       typeQ       = 0;     // used with -t option
int       cleanQ      = 0;     // used with -C option
const char* clean      = NULL; // used with -C option
const char* membership = NULL; // used with -m optnion

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

int main(int argc, char* argv[]) {
   checkOptions(options, argc, argv);
   int numinputs = options.getArgCount();
   MuseDataSet infile;

   int i, j;
   if (numinputs > 1) {
      for (i=0; i<numinputs; i++) {
         // multiple files, each containing one part.
         infile.readPart(options.getArg(i+1));
      }
   } else if (numinputs == 1) {
      // a single file containing multiple parts
      infile.read(options.getArg(1));
   } else {
      // standard input is a stream of possibly multiple parts
      infile.read(cin);
   }

   if (cleanQ) {
      cleanData(infile, clean);
   }

   char buffer[1024] = {0};
   if (countQ) {
      cout << infile.getPartCount() << endl;
   } else if (instrumentQ) {
      for (i=0; i<infile.getPartCount(); i++) {
         cout << "Part " << i+1 << ":\t" 
              << infile[i].getPartName(buffer) << endl;
      }
   } else if (typeQ) {
      for (i=0; i<infile.getPartCount(); i++) {
         if (part >= 0 && part < infile.getPartCount()) {
            if (part != i) {
               continue;
            }
         }
         for (j=0; j<infile[i].getNumLines(); j++) {
            cout << (char)infile[i][j].getType() << "\t" 
                 << infile[i][j] << "\n";
         }
	 cout << "@================================" << endl;
      }
   } else if (cleanQ) {
      for (i=0; i<infile.getPartCount(); i++) {
         if (infile[i].isMember(clean)) {
            cout << infile[i];
         }
      }
   } else {
      // print all read data:
      if (membership != NULL) {
         int pcounter = 0;
         for (i=0; i<infile.getPartCount(); i++) {
            if (infile[i].isMember(membership)) {
               pcounter++;
               if (partQ && pcounter == part) {
                  cout << infile[i];
               } else if (!partQ) {
                  cout << infile[i];
               }
            }
         }
      } else if (part < 0 || part >= infile.getPartCount()) { 
         cout << infile;
      } else {
         cout << infile[part];
      }
   }

   return 0;
}


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


//////////////////////////////
//
// cleanData -- force the group memberships to only be those
//    listed in the clean string. (only one membership at the moment).
//

void cleanData(MuseDataSet& infile, const char* cleanstring) {
   int i;
   for (i=0; i<infile.getPartCount(); i++) {
      if (infile[i].isMember(cleanstring)) {
         infile[i].selectMembership(cleanstring);
      }
   }
}



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

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("m|membership=s:score", "select a particular membership set");
   opts.define("c|count=b", "count number of parts in set");
   opts.define("C|clean=s:score", "clean the input to a single membership");
   opts.define("i|instrument=b", "print the instrument name");
   opts.define("t|type=b", "print the line type");
   opts.define("p|part=i:-1", "work with only a single part");

   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, June 2010" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 18 June 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);
   }

   countQ      = opts.getBoolean("count");
   instrumentQ = opts.getBoolean("instrument");
   typeQ       = opts.getBoolean("type");
   part        = opts.getInteger("part");

   cleanQ      = opts.getBoolean("clean");
   if (cleanQ) {
      clean = opts.getString("clean");
   }

   partQ       = opts.getBoolean("part");
   if (opts.getBoolean("membership")) {
      membership  = opts.getString("membership");
   } else {
      part--; // change offset from 1 to 0;
   }
}



//////////////////////////////
//
// 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: 8f1e4903013898ee959f4e08ed82d943 museset.cpp [20100623]