/*
 * Copyright (C) 2009 the original author or authors.
 *
 * 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.sonatype.gshell.commands.network;

import org.codehaus.plexus.util.IOUtil;
import org.sonatype.gshell.command.Command;
import org.sonatype.gshell.command.CommandActionSupport;
import org.sonatype.gshell.command.CommandContext;
import org.sonatype.gshell.command.IO;
import org.sonatype.gshell.io.Closer;
import org.sonatype.gshell.io.Flusher;
import org.sonatype.gshell.util.cli.Argument;
import org.sonatype.gshell.util.cli.Option;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Fetch a file from a URL.
 *
 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 * @since 2.3
 */
@Command(name="wget")
public class WgetCommand
    extends CommandActionSupport
{
    @Argument(required=true)
    private URL source;

    @Option(name="-o", aliases={"--output-file"}, argumentRequired=true)
    private File outputFile;

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

        io.info("Downloading: {}", source);
        io.info("Connecting to: {}:{}", source.getHost(), source.getPort());
        
        URLConnection conn = source.openConnection();

        io.info("Length: {} [{}]", conn.getContentLength(), conn.getContentType());

        InputStream in = conn.getInputStream();

        OutputStream out;
        if (outputFile != null) {
            io.info("Saving to file: {}", outputFile);
            out = new BufferedOutputStream(new FileOutputStream(outputFile));
        }
        else {
            out = io.streams.out;
        }

        IOUtil.copy(in, out);

        // if we write a file, close it then retun the file
        if (outputFile != null) {
            Closer.close(out);
            return outputFile;
        }

        // else flush the stream and say we did good
        Flusher.flush(out);
        return Result.SUCCESS;
    }
}
@@ -1796,16 +1796,30 @@
 source);
+ // TODO: i18n
 %0A       
@@ -1887,16 +1887,30 @@
 Port());
+ // TODO: i18n
 %0A       
@@ -2049,16 +2049,30 @@
 Type());
+ // TODO: i18n
 %0A%0A      
@@ -2228,16 +2228,30 @@
 utFile);
+ // TODO: i18n
 %0A       
@@ -2471,16 +2471,17 @@
 hen retu
+r
 n the fi
@@ -2544,24 +2544,108 @@
 close(out);%0A
+            io.info(%22Saved %7B%7D %5B%7B%7D%5D%22,outputFile, outputFile.length()); // TODO: i18n%0A
             

Add user output, mark for i18n