// autoload.esftp
// Used to upload the FESI home site,
// Distributed as a demonstration program

// FESI Copyright (c) Jean-Marc Lugrin, 2000
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// Set my default ftp server for tests
defaultServer = "worldcom.ch"; 
supportsPWD = false; // My server does not support the PWD command !
rootDir = "E:\\java"; // Where all local directories are rooted

// Utility function to copy documentation files of various type
// The parameter 'doc' is for logging only. The directories must
// be properly setup on client and server first
function copyDoc(doc) {
    with (ftp) {
        logMessageEol("Sending " + doc + " documentation");
        if (supportsPWD) doRemotePrintWorkingDirectory();
        doLocalPrintWorkingDirectory();
        var ldir = localGetWorkingDirectory();
        var lfiles = (new File(ldir)).list();
        for (var i in lfiles) {
            var file = lfiles[i];
            if (file.indexOf(".html") != -1) {
                logMessageEol("Sending '" + file + "' in text mode");
                sendTextFile(file);
            } else if (file.indexOf(".css") != -1) {
                logMessageEol("Sending '" + file + "' in text mode");
                sendTextFile(file);
            } else if (file.indexOf(".gif") != -1) {
                logMessageEol("Sending '" + file + "' in binary mode");
                sendBinaryFile(file);
            } else if (file.indexOf(".jpg") != -1) {
                logMessageEol("Sending '" + file + "' in binary mode");
                sendBinaryFile(file);
            } else if (file.indexOf(".txt") != -1) {
                logMessageEol("Sending '" + file + "' in text mode");
                sendTextFile(file);
            } else if (!(File(ldir,file)).isDirectory()) {
                logMessageEol("** File '" + file + "' of unexpected type not sent");
            }
        } // for
    } // for
}

// Utility routine to setup proper directory
function setupDir() {
      with (ftp) {
        if (!connected) return;

        // We use the exception mode rather than status, as we
        // expect all commands to be successful, or we are in trouble.
        // This avoid testing all the status
        setExceptionMode(true);

        // Assume we are in the fesi target directory
        if (supportsPWD) {  
            var rdir = remoteGetWorkingDirectory();
            var expected_rdir = "/export/home/jmlugrin/public_html/fesi"
            if (rdir!=expected_rdir) {
            logMessageEol("** Remote directory is:\n   " + rdir);
            logMessageEol("   Expected: " + expected_rdir);
            logError("Please relog to correct directory");
            return false;
            }
        }

        // Change to root of FESI area
        localChangeWorkingDirectory(rootDir);
		// Ask user to select the proper subdirectory for binaries
        doLocalChangeWorkingDirectory(); 
	}
}


// Macro to connect to the place where FESI is saved
function connectFesi() {
     with (ftp) {
        setExceptionMode(false); // Good practice to set the expected mode
        defaultServer = "home.worldcom.ch";
        defaultUsername = "jmlugrin"
        defaultPassword = "";
        doConnect();      // To get the password and do the login
        if (!connected) return;
        status = remoteChangeWorkingDirectory("public_html/fesi");
        if (!status) {
            logError(lastError);
            return;
        }
        doRemoteListDirectory();
        "Ready";
     } // with
} // connectFesi

ftp.addMacro("Login to FESI distribution server","connectFesi()");


// Macro to send FESI kit (doc and bin)
function sendFesi() {
    setupDir();
	
	with (ftp) {
		// Upload the binaries
		logMessageEol("Sending fesikit.zip");
		sendBinaryFile("fesikit.zip");
		logMessageEol("Sending fesisrc.zip");
		sendBinaryFile("fesisrc.zip");
	}
	
	// Upload the documentation
	sendFesiDoc();

	"Xfer completed";
}

ftp.addMacro("Send Fesi Kits and Doc","sendFesi()");

// Macro to send FESI kit (bin and src)
function sendFesiBin() {
    setupDir();

	// Upload the binaries
	logMessageEol("Sending fesikit.zip");
	sendBinaryFile("fesikit.zip");
	logMessageEol("Sending fesisrc.zip");
	sendBinaryFile("fesisrc.zip");

	"Xfer completed";
}

ftp.addMacro("Send Fesi Bin and Src","sendFesiBin()");


// Macro to send FESI documentation only 
function sendFesiDoc() {
	setupDir();

	with(ftp) {
		// Upload the documentation
		localChangeWorkingDirectory(rootDir+"\\fesi\\doc\\html");
		copyDoc("fesi");
	
		// Upload the API documentation
		remoteChangeWorkingDirectory("api");
		localChangeWorkingDirectory("api");
		copyDoc("fesi/api");
		remoteChangeWorkingDirectory("FESI/jslib");
		localChangeWorkingDirectory("FESI\\jslib");
		copyDoc("fesi/api/FESI/jslib");
		doRemoteChangeToParentDirectory(); // FESI
		doRemoteChangeToParentDirectory(); // api
	
		// Add the ftp doc in the examples directory
		doRemoteChangeToParentDirectory(); // fesi
		doRemoteChangeToParentDirectory(); // public_html
		remoteChangeWorkingDirectory("examples/fesiftp/doc/html"); 
		localChangeWorkingDirectory(rootDir+"\\fesi\\examples\\fesiftp\\doc\\html");
		copyDoc("fesiftp");

		doRemoteChangeToParentDirectory(); // doc
		doRemoteChangeToParentDirectory(); // fesiftp
		doRemoteChangeToParentDirectory(); // examples

		// Add the ftp doc as a special case
		remoteChangeWorkingDirectory("fesiform/doc/html");
		localChangeWorkingDirectory(rootDir+"\\fesi\\examples\\fesiform\\doc\\html");
		copyDoc("fesiform");
	
		doRemoteChangeToParentDirectory(); // doc
		doRemoteChangeToParentDirectory(); // fesiform
		doRemoteChangeToParentDirectory(); // examples
		doRemoteChangeToParentDirectory(); // public_html
	
		remoteChangeWorkingDirectory("fesi"); // back to root of fesi
	}
	
	"Xfer documentation completed";
}
ftp.addMacro("Send Fesi Doc Only","sendFesiDoc()");

ftp.logMessageEol("Ready");