var fs = require('fs');
var child_process = require('child_process');
var request = require('request');
var Promise = require('bluebird');
var qs = require('querystring')

var env = process.env.NODE_ENV;

if(!env){
	console.error("Please set NODE_ENV variable.");
	return 1;	
} 

var VIEWSDIR = 'views/';

const getConfigFile = function (env, base_directory) {
  try {
    // Try to load the user's personal configuration file
    return require(base_directory + '/conf.my.' + env + '.json');
  } catch (e) {
    // Else, read the default configuration file
    return require(base_directory + '/conf.' + env + '.json');
  }
};

const getAllProviders = function(conf){
	var temp = [];
	for(var servercodename in conf.couchdblist){
    	var couchdbparams = conf.couchdblist[servercodename];
    	temp.push(couchdbparams.couchdb + couchdbparams.database);
    }
	return temp;
}

const createDB = function(name){
	return new Promise(function(resolve, reject){
		request.put(name, function(err, res, body){
			if(err) reject(err.message);
			try{
				if(JSON.parse(body).error === "not_found"){
					request.put(name, function(err, res, body){
						resolve(JSON.parse(body));
					});
				}else{
					resolve(JSON.parse(body));
				}
			}catch(e){
				console.error(name);
				console.error(e);
				reject(e);
			}
			
		})
	});
}

const updateDocuments = function(db, viewsdir){

	var fileviews = fs.readdirSync(viewsdir); 

	var testView = function(file){
		return new Promise(function(resolve, reject){
			try{
				if(file.indexOf(".json") === -1) reject(file);

				fs.readFile(viewsdir+file, function (err, data) {
					if (err) throw err;
					
					var designdoc = JSON.parse(data);

					var options = {
						uri: db + "/" + designdoc._id
					}

					request(options, function(err, res, body){
						var couchdesigndoc = body;

				  		if(JSON.stringify(designdoc.views) !== JSON.stringify(couchdesigndoc.views)){

				  			var uri = db + "/" + designdoc._id;
				  			if(couchdesigndoc._rev){
				  				designdoc._rev = couchdesigndoc._rev;
				  				uri += "?rev="+designdoc._rev;
				  			}
					  		
					  		var options = {
								uri :  uri,
								method : 'PUT',
								json : designdoc
							}

							request(options, function(err, res, body){
								if(err) reject(err.message);
								resolve(body);
							});

					  	}else{
				  			console.log("No changes in document: " + viewsdir+file + ", in db: " + db);
				  		}
					});
				});
			}catch(e){
				reject(e);
			}
		});
	}

	return Promise.map(fileviews, testView);
}

var conf = getConfigFile(env, "../");
var dataprovider = conf.dataproviders[conf.default.dataprovider];
var clusterdb = dataprovider.hostname + "/" + dataprovider.database;

createDB(clusterdb)
.then(function(result){
	return updateDocuments(clusterdb, VIEWSDIR);
})
.then(console.log)
.catch(console.error)


@@ -163,16 +163,45 @@
 string')
+;%0Avar path = require('path');
 %0A%0Avar en
@@ -303,35 +303,8 @@
 ;%09%0A%7D
- %0A%0Avar VIEWSDIR = 'views/';
 %0A%0Aco
@@ -2548,16 +2548,61 @@
 ew);%0A%7D%0A%0A
+var basedir = path.dirname(process.argv%5B1%5D);%0A
 var conf
@@ -2765,16 +2765,52 @@
 atabase;
+%0Avar viewsdir = basedir + '/views/';
 %0A%0Acreate
@@ -2886,16 +2886,16 @@
 db, 
-VIEWSDIR
+viewsdir
 );%0A%7D

Change the views dir to use complete path