/*
 * Copyright (c) 2014 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.oomph.setup.util;

import org.eclipse.oomph.internal.setup.SetupPlugin;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Eike Stepper
 */
public final class FileUtil
{
  public static File rename(File from) throws IOException, InterruptedException
  {
    File to = new File(from.getParentFile(), from.getName() + "." + System.currentTimeMillis());
    rename(from, to);
    return to;
  }

  public static void rename(File from, File to) throws IOException, InterruptedException
  {
    if (from.exists())
    {
      for (int i = 0; i < 1000; i++)
      {
        if (from.renameTo(to))
        {
          return;
        }

        Thread.sleep(5);
      }
    }

    throw new IOException("Could not rename '" + from.getAbsolutePath() + "' to '" + to.getAbsolutePath() + "'");
  }

  public static void deleteAsync(final File file) throws IOException, InterruptedException
  {
    new Job("Deleting old files")
    {
      @Override
      protected IStatus run(IProgressMonitor monitor)
      {
        try
        {
          delete(file, monitor, false);
          return Status.OK_STATUS;
        }
        catch (Exception ex)
        {
          SetupPlugin.INSTANCE.log(ex);
          return SetupPlugin.INSTANCE.getStatus(ex);
        }
      }
    }.schedule();
  }

  public static void delete(File file, IProgressMonitor monitor) throws IOException, InterruptedException
  {
    delete(file, monitor, true);
  }

  private static void delete(File file, IProgressMonitor monitor, boolean verbose) throws IOException, InterruptedException
  {
    List<File> files = listAllFiles(file);
    if (files.isEmpty())
    {
      return;
    }

    if (verbose)
    {
      monitor.beginTask("Deleting files in " + file.getAbsolutePath(), files.size());
    }

    try
    {
      Collections.reverse(files);
      for (File child : files)
      {
        String childPath = child.getAbsolutePath();

        if (verbose)
        {
          monitor.setTaskName("Deleting file " + childPath);
        }

        doDelete(child);

        monitor.worked(1);
        if (monitor.isCanceled())
        {
          throw new OperationCanceledException();
        }
      }
    }
    finally
    {
      monitor.done();
    }
  }

  private static void doDelete(File file) throws IOException, InterruptedException
  {
    for (int i = 0; i < 1000; i++)
    {
      if (file.delete())
      {
        return;
      }

      Thread.sleep(5);
    }

    throw new IOException("Could not delete '" + file.getAbsolutePath() + "'");
  }

  private static List<File> listAllFiles(File file)
  {
    List<File> result = new ArrayList<File>();
    if (file != null && file.exists())
    {
      listAllFiles(file, result);
    }

    return result;
  }

  private static void listAllFiles(File file, List<File> result)
  {
    result.add(file);
    if (file.isDirectory())
    {
      for (File child : file.listFiles())
      {
        listAllFiles(child, result);
      }
    }
  }
}

@@ -2343,26 +2343,49 @@
    i
-f (files.isEmpty()
+nt size = files.size();%0A    if (size == 0
 )%0A  
@@ -2442,26 +2442,97 @@
     
-monitor.beginTask(
+String message = %22%22;%0A      if (file.isDirectory() && size %3E 1)%0A      %7B%0A        message = 
 %22Del
@@ -2576,22 +2576,56 @@
 th()
-, files.
+;%0A      %7D%0A%0A      monitor.beginTask(message, 
 size
-()
 );%0A 
@@ -2709,32 +2709,65 @@
  files)%0A      %7B%0A
+        if (verbose)%0A        %7B%0A  
         String c
@@ -2805,40 +2805,8 @@
 h();
-%0A%0A        if (verbose)%0A        %7B
 %0A   
@@ -3059,24 +3059,53 @@
 nally%0A    %7B%0A
+      if (verbose)%0A      %7B%0A  
       monito
@@ -3110,24 +3110,32 @@
 tor.done();%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A 

Make deletion of single files less verbose