#include <iostream>
#include <iomanip>

#include "JDB/JDB.hh"
#include "JDB/JDBToolkit.hh"

#include "JLang/JLangToolkit.hh"

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

namespace {

  const char* const int_t     =  "int";
  const char* const string_t  =  "string";
}

/**
 * \file
 *
 * Auxiliary program to get serial number or object identifier of detector from database.
 * \author mdejong
 */
int main(int argc, char **argv)
{
  using namespace std;
  using namespace JPP;
 
  JServer         server;
  string          usr;
  string          pwd;
  string          cookie;
  string          detid;
  string          option;
  int             debug;

  try {

    JParser<> zap("Auxiliary program to get serial number or object identifier of detector from database.");
    
    zap['s'] = make_field(server)        = getServernames();
    zap['u'] = make_field(usr)           = "";
    zap['!'] = make_field(pwd)           = "";
    zap['C'] = make_field(cookie)        = "";
    zap['D'] = make_field(detid);
    zap['O'] = make_field(option)        = "", int_t, string_t;
    zap['d'] = make_field(debug)         = 1;

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


  try {

    JDB::reset(usr, pwd, cookie);

    if (is_integer(detid))
      (option == string_t || option == "" ? cout << getDetector(to_value<int>(detid)) : cout << detid) << endl;
    else
      (option == int_t    || option == "" ? cout << getDetector(detid)                : cout << detid) << endl;
  }
  catch(const exception& error) {
    FATAL(error.what() << endl);
  }
}