// Copyright (C) 2013 - Texas Instruments, Jason Kridner
//
//
var fs = require('fs');
var child_process = require('child_process');
var winston = require('winston');

var ar = '/var/lib/cloud9/autorun';
var apps = {};

fs.exists(ar, arExists);

function arExists(exists) {
    if(!exists) fs.mkdir(ar, arWatch);
    else fs.readdir(ar, arFound);
}

function arFound(err, files) {
    if(err) {
        winston.err('Error reading directory: ' + err);
        arWatch();
        return;
    }
    var i = 0;
    arTestNext();

    function arTestNext() {
        if(i == files.length) {
            arWatch();
            return;
        }
        appStart(files[i]);
        i++;
        arTestNext();
    }
}

function appStart(file) {
    if(apps[file]) return;
    appTest();

    function appTest() {
        fs.exists(ar + '/' + file, appExists);
    }

    function appExists(exists) {
        if(exists) {
            if(file.match(/\.js$/)) {
                winston.info('start: ' + file);
                apps[file] = child_process.spawn(process.argv[0], [ar + '/' + file]);
                apps[file].on('close', appClosed);
                var onStdout = function(data) {
                    winston.info('stdout (' + file + '): ' + data);
                };
                var onStderr = function(data) {
                    winston.info('stderr (' + file + '):' + data);
                };
                apps[file].stdout.on('data', onStdout);
                apps[file].stderr.on('data', onStderr);
            }
        }
    }

    function appClosed(code, signal) {
        apps[file] = false;
        if(signal == 'SIGKILL') setTimeout(appTest, 1000);
    }
}

function arWatch() {
    fs.watch(ar, arWatcher);
}

function arWatcher(event, filename) {
    if(event == 'change') {
        winston.info('change: ' + filename);
        appStop(filename);
        appStart(filename);
    } else if(event == 'rename') {
        winston.info('rename: ' + filename);
        appStop(filename);
        appStart(filename);
    }
}

function appStop(file) {
    if(apps[file]) {
        winston.info('stop: ' + file);
        apps[file].kill('SIGKILL');
    }
}

@@ -213,16 +213,70 @@
  = %7B%7D;%0A%0A
+winston.info('Starting bonescript autorun service');%0A%0A
 fs.exist
@@ -951,20 +951,404 @@
     
-if(exists) %7B
+function onStdout(data) %7B%0A            winston.info('stdout (' + file + '): ' + data);%0A        %7D%0A        function onStderr(data) %7B%0A            winston.info('stderr (' + file + '):' + data);%0A        %7D%0A                %0A        if(exists) %7B%0A            if(typeof apps%5Bfile%5D != 'undefined') %7B%0A                winston.info('already running: ' + file);%0A                return;%0A            %7D%0A            
 %0A   
@@ -1587,44 +1587,48 @@
     
-var onStdout = functi
+apps%5Bfile%5D.stdout.
 on(
+'
 data
-) %7B%0A    
+', onStdout);%0A
     
@@ -1643,187 +1643,226 @@
     
-winston.info('stdout (' + file + '): ' + data);%0A                %7D;%0A                var onStderr = function(data) %7B%0A                    winston.info('stderr (' + file
+apps%5Bfile%5D.stderr.on('data', onStderr);%0A            %7D else if(file.match(/%5C.py$/)) %7B%0A                winston.info('start: ' + file);%0A                apps%5Bfile%5D = child_process.spawn('/usr/bin/python', %5Bar
  + '
-):
+/
 ' + 
-data
+file%5D
 );%0A 
@@ -1876,17 +1876,49 @@
         
-%7D
+apps%5Bfile%5D.on('close', appClosed)
 ;%0A      
@@ -2093,32 +2093,39 @@
 gnal) %7B%0A        
+delete 
 apps%5Bfile%5D = fal
@@ -2122,16 +2122,8 @@
 ile%5D
- = false
 ;%0A  
@@ -2575,32 +2575,39 @@
 (file) %7B%0A    if(
+typeof 
 apps%5Bfile%5D) %7B%0A  
@@ -2600,16 +2600,31 @@
 ps%5Bfile%5D
+ != 'undefined'
 ) %7B%0A    
@@ -2647,32 +2647,67 @@
 ('stop: ' + file
+ + ' (pid: ' + apps%5Bfile%5D.pid + ')'
 );%0A        apps%5B

Add support for Python scripts and eliminate duplicate runs