#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#ifdef HAVE_LANGINFO_H
#include <langinfo.h>
#endif
 
#include "config.h"
#include "common.h"
#include "util.h"
#include "connect.h"
#include "pathutils.h"
 
 
void sendtrack_usage (void)
{
  fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ]\n");
  fprintf(stderr, "-t <title> -a <artist> -A <Album artist> -w <writer or composer>\n");
  fprintf(stderr, "    -l <album> -c <codec> -g <genre> -n <track number> -y <year>\n");
  fprintf(stderr, "       -d <duration in seconds> -s <storage_id> <local path> <remote path>\n");
  fprintf(stderr, "(-q means the program will not ask for missing information.)\n");
}
 
static char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
{
  char *cp, *bp;
 
  while (1) {
    fprintf(stdout, "%s> ", prompt);
    if ( fgets(buffer, bufsz, stdin) == NULL ) {
      if (ferror(stdin)) {
        perror("fgets");
      } else {
        fprintf(stderr, "EOF on stdin\n");
      }
      return NULL;
    }
 
    cp = strrchr(buffer, '\n');
    if ( cp != NULL ) *cp = '\0';
 
    bp = buffer;
    while ( bp != cp ) {
      if ( *bp != ' ' && *bp != '\t' ) return bp;
      bp++;
    }
 
    if (! required) return bp;
  }
}
 
