#!/bin/env node

{
  const chalk = require("chalk");
  const isOnline = require('is-online');
  const io = require('socket.io-client');
  const socket = io('http://localhost:1337');
  const debug = require('debug')('main');
  const {
    spawn
  } = require('child_process');
  let wifiConnect;
  socket.on('connect', () => {
    'use strict';
    socket.emit('identify', 'network');
    isOnline().then(online => {
      if (!online) {
        console.log(chalk.yellow("online check failed, spawning wifi-connect"));
        socket.emit("emoji", "wifi");
        wifiConnect = spawn(__dirname + '/.wifi-connect', ['-a', '600', '-u', '/usr/src/app/ui']);
        wifiConnect.on('close', (code) => {
          console.log(chalk.cyan("wifi-connect exited"));
          socket.emit('emoji', "smile");
        });
        wifiConnect.on('data', (output) => {
          console.log(chalk.cyan("wifi-connect: ") + output);
        });
      } else {
        console.log(chalk.green("online check ok, skipping wifi-connect"));
      }
    });
  });
}

@@ -379,16 +379,21 @@
 rk');%0A  
+%7D);%0A%0A
   isOnli
@@ -412,24 +412,40 @@
 line =%3E %7B%0A  
+  %22use strict%22;%0A
     if (!onl
@@ -451,18 +451,16 @@
 line) %7B%0A
-  
       co
@@ -532,26 +532,24 @@
 t%22));%0A      
-  
 socket.emit(
@@ -564,34 +564,32 @@
  %22wifi%22);%0A      
- 
- 
 wifiConnect = sp
@@ -606,17 +606,16 @@
 ame + '/
-.
 wifi-con
@@ -640,17 +640,16 @@
 ', '-u',
- 
 '/usr/sr
@@ -659,34 +659,32 @@
 pp/ui'%5D);%0A      
-  
 wifiConnect.on('
@@ -703,34 +703,32 @@
 e) =%3E %7B%0A        
- 
- 
 console.log(chal
@@ -763,26 +763,24 @@
 ));%0A        
-  
 socket.emit(
@@ -800,32 +800,28 @@
 le%22);%0A      
-  
 %7D);%0A
-  
       wifiCo
@@ -855,26 +855,24 @@
 %3E %7B%0A        
-  
 console.log(
@@ -899,19 +899,17 @@
 nect: %22)
- + 
++
 output);
@@ -909,18 +909,16 @@
 utput);%0A
-  
       %7D)
@@ -923,18 +923,16 @@
 %7D);%0A    
-  
 %7D else %7B
@@ -932,18 +932,16 @@
  else %7B%0A
-  
       co
@@ -1014,24 +1014,15 @@
     
-  
 %7D%0A  
- 
- 
 %7D);%0A
-  %7D);
 %0A%7D%0A

fix typo in wifi-connect path