/* 
iTunes Folder-Sync
By: Gareth Farrington
From: http://www.waves.ky/iTunes/iTunesFolderSync.js
Version: 0.2
License: This is Freeware. No Warrantees are given or implied.

	* Find new tracks in the folders where you store your music (e.g. new rips, downloads, podcasts)
	* Make it easy to store your music any way you like
	* remove tracks from iTunes Library that are no longer on disk (e.g. you deleted or renamed a track)
	* update your iPod when everything is done
	
	Note on Converting Songs prompt:
	During the sync you you may be asked to convert tracks in WMA format to something iTunes can play. If you have 
	already converted tracks duplicates will be created! I suggest just pressing skip to avoid this.

0.2 - 3/Oct/2005
	+ Support for specifying your own music folders
	+ Ignores sub folders that fall under user defined music folders
	+ Batch import, shows progress bar correctly
	+ Runs Faster
	
0.1 - original version
	+ Discover new songs to import
	+ Sync iPod when finished

Credits:
	Testing:  Howard Darwen, Jay McNeely
*/ 

var musicFolders = new Array;

/****** Custom Music Folder Support ******
 * Uncomment the line below and list the folders where you store your music. Here is an example:
 *
 * musicFolders = ["C:\\Music\\Albums", "D:\\Podcasts"];
 *
 * Follow the format carefully, each directory seperator needs to be a double slash "\\".
 * Seperate each entry with a comma.
 */

//musicFolders = ["C:\\Music\\Albums", "D:\\Podcasts"];


var ITTrackKindFile	= 1;
var iTunesApp = WScript.CreateObject("iTunes.Application");
var mainLibrary = iTunesApp.LibraryPlaylist;
var tracks = mainLibrary.Tracks;
var numTracks = tracks.Count;
var deleted = 0;
var paths = new Array;
var path;

// run through all the tracks, collecting directory information & removing those that are "broken"
while (numTracks !== 0)
{
	var currTrack = tracks.Item(numTracks);
	
	// is this a file/CD track?
	if (currTrack.Kind == ITTrackKindFile)
	{
		path = currTrack.Location;
		// dead track removal
		if(path === "") {
			currTrack.Delete();
			++deleted;
		}
		// record valid path for import
		else {
			var isSubPath = false;
			
			// trim the path at last separator
			path = path.substring(0, path.lastIndexOf("\\"));
						
			// sub path detection:
			for (var source in musicFolders) {
				if(path.indexOf(musicFolders[source]) != -1) {
					isSubPath = true;
					break;
				}
			}
			
			if(!isSubPath) {
				paths[path] = path;
			}	
		}
	}
	--numTracks;
}

// batch all import paths into a single array
var imports = new Array;
for (path in musicFolders)
	imports.push(musicFolders[path]);
for (path in paths)
	imports.push(paths[path]);
	
// DEBUG: see what paths are being imported
//WScript.Echo("Importing: \n'" + imports.join("',\n'") + "'");

// import files as a batch
numTracks = tracks.Count;	// library size before adding tracks
var result = mainLibrary.AddFiles(imports);

// initiate an iPod sync if one is connected (runs in the background)
iTunesApp.UpdateIPod();

// Show results, 
WScript.Echo("iTunes Folder Sync (0.2) Finished:\n\nTracks Removed: " + deleted + "\nTracks Found: " + (tracks.Count - numTracks) + "\n\n (c) Gareth Farrington, http://www.waves.ky");
