var Session = require('../lib/session');
var SessionStore = require('../server/session_store');

function savedTo(store, transform) {
    if (typeof store === 'string') store = new SessionStore(store);
    return function(session, done) {
        transform(session, function(err, session) {
            if (err) return done(err);
            store.save(session, done);
        });
    };
}

var async = require('async');
var path = require('path');
var config = require('../server/config');

var argv = require('minimist')(process.argv.slice(2), {
    alias: {
        i: 'input',
        o: 'output'
    },
    default: {
        input: path.join(config.var, 'sessions')
    }
});
var input = new SessionStore(argv.input);
var output = argv.output ? argv.output : input;
var transforms = argv._.map(function(transform) {
    return require(path.join(process.cwd(), transform));
});

var transform = savedTo(output, function transform(session, done) {
    session = new Session(session.getData());
    for (var n=transforms.length, i=0; i<n; i++) {
        var r = transforms[i](session);
        if (r) session = r;
    }
    done(null, session);
});

input.keys(function(err, keys) {
    if (err) return allDone(err);
    async.each(keys, function(key, keyDone) {
        input.load(key, function(err, session) {
            if (err) return keyDone(err);
            transform(session, keyDone);
        });
    }, allDone);
});

function allDone(err) {
    if (err) return console.error(err);
}

@@ -86,24 +86,141 @@
 n_store');%0A%0A
+function toAsync(transform) %7B%0A    return function(session, done) %7B%0A        done(null, transform(session));%0A    %7D;%0A%7D%0A%0A
 function sav
@@ -1002,133 +1002,305 @@
 var 
-transform = savedTo(output, function transform(session, done) %7B%0A    session = new Session(session.getData());%0A    for (
+steps = transforms.map(function(trans) %7B%0A    var transform = trans;%0A    if (!trans.async) transform = toAsync(transform);%0A    return function(session, done) %7B%0A        transform(session, function(err, r) %7B%0A            if (r) session = r;%0A            done(err, session);%0A        %7D);%0A    %7D;%0A%7D);%0A%0A
 var 
-n=
 tran
@@ -1308,131 +1308,206 @@
 form
-s.length, i=0; i%3Cn; i++) %7B%0A        var r = transforms%5Bi%5D(session);%0A        if (r) session = r;
+ = savedTo(output, function transform(session, done) %7B%0A    session = new Session(session.getData());%0A    async.series(steps.map(function(step) %7B
 %0A    
-%7D%0A
     
-done(null, session
+return step.bind(this, session);%0A    %7D), done
 );%0A%7D

make all transform steps async