1c3a6eeb8ef3cd67f286135c93c1e220fb4d058d0Linus Walleij/**
23d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * \file sendtr.c
33d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * Example program to send a music track to a device.
471f45f0cdc69d859e3daad4c5999f9b38993adbacjdebenh * This program is derived from the exact equivalent in libnjb.
5c3a6eeb8ef3cd67f286135c93c1e220fb4d058d0Linus Walleij * based on Enrique Jorreto Ledesma's work on the original program by
671f45f0cdc69d859e3daad4c5999f9b38993adbacjdebenh * Shaun Jackman and Linus Walleij.
73d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij *
8c3a6eeb8ef3cd67f286135c93c1e220fb4d058d0Linus Walleij * Copyright (C) 2003-2010 Linus Walleij <triad@df.lth.se>
93d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * Copyright (C) 2003-2005 Shaun Jackman
103d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * Copyright (C) 2003-2005 Enrique Jorrete Ledesma
113d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * Copyright (C) 2006 Chris A. Debenham <chris@adebenham.com>
1275fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij * Copyright (C) 2008 Nicolas Pennequin <nicolas.pennequin@free.fr>
134347fda58929ff3e4f6aa93a510b268280362d9cLinus Walleij * Copyright (C) 2008 Joseph Nahmias <joe@nahmias.net>
143d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij *
153d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * This library is free software; you can redistribute it and/or
163d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * modify it under the terms of the GNU Lesser General Public
173d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * License as published by the Free Software Foundation; either
183d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * version 2 of the License, or (at your option) any later version.
193d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij *
203d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * This library is distributed in the hope that it will be useful,
213d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * but WITHOUT ANY WARRANTY; without even the implied warranty of
223d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
233d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * Lesser General Public License for more details.
243d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij *
253d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * You should have received a copy of the GNU Lesser General Public
263d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * License along with this library; if not, write to the
273d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
283d78c4ce5b4b4f38929792b6e392df1416b578b7Linus Walleij * Boston, MA 02111-1307, USA.
2971f45f0cdc69d859e3daad4c5999f9b38993adbacjdebenh */
300535838b0a8a745d60630c1612adab3f2da26ac8Linus Walleij
312eaaff0477a5c5ba2aa7631e998f8bd4f77cf75cLinus Walleij#include <stdlib.h>
322eaaff0477a5c5ba2aa7631e998f8bd4f77cf75cLinus Walleij#include <limits.h>
33bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij#include <string.h>
34cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh#include <libgen.h>
35bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij#include <sys/stat.h>
36cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh#include <sys/types.h>
37cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh#include <fcntl.h>
3856c6395f9b8153c0d74256033e4c894253ba4adcLinus Walleij#ifdef HAVE_LANGINFO_H
3956c6395f9b8153c0d74256033e4c894253ba4adcLinus Walleij#include <langinfo.h>
4056c6395f9b8153c0d74256033e4c894253ba4adcLinus Walleij#endif
41843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
42843210f205cf470eab0980bd6226cf244c564f21Linus Walleij#include "config.h"
43843210f205cf470eab0980bd6226cf244c564f21Linus Walleij#include "common.h"
44843210f205cf470eab0980bd6226cf244c564f21Linus Walleij#include "util.h"
45843210f205cf470eab0980bd6226cf244c564f21Linus Walleij#include "connect.h"
46cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh#include "libmtp.h"
47cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh#include "pathutils.h"
48cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh
49cb4ac9f611bfb9db628378291da95a69be84f841cjdebenhextern LIBMTP_folder_t *folders;
50cb4ac9f611bfb9db628378291da95a69be84f841cjdebenhextern LIBMTP_file_t *files;
51cb4ac9f611bfb9db628378291da95a69be84f841cjdebenhextern LIBMTP_mtpdevice_t *device;
52bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij
53c80f9b49b60e741032ffa9922088d3cef9aa4e84cjdebenhvoid sendtrack_usage (void)
5471f45f0cdc69d859e3daad4c5999f9b38993adbacjdebenh{
5531b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij  fprintf(stderr, "usage: sendtr [ -D debuglvl ] [ -q ]\n");
5631b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij  fprintf(stderr, "-t <title> -a <artist> -A <Album artist> -w <writer or composer>\n");
5731b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij  fprintf(stderr, "    -l <album> -c <codec> -g <genre> -n <track number> -y <year>\n");
584347fda58929ff3e4f6aa93a510b268280362d9cLinus Walleij  fprintf(stderr, "       -d <duration in seconds> -s <storage_id> <local path> <remote path>\n");
5971f45f0cdc69d859e3daad4c5999f9b38993adbacjdebenh  fprintf(stderr, "(-q means the program will not ask for missing information.)\n");
6071f45f0cdc69d859e3daad4c5999f9b38993adbacjdebenh}
61bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij
62bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleijstatic char *prompt (const char *prompt, char *buffer, size_t bufsz, int required)
63bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij{
64bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij  char *cp, *bp;
65843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
66bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij  while (1) {
67bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    fprintf(stdout, "%s> ", prompt);
68bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    if ( fgets(buffer, bufsz, stdin) == NULL ) {
69bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij      if (ferror(stdin)) {
70bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij	perror("fgets");
71bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij      } else {
72bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij	fprintf(stderr, "EOF on stdin\n");
73bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij      }
74bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij      return NULL;
75bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    }
76843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
77bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    cp = strrchr(buffer, '\n');
78bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    if ( cp != NULL ) *cp = '\0';
79843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
80bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    bp = buffer;
81bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    while ( bp != cp ) {
82bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij      if ( *bp != ' ' && *bp != '\t' ) return bp;
83bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij      bp++;
84bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    }
85843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
86bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    if (! required) return bp;
87bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij  }
88bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij}
89bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij
9075fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleijstatic int add_track_to_album(LIBMTP_album_t *albuminfo, LIBMTP_track_t *trackmeta)
9175fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij{
929564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij  LIBMTP_album_t *album;
930c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela  LIBMTP_album_t *album_orig;
949564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij  LIBMTP_album_t *found_album = NULL;
9575fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  int ret;
9675fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij
9775fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  /* Look for the album */
989564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij  album = LIBMTP_Get_Album_List(device);
990c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela  album_orig = album;
10075fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  while(album != NULL) {
10131b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij    if ((album->name != NULL &&
102bbd9142231c7c964799c73e097b58c1a1e609f43Linus Walleij	album->artist != NULL &&
103bbd9142231c7c964799c73e097b58c1a1e609f43Linus Walleij	!strcmp(album->name, albuminfo->name) &&
10431b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij	!strcmp(album->artist, albuminfo->artist)) ||
10531b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij	  (album->name != NULL &&
10631b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij	album->composer != NULL &&
10731b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij	!strcmp(album->name, albuminfo->name) &&
10831b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij	!strcmp(album->composer, albuminfo->composer))) {
1099564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij      /* Disconnect this album for later use */
1109564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij      found_album = album;
1119564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij      album = album->next;
1129564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij      found_album->next = NULL;
1139564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    } else {
1149564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij      album = album->next;
1150c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela    }
1160c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela  }
1170c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela
1180c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela  if (found_album == NULL) {
1190c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela    printf("Could not find Album. Retrying with only Album name\n");
1200c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela    album = album_orig;
1210c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela    while(album != NULL) {
1220c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela      if ((album->name != NULL) &&
1230c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela          !strcmp(album->name, albuminfo->name) ){
1240c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela        /* Disconnect this album for later use */
1250c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela        found_album = album;
1260c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela        album = album->next;
1270c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela        found_album->next = NULL;
1280c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela      } else {
1290c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela        album = album->next;
1300c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela      }
13175fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij    }
13275fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  }
133843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
1349564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij  if (found_album != NULL) {
1359564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    uint32_t *tracks;
1369564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij
1379564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    tracks = (uint32_t *)malloc((found_album->no_tracks+1) * sizeof(uint32_t));
1389564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    printf("Album \"%s\" found: updating...\n", found_album->name);
13975fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij    if (!tracks) {
1409564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij      printf("failed malloc in add_track_to_album()\n");
14175fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij      return 1;
14275fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij    }
1439564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    found_album->no_tracks++;
1449564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    if (found_album->tracks != NULL) {
1459564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij      memcpy(tracks, found_album->tracks, found_album->no_tracks * sizeof(uint32_t));
1469564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij      free(found_album->tracks);
1479564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    }
1489564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    tracks[found_album->no_tracks-1] = trackmeta->item_id;
149bbd9142231c7c964799c73e097b58c1a1e609f43Linus Walleij    found_album->tracks = tracks;
1509564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    ret = LIBMTP_Update_Album(device, found_album);
15175fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  } else {
1529564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    uint32_t *trackid;
153843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
1549564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    trackid = (uint32_t *)malloc(sizeof(uint32_t));
1559564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    *trackid = trackmeta->item_id;
1569564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    albuminfo->tracks = trackid;
1579564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    albuminfo->no_tracks = 1;
158ea68f1f3e59ed3fbc02cf64e8f2779d67806ad12Linus Walleij    albuminfo->storage_id = trackmeta->storage_id;
15975fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij    printf("Album doesn't exist: creating...\n");
160ea68f1f3e59ed3fbc02cf64e8f2779d67806ad12Linus Walleij    ret = LIBMTP_Create_New_Album(device, albuminfo);
1619564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    /* albuminfo will be destroyed later by caller */
16275fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  }
163c3a6eeb8ef3cd67f286135c93c1e220fb4d058d0Linus Walleij
1640c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela  /* Delete the earlier retrieved Album list */
1650c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela  album=album_orig;
1660c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela  while(album!=NULL){
1670c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela    LIBMTP_album_t *tmp;
1680c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela
1690c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela    tmp = album;
1700c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela    album = album->next;
1710c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela    LIBMTP_destroy_album_t(tmp);
1720c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela  }
1730c6c8fcd220643044af5dc8eadd3eedde105873cVenkateswara Rao Mandela
17475fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  if (ret != 0) {
17575fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij    printf("Error creating or updating album.\n");
176f329662d28af7017ffcbe6d46c38fa86151672efLinus Walleij    printf("(This could be due to that your device does not support albums.)\n");
17775fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij    LIBMTP_Dump_Errorstack(device);
17875fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij    LIBMTP_Clear_Errorstack(device);
17975fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  } else {
1809564013105ee346e4f51562a2d7ab9e02a196935Linus Walleij    printf("success!\n");
18175fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  }
18231b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij  return ret;
18375fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij}
18475fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij
18528ad9b74979d4734e59bd7ba9da36e3122b11b95Linus Walleijint 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)
186d6a4997d8d50bfd1e5ffc88e5f0759b445aac451Linus Walleij{
187cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh  char *filename, *parent;
18831b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij  char artist[80], albumartist[80], title[80], genre[80], album[80], composer[80];
189ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner  char *to_path_copy = NULL;
190bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij  char num[80];
191bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij  uint64_t filesize;
1923eb115ce7935095d8f15194189a44d41799ae40fLinus Walleij  uint32_t parent_id = 0;
193bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij  struct stat sb;
194bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij  LIBMTP_track_t *trackmeta;
19575fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  LIBMTP_album_t *albuminfo;
19675fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij  int ret;
19775fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij
19828ad9b74979d4734e59bd7ba9da36e3122b11b95Linus Walleij  printf("Sending track %s to %s\n", from_path, to_path);
19975fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij
200ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner  to_path_copy = strdup(to_path);
201ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner  parent = dirname(to_path_copy);
202cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh  parent_id = parse_path (parent,files,folders);
203cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh  if (parent_id == -1) {
204ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner    free (to_path_copy);
205cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh    printf("Parent folder could not be found, skipping\n");
206cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh    return 1;
207bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij  }
208ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner  strcpy (to_path_copy,to_path);
209ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner  filename = basename(to_path_copy);
210bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij
211843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (stat(from_path, &sb) == -1) {
212cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh    fprintf(stderr, "%s: ", from_path);
213bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij    perror("stat");
214ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner    free (to_path_copy);
215cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh    return 1;
216843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
217cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh
218ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner  if (!S_ISREG(sb.st_mode)) {
219ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner    free (to_path_copy);
220843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    return 0;
221ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner  }
222cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh
223843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  filesize = sb.st_size;
2249959f5bbf5c5c12a4622ef66930a29ddbffc792aMarcus Meissner
2259959f5bbf5c5c12a4622ef66930a29ddbffc792aMarcus Meissner  trackmeta = LIBMTP_new_track_t();
226843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  trackmeta->filetype = find_filetype (from_path);
227843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (!LIBMTP_FILETYPE_IS_TRACK(trackmeta->filetype)) {
228843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Not a valid track codec: \"%s\"\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype));
2299959f5bbf5c5c12a4622ef66930a29ddbffc792aMarcus Meissner    LIBMTP_destroy_track_t(trackmeta);
230ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner    free (to_path_copy);
231843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    return 1;
232843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
23328ad9b74979d4734e59bd7ba9da36e3122b11b95Linus Walleij
234843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if ((ptitle == NULL) && (quiet == 0)) {
235843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if ( (ptitle = prompt("Title", title, 80, 0)) != NULL )
236843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      if (!strlen(ptitle)) ptitle = NULL;
237843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
238cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh
239843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if ((palbum == NULL) && (quiet == 0)) {
240843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if ( (palbum = prompt("Album", album, 80, 0)) != NULL )
241843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      if (!strlen(palbum)) palbum = NULL;
242843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
24331b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij
244843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if ((palbumartist == NULL) && (quiet == 0)) {
245843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if ( (palbumartist = prompt("Album artist", albumartist, 80, 0)) != NULL )
246843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      if (!strlen(palbumartist)) palbumartist = NULL;
247843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
248cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh
249843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if ((partist == NULL) && (quiet == 0)) {
250843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if ( (partist = prompt("Artist", artist, 80, 0)) != NULL )
251843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      if (!strlen(partist)) partist = NULL;
252843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
253cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh
254843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if ((pcomposer == NULL) && (quiet == 0)) {
255843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if ( (pcomposer = prompt("Writer or Composer", composer, 80, 0)) != NULL )
256843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      if (!strlen(pcomposer)) pcomposer = NULL;
257843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
258cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh
259843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if ((pgenre == NULL) && (quiet == 0)) {
260843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if ( (pgenre = prompt("Genre", genre, 80, 0)) != NULL )
261843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      if (!strlen(pgenre)) pgenre = NULL;
262843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
263843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
264843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if ((tracknum == 0) && (quiet == 0)) {
265843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    char *pnum;
266843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if ( (pnum = prompt("Track number", num, 80, 0)) == NULL )
267843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      tracknum = 0;
268843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    else
269843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      tracknum = strtoul(pnum, 0, 10);
270843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
271843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
272843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if ((year == 0) && (quiet == 0)) {
273843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    char *pnum;
274843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if ( (pnum = prompt("Year", num, 80, 0)) == NULL )
275843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      year = 0;
276843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    else
277843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      year = strtoul(pnum, 0, 10);
278843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
279843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
280843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if ((length == 0) && (quiet == 0)) {
281843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    char *pnum;
282843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if ( (pnum = prompt("Length", num, 80, 0)) == NULL )
283843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      length = 0;
284843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    else
285843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      length = strtoul(pnum, 0, 10);
286843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
287843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
288843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  printf("Sending track:\n");
289843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  printf("Codec:     %s\n", LIBMTP_Get_Filetype_Description(trackmeta->filetype));
290843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (ptitle) {
291843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Title:     %s\n", ptitle);
292843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->title = strdup(ptitle);
293843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
2949959f5bbf5c5c12a4622ef66930a29ddbffc792aMarcus Meissner
2959959f5bbf5c5c12a4622ef66930a29ddbffc792aMarcus Meissner  albuminfo = LIBMTP_new_album_t();
2969959f5bbf5c5c12a4622ef66930a29ddbffc792aMarcus Meissner
297843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (palbum) {
298843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Album:     %s\n", palbum);
299843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->album = strdup(palbum);
300843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    albuminfo->name = strdup(palbum);
301843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
302843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (palbumartist) {
303843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Album artist:    %s\n", palbumartist);
304843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    albuminfo->artist = strdup(palbumartist);
305843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
306843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (partist) {
307843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Artist:    %s\n", partist);
308843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->artist = strdup(partist);
309843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if (palbumartist == NULL)
31075fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij      albuminfo->artist = strdup(partist);
311843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
312843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (pcomposer) {
313843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Writer or Composer:    %s\n", pcomposer);
314843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->composer = strdup(pcomposer);
315843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    albuminfo->composer = strdup(pcomposer);
316843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
317843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (pgenre) {
318843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Genre:     %s\n", pgenre);
319843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->genre = strdup(pgenre);
320843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    albuminfo->genre = strdup(pgenre);
321843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
322843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (year > 0) {
323843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    char tmp[80];
324843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Year:      %d\n", year);
325843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    snprintf(tmp, sizeof(tmp)-1, "%4d0101T0000.0", year);
326843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    tmp[sizeof(tmp)-1] = '\0';
327843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->date = strdup(tmp);
328843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
329843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (tracknum > 0) {
330843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Track no:  %d\n", tracknum);
331843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->tracknumber = tracknum;
332843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
333843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (length > 0) {
334843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Length:    %d\n", length);
335843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    // Multiply by 1000 since this is in milliseconds
336843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->duration = length * 1000;
337843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
338843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  // We should always have this
339843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (filename != NULL) {
340843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->filename = strdup(filename);
341843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
342843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  trackmeta->filesize = filesize;
343843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  trackmeta->parent_id = parent_id;
344843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  {
345843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    int rc;
346843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    char *desc = NULL;
347843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    LIBMTP_devicestorage_t *pds = NULL;
34831b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij
349843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if (0 != (rc=LIBMTP_Get_Storage(device, LIBMTP_STORAGE_SORTBY_NOTSORTED))) {
350843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      perror("LIBMTP_Get_Storage()");
351843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      exit(-1);
352cb4ac9f611bfb9db628378291da95a69be84f841cjdebenh    }
353843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    for (pds = device->storage; pds != NULL; pds = pds->next) {
354843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      if (pds->id == storageid) {
355843210f205cf470eab0980bd6226cf244c564f21Linus Walleij	desc = strdup(pds->StorageDescription);
356843210f205cf470eab0980bd6226cf244c564f21Linus Walleij	break;
357843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      }
358070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    }
359843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    if (NULL != desc) {
360843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      printf("Storage ID: %s (%u)\n", desc, storageid);
361843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      free(desc);
362843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    } else
363843210f205cf470eab0980bd6226cf244c564f21Linus Walleij      printf("Storage ID: %u\n", storageid);
364843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    trackmeta->storage_id = storageid;
365843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
36617e39f701249cbed29d01a752b5e9df0f0ecb2c7Linus Walleij
367843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  printf("Sending track...\n");
368843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  ret = LIBMTP_Send_Track_From_File(device, from_path, trackmeta, progress, NULL);
369843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  printf("\n");
370843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (ret != 0) {
371843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("Error sending track.\n");
372843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    LIBMTP_Dump_Errorstack(device);
373843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    LIBMTP_Clear_Errorstack(device);
374843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    ret = 1;
375843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  } else {
376843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    printf("New track ID: %d\n", trackmeta->item_id);
377843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  }
37831b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij
379843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  /* Add here add to album call */
380843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  if (palbum)
381843210f205cf470eab0980bd6226cf244c564f21Linus Walleij    ret = add_track_to_album(albuminfo, trackmeta);
38275fe2bfe994ebb2b9779b063a9fefd01fdaf9645Linus Walleij
383843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  LIBMTP_destroy_album_t(albuminfo);
384843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  LIBMTP_destroy_track_t(trackmeta);
385ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner  free (to_path_copy);
386843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
387843210f205cf470eab0980bd6226cf244c564f21Linus Walleij  return ret;
388bde621f56ce11f32603daec2538ac7e7ce0176c1Linus Walleij}
38971f45f0cdc69d859e3daad4c5999f9b38993adbacjdebenh
390843210f205cf470eab0980bd6226cf244c564f21Linus Walleijint sendtrack_command (int argc, char **argv) {
391aca5c2609ceb84cccc1d58b196db87b959e657ccMarcus Meissner  int opt, ret;
392070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  extern int optind;
393070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  extern char *optarg;
394070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  char *partist = NULL;
39531b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij  char *palbumartist = NULL;
39631b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij  char *pcomposer = NULL;
397070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  char *ptitle = NULL;
398070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  char *pgenre = NULL;
399070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  char *pcodec = NULL;
400070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  char *palbum = NULL;
401070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  uint16_t tracknum = 0;
402070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  uint16_t length = 0;
403070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  uint16_t year = 0;
404070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  uint16_t quiet = 0;
4054347fda58929ff3e4f6aa93a510b268280362d9cLinus Walleij  uint32_t storageid = 0;
4064347fda58929ff3e4f6aa93a510b268280362d9cLinus Walleij  while ( (opt = getopt(argc, argv, "qD:t:a:A:w:l:c:g:n:d:y:s:")) != -1 ) {
407070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    switch (opt) {
408070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    case 't':
409ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner      free (ptitle);
410070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      ptitle = strdup(optarg);
411070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      break;
412070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    case 'a':
413ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner      free (partist);
414070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      partist = strdup(optarg);
415070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      break;
41631b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij    case 'A':
417ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner      free (palbumartist);
41831b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij      palbumartist = strdup(optarg);
41931b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij      break;
42031b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij    case 'w':
421ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner      free (pcomposer);
42231b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij      pcomposer = strdup(optarg);
42331b742984086f29f94d427cc4720872b0a98c9f6Linus Walleij      break;
424070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    case 'l':
425ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner      free (palbum);
426070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      palbum = strdup(optarg);
427070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      break;
428070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    case 'c':
429ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner      free (pcodec);
430070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      pcodec = strdup(optarg); // FIXME: DSM check for MP3, WAV or WMA
431070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      break;
432070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    case 'g':
433ff7e5451e54c778f94a5ef3750f29423afdb1b6dMarcus Meissner      free (pgenre);
434070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      pgenre = strdup(optarg);
435070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      break;
436070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    case 'n':
437070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      tracknum = atoi(optarg);
438070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      break;
4394347fda58929ff3e4f6aa93a510b268280362d9cLinus Walleij    case 's':
4404347fda58929ff3e4f6aa93a510b268280362d9cLinus Walleij      storageid = (uint32_t) strtoul(optarg, NULL, 0);
4414347fda58929ff3e4f6aa93a510b268280362d9cLinus Walleij      break;
442070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    case 'd':
443070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      length = atoi(optarg);
444070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      break;
445070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    case 'y':
446070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      year = atoi(optarg);
447070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      break;
448070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    case 'q':
449070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      quiet = 1;
450070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      break;
451070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    default:
452070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij      sendtrack_usage();
453070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    }
454070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  }
455070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  argc -= optind;
456070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  argv += optind;
457843210f205cf470eab0980bd6226cf244c564f21Linus Walleij
458070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  if ( argc != 2 ) {
459070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    printf("You need to pass a filename and destination.\n");
460070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij    sendtrack_usage();
46161fe60fc30d8f4a5cf2b6d95f3f8344167a37267Marcus Meissner    ret = 0;
46261fe60fc30d8f4a5cf2b6d95f3f8344167a37267Marcus Meissner  } else {
46361fe60fc30d8f4a5cf2b6d95f3f8344167a37267Marcus Meissner    checklang();
46461fe60fc30d8f4a5cf2b6d95f3f8344167a37267Marcus Meissner    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);
46561fe60fc30d8f4a5cf2b6d95f3f8344167a37267Marcus Meissner    ret = sendtrack_function(argv[0],argv[1],partist,palbumartist,ptitle,pgenre,palbum,pcomposer, tracknum, length, year, storageid, quiet);
466070e9b4a25c2384f070f20b92c8101c9e3f58f33Linus Walleij  }
467aca5c2609ceb84cccc1d58b196db87b959e657ccMarcus Meissner  free (ptitle);
468aca5c2609ceb84cccc1d58b196db87b959e657ccMarcus Meissner  free (partist);
469aca5c2609ceb84cccc1d58b196db87b959e657ccMarcus Meissner  free (palbumartist);
470aca5c2609ceb84cccc1d58b196db87b959e657ccMarcus Meissner  free (pcomposer);
471aca5c2609ceb84cccc1d58b196db87b959e657ccMarcus Meissner  free (palbum);
472aca5c2609ceb84cccc1d58b196db87b959e657ccMarcus Meissner  free (pcodec);
473aca5c2609ceb84cccc1d58b196db87b959e657ccMarcus Meissner  free (pgenre);
474aca5c2609ceb84cccc1d58b196db87b959e657ccMarcus Meissner  return ret;
47571f45f0cdc69d859e3daad4c5999f9b38993adbacjdebenh}
476