var fs = require('fs'),
    hapi = require('hapi'),
    pg = require('pg'),
    boom = require('boom'),
    pg_copy = require('pg-copy-streams'),
    reformatCsv = require('./lib/reformat-csv');

var user = process.env.DBUsername;
var password = process.env.DBPassword;
var address = process.env.DBAddress;
var database = process.env.Database;

// on RDS, how do I set a security group?
    // or whitelist this instance or something, somehow
    // this must not happen in the main app, probably in install somewhere

var conString = 'postgres://' +
    user + ':' +
    password + '@' +
    address + '/' +
    database;

var server = new hapi.Server();

server.connection({
    port: 8000
});

server.route({
    method: 'GET',
    path:'/stuff',
    handler: function(request, reply) {
        reply('between the bars');
    }
});

server.route({
    method: 'GET',
    path: '/status',
    handler: function(request, reply) {
        reply('ok');
    }
});

server.route({
    method: 'POST',
    path: '/csv',
    config: {
        payload: {
            maxBytes: 200000000,
            output: 'stream',
            parse: true,
            allow: 'multipart/form-data'
        }
    },
    handler: function(request, reply) {
        var data = request.payload;

        if (data.file) {
            var name = data.file.hapi.filename;

            // just looking at the extension for now
            if (name.slice(-4) != '.csv') {
                return reply(boom.badRequest('.csv files only'));
            }

            var path = (process.env.UploadPath || '/mnt/uploads');
            if (path[path.length-1] !== '/') path = path + '/';

            var file = fs.createWriteStream(path + name);

            file.on('error', function (err) {
                reply(boom.badRequest(err));
            });

            data.file.pipe(file);

            data.file.on('end', function (err) {
                reformatCsv(path, path + name, function(err, filename) {
                    if (err) {
                        fs.unlink(path + name, function() {
                            reply(boom.badRequest(err));
                        });
                    } else {
                        // return reply('successfully uploaded');
                        // COPY to postgres

                        // check if the table already exists
                        // if it doesn't just write it
                            // if it does, create a unique id, keep it there and make a process for renaming the table eventually

                        console.log(conString);

                        pg.connect(conString, function(err, client, done) {
                            if (err) return reply(boom.badRequest(err));

                            client.query('create table if not exists temp (key varchar(255), value text);', function(err, results) {
                                if (err) return reply(boom.badRequest(err));
                            });

                            var stream = client.query(pg_copy.from('COPY temp FROM STDIN (format csv);'));

                            var fileStream = fs.createReadStream(filename);

                            fileStream.on('error', function(err) {
                                return reply(boom.badRequest(err));
                            }).pipe(stream)
                                .on('finish', function() {
                                    return reply('ok');
                                })
                                .on('error', function(err) {
                                    return reply(boom.badRequest(err));
                                });
                        });

                    }
                });
            });

        }
    }
});

server.start();

// soooo, we need server side user auth
    // in order to restrict uploads to specific users
    // just use an env var for first pass?
        // require a cooldown
    // every POST must then

// first pass, upload a csv
    // retructure into k/v, hash, full object as string
    // later we can pull special columns, latitude, longitude, _country
        // what does the editor geocoder do? ffforest?

// curl -i -F name=something -F file=@with-cats.csv http://localhost:8000/csv

@@ -648,16 +648,33 @@
 erver();
+%0Avar port = 8000;
 %0A%0Aserver
@@ -697,20 +697,20 @@
   port: 
-8000
+port
 %0A%7D);%0A%0Ase
@@ -1149,50 +1149,8 @@
 true
-,%0A            allow: 'multipart/form-data'
 %0A   
@@ -1408,34 +1408,16 @@
  '.csv')
- %7B%0A               
  return 
@@ -1458,30 +1458,16 @@
 only'));
-%0A            %7D
 %0A%0A      
@@ -2600,24 +2600,111 @@
 nt, done) %7B%0A
+                            // why does this not catch basic non-auth errors from rds?%0A
             
@@ -3111,25 +3111,24 @@
 t csv);'));%0A
-%0A
             
@@ -3230,16 +3230,49 @@
 leStream
+%0A                                
 .on('err
@@ -3284,32 +3284,36 @@
 function(err) %7B%0A
+    
                 
@@ -3392,18 +3392,55 @@
         
-%7D)
+    %7D)%0A                                
 .pipe(st
@@ -3473,24 +3473,28 @@
             
+    
 .on('finish'
@@ -3536,32 +3536,36 @@
                 
+    
 return reply('ok
@@ -3564,24 +3564,28 @@
 eply('ok');%0A
+    
             
@@ -3631,32 +3631,36 @@
                 
+    
 .on('error', fun
@@ -3664,32 +3664,36 @@
 function(err) %7B%0A
+    
                 
@@ -3740,32 +3740,36 @@
 dRequest(err));%0A
+    
                 
@@ -3910,16 +3910,72 @@
 r.start(
+function() %7B%0A    console.log('started on port', port);%0A%7D
 );%0A%0A// s

Clean up a bit