var path = require('path');
var os = require('os');
var fs = require('fs-extra');
var config  = require('./config');


exports.download = function(done) {
  var request = require('request');
  var unzip   = require('unzip2');
  var src = source();
  var url = config.get().url;
  var target = path.join(os.tmpdir(), 'tldr');
  var inside = target;
  if (src) {
    url = 'https://github.com/' + src.user + '/' + src.repo + '/archive/' + src.branch + '.zip';
    inside = path.join(target, src.repo + '-' + src.branch);
  }

  fs.removeSync(target);
  fs.mkdirsSync(target);

  var extractor = unzip.Extract({path: target});
  extractor.on('error', function () {
    done(new Error('Cannot update from ' + url), inside);
  });
  extractor.on('close', function() {
    done(null, inside);
  });

  if (config.get().proxy) {
    request = request.defaults({
      'proxy':config.proxy
    });
  }

  var req = request.get({
    url: url,
    headers: {'User-Agent' : 'tldr-node-client'}
  });

  req.on('error', done);
  req.pipe(extractor);
};

function source() {
  var repository = config.get().repository;
  if (repository) {
    var parts  = repository.split('#');
    var github = parts[0].match(/^(.*)\/(.*)$/);
    return {
      user: github[1],
      repo: github[2],
      branch: parts[1] || 'master'
    };
  }
  return null;
}

@@ -1,11 +1,28 @@
-var
+'use strict';%0A%0Aconst
  path = 
@@ -38,19 +38,21 @@
 path');%0A
-var
+const
  os = re
@@ -64,19 +64,21 @@
 ('os');%0A
-var
+const
  fs = re
@@ -96,19 +96,21 @@
 xtra');%0A
-var
+const
  config 
@@ -134,16 +134,378 @@
 fig');%0A%0A
+function source() %7B%0A  let repository = config.get().repository;%0A  if (repository) %7B%0A    let parts  = repository.split('#');%0A    let github = parts%5B0%5D.match(/%5E(.*)%5C/(.*)$/);%0A    return %7B%0A      user: github%5B1%5D,%0A      repo: github%5B2%5D,%0A      branch: parts%5B1%5D %7C%7C 'master'%0A    %7D;%0A  %7D%0A  return null;%0A%7D%0A%0A// Downloads the zip file from github and extracts it to /tmp/tldr
 %0Aexports
@@ -535,19 +535,19 @@
 ne) %7B%0A  
-var
+let
  request
@@ -571,19 +571,19 @@
 st');%0A  
-var
+let
  unzip  
@@ -606,19 +606,19 @@
 p2');%0A  
-var
+let
  src = s
@@ -628,19 +628,19 @@
 ce();%0A  
-var
+let
  url = c
@@ -658,19 +658,19 @@
 .url;%0A  
-var
+let
  target 
@@ -705,19 +705,19 @@
 dr');%0A  
-var
+let
  inside 
@@ -906,64 +906,155 @@
  %7D%0A%0A
+  // Empty the tmp dir%0A
   fs.
-r
 em
-oveSync(target);%0A  fs.mkdirsSync(target);%0A%0A  var
+ptyDir(target, (err) =%3E %7B%0A    if (err) %7B%0A      return done(err, inside);%0A    %7D%0A%0A    // Creating the extractor%0A    let
  ext
@@ -1089,24 +1089,26 @@
 : target%7D);%0A
+  
   extractor.
@@ -1123,26 +1123,22 @@
 r', 
-function ()
+() =%3E
  %7B%0A    
+ 
+ 
 done
@@ -1187,22 +1187,26 @@
 nside);%0A
+  
   %7D);%0A
+  
   extrac
@@ -1225,25 +1225,22 @@
 e', 
-function()
+() =%3E
  %7B%0A    
+  
 done
@@ -1255,23 +1255,69 @@
 nside);%0A
+  
   %7D);%0A%0A
+    // Setting the proxy if set by config%0A  
   if (co
@@ -1340,16 +1340,18 @@
 ) %7B%0A    
+  
 request 
@@ -1371,16 +1371,18 @@
 aults(%7B%0A
+  
       'p
@@ -1408,22 +1408,82 @@
     
+  
 %7D);%0A
+  
   %7D%0A%0A  
-var
+  // Creating the request and passing the extractor%0A    let
  req
@@ -1503,16 +1503,18 @@
 t(%7B%0A    
+  
 url: url
@@ -1515,16 +1515,18 @@
 l: url,%0A
+  
     head
@@ -1566,23 +1566,27 @@
 lient'%7D%0A
+  
   %7D);%0A%0A
+  
   req.on
@@ -1602,16 +1602,18 @@
  done);%0A
+  
   req.pi
@@ -1631,303 +1631,13 @@
 r);%0A
-%7D;%0A%0Afunction source() %7B%0A  var repository = config.get().repository;%0A  if (repository) %7B%0A    var parts  = repository.split('#');%0A    var github = parts%5B0%5D.match(/%5E(.*)%5C/(.*)$/);%0A    return %7B%0A      user: github%5B1%5D,%0A      repo: github%5B2%5D,%0A      branch: parts%5B1%5D %7C%7C 'master'%0A    %7D;%0A  %7D%0A  return null;%0A%7D
+  %7D);%0A%7D;
 %0A

update remote.js