summaryrefslogtreecommitdiff
path: root/mugglei.c
blob: 1e2f7ae08c1937675d303ab81d37498c43d2cf55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*!
 * \file mugglei.c
 * \brief implement a small utility for importing files
 *
 * \author  Lars von Wedel
 */

// #define VERBOSE

#include <unistd.h>
#include <string>

#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <mysql/mysql.h>
#include <getopt.h>
/*extern "C"
{*/
  #include <stdarg.h>
  #include <stdio.h>
/*}
*/
#include <stdlib.h>

#include <tag.h>
#include <mpegfile.h>
#include <flacfile.h>
#include <id3v2tag.h>
#include <fileref.h>

#include "mg_tools.h"
#include "mg_setup.h"
#include "mg_sync.h"


using namespace std;

int SysLogLevel = 1;

bool import_assorted, delete_mode, create_mode;

void showmessage(const char *msg,int duration)
{
}

void showimportcount(unsigned int count)
{
}

const char *I18nTranslate(const char *s,const char *Plugin)
{
	return s;
}

int main( int argc, char *argv[] )
{
	mgSetDebugLevel(1);
      
  if( argc < 2 )
    { // we need at least a filename!
      std::cout << "mugglei -- import helper for Muggle VDR plugin" << std::endl;
      std::cout << "(C) Lars von Wedel" << std::endl;
      std::cout << "This is free software; see the source for copying conditions." << std::endl;
      std::cout << "" << std::endl;
      std::cout << "Usage: mugglei [OPTION]... [FILE]..." << std::endl;
      std::cout << "" << std::endl;
      std::cout << "  all FILE arguments will be imported. If they are directories, their content is imported"<< std::endl;
      std::cout << "" << std::endl;
      std::cout << "Only files ending in .flac, .mp3, .ogg (ignoring case) will be imported" << std::endl;
      std::cout << "" << std::endl;
      std::cout << "Options:" << std::endl;
      std::cout << "  -h <hostname>       - specify host of mySql database server (default is 'localhost')" << std::endl;
      std::cout << "  -s <socket>         - specify a socket for mySQL communication (default is TCP)" << std::endl;
      std::cout << "  -n <database>       - specify database name (default is 'GiantDisc')" << std::endl;
      std::cout << "  -u <username>       - specify user of mySql database (default is empty)" << std::endl;
      std::cout << "  -p <password>       - specify password of user (default is empty password)" << std::endl;
      std::cout << "  -t <topleveldir>    - name of music top level directory" << std::endl;
      std::cout << "  -z                  - scan all database entries and delete entries for files not found" << std::endl;
      std::cout << "                        -z is not yet implemented" << std::endl;
      std::cout << "  -c                  - delete the entire database and recreate a new empty one" << std::endl;
#ifndef HAVE_SERVER
      std::cout << "  -d <datadir>        - the data directory for the embedded mysql server. Defaults to ./.muggle" << std::endl;
#endif
      std::cout << "  -v                  - the wanted log level, the higher the more. Default is 1" << std::endl;

      exit( 1 );
    }

  // option defaults
  import_assorted = false;
  delete_mode = false;
  create_mode = false;
#ifndef HAVE_SERVER
  char *buf;
  asprintf(&buf,"%s/.muggle",getenv("HOME"));
  set_datadir(buf);
  free(buf);
#endif

  // parse command line options
  while( 1 )
    {
#ifndef HAVE_SERVER
      int c = getopt(argc, argv, "h:s:n:u:p:t:zcv:d:");
#else
      int c = getopt(argc, argv, "h:s:n:u:p:t:zcv:");
#endif

      if (c == -1)
	break;
      
      switch (c) 
	{
	case 0:
	  { // long option
	    
	  } break;
	case 'h':
	  {
	    the_setup.DbHost = optarg;
	  } break;
	case 'n':
	  {
	    the_setup.DbName = optarg;
	  } break;
	case 'u':
	  {
	    the_setup.DbUser = optarg;
	  } break;
	case 'p':
	  {
	    the_setup.DbPass = optarg;
	  } break;
	case 's':
	  {
	    the_setup.DbSocket = optarg;
	  } break;
	case 't':
	  {
	    the_setup.ToplevelDir = optarg;
	  } break;
        case 'z':
          {
            delete_mode = true;
          } break;
        case 'c':
          {
            create_mode = true;
          } break;
        case 'v':
          {
	    mgSetDebugLevel(atol(optarg));
          } break;
#ifndef HAVE_SERVER
        case 'd':
          {
	    set_datadir(optarg);
          } break;
#endif
	}
    }
  mgSync *sync = new mgSync; // because we want to delete it before database_end
  if (create_mode)
	  sync->Create();
  if (optind<argc)
	  sync->Sync(argv+optind,delete_mode);
  delete sync;
  database_end();
  return 0;
}