var google = require('googleapis');
var fs = require('fs');
var rimraf = require('rimraf');
var constants = require('../constants.js');

/**
 * Download script json from Google Drive
 *
 * @param {google.auth.OAuth2} auth - An authorized OAuth2 client.
 * @param {string} fileId - Id of file to download.
 * @param {string} dir - Optional directory in which the project is located.
 * @param {string} method - Identifier for the flow calling this function.
 * @param {callback} callback - The callback that handles the response.
 */
function downloadRemote(auth, fileId, dir, method, callback) {
    if (dir) {
        var gasDir = './' + dir + '/' + constants.META_DIR;
    } else {
        var gasDir = './' + constants.META_DIR;
    }

    switch (method) {
        case 'clone':
            if (fs.existsSync(gasDir)) {
                callback('Oops, you seem to be cloning in a directory already containing a .gas folder.');
                callback('Please check your project strucure and try again.');
                return;
            } else {
                if (dir) {
                    fs.mkdirSync('./' + dir + '/');
                }
                fs.mkdirSync(gasDir);
            }
            break;
        case 'pull':
            break;
        case 'link':
            if (!fs.existsSync(gasDir)) {
                fs.mkdirSync(gasDir);
            }
            break;
        default:
            callback('Unsupported method.');
            return;
            break;
    }

    fs.writeFile(gasDir + '/' + constants.META_ID, fileId, function() {});

    var dest = fs.createWriteStream(gasDir + '/' + constants.META_REMOTE);
    var drive = google.drive('v3');

    drive.files.export({
        auth: auth,
        fileId: fileId,
        mimeType: constants.MIME_GAS_JSON
    }, function(err, result) {
        if (err) {
            rimraf(gasDir + '/' + constants.META_REMOTE, function() {});
            callback(err);
        }
    }).pipe(dest);

    dest.on('finish', function() {
        callback();
    });
}

module.exports = downloadRemote;

@@ -920,19 +920,22 @@
  folder.
-');
+ %5Cn' +
 %0A       
@@ -943,25 +943,16 @@
         
-callback(
 'Please 
@@ -1013,32 +1013,303 @@
         return;%0A
+            %7D else if (fs.existsSync('./' + dir)) %7B%0A                callback('Oops, the directory %5C'' + dir + '%5C' seems to exist already. %5Cn' +%0A                'Remove this folder or use %5C'gas link%5C' to link your project to the correct folder.');%0A                return;%0A
             %7D el

fix folder name collision issue