var temp  = require('temp'),
    fs    = require('fs'),
    util  = require('util'),
    path  = require('path'),
    gutil = require('gulp-util'),
    exec  = require('child_process').execSync,
    shell = require('gulp-shell'),
    randomstring = require('randomstring'),
    mkdirp = require('mkdirp');

var Reloadfile = require('./reloadfile').ReloadFile;
var AuthMethods = require('./reloadfile').AuthMethods;

var _clientMountDir = {};

var SyncUtil = {

    mountFs: function() {

        _clientMountDir = _mkDir();

        var config = Reloadfile.config;

        switch (Reloadfile.getAuthModeSync()) {
            case AuthMethods.KEYFILE:

                var user =  config.sync.user;
                var host = config.device.host;
                var port = config.device.port;
                var mount = _clientMountDir;
                var targetMount = "/";
                var keyfile = config.sync.keyfile;

                var cmd = util.format('sshfs -o ssh_command="ssh -p %s -i %s" %s@%s:%s %s', port, keyfile, user, host, targetMount, mount );

                gutil.log("Mounting filesystem with " + cmd);
                exec(cmd);

                break;
            case AuthMethods.USERNAME_PASSWORD:
                
                break;
            case AuthMethods.PUBLIC_KEY:
                break;
            default:
        }
    },

    unmountFs: function() {

        var cmd = "umount <%= mount %>";
        var templateData = {
            mount: _clientMountDir
        };

        shell(cmd, { templateData: templateData, ignoreErrors: true } );
    },

    getClientMountDir: function() {
        return _clientMountDir;
    }
}

exports.SyncUtil = SyncUtil;


function _mkDir() {
    var name = "/tmp/mount/" + randomstring.generate(4);
    return mkdirp.sync(name);
}


@@ -1,114 +1,29 @@
 var 
-temp  = require('temp'),%0A    fs    = require('fs'),%0A    util  = require('util'),%0A    path  = require('path
+util  = require('util
 '),%0A
@@ -107,43 +107,8 @@
 nc,%0A
-    shell = require('gulp-shell'),%0A
     
@@ -315,10 +315,12 @@
 r = 
-%7B%7D
+null
 ;%0A%0Av
@@ -1002,15 +1002,22 @@
 ith 
+%5B
 %22 + cmd
+ + %22%5D%22
 );%0A 
@@ -1114,32 +1114,16 @@
 SSWORD:%0A
-                
 %0A       
@@ -1273,197 +1273,326 @@
 ) %7B%0A
-%0A        var cmd = %22umount %3C%25= mount %25%3E%22;%0A        var templateData = %7B%0A            mount: _clientMountDir%0A        %7D;%0A%0A        shell(cmd, %7B templateData: templateData, ignoreErrors: true %7D )
+        if (_clientMountDir) %7B%0A            var cmd = util.format(%22umount %25s%22, _clientMountDir);%0A            gutil.log(_clientMountDir);%0A            gutil.log(%22Unmounting filesystem with %5B%22, cmd + %22%5D%22);%0A            exec(cmd);%0A        %7D%0A    %7D,%0A%0A    isMounted: function() %7B%0A        return this.getClientMountDir() != null
 ;%0A  
@@ -1704,17 +1704,16 @@
 cUtil;%0A%0A
-%0A
 function
@@ -1813,9 +1813,8 @@
 ame);%0A%7D%0A
-%0A

introduce isMounted function in sync.js