{
  int ret;
 
  
  album_orig = album;
  while(album != NULL) {
    if ((album->
name != NULL &&
 
        !strcmp(album->
name, albuminfo->
name) &&
        !strcmp(album->
name, albuminfo->
name) &&
      
      found_album = album;
      found_album->
next = NULL;
    } else {
    }
  }
 
  if (found_album == NULL) {
    printf("Could not find Album. Retrying with only Album name\n");
    album = album_orig;
    while(album != NULL) {
      if ((album->
name != NULL) &&
 
          !strcmp(album->
name, albuminfo->
name) ){
        
        found_album = album;
        found_album->
next = NULL;
      } else {
      }
    }
  }
 
  if (found_album != NULL) {
    uint32_t *tracks;
 
    tracks = (uint32_t *)malloc((found_album->
no_tracks+1) * 
sizeof(uint32_t));
    printf(
"Album \"%s\" found: updating...\n", found_album->
name);
    if (!tracks) {
      printf("failed malloc in add_track_to_album()\n");
      return 1;
    }
    if (found_album->
tracks != NULL) {
 
      memcpy(tracks, found_album->
tracks, found_album->
no_tracks * 
sizeof(uint32_t));
    }
  } else {
    uint32_t *trackid;
 
    trackid = (uint32_t *)malloc(sizeof(uint32_t));
    printf("Album doesn't exist: creating...\n");
    
  }
 
  
  album=album_orig;
  while(album!=NULL){
 
    tmp = album;
  }
 
  if (ret != 0) {
    printf("Error creating or updating album.\n");
    printf("(This could be due to that your device does not support albums.)\n");
  } else {
    printf("success!\n");
  }
  return ret;
}
 
int sendtrack_function(char * from_path, char * to_path, char *partist, char *palbumartist, char *ptitle, char *pgenre, char *palbum, char *pcomposer, uint16_t tracknum, uint16_t length, uint16_t year, uint32_t storageid, uint16_t quiet)
{
  char *filename, *parent;
  char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80];
  char *to_path_copy = NULL;
  char num[80];
  uint64_t filesize;
  uint32_t parent_id = 0;
  struct stat sb;
  int ret;
 
  printf("Sending track %s to %s\n", from_path, to_path);
 
  to_path_copy = strdup(to_path);
  parent = dirname(to_path_copy);
  parent_id = parse_path (parent,files,folders);
  if (parent_id == -1) {
    free (to_path_copy);
    printf("Parent folder could not be found, skipping\n");
    return 1;
  }
  strcpy (to_path_copy,to_path);
  filename = basename(to_path_copy);
 
  if (stat(from_path, &sb) == -1) {
    fprintf(stderr, "%s: ", from_path);
    perror("stat");
    free (to_path_copy);
    return 1;
  }
 
  if (!S_ISREG(sb.st_mode)) {
    free (to_path_copy);
    return 0;
  }
 
  filesize = sb.st_size;
 
  trackmeta->filetype = find_filetype (from_path);
    free (to_path_copy);
    return 1;
  }
 
  if ((ptitle == NULL) && (quiet == 0)) {
    if ( (ptitle = prompt("Title", title, 80, 0)) != NULL )
      if (!strlen(ptitle)) ptitle = NULL;
  }
 
  if ((palbum == NULL) && (quiet == 0)) {
    if ( (palbum = prompt("Album", album, 80, 0)) != NULL )
      if (!strlen(palbum)) palbum = NULL;
  }
 
  if ((palbumartist == NULL) && (quiet == 0)) {
    if ( (palbumartist = prompt("Album artist", albumartist, 80, 0)) != NULL )
      if (!strlen(palbumartist)) palbumartist = NULL;
  }
 
  if ((partist == NULL) && (quiet == 0)) {
    if ( (partist = prompt("Artist", artist, 80, 0)) != NULL )
      if (!strlen(partist)) partist = NULL;
  }
 
  if ((pcomposer == NULL) && (quiet == 0)) {
    if ( (pcomposer = prompt("Writer or Composer", composer, 80, 0)) != NULL )
      if (!strlen(pcomposer)) pcomposer = NULL;
  }
 
  if ((pgenre == NULL) && (quiet == 0)) {
    if ( (pgenre = prompt("Genre", genre, 80, 0)) != NULL )
      if (!strlen(pgenre)) pgenre = NULL;
  }
 
  if ((tracknum == 0) && (quiet == 0)) {
    char *pnum;
    if ( (pnum = prompt("Track number", num, 80, 0)) == NULL )
      tracknum = 0;
    else
      tracknum = strtoul(pnum, 0, 10);
  }
 
  if ((year == 0) && (quiet == 0)) {
    char *pnum;
    if ( (pnum = prompt("Year", num, 80, 0)) == NULL )
      year = 0;
    else
      year = strtoul(pnum, 0, 10);
  }
 
  if ((length == 0) && (quiet == 0)) {
    char *pnum;
    if ( (pnum = prompt("Length", num, 80, 0)) == NULL )
      length = 0;
    else
      length = strtoul(pnum, 0, 10);
  }
 
  printf("Sending track:\n");
  if (ptitle) {
    printf("Title:     %s\n", ptitle);
    trackmeta->title = strdup(ptitle);
  }
 
 
  if (palbum) {
    printf("Album:     %s\n", palbum);
    trackmeta->album = strdup(palbum);
    albuminfo->
name = strdup(palbum);
  }
  if (palbumartist) {
    printf("Album artist:    %s\n", palbumartist);
    albuminfo->
artist = strdup(palbumartist);
  }
  if (partist) {
    printf("Artist:    %s\n", partist);
    trackmeta->artist = strdup(partist);
    if (palbumartist == NULL)
      albuminfo->
artist = strdup(partist);
  }
  if (pcomposer) {
    printf("Writer or Composer:    %s\n", pcomposer);
    trackmeta->composer = strdup(pcomposer);
    albuminfo->
composer = strdup(pcomposer);
  }
  if (pgenre) {
    printf("Genre:     %s\n", pgenre);
    trackmeta->genre = strdup(pgenre);
    albuminfo->
genre = strdup(pgenre);
  }
  if (year > 0) {
    char tmp[80];
    printf("Year:      %d\n", year);
    snprintf(tmp, sizeof(tmp)-1, "%4d0101T0000.0", year);
    tmp[sizeof(tmp)-1] = '\0';
    trackmeta->date = strdup(tmp);
  }
  if (tracknum > 0) {
    printf("Track no:  %d\n", tracknum);
    trackmeta->tracknumber = tracknum;
  }
  if (length > 0) {
    printf("Length:    %d\n", length);
    
    trackmeta->duration = length * 1000;
  }
  
  if (filename != NULL) {
    trackmeta->filename = strdup(filename);
  }
  trackmeta->filesize = filesize;
  trackmeta->parent_id = parent_id;
  {
    int rc;
    char *desc = NULL;
 
      perror("LIBMTP_Get_Storage()");
      exit(-1);
    }
    for (pds = device->
storage; pds != NULL; pds = pds->
next) {
 
      if (pds->
id == storageid) {
 
        break;
      }
    }
    if (NULL != desc) {
      printf("Storage ID: %s (%u)\n", desc, storageid);
      free(desc);
    } else
      printf("Storage ID: %u\n", storageid);
    trackmeta->storage_id = storageid;
  }
 
  printf("Sending track...\n");
  printf("\n");
  if (ret != 0) {
    printf("Error sending track.\n");
    ret = 1;
  } else {
    printf("New track ID: %d\n", trackmeta->item_id);
  }
 
  
  if (palbum)
    ret = add_track_to_album(albuminfo, trackmeta);
 
  free (to_path_copy);
 
  return ret;
}
 
