"use strict";

var chalk = require('chalk');
var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
var sys = require('sys');
var exec = require('child_process').exec;
var fs = require('fs');
var request = require('request');
var tempWrite = require('temp-write');

console.log(chalk.magenta("[UPDATER] Checking for updates..."));
fs.readFile('./version.txt', 'utf-8', function(err, data) {
	if (err) {
		console.error(err);
		process.exit();
	}
	request('https://raw.githubusercontent.com/LifeMushroom/Modular-Node.js-IRC-Bot/master/version.txt', function(error, response, body) {
	if (!error && response.statusCode == 200) {
		if (body > data) {
			console.log(chalk.magenta("[UPDATER]" + " Update Found!"));
			rl.question(chalk.magenta("Update? [yes]/no: "), function(answer) {
				if (answer == 'yes') {
					fs.readFile('./config.js', 'utf-8', function (err, data) {
						if (err) {
							console.error(err);
							process.exit();
						}
						var tempconfig = tempWrite.sync(data);
						exec("git pull");
						fs.writeFile('./old_config.js', fs.readFileSync(tempconfig, 'utf-8'), function (err) {
							if (err) {
								console.error(err);
								process.exit();
							}
							console.log(chalk.magenta("[UPDATER] Successfully updated! Your new configuration's name is \"config_old.js\"."));
							console.log(chalk.magenta("[UPDATER] Please check your \"config.js\" to add the new features to your \"config_old.js\" and then rename your \"config_old.js\" to \"config.js\""));
							console.log(chalk.magenta("[UPDATER] Please re-run your bot when you are ready!"));
							process.exit();
						});
					});
				} else {
					console.log(chalk.magenta("[UPDATER] Update Aborted"));
				}
			});
		} else {
			console.log(chalk.magenta("[UPDATER] No updates found... Booted up!"));
		}
	} else {
		console.error(err);
	}
});
});

@@ -591,27 +591,28 @@
 /master/
-version.txt
+package.json
 ', funct
@@ -700,20 +700,40 @@
 (body %3E 
-data
+JSON.parse(data).version
 ) %7B%0D%0A%09%09%09

Read from package.json, no longer use version.txt