"use strict";

var fs = require('fs'),
    path = require('path');

var baseDirectory = path.join(__dirname, '..', '..');
var relativeMigrationDirectory = path.join('db', 'migrations');
var absoluteMigrationDirectory = path.join(baseDirectory, relativeMigrationDirectory);

function padNumeral(numeral) {
    return Array(7 - numeral.toString().length).join('0') + numeral;
};

function makeNameFilename(migrationName) {
	return migrationName.replace(/\s+/g, '-');
};

exports.up = function(migrationName) {
};

exports.down = function(migrationName) {
};

exports.create = function(migrationName) {
    var migrationTemplate = [
        '"use strict"',
        '',
        'exports.up = function(mongoose, next) {',
        '    next();',
        '}',
        '',
        'exports.down = function(mongoose, next) {',
        '    next();',
        '}',
        ''
    ].join('\n');

    try {
        fs.mkdirSync(absoluteMigrationDirectory, parseInt('0775', 8));
    } catch (err) {
        // ignore error creating directory
    }

	var existingMigrationOrdinals = fs.readdirSync(absoluteMigrationDirectory).map(function(filename) {
        return parseInt(filename.match(/^(\d+)/)[1], 10);
    }).sort(function(a, b) {
        return a - b;	
	});
    
	var nextOrdinal = (existingMigrationOrdinals.pop() || 0) + 1;
    var fileName = padNumeral(nextOrdinal) + "-" + makeNameFilename(migrationName) + ".js";
	var absoluteFileName = path.join(absoluteMigrationDirectory, fileName);
	fs.writeFileSync(absoluteFileName, migrationTemplate, {mode: parseInt('0664', 8)});
	console.log("  Created Migration '" + path.join(relativeMigrationDirectory, fileName)); + "'";
};

    


@@ -1028,17 +1028,20 @@
 %0A    %7D%0A%0A
-%09
+    
 var exis
@@ -1242,17 +1242,20 @@
 a - b;%09%0A
-%09
+    
 %7D);%0A    
@@ -1255,17 +1255,20 @@
 );%0A    %0A
-%09
+    
 var next
@@ -1413,17 +1413,20 @@
  %22.js%22;%0A
-%09
+    
 var abso
@@ -1489,17 +1489,20 @@
 eName);%0A
-%09
+    
 fs.write
@@ -1577,17 +1577,20 @@
 , 8)%7D);%0A
-%09
+    
 console.

fix indentation