#!/usr/bin/env node

var fs = require('fs-sync');
var node_path = require('path');

var USER_HOME = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];

// get user profile from
// 		~/.cortexrc.js  // require as an module
//		~/.cortexrc		// read as an JSON file	
function user_options() {
	var user_profile_file = node_path.join( USER_HOME, '.cortexrc' );
	var user_profile_file_js = user_profile_file + '.js';

	if( fs.exists( user_profile_file_js ) ){
		try {
			return require( user_profile_file_js );
		} catch(e) {
			process.stdout.write('Failed to load "' + user_profile_file_js + '"\n');
			throw e;
			// process.exit(1);
		}
	}

	if( fs.exists( user_profile_file ) ){
		try {
			return fs.readJSON( user_profile_file );
		} catch(e) {
			process.stdout.write('Failed to load "' + user_profile_file + '"\n');
			throw e;
		}
	}

	return {};
};


var cortex_root = node_path.join(USER_HOME, '.cortex');
var DEFAULT_OPTIONS = {
	cortex_root	: cortex_root,
	module_root	: node_path.join(cortex_root, 'modules'),
	built_root	: node_path.join(cortex_root, 'built_modules'),
	temp_root	: node_path.join(cortex_root, 'tmp'),
	registry	: 'http://registry.npmjs.org'
};

var options = exports.OPTIONS = user_options();
var option;

for(option in DEFAULT_OPTIONS){
	if( !( option in options ) ){
		options[option] = DEFAULT_OPTIONS[option];
	}
}

exports.option = function(key) {
    return options[key];
};

// TODO
// dir checking










@@ -191,18 +191,24 @@
 from%0A// 
-%09%09
+        
 ~/.corte
@@ -241,18 +241,24 @@
 odule%0A//
-%09%09
+        
 ~/.corte
@@ -260,18 +260,24 @@
 cortexrc
-%09%09
+        
 // read 
@@ -291,17 +291,20 @@
 SON file
-%09
+    
 %0Afunctio
@@ -322,17 +322,20 @@
 ons() %7B%0A
-%09
+    
 var user
@@ -392,17 +392,20 @@
 xrc' );%0A
-%09
+    
 var user
@@ -451,17 +451,20 @@
 '.js';%0A%0A
-%09
+    
 if( fs.e
@@ -492,35 +492,50 @@
 ile_js ) )%7B%0A
-%09%09try %7B%0A%09%09%09
+        try %7B%0A            
 return requi
@@ -558,26 +558,32 @@
 _file_js );%0A
-%09%09
+        
 %7D catch(e) %7B
@@ -575,35 +575,44 @@
    %7D catch(e) %7B%0A
-%09%09%09
+            
 process.stdout.w
@@ -672,52 +672,82 @@
 ');%0A
-%09%09%09throw e;%0A%09%09%09// process.exit(1);%0A%09%09%7D%0A%09%7D%0A%0A%09
+            throw e;%0A            // process.exit(1);%0A        %7D%0A    %7D%0A%0A    
 if( 
@@ -784,19 +784,34 @@
  )%7B%0A
-%09%09try %7B%0A%09%09%09
+        try %7B%0A            
 retu
@@ -847,18 +847,24 @@
 file );%0A
-%09%09
+        
 %7D catch(
@@ -868,19 +868,28 @@
 ch(e) %7B%0A
-%09%09%09
+            
 process.
@@ -954,29 +954,50 @@
 ');%0A
-%09%09%09throw e;%0A%09%09%7D%0A%09%7D%0A%0A%09
+            throw e;%0A        %7D%0A    %7D%0A%0A    
 retu
@@ -1088,17 +1088,20 @@
 ONS = %7B%0A
-%09
+    
 cortex_r
@@ -1103,17 +1103,20 @@
 tex_root
-%09
+    
 : cortex
@@ -1122,17 +1122,20 @@
 x_root,%0A
-%09
+    
 module_r
@@ -1133,25 +1133,28 @@
  module_root
-%09
+    
 : node_path.
@@ -1183,17 +1183,20 @@
 ules'),%0A
-%09
+    
 built_ro
@@ -1193,25 +1193,28 @@
   built_root
-%09
+    
 : node_path.
@@ -1249,17 +1249,20 @@
 ules'),%0A
-%09
+    
 temp_roo
@@ -1262,17 +1262,20 @@
 emp_root
-%09
+    
 : node_p
@@ -1308,17 +1308,20 @@
 '),%0A
-%09
+    
 registry
 %09: '
@@ -1316,17 +1316,20 @@
 registry
-%09
+    
 : 'http:
@@ -1447,17 +1447,20 @@
 TIONS)%7B%0A
-%09
+    
 if( !( o
@@ -1485,10 +1485,16 @@
  )%7B%0A
-%09%09
+        
 opti
@@ -1536,9 +1536,12 @@
 n%5D;%0A
-%09
+    
 %7D%0A%7D%0A

fix tab indent