var fs = require('fs');
var CONST = require('./gpio_defines');

var GPIO_PATH = CONST.GPIO_PATH;
var NOOP = function(){};

var _write = function(file, str, callback, override) {
	if(callback){
    return fs.writeFile(file, str, function(err) {
      if(err && !override) {
				return callback(err instanceof Error?err:new Error(err));
      }
      return callback();
    });
  }
  try{
    return fs.writeFileSync(file, str);
  }catch(e){
    if(!override) {
      console.error('WRITE ERROR: ', file);
      console.error(e.stack||e);
      throw e;
    }
  }
};

var _read = function(file, callback) {
	if(callback){
    fs.readFile(file, "utf-8", function(err, data) {
      if(err) {
        return callback(err instanceof Error?err:new Error(err));
      }
      callback(null, data);
    });
  }else{
    try{
      return fs.readFileSync(file, "utf-8");
    }catch(e){
      if(!override) {
        console.error('READ ERROR: ', file);
        console.error(e.stack||e);
        throw e;
      }
    }
  }
};

var _unexport = function(pinNumber, callback) {
	return _write(GPIO_PATH + 'unexport', pinNumber, callback, true);
};

var _export = function(pinNumber, callback) {
	if(fs.existsSync(GPIO_PATH + 'gpio'+pinNumber)){
		return _unexport(n, function(){
			return _export(n, callback);
		});
	}
	return _write(GPIO_PATH + 'export', pinNumber, function(err) {
		if(err){
      return _unexport(pinNumber, function(){
				return _export(pinNumber, callback);
			});
    }
    return (callback||NOOP)();
	}, 1);
};

var Rock = function(options){
};

Rock.prototype.export = function(pinNumber, callback){
  _export(pinNumber, callback);
};

Rock.prototype.unexport = function(pinNumber, callback){
  _unexport(pinNumber, callback);
};

Rock.prototype.getPinMode = function(pinNumber, callback){
	var self = this;
	var path = GPIO_PATH + 'gpio'+ pinNumber + '/direction';
	try{
		return _read(path, function(err, dir){
			return callback(null, dir||'undefined');
		});
	}catch(e){
		return 'undefined';
	}
};

Rock.prototype.setPinMode = function(pinNumber, direction, callback){
  var self = this;
  var mode = direction === 'in' || direction === 'input' ? 'in':'out';
  var path = GPIO_PATH + 'gpio'+ pinNumber + '/direction';
  _read(path, function(err, dir){
    if(err){
			return self.export(pinNumber, function(err){
				if(err){
					return callback(err);
				}
				return _write(path, dir, callback);
			});
    }
    if(dir.indexOf(direction)===-1){
      return _write(path, dir, callback);
    }
    return process.nextTick(callback||NOOP);
  });
};

Rock.prototype.get = Rock.prototype.read = function(pinNumber, callback){
  var self = this;
  var path = GPIO_PATH + 'gpio'+ pinNumber + '/value';
  return _read(path, callback);
};

Rock.prototype.set = Rock.prototype.write = function(pinNumber, value, callback){
  var self = this;
  var path = GPIO_PATH + 'gpio'+ pinNumber + '/value';
  return _write(path, value, callback);
};

module.exports = Rock;

@@ -167,25 +167,26 @@
 override) %7B%0A
-%09
+  
 if(callback)
@@ -267,20 +267,24 @@
 ride) %7B%0A
-%09%09%09%09
+        
 return c
@@ -603,17 +603,18 @@
 back) %7B%0A
-%09
+  
 if(callb
@@ -1065,17 +1065,18 @@
 back) %7B%0A
-%09
+  
 return _
@@ -1183,17 +1183,18 @@
 back) %7B%0A
-%09
+  
 if(fs.ex
@@ -1234,18 +1234,20 @@
 mber))%7B%0A
-%09%09
+    
 return _
@@ -1270,19 +1270,22 @@
 tion()%7B%0A
-%09%09%09
+      
 return _
@@ -1309,18 +1309,22 @@
 k);%0A
-%09%09%7D);%0A%09%7D%0A%09
+    %7D);%0A  %7D%0A  
 retu
@@ -1382,18 +1382,20 @@
 (err) %7B%0A
-%09%09
+    
 if(err)%7B
@@ -1441,20 +1441,24 @@
 tion()%7B%0A
-%09%09%09%09
+        
 return _
@@ -1478,35 +1478,38 @@
 ber, callback);%0A
-%09%09%09
+      
 %7D);%0A    %7D%0A    re
@@ -1533,17 +1533,18 @@
 OOP)();%0A
-%09
+  
 %7D, 1);%0A%7D
@@ -1825,17 +1825,18 @@
 lback)%7B%0A
-%09
+  
 var self
@@ -1844,17 +1844,18 @@
 = this;%0A
-%09
+  
 var path
@@ -1907,16 +1907,19 @@
 n';%0A
-%09
+  
 try%7B%0A
-%09%09
+    
 retu
@@ -1949,27 +1949,30 @@
 (err, dir)%7B%0A
-%09%09%09
+      
 return callb
@@ -2004,15 +2004,18 @@
 ');%0A
-%09%09
+    
 %7D);%0A
-%09
+  
 %7Dcat
@@ -2021,18 +2021,20 @@
 tch(e)%7B%0A
-%09%09
+    
 return '
@@ -2045,17 +2045,18 @@
 fined';%0A
-%09
+  
 %7D%0A%7D;%0A%0ARo
@@ -2319,19 +2319,22 @@
 f(err)%7B%0A
-%09%09%09
+      
 return s
@@ -2370,20 +2370,24 @@
 n(err)%7B%0A
-%09%09%09%09
+        
 if(err)%7B
@@ -2391,13 +2391,18 @@
 r)%7B%0A
-%09%09%09%09%09
+          
 retu
@@ -2423,18 +2423,26 @@
 r);%0A
-%09%09%09%09%7D%0A%09%09%09%09
+        %7D%0A        
 retu
@@ -2477,11 +2477,14 @@
 k);%0A
-%09%09%09
+      
 %7D);%0A

Fix tabs to spaces