package io.bitsquare.storage;

import com.google.common.io.Files;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class FileUtil {
    private static final Logger log = LoggerFactory.getLogger(FileUtil.class);

    public static void rollingBackup(File dir, String fileName) {
        File backupDir = new File(Paths.get(dir.getAbsolutePath(), "backup").toString());
        if (!backupDir.exists())
            if (!backupDir.mkdir())
                log.warn("make dir failed");

        File origFile = new File(Paths.get(dir.getAbsolutePath(), fileName).toString());
        if (origFile.exists()) {
            String dirName = "backups_" + fileName;
            if (dirName.contains("."))
                dirName = dirName.replace(".", "_");
            File backupFileDir = new File(Paths.get(backupDir.getAbsolutePath(), dirName).toString());
            if (!backupFileDir.exists())
                if (!backupFileDir.mkdir())
                    log.warn("make backupFileDir failed");

            File backupFile = new File(Paths.get(backupFileDir.getAbsolutePath(), new Date().getTime() + "_" + fileName).toString());

            try {
                Files.copy(origFile, backupFile);

                pruneBackup(backupFileDir);
            } catch (IOException e) {
                log.error("Backup key failed " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    private static void pruneBackup(File backupDir) {
        if (backupDir.isDirectory()) {
            File[] files = backupDir.listFiles();
            if (files != null) {
                List<File> filesList = Arrays.asList(files);
                if (filesList.size() > 10) {
                    filesList.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
                    File file = filesList.get(0);
                    if (file.isFile()) {
                        if (!file.delete())
                            log.error("Failed to delete file: " + file);
                        else
                            pruneBackup(backupDir);
                        
                    } else {
                        pruneBackup(new File(Paths.get(backupDir.getAbsolutePath(), file.getName()).toString()));
                    }
                }
            }
        }
    }

    public static void deleteDirectory(File file) throws IOException {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null)
                for (File c : files)
                    deleteDirectory(c);
        }
        if (file.exists() && !file.delete())
            throw new FileNotFoundException("Failed to delete file: " + file);
    }
}

@@ -470,24 +470,56 @@
 fileName) %7B%0A
+        if (dir.exists()) %7B%0A    
         File
@@ -596,32 +596,36 @@
 ing());%0A        
+    
 if (!backupDir.e
@@ -637,32 +637,36 @@
 ())%0A            
+    
 if (!backupDir.m
@@ -681,32 +681,36 @@
                 
+   
+ 
 log.warn(%22make d
@@ -710,37 +710,84 @@
 %22make dir failed
-%22);%0A%0A
+.%5CnBackupDir=%22 + backupDir.getAbsolutePath());%0A%0A    
         File ori
@@ -863,24 +863,28 @@
 ));%0A        
+    
 if (origFile
@@ -908,16 +908,20 @@
         
+ 
+   
 String d
@@ -960,24 +960,28 @@
             
+    
 if (dirName.
@@ -1011,16 +1011,20 @@
         
+   
+ 
 dirName 
@@ -1052,16 +1052,20 @@
 , %22_%22);%0A
+    
         
@@ -1163,32 +1163,36 @@
 ));%0A            
+    
 if (!backupFileD
@@ -1212,32 +1212,36 @@
                 
+  
+  
 if (!backupFileD
@@ -1244,32 +1244,36 @@
 ileDir.mkdir())%0A
+    
                 
@@ -1312,24 +1312,28 @@
  failed%22);%0A%0A
+    
             
@@ -1471,14 +1471,22 @@
     
+    
 try %7B%0A
+    
     
@@ -1540,32 +1540,36 @@
                 
+   
+ 
 pruneBackup(back
@@ -1576,24 +1576,28 @@
 upFileDir);%0A
+    
             
@@ -1630,32 +1630,36 @@
                 
+    
 log.error(%22Backu
@@ -1708,16 +1708,20 @@
         
+ 
+   
 e.printS
@@ -1729,24 +1729,42 @@
 ackTrace();%0A
+                %7D%0A
             
@@ -2443,32 +2443,8 @@
 r);%0A
-                        
 %0A   

check id parent dir exists for backup