//! Process command line args
//!
//! @param argc - count of args
//! @param argv - the actual arguments
//!
//! @return error code
//!
USBDM_ErrorCode doArgs(int argc, char **argv) {
   LOGGING;
   bool listDevices = false;
   const char *deviceName = NULL;

   while (argc-- > 1) {
    	  if (stricmp(argv[argc], "-D")==0) {
         // List targets
         listDevices = true;
      }
      else {
         // Assume device name
         deviceName = argv[argc];
      }
   }
   if ((deviceName == NULL) && !listDevices) {
      fprintf(stderr, "No device specified\n");
      Logging::print("No device specified\n");
      return BDM_RC_ILLEGAL_PARAMS;
   }
   if (listDevices) {
      int count = 0;
      std::vector<DeviceDataPtr>::const_iterator it;
      DeviceDataBase *deviceDatabase = shared->getDeviceDataBase();
      for (it = deviceDatabase->begin(); it != deviceDatabase->end(); it++) {
         fprintf(stderr, "%s,\t", (*it)->getTargetName().c_str());
         if (++count == 3) {
            count = 0;
            fprintf(stderr, "\n");
         }
      }
      if (count != 0) {
         fprintf(stderr, "\n");
      }
      return BDM_RC_ERROR_HANDLED;
   }
   else {
      // Find device details from database
      USBDM_ErrorCode rc = shared->setCurrentDeviceByName(deviceName);
      if (rc != BDM_RC_OK) {
         return rc;
      }
      Logging::print( "Loaded device description\n");
   }
   return BDM_RC_OK;
}
//!  Does the following:
//!   - Loads device name from Codewarrior
//!   - Loads corresponding device data from database
//!
//!  @return error code, see \ref USBDM_ErrorCode
//!
USBDM_ErrorCode getDeviceData(DeviceData &deviceData) {
   LOGGING;
   DiReturnT       diRC = DI_OK;
   string          deviceName;
   int value;

   // Device name
   diRC = mtwksGetStringValue(processorKey, deviceName);
   if ((diRC != DI_OK) || (deviceName == emptyString)) {
      Logging::print("Device name not set\n");
      Logging::print("key = %s\n", processorKey.c_str());
      return  BDM_RC_UNKNOWN_DEVICE;
   }
   Logging::print("Device name = \'%s\'\n", (const char *)deviceName.c_str());

   DeviceDataBase deviceDataBase;
   try {
      deviceDataBase.loadDeviceData();
   }
   catch (MyException &exception) {
      Logging::print("Failed to load device database\n");
      string("Failed to load device database\nReason: ")+exception.what();
      displayDialogue((string("Failed to load device database\nReason: ")+exception.what()).c_str(),
                   "Error loading devices",
                   wxOK);
      return BDM_RC_DEVICE_DATABASE_ERROR;
   }
   catch (...){
      Logging::print("Failed to load device database\n");
      displayDialogue("Failed to load device database\n",
                      "Error loading devices",
                      wxOK);
      return BDM_RC_DEVICE_DATABASE_ERROR;
   }
   DeviceDataConstPtr dev = deviceDataBase.findDeviceFromName(deviceName);
   if (dev == NULL) {
      Logging::print("Unknown device\n");
      mtwksDisplayLine("Unrecognised device - using default settings");
      dev = deviceDataBase.getDefaultDevice();
//      return BDM_RC_UNKNOWN_DEVICE;
   }
   else {
      Logging::print("Found device \'%s\' in database\n", (const char *)dev->getTargetName().c_str());
   }
   deviceData = *dev;
   getAttribute(KeyTrimTargetClock, value, 0);
   if (value != 0) {
      getAttribute(KeyClockTrimFrequency, value, deviceData.getClockTrimFreq());
      if (value == 0) {
         value = deviceData.getClockTrimFreq();
      }
      deviceData.setClockTrimFreq(value);
      getAttribute(KeyClockTrimNVAddress, value, deviceData.getClockTrimNVAddress());
      if (value == 0) {
         value = deviceData.getClockTrimNVAddress();
      }
      deviceData.setClockTrimNVAddress(value);
   }
   else {
      deviceData.setClockTrimFreq(0);
   }
   int eraseOptions;
   getAttribute(KeyEraseMethod, eraseOptions, (int)DeviceData::eraseMass);
   deviceData.setEraseOption((DeviceData::EraseOptions)eraseOptions);

   int securityOption;
   getAttribute(KeySecurity, securityOption, (int)SEC_SMART);
   deviceData.setSecurity((SecurityOptions_t)securityOption);

   return BDM_RC_OK;
}
//
// Device name e.g. MCF51CN128
// -noload - don't load (program) flash
//
USBDM_ErrorCode doArgs(int argc, char **argv) {
   bool noLoad = false;
   bool listDevices = false;
   const char *deviceName = NULL;

   while (argc-- > 1) {
//      fprintf(stderr, "doArgs() - arg = \'%s\'\n", argv[argc]);
      if (stricmp(argv[argc], "-noload")==0) {
         noLoad = true;
      }
      else if (stricmp(argv[argc], "-D")==0) {
         // List targets
         listDevices = true;
      }
      else {
         // Assume device name
         deviceName = argv[argc];
      }
   }
   if ((deviceName == NULL) && !listDevices) {
      fprintf(stderr, "No device specified\n");
      print("doArgs() - No device specified\n");
      return BDM_RC_ILLEGAL_PARAMS;
   }
//   fprintf(stderr, "doArgs() - opening device database\n");
   // Find device details from database
   DeviceDataBase *deviceDatabase = new DeviceDataBase;
   fprintf(stderr, "Loading device database...\n");
   print( "doArgs() - Loading device database\n");
   try {
      deviceDatabase->loadDeviceData();
   } catch (MyException &exception) {
      fprintf(stderr, "Loading device database...Failed\n");
      print("doArgs() - Failed to load device database\n");
      return BDM_RC_DEVICE_DATABASE_ERROR;
   }
   print( "doArgs() - Loaded device database\n");
   if (listDevices) {
      int count = 0;
      std::vector<DeviceData *>::iterator it;
      try {
         for (it = deviceDatabase->begin(); it != deviceDatabase->end(); it++) {
            fprintf(stderr, "%s,\t", (*it)->getTargetName().c_str());
            if (++count == 3) {
               count = 0;
               fprintf(stderr, "\n");
            }
         }
         if (count != 0) {
            fprintf(stderr, "\n");
         }
      } catch (...) {
      }
      delete deviceDatabase;
      return BDM_RC_ERROR_HANDLED;
   }
   else {
      const DeviceData *devicePtr = deviceDatabase->findDeviceFromName(deviceName);
      if (devicePtr == NULL) {
         fprintf(stderr, "Failed to find device '%s' in database\n", deviceName);
         print("doArgs() - Failed to find device '%s' in database\n", deviceName);
         return BDM_RC_UNKNOWN_DEVICE;
      }
//      if (!devicePtr->valid) {
//         fprintf(stderr, "OPPS #1!\n");
//         print("doArgs() - OPPS #1!\n");
//         return E_FATAL;
//      }
      deviceData = *devicePtr;
//      if (!deviceData.valid) {
//         fprintf(stderr, "OPPS #2!\n");
//         print("doArgs() - OPPS #2!\n");
//         return E_FATAL;
//      }
      delete deviceDatabase;
//      if (!deviceData.valid) {
//         fprintf(stderr, "OPPS #3!\n");
//         print("doArgs() - OPPS #3!\n");
//         return E_FATAL;
//      }
   }
   return BDM_RC_OK;
}