// wuw server
'use strict';

// import packages (api)
var express = require('express');
var expressValidator = require('express-validator');
var bodyParser = require('body-parser');
var util = require('util');
var mongoose = require('mongoose');
var morgan = require('morgan');
var https = require('https');
var fs = require('fs');
var schedule = require('node-schedule');

// import parser modules
var parser = {
    lsf: require('./parser_xml'),
    mensa: require('./parser_mensa'),
    news: require('./parser_news'),
    events: require('./parser_events')
};


// api version & url
var apiVersion = 0;
var apiBaseUrl = '/api/v' + apiVersion;


// ssl
try {
    var use_ssl = true;
    var key = fs.readFileSync('./ssl.key');
    var cert = fs.readFileSync('./ssl.crt');
    var dhparam = fs.readFileSync('./dh_2048.pem');
    // load passphrase from file
    var pass = require('./ssl-pass');
    var https_options = {
        key: key,
        cert: cert,
        passphrase: pass.passphrase,
        dhparam: dhparam,
        ciphers: [
            'ECDHE-RSA-AES256-SHA384',
            'DHE-RSA-AES256-SHA384',
            'ECDHE-RSA-AES256-SHA256',
            'DHE-RSA-AES256-SHA256',
            'ECDHE-RSA-AES128-SHA256',
            'DHE-RSA-AES128-SHA256',
            'HIGH',
            '!aNULL',
            '!eNULL',
            '!EXPORT',
            '!DES',
            '!RC4',
            '!MD5',
            '!PSK',
            '!SRP',
            '!CAMELLIA'
        ].join(':'),
        honorCipherOrder: true
    };
} catch(err) {
    if (err) { use_ssl = false; }
}


// mongodb
var mongohost='localhost:27017';
var mongodb=process.env.WUWDB || 'wuw';
var mongoConnection='mongodb://' + mongohost + '/' + mongodb;
// connect
if(mongoose.connection.readyState === 0) {
    mongoose.connect(mongoConnection);
}

// create models from our schemas
var Lecture = require('./models/model_lecture');
var Deadline = require('./models/model_deadline');

// create the express app, router & configure port
var app = express();
var router = express.Router();
var apiPort = process.env.WUWPORT || 4342;

// use morgan to log
app.use(morgan('dev'));

// configure body parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(expressValidator());

// allow cross origin resource sharing
app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();
});

// middleware to use for all requests
//router.use(function(req, res, next) {  next(); });

// api home route (GET /$apiBaseUrl)
router.get('/', function(req, res) {
    res.status(200).json({ message: 'welcome to the wuw api v' + apiVersion });
    res.end();
});

// register our routes & routers
router.use('/news', require('./routes/news'));
router.use('/events', require('./routes/events'));
router.use('/lectures', require('./routes/lectures'));
router.use('/deadlines', require('./routes/deadlines'));
router.use('/rooms', require('./routes/rooms'));
router.use('/groups', require('./routes/groups'));
router.use('/dishes', require('./routes/dishes'));
router.use('/users', require('./routes/users'));
router.use('/printers', require('./routes/printers'));
// register base & default router
app.use(apiBaseUrl, router);


// catch-all route to handle 404 errors
app.get('*', function(req, res, next) {
    var err = new Error();
    err.status = 404;
    err.message = "404 Not Found";
    next(err);
});

// handle error
app.use(function(err, req, res, next) {
    console.error({
        status: err.status,
        message: err.message,
        stack: err.stack.split('\n')
    });
    res.status(err.status || 500).send({ error: err.message });
});


// schedule parser jobs
var jobs = [
    // parser_lsf
    schedule.scheduleJob('45 4 * * *', function(){
        parser.lsf.startParser();
    }),
    // parser_mensa
    schedule.scheduleJob('35 4 * * *', function(){
        parser.mensa.startParser();
    }),
    // parser_news
    schedule.scheduleJob('25 4 * * *', function(){
        parser.news.startParser();
    }),
    // parser_events
    schedule.scheduleJob('15 4 * * *', function(){
        parser.events.startParser();
    })
];


// start the server
console.log('\n* starting the wuw api\n');
console.log('  mongodb:  ' + mongoConnection);
console.log('  ssl:      ' + use_ssl);
if (use_ssl) {
    var server = https.createServer(https_options, app).listen(apiPort);
    console.log('  url:      https://localhost:' + apiPort + apiBaseUrl);
} else {
    var server = app.listen(apiPort);
    console.log('  url:      http://localhost:' + apiPort + apiBaseUrl);
}
console.log();


// starter & stopper functions for testing
var startApi = function() {
    if (!server) { server = app.listen(apiPort); }
};
var stopApi = function() {
    server.close();
};


// export functions
module.exports = { startApi: startApi, stopApi: stopApi };

@@ -641,16 +641,76 @@
 rsion;%0A%0A
+// write pid%0Afs.writeFile(%22/tmp/wuw-api.pid%22, process.pid);%0A
 %0A// ssl%0A

write pidfile