const spawn = require('child_process').spawn;

const TVSERVICE_PATH    = "/opt/vc/bin/tvservice"

const HDMI_ON_CMD       = "-p"
const HDMI_OFF_CMD      = "-o"
const HDMI_STATUS_CMD   = "--status"


var runCmd = function(arg, chain){
  chain = chain || Function.prototype;
  var child = spawn(TVSERVICE_PATH, [arg])
  var status = "";
  
  child.stdout.on("data" , function(data){
    status += data;
  })
  
  child.stdout.on("end" , function(){
    chain(null , status);
  });
  
  child.stdout.on("error" , function(err){
    chain(err);
  })
}

var status = function(chain){
  runCmd(HDMI_STATUS_CMD , chain);
}

module.exports.status = status

module.exports.on = function(chain){
  runCmd(HDMI_ON_CMD , chain);
}

module.exports.off = function(chain){
  runCmd(HDMI_OFF_CMD , chain);
}


module.exports.isConnected = function(chain){
  chain = chain || Function.prototype;
  status(function(err , data){
    if(err)
      return chain(err)
    chain(null , (data.indexOf("TV is off") === -1) )
  });
}


@@ -1,13 +1,28 @@
-const
+%22use strict%22;%0D%0A%0D%0Avar
  spawn =
@@ -57,21 +57,19 @@
 awn;%0D%0A%0D%0A
-const
+var
  TVSERVI
@@ -108,17 +108,16 @@
 ice%22
+;
 %0D%0A%0D%0A
-const
+var
  HDM
@@ -137,23 +137,22 @@
   = %22-p%22
+;
 %0D%0A
-const
+var
  HDMI_OF
@@ -172,15 +172,14 @@
 %22-o%22
+;
 %0D%0A
-const
+var
  HDM
@@ -205,16 +205,17 @@
 -status%22
+;
 %0D%0A%0D%0A%0D%0Ava
@@ -330,16 +330,17 @@
 , %5Barg%5D)
+;
 %0D%0A  var 
@@ -585,24 +585,38 @@
 %0D%0A%0D%0Avar 
+hdmi = %7B%0D%0A    
 status 
-=
+:
  functio
@@ -618,32 +618,36 @@
 nction(chain)%7B%0D%0A
+    
   runCmd(HDMI_ST
@@ -670,66 +670,27 @@
 );%0D%0A
-%7D%0D%0A%0D%0Amodule.exports.status = status%0D%0A%0D%0Amodule.exports.on =
+    %7D,%0D%0A%0D%0A    on : 
  fun
@@ -696,32 +696,36 @@
 nction(chain)%7B%0D%0A
+    
   runCmd(HDMI_ON
@@ -744,33 +744,25 @@
 );%0D%0A
-%7D%0D%0A%0D%0Amodule.exports.
+    %7D,%0D%0A    
 off 
-=
+:
  fun
@@ -776,16 +776,20 @@
 hain)%7B%0D%0A
+    
   runCmd
@@ -817,30 +817,21 @@
 );%0D%0A
-%7D%0D%0A%0D%0A%0D%0Amodule.exports.
+    %7D,%0D%0A%0D%0A   
 isCo
@@ -850,32 +850,34 @@
 nction(chain)%7B%0D%0A
+  
   chain = chain 
@@ -902,16 +902,23 @@
 ype;%0D%0A  
+  hdmi.
 status(f
@@ -943,16 +943,18 @@
 )%7B%0D%0A    
+  
 if(err)%0D
@@ -960,16 +960,18 @@
 %0D%0A      
+  
 return c
@@ -977,24 +977,26 @@
 chain(err)%0D%0A
+  
     chain(nu
@@ -1042,16 +1042,46 @@
  )%0D%0A
+  
   %7D);%0D%0A
-%7D%0D%0A%0D%0A
+  %7D%0D%0A%7D;%0D%0A%0D%0Amodule.exports = hdmi;

remove const & use strict