import java.io.*;
import java.lang.reflect.*;

public class Backup {
  public static void main(String[] args) {
    if(Array.getLength(args)>1)
      new Backup(args[0],args[1]);

  }

  public Backup(String src, String dst) {
    try {
      myProc=Runtime.getRuntime().exec("command.com");
      myIn=myProc.getInputStream();
      myOut=new PrintWriter(myProc.getOutputStream());
    }
    catch(Exception e) {
      System.out.println(e);
    }

    boolean done=false;
    File f1=new File(src);
    File f2=new File(dst);

    while(!done) {
System.out.println("Attempting backup of "+src+" to "+dst);
      if(f1.isDirectory()) {
        if(!f2.exists()) {
          f2.mkdirs();
        }
        if(f2.exists()) {
          dir(f1,f2);
          done=true;
        }
        else  {
          try {
            Thread.sleep(100000);
          }
          catch(Exception e) {
            System.out.println(e);
          }
        }
      }
      else
        done=true;    // finish this later
    }

    try {
      int c;
      myOut.println("exit");
      myOut.flush();
      myOut.close();

System.out.println("final read");
      done=false;      
      boolean first=true;
      while (!done) {
        if(myIn.available()>0) {
          c = myIn.read();
          System.out.print((char)c);
          first=true;
        }
        else {
          if(first) {
            Thread.sleep(3000);
            first=false;
          }
          else 
            done=true;
        }
      }    
      myIn.close();
System.out.println("wait");
      myProc.waitFor();
System.out.println("done");
      myProc.destroy();
    }
    catch(Exception e) {
      System.out.println(e);
    }
  }

  private void dir(File f, File g) {
    File aFile,bFile;
    String [] fList=f.list();
    int length=Array.getLength(fList);

 //   System.out.println("backing up "+f);
    for(int i=0;i<length;i++) {
      aFile=new File(f,fList[i]);
      bFile=new File(g,fList[i]);
      if(aFile.isDirectory()) {
        bFile.mkdir();
        dir(aFile,bFile);   
      }
      else { 
        file(aFile,bFile);
      }
    }
  }

  private void file(File f, File g) {
    if(g.exists() && f.exists()) {
      if(f.lastModified()>g.lastModified()) {
        copy(f,g);
      }
      else if(f.lastModified()<g.lastModified()) {
        System.out.println("Destination newer:"+g);
      }
      else if(f.length()!=g.length()) {
        copy(f,g);
      }
    }
    else if(f.exists()) {
      copy(f,g);
    }
  }

  private void copy(File f,File g) {
 //   System.out.println(" "+f.getName());
    myOut.println("copy /y \""+f.getPath()+"\" \""+g.getPath()+"\"");
    myOut.flush();
 
    try {
      int c;
      Thread.sleep(500);
      while (myIn.available()>0 && (c = myIn.read()) != -1) {
        System.out.print((char)c);
      }
    }
    catch(Exception e) {
      System.out.println(e);
    }
  }

  private void copyQ(File f,File g) {
    try {
      FileInputStream fIn=new FileInputStream(f);
      FileOutputStream fOut=new FileOutputStream(g);
      byte []buf=new byte[4096];
      int len;   

      while((len=fIn.read(buf))>0) {
        fOut.write(buf,0,len);
      }

      fOut.close();
      fIn.close();
    }
    catch(Exception e) {
      System.out.println(e);
    }
  }

  Process myProc;
  InputStream myIn;
  PrintWriter myOut;
}