/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.geronimo.gshell.commands.vfs;

import org.apache.commons.vfs.FileContent;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.FileFilter;
import org.apache.commons.vfs.FileSelectInfo;
import org.apache.commons.vfs.FileFilterSelector;
import org.apache.geronimo.gshell.clp.Argument;
import org.apache.geronimo.gshell.clp.Option;
import org.apache.geronimo.gshell.command.CommandContext;
import org.apache.geronimo.gshell.io.IO;

import java.text.DateFormat;
import java.util.Date;

/**
 * List the contents of a file or directory.
 *
 * @version $Rev$ $Date$
 */
public class ListDirectoryAction
    extends VfsActionSupport
{
    @Argument
    private String path;

    @Option(name="-a")
    private boolean includeHidden = false;

    @Option(name="-r", aliases={"--recursive"})
    private boolean recursive = false;

    public Object execute(final CommandContext context) throws Exception {
        assert context != null;
        IO io = context.getIo();

        FileObject file;
        if (path != null) {
            file = resolveFile(context, path);
        }
        else {
            file = getCurrentDirectory(context);
        }

        if (file.getType() == FileType.FOLDER) {
            listChildren(io, file, recursive, "");
        }
        else {
            io.info("{}", file.getName());

            FileContent content = file.getContent();
            io.verbose("Size: {} bytes", content.getSize());

            DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
            String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
            io.verbose("Last modified: {}", lastMod);
        }

        return Result.SUCCESS;
    }

    private void listChildren(final IO io, final FileObject dir, final boolean recursive, final String prefix) throws FileSystemException {
        assert io != null;
        assert dir != null;
        assert prefix != null;

        FileFilter filter = new FileFilter() {
            public boolean accept(final FileSelectInfo selection) {
                assert selection != null;

                // When includeHidden only include the file if there is not "." prefix, else include everything
                return includeHidden || !selection.getFile().getName().getBaseName().startsWith(".");
            }
        };

        FileObject[] files = dir.findFiles(new FileFilterSelector(filter));

        for (FileObject file : files) {
            io.out.print(prefix);
            io.out.print(file.getName().getBaseName());

            if (file.getType() == FileType.FOLDER) {
                io.out.println("/");

                if (recursive) {
                    listChildren(io, file, recursive, prefix + "    ");
                }
            }
            else {
                io.out.println();
            }
        }
    }
}
@@ -2879,32 +2879,158 @@
 refix != null;%0A%0A
+        FileObject%5B%5D files;%0A%0A        if (includeHidden) %7B%0A            files = dir.getChildren();%0A        %7D%0A        else %7B%0A    
         FileFilt
@@ -3056,24 +3056,28 @@
 eFilter() %7B%0A
+    
             
@@ -3144,24 +3144,28 @@
             
+ 
+   
 assert selec
@@ -3199,104 +3199,27 @@
     
-// When includeHidden only include the file if there is not %22.%22 prefix, else include everything%0A
+    try %7B%0A         
     
@@ -3233,32 +3233,14 @@
     
- 
 return
- includeHidden %7C%7C
  !se
@@ -3261,46 +3261,146 @@
 e().
-getName().getBaseName().startsWith(%22.%22
+isHidden();%0A                    %7D%0A                    catch (FileSystemException e) %7B%0A                        throw new RuntimeException(e
 );%0A 
@@ -3402,34 +3402,64 @@
 e);%0A            
+ 
-%7D%0A
+       %7D%0A                %7D%0A    
         %7D;%0A%0A    
@@ -3458,36 +3458,27 @@
 %7D;%0A%0A        
-FileObject%5B%5D
+   
  files = dir
@@ -3520,16 +3520,26 @@
 ilter));
+%0A        %7D
 %0A%0A      

Optimize a wee bit