/* iImport - Windows iTunes to Linux Songbird Library Converter
 *
 * Author: Marco Oliveira
 * Contact: marcooliveira@ua.pt
 * Date: 18 May 2008
 *
 *
 *
 * DESCRIPTION:
 * This application is for those who have iTunes running on Microsoft Windows,
 * and want to automatically import, to Songbird, changes made in the iTunes Library.
 * This application was designed to be used with the "iTunes Library Importer" extension,
 * which allows you to import iTunes libraries, but doesn't support multiple OS imports
 * (different paths to the files).
 *
 * This application was only tested with iTunes on Windows Vista and Ubuntu, but I think it
 * is OS independent, so it should work with any combination of OS's.
 *
 *
 *
 * CONCEPT:
 * The idea is that every time you run Songbird, iImport will convert the iTunes Library
 * into a valid iTunes Library on the current OS, and then Songbird will load this new
 * Library (which I call Songbird Library). To do this, instead of launching Songbird,
 * you launch iImport, which takes the original iTunes Library file, replaces every occurrences
 * of your other OS path to the songs with the current one, and finally launches Songbird.
 *
 *
 *
 *
 * INSTALLATION
 *
 * This is what you need to do to install iImport.
 *
 * NOTE: If you don't have iImport compiled to your OS, just see below how to get it.
 *
 * 1st - Place iImport on your Songbird folder
 *
 * 2nd - Install iTunes Library Importer, you can get it from http://addons.songbirdnest.com/,
 *       and note that the Library Path you set in the addon config panel is important to the
 *       next step.
 *
 * 3rd - Modify your current (or create a new) link to Songbird, and change its "Command" to
 *       something similar to:
 *
 * 	/home/sonic/Songbird/iImport "/home/sonic/Songbird/songbird" "/media/sda1/Users/SonicSpot/Music/iTunes/iTunes Music Library.xml" "/media/sda1/Users/SonicSpot/Music/iTunes/Songbird Library.xml" "file://localhost/C:/Users/SonicSpot/Music/" "/media/sda1/Users/SonicSpot/Music/"
 *
 * Don't get scared! This is what that whole mambo jambo means:
 *
 * /home/sonic/Songbird/iImport <path to songbird executable> <path to original iTunes Library> <name of the output library (the Songbird Library)> <other OS path to songs> <current OS path to songs>
 *
 * Just to be safe, put all the paths in "quotes". Double check all paths, since they're
 * all important. By the way, don't forget to change the path to iImport.
 *
 * That's it! You're good to go! Launch your new Songbird link, and iImport will do his thing.
 *
 *
 *
 * COMPILING iImport TO MY OWN OS
 *
 * You will need a C compiler.
 *
 * In ubuntu, you can use gcc.
 *
 * Just use this command: gcc iImport.c -o iImport
 * It will generate an iImport executable file, which you'll need to place on the Songbird directory, if it isn't yet.
 * This is pretty much straight forward C, so you won't be needing any special libraries.
 *
 * Any questions, feel free to contact me.
*/
 

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

#define MAXLINE 4096

int main(int argc,char **argv) {
	FILE *in, *out;
	char tmp[MAXLINE];
	char *PtrOldDir;


	/* required args */
	char *songbird, *iTunesLibrary, *newiTunesLibrary, *dirMusics, *newDirMusics;
	if (argc <= 5) {
		printf("%s: Invalid arguments\nUsage: %s <path to songbird executable> <path to original iTunes Library> <name of the output library (the Songbird Library)> <other OS path to songs> <current OS path to songs>\n", argv[0], argv[0]);
		exit(1);
	}

	songbird = argv[1];
	iTunesLibrary = argv[2];
	newiTunesLibrary = argv[3];
	dirMusics = argv[4];
	newDirMusics = argv[5];


	/* open input file */
	if ((in = fopen(iTunesLibrary, "r")) == NULL) {
		fprintf(stderr, "Unable to open iTunes Library\n");
		exit(1);
	}

	/* create output file - if exists, will be overwritten */
	if ((out = fopen(newiTunesLibrary, "w")) == NULL) {
		fprintf(stderr, "Unable to create new Songbird Library\n");
		exit(1);
	}

	int x;

	/* read every line from input file, and place it on output file */
	while (fgets(tmp, MAXLINE, in) != NULL) {
		x=0;
		if ((PtrOldDir = strstr(tmp, dirMusics)) != NULL) { /* if old path was found on current line */
			fputs(PtrOldDir+strlen(PtrOldDir), stdout);
			*PtrOldDir = '\0';
			fputs(tmp, out);

			fputs(newDirMusics, out); /* write new path */

			fputs(PtrOldDir+strlen(dirMusics), out);
		}
		else {
			fputs(tmp, out);

		}
	}

	/* close files */
	fclose(in);
	fclose(out);
	execl(songbird,songbird, NULL);

	return 0;
}
