'use strict';

/**
 * Kalabox download utility module.
 * @module kbox.util.download
 */

// Npm modules
var Promise = require('../promise.js');
var internet = require('./internet.js');

/**
 * Downloads a bunch of files and extracts those as appropriate
 * @arg {array} urls - Array of URLs to download.
 * @arg {error} dest - Where we want the downloads to end up
 * @arg {vinyl} callback.files - Array of vinyl objects
 * @example
 * kbox.util.download.downloadFiles()
 *
 * .then(function(files) {
 * });
 */
exports.downloadFiles = function(urls, dest) {

  // Moved these to inside the function so they will lazy load.
  var Download = require('download');
  var progress = require('download-status');
  var download = new Download({extract: true});

  // Set each URL
  urls.forEach(function(url) {
    download.get(url);
  });

  // Set the destination and use the download progress
  // middleware
  download.dest(dest).use(progress());

  // Check to make sure the Internet is accessable.
  return internet.check()
  // Promisify our download run
  .retry(function() {
    return Promise.fromNode(function(callback) {
      download.run(callback);
    });
  });

};

@@ -91,10 +91,104 @@
 // N
-pm
+ative modules%0Avar path = require('path');%0A%0A// Npm modules%0Avar _ = require('lodash');%0A%0A// Kalabox
  mod
@@ -809,16 +809,23 @@
 download
+Extract
  = new D
@@ -854,58 +854,420 @@
 %7D);%0A
-%0A  // Set each URL%0A  urls.forEach(function(url) %7B%0A
+  var download = new Download(%7Bextract: false%7D);%0A%0A  // List of zippy extensions to check so we use the right%0A  // parser. We do this because download tries to extract%0A  // a DMG which doesnt go over so well%0A  var zippyExts = %5B'.7z', 'bz2', '.gzip', '.tar', '.tar.gz', '.tgz', '.zip'%5D;%0A%0A  // Set each URL depending on extension%0A  urls.forEach(function(url) %7B%0A    if (_.includes(zippyExts, path.extname(url))) %7B%0A  
     
@@ -1274,16 +1274,23 @@
 download
+Extract
 .get(url
@@ -1292,16 +1292,64 @@
 t(url);%0A
+    %7D%0A    else %7B%0A      download.get(url);%0A    %7D%0A
   %7D);%0A%0A 
@@ -1417,16 +1417,51 @@
 ddleware
+ and then arrayify before promosify
 %0A  downl
@@ -1491,16 +1491,111 @@
 ress());
+%0A  downloadExtract.dest(dest).use(progress());%0A  var downloaders = %5Bdownload, downloadExtract%5D;
 %0A%0A  // C
@@ -1666,16 +1666,17 @@
 check()%0A
+%0A
   // Pro
@@ -1717,24 +1717,86 @@
 unction() %7B%0A
+    return Promise.each(downloaders, function(downloader) %7B%0A  
     return P
@@ -1838,24 +1838,26 @@
 %7B%0A      
+  
 download
 .run(cal
@@ -1848,16 +1848,18 @@
 download
+er
 .run(cal
@@ -1866,16 +1866,26 @@
 lback);%0A
+      %7D);%0A
     %7D);%0A

Update download module to handle dmg files