/*! @file

    @id $Id$
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

// #include <pcsc.hxx>
// #include <cryptaux.hxx>
// #include <openssl.hxx>
#include <cardos.hxx>

#include <mrw/vector.hxx>
#include <mrw/args.hxx>

#include <string>
#include <mrw/checkcxx11.hxx>
#include <memory>
#include <cctype>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include <iomanip>

void list() {
  pcsc::Connection::Strings readers(pcsc::Connection::scan());
  std::cout<<"Found "<<readers.size()<<" readers"
           <<(readers.size()?":":".")<<std::endl;
  int i(0);
  for (pcsc::Connection::Strings::iterator r(readers.begin());
       r!=readers.end(); ++r, ++i)
    std::cout<<i<<": "<<*r<<std::endl;
}

int main(int argc, char** argv) try {
  unsigned int reader(0);
  std::string pin;
  std::string path("3f00");
  std::string id("8888");
  std::string data("Hallo Welt");
  mrw::args::parse(argc, argv, "Write data to card.",
                   mrw::args::defaults()
                   <<mrw::args::decl("l", "list", "list readers",
                                     mrw::args::decl::param_list()
                                     <<mrw::args::func(list)
                                     <<mrw::args::exit())
                   <<mrw::args::decl("r", "reader", "select reader",
                                     mrw::args::decl::param_list()
                                     <<mrw::args::param(reader, "num"))
                   <<mrw::args::decl("p", "path", "full path",
                                     mrw::args::decl::param_list()
                                     <<mrw::args::param(path, "path"))
                   <<mrw::args::decl("l", "pin", "full path",
                                     mrw::args::decl::param_list()
                                     <<mrw::args::param(path, "path"))
                   <<mrw::args::decl("i", "id", "file id",
                                     mrw::args::decl::param_list()
                                     <<mrw::args::param(id, "file"))
                   <<mrw::args::decl("d", "data", "data",
                                     mrw::args::decl::param_list()
                                     <<mrw::args::param(data, "text"))
                   );
  pcsc::Connection::Strings readers(pcsc::Connection::scan());
  if (readers.size()<reader) {
    std::cerr<<"reader not found: "<<reader<<std::endl;
    return 1;
  }
  cardos::Commands cmd(pcsc::Connection::reader(readers[reader]));
  cardos::Dir d(cmd, path);
  //cardos::BerValues d(cmd.readBerFile(path));
  //cardos::BerValues d(cmd.directory(path));
  std::cout<<"-----------------------------------------------------"<<std::endl
           <<d.print()<<std::endl
           <<"-----------------------------------------------------"<<std::endl;
  //std::string res(cmd.readBinary(path));
  //std::cout<<"HEX:"<<std::endl<<crypto::readable(res)<<std::endl;
  //std::cout<<"BER:"<<std::endl<<cardos::BerValues(res).print()<<std::endl;
  // if (!pin.size()) {
  //   std::cout<<"PIN: ";
  //   std::cin>>pin;
  // }
  // if (pin.size()) cmd.logonTransport(pin);
  // cmd.phaseControl();
  // cmd.createBinary(path, id, data);
  // cmd.phaseControl();
  return 0;
 } catch (std::exception& x) {
  std::cerr<<"ERROR: "<<x.what()<<std::endl;
  return 2;
 }
