package org.datavault.worker.operations;

import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import gov.loc.repository.bagit.*;

public class Packager {
    
    // Create a bag from an existing directory.
    public static boolean createBag(File dir) throws Exception {

        BagFactory bagFactory = new BagFactory();
        PreBag preBag = bagFactory.createPreBag(dir);
        Bag bag = preBag.makeBagInPlace(BagFactory.LATEST, false);
        
        boolean result = false;
        try {
            result = bag.verifyValid().isSuccess();
        } finally {
            bag.close();
        }
        
        return result;
    }
    
    // Extract the top-level metadata files from a bag and copy to a new directory.
    public static boolean extractMetaFiles(File bagDir, File metaDir) {
        
        // TODO: could we use the built-in "holey" bag methods instead?
        
        boolean result;
        Path bagPath = bagDir.toPath();
        
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(bagPath)) {
            for (Path entry : stream) {
                
                /*
                Expected:
                - data (dir)
                - bag-info.txt
                - bagit.txt
                - manifest-md5.txt
                - tagmanifest-md5.txt
                */
                
                String entryFileName = entry.getFileName().toString();
                
                // Copy any regular text files at the top level (but not directories)
                if (!Files.isDirectory(entry) && entryFileName.endsWith(".txt")) {
                    FileUtils.copyFileToDirectory(entry.toFile(), metaDir);
                }
            }
            
            // All files copied
            result = true;
            
        } catch (IOException e) {
            System.out.println(e.toString());
            result = false;
        }
        
        return result;
    }
}

@@ -177,16 +177,44 @@
 e.Path;%0A
+import java.nio.file.Paths;%0A
 import o
@@ -1455,32 +1455,86 @@
 anifest-md5.txt%0A
+                - other metadata files or directories%0A
                 
@@ -1620,24 +1620,25 @@
 toString();%0A
+%0A
             
@@ -1645,177 +1645,641 @@
     
-%0A                // Copy any regular text files at the top level (but not directories)%0A                if (!Files.isDirectory(entry) && 
+if (Files.isDirectory(entry)) %7B%0A                    // Handle directories%0A                    if (entryFileName.equals(%22data%22)) %7B%0A                        // Create an empty %22data%22 directory%0A                        Path metaDirPath = Paths.get(metaDir.toURI());%0A                        File emptyDataDir = metaDirPath.resolve(%22data%22).toFile();%0A                        emptyDataDir.mkdir();%0A                    %7D else %7B%0A                        FileUtils.copyDirectoryToDirectory(
 entry
+.to
 File
-Name.endsWith(%22.txt%22)) %7B
+(), metaDir);%0A                    %7D%0A                %0A                %7D else if (!Files.isDirectory(entry)) %7B%0A                    // Handle files
 %0A   

Copy metadata directories