'use strict';

/**
 * Module dependencies
 */
var express = require('express');
var async = require('async');
var ical = require('ical');

var config = require('./config/config.js');

var modules = [].concat(
    config.modules.lessons.letters,
    config.modules.lessons.numbers,
    config.modules.projets,
    config.modules.seminaires.insa,
    config.modules.seminaires.huma,
    config.modules.seminaires.entreprises,
    config.modules.seminaires.etrangers,
    config.modules.seminaires.facultatifs
);


var app = express();

app.set('port', process.env.PORT || 3000);

app.use(express.static(__dirname + '/public'));

app.get('/lessons', function(req, res) {
    res.json(config.modules);
    res.end();
});

app.get('/calendar', function(req, res) {
    var icalGenerator = require('ical-generator')();

    icalGenerator.setName(config.name);
    icalGenerator.setDomain(config.domain);
    icalGenerator.setProdID(config.prodID);

    var patterns = config.modules.common;

    async.each(modules, function(module, callback) {
        if (req.param(module.urlParam)) {
            patterns.push(module.pattern);
        }
        callback();
    }, function() {
        ical.fromURL(config.url, {}, function(err, events) {
            // Loop on all events and add only lessons in modules
            async.each(Object.keys(events), function(k, callback) {
                var ev = events[k];
                async.each(patterns, function(pattern, cb) {

                    // Test if the lesson is in modules
                    if (ev.summary.match(pattern) !== null) {
                        cb(true);
                    } else {
                        cb();
                    }
                }, function(result) {
                    if (result) {
                        icalGenerator.addEvent(ev);
                    }
                    callback();
                });
            }, function() {
                res.send(icalGenerator.toString());
                res.end();
            });
        });
    });
});


/**
 * Start Server
 */
app.listen(app.get('port'), function() {
    console.log('Express server listening on port ' + app.get('port'));
});

@@ -130,16 +130,69 @@
 'ical');
+%0Avar icalGeneratorModule = require('ical-generator');
 %0A%0Avar co
@@ -830,39 +830,29 @@
 rator = 
-require('
 ical
--g
+G
 enerator
 ')();%0A%0A 
@@ -843,18 +843,22 @@
 enerator
-')
+Module
 ();%0A%0A   
@@ -1023,16 +1023,25 @@
 s.common
+.slice(0)
 ;%0A%0A    a

Fix problem of separation of request.