/**
 * This is a modified version of detect.c example from libmtp 1.1.8.
 * All it does is to list all the raw devices, without connecting
 * (which sometimes can cause hang). 
 * 
 * It is meant to be used as a replacement of "simple-mtpfs -l".
 * This will provide compatible list (ie devices are listed in the same
 * order) but much more detailed information (USB ids, USB busids)
 * than just the device name and vendor.
 * 
 * \file detect.c
 * Example program to detect a device and list capabilities.
 *
 * Copyright (C) 2005-2008 Linus Walleij <triad@df.lth.se>
 * Copyright (C) 2007 Ted Bullock <tbullock@canada.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <libmtp.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main (int argc, char **argv)
{
  LIBMTP_raw_device_t * rawdevices;
  int numrawdevices;
  LIBMTP_error_number_t err;
  int i;

  int opt;
  extern int optind;
  extern char *optarg;

  while ((opt = getopt(argc, argv, "d")) != -1 ) {
    switch (opt) {
    case 'd':
      LIBMTP_Set_Debug(LIBMTP_DEBUG_PTP | LIBMTP_DEBUG_DATA);
      break;
    }
  }

  argc -= optind;
  argv += optind;

  LIBMTP_Init();

  fprintf(stdout, "libmtp version: " LIBMTP_VERSION_STRING "\n\n");

  err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
  switch(err) {
  case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
    fprintf(stdout, "Devices: %d\n", 0);
    return 0;
  case LIBMTP_ERROR_CONNECTING:
    fprintf(stderr, "Problem: There has been an error connecting. Exiting\n");
    return 1;
  case LIBMTP_ERROR_MEMORY_ALLOCATION:
    fprintf(stderr, "Problem: Encountered a Memory Allocation Error. Exiting\n");
    return 1;
  case LIBMTP_ERROR_NONE:
    {
      int i;

      fprintf(stdout, "Devices: %d\n", numrawdevices);
      for (i = 0; i < numrawdevices; i++) {
	  fprintf(stdout, "%d: %04x:%04x @ %03d:%03d Name: %s Vendor: %s\n",
	      (i+1),
		  rawdevices[i].device_entry.vendor_id,
		  rawdevices[i].device_entry.product_id,
		  rawdevices[i].bus_location,
		  rawdevices[i].devnum,
		  rawdevices[i].device_entry.product,		  
		  rawdevices[i].device_entry.vendor
		  );
      }
    }
    break;
  case LIBMTP_ERROR_GENERAL:
  default:
    fprintf(stderr, "Unknown connection error.\n");
    return 1;
  }

  free(rawdevices);
  return 0;
}
