/*
 * Autopsy Forensic Browser
 *
 * Copyright 2020 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.commandlineingest;

import java.io.File;
import java.nio.file.Paths;
import java.util.logging.Level;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.casemodule.CaseActionException;
import static org.sleuthkit.autopsy.casemodule.CaseMetadata.getFileExtension;
import org.sleuthkit.autopsy.coreutils.Logger;

/**
 * Base class for the command line managers.
 */
class CommandLineManager {

    private static final Logger LOGGER = Logger.getLogger(CommandLineOpenCaseManager.class.getName());

    /**
     * Opens existing case.
     *
     * @param caseFolderPath full path to case directory
     *
     * @throws CaseActionException
     */
    Case openCase(String caseFolderPath) throws CaseActionException {

        LOGGER.log(Level.INFO, "Opening case in directory {0}", caseFolderPath);

        String metadataFilePath = findAutFile(caseFolderPath);
        Case.openAsCurrentCase(metadataFilePath);

        Case newCase = Case.getCurrentCase();
        LOGGER.log(Level.INFO, "Opened case {0}", newCase.getName());

        return newCase;
    }

    /**
     * Finds the path to the .aut file for the specified case directory.
     *
     * @param caseDirectory the directory to check for a .aut file
     *
     * @return the path to the first .aut file found in the directory
     *
     * @throws CaseActionException if there was an issue finding a .aut file
     */
    private String findAutFile(String caseDirectory) throws CaseActionException {
        File caseFolder = Paths.get(caseDirectory).toFile();
        if (caseFolder.exists()) {
            /*
             * Search for '*.aut' files.
             */
            File[] fileArray = caseFolder.listFiles();
            if (fileArray == null) {
                throw new CaseActionException("No files found in case directory");
            }
            String autFilePath = null;
            for (File file : fileArray) {
                String name = file.getName().toLowerCase();
                if (autFilePath == null && name.endsWith(getFileExtension())) {
                    return file.getAbsolutePath();
                }
            }
            throw new CaseActionException("No .aut files found in case directory");
        }
        throw new CaseActionException("Case directory was not found");
    }

}

@@ -1281,30 +1281,24 @@
  @param case
-Folder
 Path full pa
@@ -1309,32 +1309,58 @@
 o case directory
+ or full path to .aut file
 %0A     *%0A     * @
@@ -1419,30 +1419,24 @@
 (String case
-Folder
 Path) throws
@@ -1455,24 +1455,259 @@
 xception %7B%0A%0A
+        String metadataFilePath;%0A        if (casePath.endsWith(%22.aut%22) && (new File(casePath)).isFile()) %7B%0A            LOGGER.log(Level.INFO, %22Opening case %7B0%7D%22, casePath);%0A            metadataFilePath = casePath;%0A        %7D else %7B%0A    
         LOGG
@@ -1766,30 +1766,23 @@
 case
-Folder
 Path);%0A
-%0A
         
 Stri
@@ -1769,38 +1769,35 @@
 ePath);%0A        
-String
+   
  metadataFilePat
@@ -1820,20 +1820,24 @@
 case
-Folder
 Path);
+%0A        %7D
 %0A   

Allow a case to be opened from command line using the .aut file