new frontend - FUSE
[mediadatabase.git] / cui / main.c
blob7a540f28cc225e5e53e5e73599cea6340cf2453a
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * DESCRIPTION:
5 * Portable frontend to MediaDatabase
7 * COMPILATION:
8 * gcc main.c -lmysqlclient -o mdb_cui [-DOPENBSD]
10 * AUTHOR:
11 * Nedko Arnaudov <nedko@users.sourceforge.net>
13 * LICENSE:
14 * GNU GENERAL PUBLIC LICENSE version 2
16 *****************************************************************************/
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <dirent.h>
23 #include <sys/stat.h>
24 #include <errno.h>
25 #include <sys/time.h>
26 #include <time.h>
27 #include <sys/wait.h>
28 #include <pwd.h>
29 #include <unistd.h>
30 #include <cfl.h>
31 #include <assert.h>
32 extern char **environ;
34 #include "../result.h"
35 #include "../libdb/libdb.h"
36 #include "../libdb/memory.h"
37 #include "../libfrontend/error.h"
38 #include "../libfrontend/conf.h"
39 #include "../libfrontend/db.h"
40 #include "../libfrontend/disk.h"
41 #include "../libfrontend/helper.h"
43 #define MAX_TITLE_SIZE 128 /* in db */
44 #define MAX_PASS_SIZE 1024
45 #define MAX_READ_STRING_SIZE 10240
46 //#define PRINT_FILENAMES
48 char strTitle[MAX_TITLE_SIZE+2]; /* + newline + null */
49 char strPass[MAX_PASS_SIZE];
50 mediadb_uint nMediaID = 0ULL;
51 int blnExiting = 0;
52 char *pszTitle = NULL;
53 const char *pszHOME;
55 #define MODE_LIST_MEDIA 0
56 #define MODE_ADD_MEDIA 1
57 #define MODE_UPDATE_MEDIA 2
58 #define MODE_SEARCH_FILES 3
59 unsigned int nMode = MODE_SEARCH_FILES;
61 void
62 ExecuteCommand(const char *pszCommand);
64 void
65 MountMedia()
67 printf("--> Mounting media ...\n");
68 disk_open();
69 printf("--> Media mounted.\n");
72 void
73 UnmountMedia()
75 printf("--> Unmounting media ...\n");
76 disk_close();
77 printf("--> Media unmounted.\n");
80 void
81 Exit(int nRet)
83 if (blnExiting)
84 return;
86 blnExiting = 1;
88 disk_uninit();
90 conf_cleanup();
92 db_uninit();
94 if (pszTitle != NULL)
95 free(pszTitle);
97 exit(nRet);
100 void
101 mediadb_error_callback(
102 unsigned int nCritical,
103 const char *pszErrorDescription
106 fprintf(stderr,
107 "%s\n",
108 pszErrorDescription);
110 if (nCritical == MEDIADB_ERROR_CRITICAL)
112 Exit(1);
116 void
117 ExecuteCommand(const char *pszCommand)
119 int nRet;
121 nRet = system(pszCommand);
122 if (nRet == -1)
124 fprintf(stderr,
125 "Cannot execute command \"%s\". "
126 "system() returned -1, error is %d (%s)\n",
127 pszCommand,
128 errno,
129 strerror(errno));
130 Exit(1);
133 if (WIFEXITED(nRet))
135 if (WEXITSTATUS(nRet) != 0)
137 fprintf(stderr,
138 "Cannot execute command \"%s\". "
139 "Command returned %d\n",
140 pszCommand,
141 (int)WEXITSTATUS(nRet));
142 Exit(1);
145 else if (WIFSIGNALED(nRet))
147 fprintf(stderr,
148 "Cannot execute command \"%s\". "
149 "Cammand was terminated because of signal %d\n",
150 pszCommand,
151 (int)WTERMSIG(nRet));
152 Exit(1);
154 else
156 fprintf(stderr,
157 "Cannot execute command \"%s\". "
158 "system() returned %d\n",
159 pszCommand,
160 nRet);
161 Exit(1);
165 void
166 ReadTitleExitOnEmpty()
168 char *pch;
170 printf("Title: ");
171 if (fgets(strTitle, sizeof(strTitle), stdin) == NULL)
173 printf("\n");
175 if (feof(stdin))
177 Exit(0);
180 fprintf(stderr, "Error occured while reading standard input.\n");
181 Exit(1);
183 else if (strTitle[0] == '\n')
185 Exit(0);
188 if ((pch = strchr(strTitle, '\n')) == NULL)
190 fprintf(stderr, "input line too long.\n");
191 Exit(1);
194 *pch = '\0';
197 void
198 ReadMediaID()
200 char *pch;
201 char strMediaID[100];
203 printf("Media ID: ");
204 if (fgets(strMediaID, sizeof(strMediaID), stdin) == NULL)
206 printf("\n");
208 if (feof(stdin))
210 Exit(0);
213 fprintf(stderr, "Error occured while reading standard input.\n");
214 Exit(1);
216 else if (strMediaID[0] == '\n')
218 Exit(0);
221 if ((pch = strchr(strMediaID, '\n')) == NULL)
223 fprintf(stderr, "input line too long.\n");
224 Exit(1);
227 *pch = '\0';
229 nMediaID = strtoull(strMediaID, NULL, 10);
230 if (nMediaID == 0)
232 Exit(1);
236 mediadb_bool
237 ReadInteger(
238 const char *pszQuestion,
239 mediadb_bool blnExitOnEmpty,
240 mediadb_uint *pn)
242 char *pch;
243 char str[100];
245 printf("%s", pszQuestion);
246 if (fgets(str, sizeof(str), stdin) == NULL)
248 printf("\n");
250 if (feof(stdin))
252 Exit(0);
255 fprintf(stderr, "Error occured while reading standard input.\n");
256 Exit(1);
258 else if (str[0] == '\n')
260 if (blnExitOnEmpty)
262 Exit(0);
264 else
266 return MEDIADB_FALSE;
270 if ((pch = strchr(str, '\n')) == NULL)
272 fprintf(stderr, "input line too long.\n");
273 Exit(1);
276 *pch = '\0';
278 *pn = strtoull(str, NULL, 10);
279 return MEDIADB_TRUE;
282 mediadb_bool
283 ReadString(
284 const char *pszQuestion,
285 mediadb_bool blnExitOnEmpty,
286 char *pBuffer)
288 char *pch;
290 printf("%s", pszQuestion);
291 if (fgets(pBuffer, MAX_READ_STRING_SIZE, stdin) == NULL)
293 printf("\n");
295 if (feof(stdin))
297 Exit(0);
300 fprintf(stderr, "Error occured while reading standard input.\n");
301 Exit(1);
303 else if (pBuffer[0] == '\n')
305 if (blnExitOnEmpty)
307 Exit(0);
309 else
311 return MEDIADB_FALSE;
315 if ((pch = strchr(pBuffer, '\n')) == NULL)
317 fprintf(stderr, "input line too long.\n");
318 Exit(1);
321 *pch = '\0';
322 return MEDIADB_TRUE;
325 void
326 Menu(
327 const char *pszQuestion,
328 const char **ppszOptions,
329 unsigned int nOptionsCount,
330 unsigned int nDefaultIndex,
331 unsigned int *pnIndex)
333 char *pch;
334 char str[100];
335 unsigned int i;
336 unsigned long choice;
338 assert(nDefaultIndex < nOptionsCount);
340 Retry:
341 printf("%s\n", pszQuestion);
342 for (i = 0 ; i < nOptionsCount ; i++)
344 printf(" [%u] %s", i+1, ppszOptions[i]);
345 if (i == nDefaultIndex)
347 printf(" (default)\n");
349 else
351 printf("\n");
355 printf("What is your choice ? [default %u] ", nDefaultIndex+1);
356 if (fgets(str, sizeof(str), stdin) == NULL)
358 printf(" \n");
360 if (feof(stdin))
362 Exit(0);
365 fprintf(stderr, "Error occured while reading standard input.\n");
366 Exit(1);
368 else if (str[0] == '\n')
370 *pnIndex = nDefaultIndex;
371 return;
374 if ((pch = strchr(str, '\n')) == NULL)
376 fprintf(stderr, "input line too long.\n");
377 Exit(1);
380 *pch = '\0';
382 choice = strtoul(str, NULL, 10);
384 if (choice < 1 || choice > nOptionsCount)
386 printf("Cannot interpret \"%s\" as valid menu selection.", str);
387 goto Retry;
390 *pnIndex = choice - 1;
393 void
394 ReadTitleIgnoreOnEmpty()
396 char *pch;
398 printf("Title: ");
399 if (fgets(strTitle, sizeof(strTitle), stdin) == NULL)
401 printf("\n");
403 if (feof(stdin))
405 Exit(0);
408 fprintf(stderr, "Error occured while reading standard input.\n");
409 Exit(1);
411 else if (strTitle[0] == '\n')
413 strcpy(strTitle, pszTitle);
414 return;
417 if ((pch = strchr(strTitle, '\n')) == NULL)
419 fprintf(stderr, "input line too long.\n");
420 Exit(1);
423 *pch = '\0';
426 void
427 AskForPassword()
429 char *pszPass;
431 pszPass = getpass("MySQL password: ");
433 strcpy(strPass, pszPass);
436 void
437 MaybeEnlargeBuffer(char ** ppBuffer,
438 size_t *pnBufferSize,
439 size_t nSizeRequired)
441 mediadb_result r;
443 r = maybe_enlarge_buffer(
444 ppBuffer,
445 pnBufferSize,
446 nSizeRequired);
447 if (MEDIADB_IS_ERROR(r))
449 fprintf(stderr,
450 "Cannot enlarge buffer.\n");
451 Exit(0);
455 mediadb_result
456 mediadb_scan_callback(
457 void *pUserContext,
458 const char *pszFilename,
459 const mediadb_filetype nType,
460 const char *pszPath,
461 mediadb_uint nFileSize,
462 mediadb_uint nFileTime)
464 mediadb_result r;
466 r = mediadb_file_add_new(
467 g_hDB,
468 nMediaID,
469 nType,
470 pszPath,
471 pszFilename,
472 nFileSize,
473 nFileTime);
474 if (MEDIADB_IS_ERROR(r))
476 fprintf(stderr,
477 "Failed to add new file to database. Error is %d (%s)\n",
478 (int)r,
479 mediadb_get_error_message(g_hDB));
482 #ifdef PRINT_FILENAMES
483 printf("%s/%s", pszPath, pszFilename);
484 #endif
486 if (nType == MEDIADB_FILETYPE_DIR)
488 #ifdef PRINT_FILENAMES
489 printf("/\n");
490 #else
491 printf("/");
492 #endif
494 else
496 #ifdef PRINT_FILENAMES
497 printf("\n");
498 #else
499 printf(".");
500 #endif
503 fflush(stdout);
505 return r;
508 void
509 SetDefaults()
511 db_set_defaults();
513 disk_set_defaults();
516 void
517 Help(char *arg0)
519 char *pszExecutable;
521 pszExecutable = strrchr(arg0, '/');
523 if (pszExecutable == NULL)
525 pszExecutable = arg0;
527 else
529 pszExecutable++;
532 SetDefaults();
534 printf("==============================\n");
535 printf("Offline media content database\n");
536 printf("==============================\n");
537 printf("Usage:\n");
538 printf("[<path_to>]%s [options]\n", pszExecutable);
539 printf(" Options can be:\n");
540 printf(" -? - This help\n");
541 /* printf(" -L - List media mode\n"); */
542 printf(" -A - Add media mode\n");
543 printf(" -U - Update media mode\n");
544 printf(" -F - Search files mode\n");
545 printf(" -m <mount_cmd> - command to mount media, default is \"%s\"\n", disk_get_mount_command());
546 printf(" -n <unmount_cmd> - command to unmount, default is \"%s\"\n", disk_get_unmount_command());
547 printf(" -M - Use MySQL backend%s\n", (db_get_type() == MEDIADB_DBTYPE_MYSQL)?" (default)":"");
548 printf(" -d <mount_dir> - where media is mounted, default is \"%s\"\n", disk_get_path());
549 printf(" -h <mysql_host> - MySQL server host, default is \"%s\"\n", db_get_mysql_host());
550 printf(" -u <mysql_user> - MySQL user, default is \"%s\"\n", db_get_mysql_user());
551 printf(" -p [mysql_pass] - MySQL password, default is \"%s\"\n", db_get_mysql_pass());
552 printf(" -b <mysql_database> - MySQL database, default is \"%s\"\n", db_get_mysql_database());
553 printf(" -S - Use SQLite backend%s\n", (db_get_type() == MEDIADB_DBTYPE_SQLITE)?" (default)":"");
554 printf(" -f <sqlite_database_file> - SQLite database file, default is \"%s\"\n", db_get_sqlite_database());
555 Exit(0);
558 void
559 AddMedia()
561 mediadb_result r;
562 time_t timeAdded;
563 mediadb_uint nTotalFiles;
564 mediadb_uint nTotalSize;
566 printf("NOTE: Currently audio media is not supported.\n");
568 Loop:
569 printf("--> Please insert media and enter title.\n");
571 ReadTitleExitOnEmpty();
573 MountMedia();
575 printf("--> Processing media ...\n");
577 r = mediadb_media_add_new(
578 g_hDB,
579 strTitle,
581 MEDIADB_MT_DATA,
582 &nMediaID);
584 if (MEDIADB_IS_ERROR(r))
586 fprintf(stderr,
587 "Failed to add new media to database. Error is %d (%s)\n",
588 (int)r,
589 mediadb_get_error_message(g_hDB));
590 Exit(1);
593 disk_scan(mediadb_scan_callback,
594 NULL,
595 &nTotalFiles,
596 &nTotalSize);
598 printf("\n");
600 timeAdded = time(NULL);
602 r = mediadb_media_update_properties(
603 g_hDB,
604 nMediaID,
605 timeAdded,
606 nTotalFiles,
607 nTotalSize);
609 if (MEDIADB_IS_ERROR(r))
611 fprintf(stderr,
612 "Failed to update new media to database. Error is %d (%s)\n",
613 (int)r,
614 mediadb_get_error_message(g_hDB));
615 Exit(1);
618 UnmountMedia();
620 printf("--> SUCCESS - Media added.\n");
621 printf("Media title: %s\n", strTitle);
622 printf("Media ID: %u\n", (unsigned int)nMediaID);
623 printf("Added: %s", ctime(&timeAdded));
624 printf("Total files: %u\n", (unsigned int)nTotalFiles);
625 printf("Total size: %u\n", (unsigned int)nTotalSize);
627 goto Loop;
630 void
631 UpdateMedia()
633 mediadb_result r;
634 time_t timeAdded;
635 mediadb_uint nTimeAdded;
636 mediadb_uint nTotalFiles;
637 mediadb_uint nTotalSize;
638 mediadb_mediatype nType;
639 mediadb_uint nLocationID;
641 printf("NOTE: Currently audio media is not supported.\n");
643 Loop:
644 printf("--> Please enter Media ID.\n");
646 ReadMediaID();
648 r = mediadb_media_get_properties(
649 g_hDB,
650 nMediaID,
651 &nTimeAdded,
652 &nType,
653 &nLocationID,
654 &pszTitle);
655 if (MEDIADB_IS_ERROR(r))
657 fprintf(stderr,
658 "Failed to get media properties. Error is %d (%s)\n",
659 (int)r,
660 mediadb_get_error_message(g_hDB));
661 Exit(1);
664 if (nType != MEDIADB_MT_DATA)
666 fprintf(stderr,
667 "Only data cd type is supprted.\n");
668 Exit(1);
671 r = mediadb_media_get_properties_data(
672 g_hDB,
673 nMediaID,
674 &nTotalFiles,
675 &nTotalSize);
676 if (MEDIADB_IS_ERROR(r))
678 fprintf(stderr,
679 "Failed to get media properties. Error is %d (%s)\n",
680 (int)r,
681 mediadb_get_error_message(g_hDB));
682 Exit(1);
685 if (strlen(pszTitle) + 1 > MAX_TITLE_SIZE)
687 fprintf(stderr,
688 "Title too big\n");
689 Exit(1);
692 timeAdded = (time_t)nTimeAdded;
694 printf("Media title: %s\n", pszTitle);
695 printf("Added: %s", ctime(&timeAdded));
696 printf("Total files: %u\n", (unsigned int)nTotalFiles);
697 printf("Total size: %u\n", (unsigned int)nTotalSize);
699 printf("--> Please insert media and enter title (Just press return to keep current title).\n");
701 ReadTitleIgnoreOnEmpty();
703 MountMedia();
705 printf("--> Processing media ...\n");
707 if (strcmp(pszTitle, strTitle) != 0)
709 r = mediadb_media_update_name(
710 g_hDB,
711 nMediaID,
712 strTitle);
713 if (MEDIADB_IS_ERROR(r))
715 fprintf(stderr,
716 "Cannot update media title. Error is %d (%s)\n",
717 (int)r,
718 mediadb_get_error_message(g_hDB));
719 Exit(1);
723 r = mediadb_delete_media_files(
724 g_hDB,
725 nMediaID);
727 if (MEDIADB_IS_ERROR(r))
729 fprintf(stderr,
730 "Failed to delete media files in database. Error is %d (%s)\n",
731 (int)r,
732 mediadb_get_error_message(g_hDB));
733 Exit(1);
736 disk_scan(mediadb_scan_callback,
737 NULL,
738 &nTotalFiles,
739 &nTotalSize);
741 printf("\n");
743 //timeAdded = time(NULL);
745 r = mediadb_media_update_properties(
746 g_hDB,
747 nMediaID,
748 timeAdded,
749 nTotalFiles,
750 nTotalSize);
752 if (MEDIADB_IS_ERROR(r))
754 fprintf(stderr,
755 "Failed to update new media to database. Error is %d (%s)\n",
756 (int)r,
757 mediadb_get_error_message(g_hDB));
758 Exit(1);
761 UnmountMedia();
763 printf("--> SUCCESS - Media added.\n");
764 printf("Media title: %s\n", strTitle);
765 printf("Media ID: %u\n", (unsigned int)nMediaID);
766 printf("Added: %s", ctime(&timeAdded));
767 printf("Total files: %u\n", (unsigned int)nTotalFiles);
768 printf("Total size: %u\n", (unsigned int)nTotalSize);
770 goto Loop;
773 /* void */
774 /* ListMedia() */
775 /* { */
776 /* printf("List media mode not implemented yet.\n"); */
777 /* } */
779 mediadb_uint g_SearchFilesCounter;
781 void
782 SearchFilesCallback(
783 void *pUserContext,
784 mediadb_uint nMediaID,
785 mediadb_mediatype nMediaType,
786 const char *pszMediaName,
787 const char *pszMediaLocation,
788 const char *pszPath,
789 const char *pszName,
790 mediadb_filetype Filetype,
791 mediadb_uint nSize,
792 mediadb_uint nTime)
794 struct tm tm;
795 time_t time = nTime;
796 char strTime[26];
797 char strSize[MAX_SIZE_DESCRIPTION];
799 localtime_r(&time, &tm);
800 sprintf(strTime,
801 "%u-%02u-%02u %02u:%02u:%02u",
802 1900+tm.tm_year,
803 1+tm.tm_mon,
804 tm.tm_mday,
805 tm.tm_hour,
806 tm.tm_min,
807 tm.tm_sec);
809 get_size_description(nSize, strSize);
811 printf("--------------------\n");
812 printf("Name: %s%s\n", pszPath, pszName);
813 if (Filetype == MEDIADB_FILETYPE_FILE)
815 printf("Size: %s\n", strSize);
817 printf("Time: %s\n", strTime);
818 printf("MediaID: %u\n", (unsigned int)nMediaID);
819 printf("Media name: %s\n", pszMediaName);
820 printf("Media location: %s\n", pszMediaLocation);
822 g_SearchFilesCounter++;
825 void
826 SearchFiles()
828 mediadb_result r;
829 mediadb_uint nMinSize;
830 mediadb_uint nMaxSize;
831 char strFilenamePattern[MAX_READ_STRING_SIZE];
832 char strPathPattern[MAX_READ_STRING_SIZE];
833 const char **ppszPMMS;
834 const struct mediadb_pattern_match_method *pPMM;
835 unsigned int nPPMCount;
836 unsigned int nFilenamePMMIndex;
837 unsigned int nPathPMMIndex;
838 unsigned int i;
839 struct timeval tv1, tv2;
840 struct timezone tz;
841 double t1, t2;
843 r = mediadb_get_pattern_match_methods(
844 g_hDB,
845 &pPMM);
846 if (MEDIADB_IS_ERROR(r))
848 fprintf(stderr,
849 "Cannot get pattern match methods from backend. Error is %d (%s)\n",
850 (int)r,
851 mediadb_get_error_message(g_hDB));
852 Exit(1);
855 nPPMCount = 0;
856 while (pPMM[nPPMCount].nID != MEDIADB_PMM_NULL)
858 nPPMCount++;
861 if (nPPMCount == 0)
863 fprintf(stderr,
864 "Backend returned zero pattern march methods.\n");
865 Exit(1);
868 ppszPMMS = malloc(sizeof(const char *) * nPPMCount);
869 if (ppszPMMS == NULL)
871 fprintf(stderr,
872 "Out of memory.\n");
873 Exit(1);
876 for (i = 0 ; i < nPPMCount ; i++)
878 ppszPMMS[i] = pPMM[i].pszName;
881 if (!ReadString("Filename pattern: ", MEDIADB_FALSE, strFilenamePattern))
883 strFilenamePattern[0] = 0;
885 else
887 Menu("Filename pattern match method",
888 ppszPMMS,
889 nPPMCount,
891 &nFilenamePMMIndex);
894 if (!ReadString("Path pattern: ", MEDIADB_FALSE, strPathPattern))
896 strPathPattern[0] = 0;
898 else
900 Menu("Path pattern match method",
901 ppszPMMS,
902 nPPMCount,
904 &nPathPMMIndex);
907 free(ppszPMMS);
909 if (!ReadInteger("Min size: ", MEDIADB_FALSE, &nMinSize))
911 nMinSize = 0;
914 if (!ReadInteger("Max size: ", MEDIADB_FALSE, &nMaxSize))
916 nMaxSize = 0;
919 printf("--> Searching ...\n");
920 g_SearchFilesCounter = 0;
922 if (gettimeofday(&tv1, &tz) == -1)
924 fprintf(stderr,
925 "gettimeofday() failed. Error is %d (%s)\n",
926 errno,
927 strerror(errno));
928 Exit(1);
931 r = mediadb_files_search(
932 g_hDB,
933 strFilenamePattern[0]?pPMM[nFilenamePMMIndex].nID:MEDIADB_PMM_NULL,
934 strFilenamePattern[0]?strFilenamePattern:NULL,
935 strPathPattern[0]?pPMM[nPathPMMIndex].nID:MEDIADB_PMM_NULL,
936 strPathPattern[0]?strPathPattern:NULL,
937 nMinSize?&nMinSize:NULL,
938 nMaxSize?&nMaxSize:NULL,
939 SearchFilesCallback,
940 NULL);
942 if (gettimeofday(&tv2, &tz) == -1)
944 fprintf(stderr,
945 "gettimeofday() failed. Error is %d (%s)\n",
946 errno,
947 strerror(errno));
948 Exit(1);
951 printf("--------------------\n");
953 if (MEDIADB_IS_ERROR(r))
955 fprintf(stderr,
956 "Search failed. Error is %d (%s)\n",
957 (int)r,
958 mediadb_get_error_message(g_hDB));
959 Exit(1);
962 printf("%u file(s) found\n", (unsigned int)g_SearchFilesCounter);
964 t1 = tv1.tv_sec;
965 t1 += (double)tv1.tv_usec / 1000000.0;
966 t2 = tv2.tv_sec;
967 t2 += (double)tv2.tv_usec / 1000000.0;
968 printf("Search took %f seconds\n", t2-t1);
972 main(
973 int argc,
974 char ** argv
977 int i;
978 char *pszExecutable;
979 mediadb_result r;
981 pszExecutable = strrchr(argv[0], '/');
983 if (pszExecutable == NULL)
985 pszExecutable = argv[0];
987 else
989 pszExecutable++;
992 if (strcmp(pszExecutable, "mdb_add") == 0)
994 nMode = MODE_ADD_MEDIA;
996 else if (strcmp(pszExecutable, "mdb_update") == 0)
998 nMode = MODE_UPDATE_MEDIA;
1000 else
1002 nMode = MODE_SEARCH_FILES;
1005 /* Find HOME environment variable */
1006 pszHOME = getenv("HOME");
1008 if (pszHOME == NULL)
1010 fprintf(stderr,
1011 "Please set the HOME encironment variable.\n");
1012 Exit(1);
1015 /* Look into configuration file */
1016 r = conf_parse();
1018 /* Process options from command line */
1020 for (i = 1; i < argc ; i++)
1022 /* if (strcmp(argv[i],"-L") == 0) */
1023 /* { */
1024 /* nMode = MODE_LIST_MEDIA; */
1025 /* } */
1026 /* else */
1027 if (strcmp(argv[i],"-A") == 0)
1029 nMode = MODE_ADD_MEDIA;
1031 else if (strcmp(argv[i],"-U") == 0)
1033 nMode = MODE_UPDATE_MEDIA;
1035 else if (strcmp(argv[i],"-F") == 0)
1037 nMode = MODE_SEARCH_FILES;
1039 else if (strcmp(argv[i],"-h") == 0)
1041 i++;
1042 if (i < argc)
1044 db_set_mysql_host(argv[i]);
1046 else
1048 fprintf(stderr,
1049 "Missing parameter of -h option\n");
1050 Help(argv[0]);
1053 else if (strcmp(argv[i],"-u") == 0)
1055 i++;
1056 if (i < argc)
1058 db_set_mysql_user(argv[i]);
1060 else
1062 fprintf(stderr,
1063 "Missing parameter of -u option\n");
1064 Help(argv[0]);
1067 else if (strcmp(argv[i],"-p") == 0)
1069 if (i+1 < argc && argv[i+1][0] != '-')
1071 i++;
1072 db_set_mysql_pass(argv[i]);
1074 else
1076 AskForPassword();
1077 db_set_mysql_pass(strPass);
1080 else if (strcmp(argv[i],"-b") == 0)
1082 i++;
1083 if (i < argc)
1085 db_set_mysql_database(argv[i]);
1087 else
1089 fprintf(stderr,
1090 "Missing parameter of -b option\n");
1091 Help(argv[0]);
1094 else if (strcmp(argv[i],"-f") == 0)
1096 i++;
1097 if (i < argc)
1099 db_set_sqlite_database(argv[i]);
1101 else
1103 fprintf(stderr,
1104 "Missing parameter of -f option\n");
1105 Help(argv[0]);
1108 else if (strcmp(argv[i],"-m") == 0)
1110 i++;
1111 if (i < argc)
1113 disk_set_mount_command(argv[i]);
1115 else
1117 fprintf(stderr,
1118 "Missing parameter of -m option\n");
1119 Help(argv[0]);
1122 else if (strcmp(argv[i],"-n") == 0)
1124 i++;
1125 if (i < argc)
1127 disk_set_unmount_command(argv[i]);
1129 else
1131 fprintf(stderr,
1132 "Missing parameter of -n option\n");
1133 Help(argv[0]);
1136 else if (strcmp(argv[i],"-d") == 0)
1138 i++;
1139 if (i < argc)
1141 disk_set_path(argv[i]);
1143 else
1145 fprintf(stderr,
1146 "Missing parameter of -d option\n");
1147 Help(argv[0]);
1150 else if (strcmp(argv[i],"-M") == 0)
1152 db_use_mysql();
1154 else if (strcmp(argv[i],"-S") == 0)
1156 db_use_sqlite();
1158 else if (strcmp(argv[i],"-?") == 0)
1160 Help(argv[0]);
1162 else
1164 fprintf(stderr,
1165 "Unknown option \"%s\"\n",
1166 argv[i]);
1167 Help(argv[0]);
1171 SetDefaults();
1173 db_open();
1175 if (nMode == MODE_ADD_MEDIA)
1177 printf("=========================\n");
1178 printf("Add media mode\n");
1179 printf("=========================\n");
1180 AddMedia();
1182 else if (nMode == MODE_UPDATE_MEDIA)
1184 printf("=========================\n");
1185 printf("Update media mode\n");
1186 printf("=========================\n");
1187 UpdateMedia();
1189 /* else if (nMode == MODE_LIST_MEDIA) */
1190 /* { */
1191 /* printf("=========================\n"); */
1192 /* printf("List media mode\n"); */
1193 /* printf("=========================\n"); */
1194 /* ListMedia(); */
1195 /* } */
1196 else if (nMode == MODE_SEARCH_FILES)
1198 printf("=========================\n");
1199 printf("Search files mode\n");
1200 printf("=========================\n");
1201 SearchFiles();
1203 else
1205 assert(0);
1206 return 1;
1209 Exit(0);
1210 return 1;