// UpdateDir.java
//
// You may use and distribute this example. This program is 
// provided WITHOUT WARRANTY either expressed or implied.
//
// Copyright Henrik Björkman 1998
//
// Description: See usage below.
//
// History:
// 0.0 Created by Henrik Björkman 1998-06-09
//


import java.io.*;



public class UpdateDir
{
  static String copyCommand;


  static void error(String str)
  {
    System.err.println("UpdateDir: " + str);
  }

  static void debug(String str)
  {
    //error(str);
  }

  static void print(String str)
  {
    System.out.println(str);
  }

  static void show_file(String root1, String root2, String name, int depth)
  {
    File f1=new File(root1+name);
    File f2=new File(root2+name);        

    if (f1.isDirectory())
    { 
      if (!f2.exists())
      {
        f2.mkdirs();
      }

      int i;
      String[] n;
      //print(name+File.separator);
      n=f1.list();
      for (i=0;i<n.length;i++)
      {
        show_file(root1,root2,name+File.separator+n[i],depth+1);
      }
    }
    else if (f1.isFile())
    {
      /* kolla om f2 inte finns eller är äldre */
      if ((!f2.exists()) || (!f2.isFile()) || (f2.lastModified()<f1.lastModified()))
      {
        print(f1.getAbsolutePath()+" "+f2.getAbsolutePath());

        try 
        { 
          String cmd=copyCommand+" \""+f1.getAbsolutePath()+"\" \""+f2.getAbsolutePath()+"\"";

          Process pid=Runtime.getRuntime().exec(cmd);

          /*InputStream in = new DataInputStream(pid.getInputStream());
          InputStream in = new DataInputStream(pid.getErrorStream());
          while (true) 
          {
            int ch=1;
            ch = in.read();
            if (ch<0) break;
            System.out.print((char)ch);
          }*/

          pid.waitFor();
          pid.destroy();

        }
        catch(IOException e) 
        {
          error(""+e);
        } 
        catch(InterruptedException e)
        {
          error(""+e);
        } 
      }
    }
    else
    {
      print(name+" ?");
    }
  }

  public static void main(String args[]) 
  {
    debug("begin");
    //print("total memory: "+Runtime.getRuntime().totalMemory());
    //print("free memory: "+Runtime.getRuntime().freeMemory());


    if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS")>=0) 
    {
      print("dos");
      copyCommand="c:\\temp\\test.bat";
    }
    else
    {
      print("unix");
      copyCommand="cp";
    }


    if (args.length < 2 )
    {
      print("");
      print("Usage: java UpdateDir <dir1> <dir2>");
      print("");
      print("Copy files in <dir1> that don't exist or are older in <dir2>");
      print("");
      System.exit(0);
    }

    show_file(args[0],args[1],"",0);

/*  try 
    { 
      Process pid=Runtime.getRuntime().exec("c:\\temp\\copy.bat");
      InputStream in = new DataInputStream(pid.getInputStream());
      while (true) 
      {
        int ch=1;
        ch = in.read();
        if (ch<0) break;
        System.out.print((char)ch);
      }

      File.remove("c:\\temp\\copy.bat");
    }
    catch(IOException e) 
    {
      error("IOException " + e);
    } */


    debug("end");
  }
}


