//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Wed Mar 13 18:43:11 PST 2002
// Last Modified: Wed Mar 13 19:22:31 PST 2002
// Filename:      ...sig/examples/all/esac2hum.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/esac2hum.cpp
// Syntax:        C++; museinfo
//
// Description:   Converts an EsAC file into Humdrum.
//

#include "humdrum.h"

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

#ifndef OLDCPP
   #include <iostream>
   #include <fstream>
#else
   #include <iostream.h>
   #include <fstream.h>
#endif


// function declarations:
void      checkOptions(Options& opts, int argc, char** argv);
void      example(void);
void      usage(const char* command);
void      makeIndex(const char* name, HumdrumFile& infile,
                                 ostream& out);


// User interface variables:
Options   options;
int       debugQ = 0;          // used with the --debug option
int       verboseQ = 0;        // used with the -v option


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

int main(int argc, char** argv) {
   // process the command-line options
   checkOptions(options, argc, argv);
   HumdrumFile infile;
   int i;
   for (i=1; i<=options.getArgCount(); i++) {
      infile.clear();
      infile.read(options.getArg(i));
      makeIndex(options.getArg(i), infile, cout);
   }
   return 0;
}

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


//////////////////////////////
//
// makeIndex --
//

void makeIndex(const char* name, HumdrumFile& infile, ostream& out) {
   int i;
   int found = 0;
   static char nametag[1024] = {0};
   for (i=0; i<infile.getNumLines(); i++) {
      if (infile[i].getType() == E_humrec_bibliography) {
         if (strncmp(infile[i][0], "!!!id:", 6) == 0) {
            if (infile[i][0][6] != ' ') {
               strcpy(nametag, &infile[i][0][6]);
            } else {
               strcpy(nametag, &infile[i][0][7]);
            }
            found = 1;
            break;
         }
      } 
   }

   if (!found) {
      strcpy(nametag, name);
   }

   int note = 0;
   int oldnote = 0;
   int pitch = 0;

   out << nametag << ":\t";
   for (i=0; i<infile.getNumLines(); i++) {
      if (infile[i].getType() == E_humrec_data) {
         if (strcmp(infile[i][0], ".") != 0) {
            pitch = Convert::kernToBase40(infile[i][0]);
            if (pitch > 0) {
               oldnote = note;
               note = pitch;
               cout << (note - oldnote) << ' ';
            }
         }
      }
   }
   cout << endl;
}





//////////////////////////////
//
// checkOptions -- 
//

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("debug=b",      "print debug information"); 
   opts.define("v|verbose=b",  "verbose output"); 
   opts.define("h|header=s:",  "Header filename for placement in output");
   opts.define("t|trailer=s:", "Trailer filename for placement in output");
   opts.define("s|split=s:file", "Split song info into separate files");
   opts.define("x|extension=s:.krn", "Split filename extension");
   opts.define("f|first=i:1",    "Number of first split filename");

   opts.define("author=b",  "author of program"); 
   opts.define("version=b", "compilation info");
   opts.define("example=b", "example usages");   
   opts.define("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, March 2002" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 13 March 2002" << 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");
   verboseQ    = opts.getBoolean("verbose");

}



//////////////////////////////
//
// example --
//

void example(void) {


}



//////////////////////////////
//
// usage --
//

void usage(const char* command) {

}



// md5sum: 894fd4409282b46dd265889716166ed5 plindex.cpp [20050403]