import ftp.*;
import java.io.*;
/**
 * This is a example of using FtpBean.
 * It connects to a ftp server. Go to a directory.
 * Then list its content with help of the FtpListResult class.
 * Finally, it gets a binary file. In the downloading
 * progress, it tells how many bytes are being downloaded.
 *
 * Note that this class implements the FtpObserver interface, which
 * make this class have the ability to monitor the downloading or 
 * uploading progress. If you don't need to monitor it, then you
 * don't need to implement this interface.
 *
 * For using more function of this FtpBean, please see the documentation.
 */
class FtpExampleApplet extends Applet implements FtpObserver
{
    FtpBean ftp;
    long num_of_bytes = 0;

    public void FtpExample()
    {
        // Create a new FtpBean object.
        ftp = new FtpBean();
    }

    // Connect to a ftp server.
    public void connect(String host, String username, String password)
    {
        try
        {
            ftp.ftpConnect(host,username,password);
        } catch(Exception e)
        {
            System.out.println(e);
        }
    }

    // Close connection
    public void close()
    {
        try
        {
            ftp.close();
        } catch(Exception e)
        {
            System.out.println(e);
        }
    }

    // Go to directory pub and list its content.
    public void listDirectory(String directory)
    {
        FtpListResult ftplrs = null;

        try
        {
            // Go to directory 'pub/redhat/redhat-6.2/i386/RedHat/RPMS'.
            ftp.setDirectory(directory);
            // Get its directory content.
            ftplrs = ftp.getDirectoryContent();
        } catch(Exception e)
        {
            System.out.println(e);
        }

        // Print out the type and file name of each row.
        while(ftplrs.next())
        {
            int type = ftplrs.getType();
            if(type == FtpListResult.DIRECTORY)
                System.out.print("DIR\t");
            else if(type == FtpListResult.FILE)
                System.out.print("FILE\t");
            else if(type == FtpListResult.LINK)
                System.out.print("LINK\t");
            else if(type == FtpListResult.OTHERS)
                System.out.print("OTHER\t");
            System.out.println(ftplrs.getName());
        }
    }

    // Get the file.
    public void getFile()
    {
        try
        {
            // Get the binary file 'kernel-2.2.14-5.0.i386.rpm' and save it to
            // the name 'local_file_name' in the hard disk.
            // Passing this class which implements the FtpObserver interface to 
            // monitor this downloading progress. Every time new bytes are read,
            // the byteRead(int) method of this class is invoked by the bean.
            ftp.getBinaryFile("/tmp/photos/0.jpg", "local_file_name", this);
        } catch(Exception e)
        {
            System.out.println(e);
        }
    }
		
		
  
		public void putAllFiles(String uploadFromDirectory, String prefix, String suffix) {
		
		FilenameFilter only = new OnlyExt(prefix,suffix);
		File file = new File(uploadFromDirectory);
		String listing[] = file.list(only);
		
		for ( int i=0 ; i < listing.length ; i++ ) {
			System.out.println("Uploading: " + uploadFromDirectory + "/" + listing[i]);
			putFile(uploadFromDirectory + "/" + listing[i],"/tmp/photos/" + i + ".jpg");
			}
		
		
		}
	
		public void putFile(String localFile, String remoteFile)
    {
        try
        {
            // Get the binary file 'kernel-2.2.14-5.0.i386.rpm' and save it to
            // the name 'local_file_name' in the hard disk.
            // Passing this class which implements the FtpObserver interface to 
            // monitor this downloading progress. Every time new bytes are read,
            // the byteRead(int) method of this class is invoked by the bean.
            ftp.putBinaryFile(localFile, remoteFile, this);
        } catch(Exception e)
        {
            System.out.println(e);
        }
    }

    // Implemented for FtpObserver interface.
    // To monitor download progress.
    public void byteRead(int bytes)
    {
        num_of_bytes += bytes;
        System.out.println(num_of_bytes + " bytes read.");
    }

    // Needed to implements by FtpObserver interface.
    public void byteWrite(int bytes)
    {
    num_of_bytes += bytes;
    System.out.println(num_of_bytes + " bytes written.");
		}

    // Main
    public static void main(String[] args) {
			String hostname ="";
			String username ="";
			String password ="";
			String directory ="";
			String uploadFromDirectory ="";
			
			System.out.println("Arguments given: " + args.length);
			if ( args.length != 5 ) {
				System.out.println("usage: hostname username password destinationDirectory sourceDirectory");
				System.exit(1);
				} else { 
				hostname = args[0];
				username = args[1];
				password = args[2];
				directory = args[3];
				uploadFromDirectory = args[4];
				}
		

		
		  FtpExample ftp = new FtpExample();
      
			ftp.connect(hostname,username,password);
      ftp.listDirectory(directory);
			ftp.putAllFiles(uploadFromDirectory,"MVC","JPG");
			ftp.listDirectory(directory);
   		//ftp.getFile();
      ftp.close();
			System.out.println("FTP Connection Closed");
			
			System.exit(0);
    }
}