int sendtrack_command (int argc, char **argv) {
  int opt, ret;
  extern int optind;
  extern char *optarg;
  char *partist = NULL;
  char *palbumartist = NULL;
  char *pcomposer = NULL;
  char *ptitle = NULL;
  char *pgenre = NULL;
  char *pcodec = NULL;
  char *palbum = NULL;
  uint16_t tracknum = 0;
  uint16_t length = 0;
  uint16_t year = 0;
  uint16_t quiet = 0;
  uint32_t storageid = 0;
  while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:s:")) != -1 ) {
    switch (opt) {
    case 't':
      free (ptitle);
      ptitle = strdup(optarg);
      break;
    case 'a':
      free (partist);
      partist = strdup(optarg);
      break;
    case 'A':
      free (palbumartist);
      palbumartist = strdup(optarg);
      break;
    case 'w':
      free (pcomposer);
      pcomposer = strdup(optarg);
      break;
    case 'l':
      free (palbum);
      palbum = strdup(optarg);
      break;
    case 'c':
      free (pcodec);
      pcodec = strdup(optarg); 
      break;
    case 'g':
      free (pgenre);
      pgenre = strdup(optarg);
      break;
    case 'n':
      tracknum = atoi(optarg);
      break;
    case 's':
      storageid = (uint32_t) strtoul(optarg, NULL, 0);
      break;
    case 'd':
      length = atoi(optarg);
      break;
    case 'y':
      year = atoi(optarg);
      break;
    case 'q':
      quiet = 1;
      break;
    default:
      sendtrack_usage();
    }
  }
  argc -= optind;
  argv += optind;
 
  if ( argc != 2 ) {
    printf("You need to pass a filename and destination.\n");
    sendtrack_usage();
    ret = 0;
  } else {
    checklang();
    printf("%s,%s,%s,%s,%s,%s,%s,%s,%d%d,%d,%u,%d\n",argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer,tracknum, length, year, storageid, quiet);
    ret = sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer, tracknum, length, year, storageid, quiet);
  }
  free (ptitle);
  free (partist);
  free (palbumartist);
  free (pcomposer);
  free (palbum);
  free (pcodec);
  free (pgenre);
  return ret;
}
int LIBMTP_Create_New_Album(LIBMTP_mtpdevice_t *device, LIBMTP_album_t *const metadata)
Definition: libmtp.c:8820
void LIBMTP_destroy_album_t(LIBMTP_album_t *album)
Definition: libmtp.c:8537
int LIBMTP_Update_Album(LIBMTP_mtpdevice_t *device, LIBMTP_album_t const *const metadata)
Definition: libmtp.c:9382
LIBMTP_album_t * LIBMTP_new_album_t(void)
Definition: libmtp.c:8512
LIBMTP_album_t * LIBMTP_Get_Album_List(LIBMTP_mtpdevice_t *device)
Definition: libmtp.c:8674
void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device)
Definition: libmtp.c:2660
void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
Definition: libmtp.c:2633
int LIBMTP_Get_Storage(LIBMTP_mtpdevice_t *device, int const sortby)
Definition: libmtp.c:4194
char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype)
Definition: libmtp.c:807
int LIBMTP_Send_Track_From_File(LIBMTP_mtpdevice_t *device, char const *const path, LIBMTP_track_t *const metadata, LIBMTP_progressfunc_t const callback, void const *const data)
Definition: libmtp.c:5645
LIBMTP_track_t * LIBMTP_new_track_t(void)
Definition: libmtp.c:4735
void LIBMTP_destroy_track_t(LIBMTP_track_t *track)
Definition: libmtp.c:4774
#define LIBMTP_FILETYPE_IS_TRACK(a)
Definition: libmtp.h:183
LIBMTP_album_t * next
Definition: libmtp.h:760
uint32_t * tracks
Definition: libmtp.h:758
uint32_t storage_id
Definition: libmtp.h:753
uint32_t no_tracks
Definition: libmtp.h:759
char * name
Definition: libmtp.h:754
char * artist
Definition: libmtp.h:755
char * composer
Definition: libmtp.h:756
char * genre
Definition: libmtp.h:757
char * StorageDescription
Definition: libmtp.h:798
LIBMTP_devicestorage_t * next
Definition: libmtp.h:800
uint32_t id
Definition: libmtp.h:791
LIBMTP_devicestorage_t * storage
Definition: libmtp.h:656
uint32_t storage_id
Definition: libmtp.h:711
uint32_t item_id
Definition: libmtp.h:709