/*
 * 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.InetAddress;
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();

        log.debug("Fetching: {}", source);
        URLConnection conn = source.openConnection();
        InputStream in = conn.getInputStream();

        String contentType = conn.getContentType();
        log.debug("Content type: {}", contentType);

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

        IOUtil.copy(in, out);

        if (outputFile != null) {
            Closer.close(out);
            return outputFile;
        }
        else {
            Flusher.flush(out);
        }
        
        return Result.SUCCESS;
    }
}
@@ -1207,37 +1207,8 @@
 am;%0A
-import java.net.InetAddress;%0A
 impo
@@ -1765,29 +1765,79 @@
     
-log.debug(%22Fetching
+io.info(%22Downloading: %7B%7D%22, source);%0A        io.info(%22Connecting to
 : 
+%7B%7D:
 %7B%7D%22,
@@ -1835,34 +1835,71 @@
 : %7B%7D:%7B%7D%22, source
-);
+.getHost(), source.getPort());%0A        
 %0A        URLConn
@@ -1937,24 +1937,25 @@
 tion();%0A
+%0A
         
 InputStr
@@ -1950,77 +1950,59 @@
     
-InputStream in = conn.getInputStream();%0A%0A        String co
+io.info(%22Length: %7B%7D %5B%7B%7D%5D%22, conn.getConte
 nt
+L
 en
-tType =
+gth(),
  con
@@ -2019,17 +2019,19 @@
 ntType()
-;
+);%0A
 %0A       
@@ -2035,49 +2035,45 @@
     
-log.debug(%22Content type: %7B%7D%22, contentType
+InputStream in = conn.getInputStream(
 );%0A%0A
@@ -2148,23 +2148,20 @@
     
-log.debug(%22Writ
+io.info(%22Sav
 ing 
@@ -2362,24 +2362,84 @@
 (in, out);%0A%0A
+        // if we write a file, close it then retun the file%0A
         if (
@@ -2532,35 +2532,70 @@
       %7D%0A
+%0A
        
+ //
  else 
-%7B%0A    
+flush the stream and say we did good%0A
         
@@ -2618,27 +2618,8 @@
 t);%0A
-        %7D%0A        %0A
     

Add user output