/*
 * Autopsy Forensic Browser
 *
 * Copyright 2012 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.coreutils;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import org.openide.filesystems.FileObject;

/**
 * File and dir utilities
 */
public class FileUtil {
    private static final Logger logger = Logger.getLogger(FileUtil.class.getName());

    /**
     * Recursively delete a dir
     *
     * @param dirPath dir path of a dir to delete
     * @return true if the dir deleted, false otherwise
     */
    public static boolean deleteDir(File dirPath) {
        if (dirPath.exists()) {
            File[] files = dirPath.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDir(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (dirPath.delete());

    }
    
    /**
     * Check if given path is a file or directory, then delete it through
     * recursion with path leading to a file as the base case.
     * 
     * @param path  the path to the file or directory to delete
     * @return  true if the File at path is deleted, false otherwise
     */
    public static boolean deleteFileDir(File path) {
            boolean sucess = true;
            if(path.isFile()) { // If it's a file
                if(!path.delete()) {
                    sucess = false;
                    logger.log(Level.WARNING, "Failed to delete file {0}", path.getPath());
                }
            } else { // If it's a directory
                if(path.list().length==0) { // If the dir is empty
                    if(!path.delete()) {
                        sucess = false;
                        logger.log(Level.WARNING, "Failed to delete the empty directory at {0}", path.getPath());
                    }
                } else {
                    String files[] = path.list();
                    for(String s:files) {
                        File sub = new File(path, s);
                        sucess = deleteFileDir(sub);
                    }
                    if(path.list().length==0) { // Delete the newly-empty dir
                        if(!path.delete()) {
                            sucess = false;
                            logger.log(Level.WARNING, "Failed to delete the empty directory at {0}", path.getPath());
                        }
                    } else {
                        sucess = false;
                        logger.log(Level.WARNING, "Directory {0} did not recursivly delete sucessfully.", path.getPath());
                    }
                }
            }
            return sucess;
        }
    
    
    /**
     * 
     * source - source file object
     * destFolder - destination folder
     * newName - file name (without extension) of destination file
     * newExt - extension of destination file 
     * @return path to the created file
     */
        public static String copyFile (String source, String destFolder, String newName, String newExtension) 
                throws IOException {
            
            FileObject sourceFileObj = org.openide.filesystems.FileUtil.createData(new File(source));
            FileObject destFolderObj = org.openide.filesystems.FileUtil.createData(new File(destFolder));
            
            FileObject created = org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, newName, newExtension);
            
            return created.getPath();
            
        }
}

@@ -3533,47 +3533,78 @@
   * 
-newExt - extension of destination file 
+overwrite - if new file already exists, overwrite it (delete it first)
 %0A   
@@ -3741,27 +3741,25 @@
 me, 
-String newExtension
+boolean overwrite
 ) %0A 
@@ -3795,38 +3795,36 @@
 tion %7B%0A         
+%0A
    
-%0A  
           FileOb
@@ -3808,32 +3808,331 @@
    %0A            
+if (overwrite) %7B%0A               final String destFileName = destFolder + File.separator + newName;%0A               final File destFile = new File(destFileName);%0A               if (destFile.exists()) %7B%0A                   destFile.delete();%0A               %7D%0A            %7D%0A            %0A            final
  FileObject sour
@@ -4210,32 +4210,38 @@
 ));%0A            
+final 
 FileObject destF
@@ -4447,22 +4447,8 @@
 Name
-, newExtension
 );%0A 

improve copy method