var bower =  require('gulp-bower');
var config = require('./config');
var common = require('./common');
var gulp = require('gulp');
var fs = require('fs-extra');
var modify = require('gulp-modify');
var rsync = require('gulp-rsync');
var gutil = require('gulp-util');
var args = require('yargs').argv;

var stagingBasePath = config.paths.staging.cdn;
var version = config.version;
var host = config.toolsHost;
var permalink = config.permalink;
var stagingPath = stagingBasePath + '/' + version;
var testPath = process.cwd() + '/' + stagingPath + '/test';

gulp.task('clean:cdn', function() {
  fs.removeSync(stagingBasePath);
});

gulp.task('cdn:stage-bower_components', function() {
  return bower({
    directory: stagingPath,
    forceLatest: true,
    cmd: 'install'
  });
});

gulp.task('cdn:stage-vaadin-elements', function() {
  return gulp.src(['LICENSE.html', 'README.md', 'vaadin-elements.html', 'demo/*', 'apidoc/*'], {base:"."})
    .pipe(modify({
        fileModifier: function(file, contents) {
          if (/README.md/.test(file.path)) {
            contents = contents.replace(/\/latest\//mg, '/' + version + '/');
          } else {
            contents.replace('https://cdn.vaadin.com/vaadin-elements/latest/', '../../');
          }
          return contents;
        }
      }))
    .pipe(gulp.dest(stagingPath + "/vaadin-elements"));
});

gulp.task('stage:cdn', [ 'clean:cdn', 'cdn:stage-bower_components', 'cdn:stage-vaadin-elements' ]);

gulp.task('upload:cdn', ['stage:cdn'], function() {
  common.checkArguments(['cdnUsername', 'cdnDestination']);
  gutil.log('Uploading to cdn (rsync): ' + stagingPath + ' -> '+ args.cdnUsername + '@' + host + ':' + args.cdnDestination + version);
  return gulp.src(stagingPath)
    .pipe(rsync({
      username: args.cdnUsername,
      hostname: host,
      root: stagingPath,
      emptyDirectories: false,
      recursive: true,
      clean: true,
      silent: true,
      destination: args.cdnDestination + version
    }));
});

gulp.task('deploy:cdn', ['upload:cdn'], function(done) {
  if (permalink) {
    var cmd = 'rm -f ' + args.cdnDestination + permalink + '; ln -s ' + version + ' ' +  args.cdnDestination + permalink + '; ls -l ' + args.cdnDestination;
    gutil.log('Deploying CDN : ssh ' + args.cdnUsername + '@' + host + ' ' +  cmd);
    common.ssh(args.cdnUsername, host, cmd, done);
  } else {
    done();
  }
});

@@ -4,17 +4,16 @@
  bower =
- 
  require
@@ -926,16 +926,29 @@
 %5D, %7B
+%0A      
 base:
+ 
 %22.%22
+%0A    
 %7D)%0A 
@@ -965,18 +965,16 @@
 odify(%7B%0A
-  
       fi
@@ -1008,26 +1008,24 @@
 contents) %7B%0A
-  
         if (
@@ -1057,34 +1057,32 @@
 h)) %7B%0A          
-  
 contents = conte
@@ -1139,18 +1139,16 @@
         
-  
 %7D else %7B
@@ -1148,18 +1148,16 @@
  else %7B%0A
-  
         
@@ -1240,26 +1240,24 @@
 ');%0A        
-  
 %7D%0A          
@@ -1254,18 +1254,16 @@
         
- 
- 
 return c
@@ -1277,26 +1277,22 @@
 ;%0A      
-  
 %7D%0A    
- 
- 
 %7D))%0A    
@@ -1372,17 +1372,16 @@
 :cdn', %5B
- 
 'clean:c
@@ -1442,17 +1442,16 @@
 lements'
- 
 %5D);%0A%0Agul
@@ -1622,16 +1622,17 @@
 + ' -%3E '
+ 
 + args.c
@@ -2144,17 +2144,16 @@
  + ' ' +
- 
  args.cd
@@ -2289,17 +2289,16 @@
  + ' ' +
- 
  cmd);%0A 

Reformat cdn.js.