// 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 = {};

winston.info('Starting bonescript autorun service');

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) {
        function onStdout(data) {
            winston.info('stdout (' + file + '): ' + data);
        }
        function onStderr(data) {
            winston.info('stderr (' + file + '):' + data);
        }
                
        if(exists) {
            if(typeof apps[file] != 'undefined') {
                winston.info('already running: ' + file);
                return;
            }
            
            if(file.match(/\.js$/)) {
                winston.info('start: ' + file);
                apps[file] = child_process.spawn(process.argv[0], [ar + '/' + file]);
                apps[file].on('close', appClosed);
                apps[file].stdout.on('data', onStdout);
                apps[file].stderr.on('data', onStderr);
            } else if(file.match(/\.py$/)) {
                winston.info('start: ' + file);
                apps[file] = child_process.spawn('/usr/bin/python', [ar + '/' + file]);
                apps[file].on('close', appClosed);
                apps[file].stdout.on('data', onStdout);
                apps[file].stderr.on('data', onStderr);
            }
        }
    }

    function appClosed(code, signal) {
        delete apps[file];
        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(typeof apps[file] != 'undefined') {
        winston.info('stop: ' + file + ' (pid: ' + apps[file].pid + ')');
        apps[file].kill('SIGKILL');
    }
}

@@ -2015,32 +2015,883 @@
 ta', onStderr);%0A
+            %7D else if(file.match(/%5C.ino$/)) %7B%0A                winston.info('start: ' + file);%0A                apps%5Bfile%5D = child_process.spawn('/usr/bin/make', %0A                    %5B%0A                        %22-f%22,%0A                        %22/var/lib/cloud9/extras/Userspace-Arduino/Makefile%22,%0A                        %22TARGET=%22 + file.replace(/%5C.ino$/, ''),%0A                        %22LOCAL_INO_SRCS=%22 + file,%0A                        %22LOCAL_C_SRCS=%22,%0A                        %22LOCAL_CPP_SRCS=%22,%0A                        %22LOCAL_PDE_SRCS=%22,%0A                        %22LOCAL_AS_SRCS=%22,%0A                        %22COMMON_DEPS=%22%0A                    %5D,%0A                    %7B 'cwd': ar %7D%0A                );%0A                apps%5Bfile%5D.on('close', appClosed);%0A                apps%5Bfile%5D.stdout.on('data', onStdout);%0A                apps%5Bfile%5D.stderr.on('data', onStderr);%0A
             %7D%0A  
@@ -2880,32 +2880,32 @@
 ;%0A            %7D%0A
-
         %7D%0A    %7D%0A
@@ -2996,20 +2996,20 @@
  == 'SIG
-KILL
+TERM
 ') setTi
@@ -3576,12 +3576,12 @@
 'SIG
-KILL
+TERM
 ');%0A

add Userspace-Arduino sketch support