#!/usr/bin/env node

var generateShader = function generateShader (args) {
    var ruleString = args[0] || null,
        outOfBoundValue = args[1] || 'wrap';

    outOfBoundValue = outOfBoundValue == 'wrap' || outOfBoundValue == 'clamp' ? outOfBoundValue : parseInt(outOfBoundValue, 10);

    console.log(generator(ruleString, outOfBoundValue));
};

var extractInformation = function extractInformation (content) {
    // not critical, dumb regular expressions will do
    var regexVersion = new RegExp('^/\\*\\*[\\S\\s]+cellular-automata-voxel-shader ([0-9.]+)[\\S\\s]+\\*/', 'mg'),
        regexRuleString = new RegExp('^/\\*\\*[\\S\\s]+Rule : (.+)[\\S\\s]+\\*/', 'mg'),
        regexOutOfBound = new RegExp('^/\\*\\*[\\S\\s]+Out of bound value : (.+)[\\S\\s]+\\*/', 'mg'),
        versionMatch = regexVersion.exec(content),
        ruleStringMatch = regexRuleString.exec(content),
        outOfBoundMatch = regexOutOfBound.exec(content);

    return {
        version : versionMatch ? versionMatch[1] : null,
        ruleString : ruleStringMatch ? ruleStringMatch[1] : null,
        outOfBoundValue : outOfBoundMatch ? outOfBoundMatch[1] : null,
        valid : versionMatch && ruleStringMatch && outOfBoundMatch
    };
};

var getInformation = function getInformation (args) {
    var fs = require('fs'),
        filename = args[1] || null,
        content,
        information;

    if (filename) {
        try {
            content = fs.readFileSync(filename).toString();
        } catch (e) {
            content = null;
        }

        information = extractInformation(content);

        if (information.valid) {
            console.log(filename + ' : ' + information.ruleString + ' : ' + information.outOfBoundValue + ' : generated by cavoxelshader ' + information.version);
        } else {
            console.log(filename + ' was not generated by cavoxelshader');
        }
    }
};

var generator = require('./../'),
    args = process.argv.slice(2);

if (args[0] === 'info') {
    getInformation(args);
} else {
    generateShader(args);
}

#!/usr/bin/env node

var fs = require('fs');

var generateShader = function generateShader (args) {
    var ruleString = args[0] || null,
        outOfBoundValue = args[1] || 'wrap';

    outOfBoundValue = outOfBoundValue == 'wrap' || outOfBoundValue == 'clamp' ? outOfBoundValue : parseInt(outOfBoundValue, 10);

    console.log(generator(ruleString, outOfBoundValue));
};

var extractInformation = function extractInformation (content) {
    // not critical, dumb regular expressions will do
    var regexVersion = new RegExp('^/\\*\\*[\\S\\s]+cellular-automata-voxel-shader ([0-9.]+)[\\S\\s]+\\*/', 'mg'),
        regexRuleString = new RegExp('^/\\*\\*[\\S\\s]+Rule : (.+)[\\S\\s]+\\*/', 'mg'),
        regexOutOfBound = new RegExp('^/\\*\\*[\\S\\s]+Out of bound value : (.+)[\\S\\s]+\\*/', 'mg'),
        versionMatch = regexVersion.exec(content),
        ruleStringMatch = regexRuleString.exec(content),
        outOfBoundMatch = regexOutOfBound.exec(content);

    return {
        version : versionMatch ? versionMatch[1] : null,
        ruleString : ruleStringMatch ? ruleStringMatch[1] : null,
        outOfBoundValue : outOfBoundMatch ? outOfBoundMatch[1] : null,
        valid : versionMatch && ruleStringMatch && outOfBoundMatch
    };
};

var displayInformation = function displayInformation (filename) {
    var content,
        information;

    try {
        content = fs.readFileSync(filename).toString();
    } catch (e) {
        content = null;
    }

    information = extractInformation(content);

    if (information.valid) {
        console.log(filename + ' : ' + information.ruleString + ' : ' + information.outOfBoundValue + ' : generated by cavoxelshader ' + information.version);
    } else {
        console.log(filename + ' : not generated by cavoxelshader');
    }
};

var infoCommand = function infoCommand (args) {
    var filename = args[1] || null;

    if (filename) {
        displayInformation(filename);
    } else {
        fs.readdirSync('./').map(function(file) {
            if (file.indexOf('.') !== 0 && fs.lstatSync(file).isFile()) {
                displayInformation(file);
            }
        });
    }
};

var generator = require('./../'),
    args = process.argv.slice(2);

if (args[0] === 'info') {
    infoCommand(args);
} else {
    generateShader(args);
}

Change info command to list all the files in the current folder when used without filename