
import java.io.*;
import java.net.*;
import javax.swing.*;

public class PutFiles extends Command
{
	PutDialog view;
	protected DataOutputStream outStream;
	DirTreeNode CDRootNode;
	int sessionSize;
	int bytesTransmitted;

	public PutFiles(DirTreeNode CDRootNode, int sessionSize)
	{
		view = new PutDialog();
		outStream = CDcreator.outStream;
		this.CDRootNode = CDRootNode;
		this.sessionSize = sessionSize;
		filesNum = 0;
		bytesTransmitted = 0;
	}

	protected void putFile(String dirName, DirTreeNode node)
		throws java.io.IOException, CommandError
	{
		File file = node.getFile();
		String fileName = dirName + "/" + node.toString();
		fileName = spaceToEsc(fileName);
		Version.showDebugInfo(1, fileName + " " + file);
		view.setMessage(fileName);

		FileInputStream inFile;
		try
		{
			inFile = new FileInputStream(file);
		}
		catch (java.io.IOException e)
		{
			System.out.println("can't read " + file);
			return;
		}

//		int mode = getMode(fileName);
//		System.out.println(fileName + ": mode = " + mode);

		long bytesToWrite = file.length();
		outStream.writeBytes("putFile "
			+ fileName + " "
			+ bytesToWrite + " "
			+ "444 "
			+ file.lastModified() / 1000
			+ "\n");

		while (bytesToWrite > 0)
		{
			byte bs[] = new byte[16*1024];
			int len = inFile.read(bs);
			if (len == -1)
				break;
			if (len > bytesToWrite)
				len = (int)bytesToWrite;
			outStream.write(bs, 0, len);
			bytesToWrite -= len;
			bytesTransmitted += len;
			if (len % 1024 > 0)
				bytesTransmitted += 1024 - len % 1024;
			view.setProgress(bytesTransmitted);
//			System.out.println("<"
//				+ new String(bs, 0, len) + ">");
		}

		while (bytesToWrite-- > 0)
			outStream.write(0);

		readResult();
		if (!lastResultOK())
			throw new CommandError();

		if (node == MainWin.bootImageNode)
			Options.bootImage = fileName;
	}

	void putTree(String dirName, DirTreeNode node)
		throws java.io.IOException, CommandError
	{
		String dirName2;
		if (dirName.equals("/"))
			dirName2 = "";
		else
			dirName2 = dirName + '/' + node.toString();

		for (int i = 0; i < node.getChildCount(); i++)
		{
			if (cancel)
				return;
			DirTreeNode child =
				(DirTreeNode)node.getChildAt(i);
			if (child.isLeaf())
				putFile(dirName2, child);
			else
				putTree(dirName2, child);
		}

		File dir = node.getFile();
		if (dir != null && dirName2.length() != 0)
		{
			execCommand("mkdir "
				+ spaceToEsc(dirName2) + " "
				+ "444 "
				+ dir.lastModified() / 1000);
		}
	}

	public void run()
	{
//		System.out.println("PutFiles");
		view.setTitle(i18n("titlePutFiles"));
		view.setRange(0, sessionSize);
		showDialog(view);

		// for netscape
		if (CDcreator.isNetscapeVM())
		try {
			netscape.security.PrivilegeManager
				.enablePrivilege("UniversalFileRead");
		}
		catch (Exception e) {
		}

		boolean error = false;
		try
		{
			putTree("/", CDRootNode);
		}
		catch (java.io.IOException e)
		{
			System.out.println("PutFiles: IOException");
		}
		catch (CommandError cmdErr)
		{
			error = true;
		}

		hideDialog();
		if (error)
			showInfoWin(new JOptionPane(
				lastResult,
				JOptionPane.INFORMATION_MESSAGE,
				JOptionPane.DEFAULT_OPTION,
				null, okButton).createDialog(
				CDcreator.mainWin, i18n("error")));

		super.run();
//		System.out.println("PutFiles end");
	}
}
