/*
 S2's bookmark sync modified by mawcs

 this is a very small and easy jscript file for windows.
 it allows you to download your bookmarks from an ftp
 server for use with mozilla (or any other browser for
 that matter), and reups them on the server when you
 close your mozilla session.

 all you have to do is to edit the variables below, and
 launch this file instead of mozilla.exe directly.
 
 be sure to use double backslash "\\"
 
 make sure paths end in slashes "/" or "\\"

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 CHANGES: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 added no ftp window functionality
 
 added functionality to download/upload user.js and 
 cookperm.txt
 
 added functionality to download without launching 
 Mozilla.
 
 added local backup functionality.
 
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 TODO: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 error checking for paths not ending in '/' or '\\'.
 
 error checking for files not existing on server.
 
 error checking for files not existing locally.
  
 add command line args for backup, user.js and bookperm.txt
 
 add ability to wait for the Mozilla quick launch instead
 of just the main browser.
   
 add ability to prompt user for password.
 
 add ability to prompt user for parameters, if not found.
 
 write prompted parameters to props file for future
 use and load parameters from properties file.
 
 add support for other profile files.

*/


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Set parameters below ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var ftpserver="ftp.server";//Your FTP server
var ftpuser="username";				//Your FTP username
var ftppass="password";					//Your FTP password

//The directory where Mozilla lives
var mozpath="C:\\Program Files\\mozilla.org\\Mozilla\\";
//The name of the Mozilla executable (good for Netscape or Firebird?)
var mozexec="mozilla.exe";

//The directory on the FTP server to store the bookmarks file.
var ftpbookdir="/maw/";		

//The name of the bookmarks file on the FTP server.
var ftpbookfile="bookmarks.html";	
//The name of the user prefs file on the FTP server.
var ftpuserfile="user.js";	
//The name of the cookie file on the FTP server.
//var ftpcookfile="cookies.txt";
//The name of the banned cookie sites file on the FTP server.
var ftpcookbanfile="cookperm.txt";	
//The name of the history file on the FTP server.
//var ftphistfile="history.dat";

//The directory on the local machine where the bookmarks file is.
var localbookdir="C:\\Documents and Settings\\username\\Application Data\\Mozilla\\Profiles\\default\\k5sxfmfc.slt\\";

//The name of the bookmarks file on the local machine.
var localbookfile="bookmarks.html";
//The name of the user preferences file on the local machine.
var localuserfile="user.js";
//The name of the cookie file on the local machine.
//var localcookfile="cookies.txt";
//The name of the banned cookie sites file on the local machine.
var localcookbanfile="cookperm.txt";
//The name of the history file on the local machine.
//var localhistfile="history.dat";

//Set to true if you want backups made of your local bookmarks file.
var dobkup = false;
//The extension to use for backups.
var bkext = ".bak";

//Set to true if you want to upload/download user prefs
var douser = true;
//Set to true if you want to upload/download cookies and ban list
var docook = true;
//Set to true if you want to upload/download history file
var dohist = true;

//Set parameters above ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var objArgs = WScript.Arguments
var shell = WScript.CreateObject("WScript.Shell");
var fso = WScript.CreateObject("Scripting.FileSystemObject");

var env = shell.Environment("USER");
var tfile = env("TEMP") + "\\ftp.tmp";
var tempfile; //this done later = fso.createTextFile(tfile);

var ftpcommand = "ftp -s:" + tfile;

var state = 0 //0=launch, 1=nolaunch, 2=uponly

if (objArgs[0] =="uponly")
{
	 state=2;
}
else if (objArgs[0] =="launch")
{
	 state=0;
}
else if (objArgs[0] =="nolaunch")
{
	 state=1;
}

if (state < 2)
{
	getall();
	if (state == 0)
	{
		launchMoz();
		putall();
	}
		
}
else if (state == 2)
{
	putall();
}
else
{
	//print error message
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Functions below ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function getall()
{
	//backup the bookmark file
	dobackup();
	
	createTempFile();
	
	//get the bookmark file
	get(ftpbookdir + ftpbookfile, localbookdir + localbookfile);
	if (douser)
	{
		//get the user prefs
		get(ftpbookdir + ftpuserfile, localbookdir + localuserfile);
	}
	if (docook)
	{
		//get the blocked cookies file
		get(ftpbookdir + ftpcookbanfile, localbookdir + localcookbanfile);
		//get the cookies file
	//	get(ftpbookdir + ftpcookfile, localbookdir + localcookfile);
	}
	if (dohist)
	{
		//get the history file
	//	get(ftpbookdir + ftphistfile, localbookdir + localhistfile);
	}
	openftp();
}

function putall()
{
	createTempFile();
	
	//put the bookmark file
	put(ftpbookdir + ftpbookfile, localbookdir + localbookfile);
	if (douser)
	{
		//put the user prefs
		put(ftpbookdir + ftpuserfile, localbookdir + localuserfile);
	}
	if (docook)
	
		//put the blocked cookies file
		put(ftpbookdir + ftpcookbanfile, localbookdir + localcookbanfile);
		//put the cookies file
	//	put(ftpbookdir + ftpcookfile, localbookdir + localcookfile);
	}
	if (dohist)
	{
		//put the history file
	//	put(ftpbookdir + ftphistfile, localbookdir + localhistfile);
	}
	openftp();
}

function launchMoz()
{
	_executable = '"' + mozpath + mozexec + '"';
	
	moz = shell.exec(_executable);
	while (moz.Status == 0)
	{
		WScript.sleep(1000);//sleeps waiting for mozilla to close.
	}
}

function createTempFile()
{
	tempfile = fso.createTextFile(tfile);
	send("open " + ftpserver);
	send(ftpuser);
	send(ftppass);
}

function destroyTempFile()
{
	fso.DeleteFile(tfile, true);
}

function get(ftpfile, localfile)
{
	send("get " + ftpfile + " \"" + localfile + "\"");
}

function put(ftpfile, localfile)
{
	send("put \"" + localfile + "\" " + ftpfile);
}

function openftp()
{
	quit();
	tempfile.Close();
	//shell.run(ftpcommand, 1, true);//runs in new window.  uncomment if you want to see the ftp process.
	shell.run(ftpcommand, 0, true);
	destroyTempFile();
}

function quit()
{
	send("quit");
}

function send(command)
{
	tempfile.writeLine(command);
}

function dobackup()
{
	if (dobkup)
	{
		backup(localbookdir, localbookfile);
		if (douser)
		{
			backup(localbookdir, localuserfile);
		}
		if (docook)
		{
			backup(localbookdir, localcookfile);
		}
	}
}

function backup(dir, file)
{
	datetoday = new Date();
		
	_masterfile = dir + file;
	_bkupfile = dir + file + "." + datetoday.getTime() + bkext;
	if (fso.FileExists(_masterfile))
	{
		fso.CopyFile(_masterfile, _bkupfile);
	}
}


//Functions above ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~