#include <string>
#include <iostream>
#include <iomanip>

#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JModuleRouter.hh"

#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"


/**
 * \file
 *
 * Auxiliary program to print module location for a given module identifier.
 * \author mdejong
 */
int main(int argc, char **argv)
{
  using namespace std;
  using namespace JPP;

  string            detectorFile;
  JModuleIdentifier id;
  int               debug;

  try {

    JParser<> zap("Auxiliary program to print module location for a given module identifier."
		  "\nInteractive mode in absence of option -M <module>.");

    zap['a'] = make_field(detectorFile, "Detector file");
    zap['M'] = make_field(id,           "Module identifier")   = JModuleIdentifier();
    zap['d'] = make_field(debug)             = 1;

    zap(argc, argv);
  }
  catch(const exception &error) {
    FATAL(error.what() << endl);
  }

  
  JDetector detector;

  try {
    load(detectorFile, detector);
  }
  catch(const JException& error) {
    FATAL(error);
  }

  const JModuleRouter router(detector);

  if (id != JModuleIdentifier()) {

    cout << router.getModule(id).getLocation() << endl;

  } else {

    while (cin >> id && id != JModuleIdentifier()) {
      cout << router.getModule(id).getLocation() << endl;
    }
  }
}