var redis      = require("redis"),
    client     = redis.createClient(),
    listname   = process.argv[2];
    itemsToAdd = process.argv.splice(3),
    validLists = ["watchlist", "emaillist", "slackloglist", "slackalertlist"];

client.on("error", function (err) {
	console.log("Error " + err);
});

// taken from http://stackoverflow.com/questions/784012/javascript-equivalent-of-phps-in-array on 20150518 @ 19:50 EST
function inArray (needle, haystack) {
	var length = haystack.length;
	for(var i = 0; i < length; i++) {
		if(haystack[i] == needle) return true;
	}
	return false;
};

function addToList (listname, rawArgs, verbose) {
	rawArgs.forEach(function (val, index, array) {
		// skip the first two element ['node', 'add-to-list']
		if (index >= 2)
			itemsToAdd.push (val);
	});

	client.get (listname, function (err, reply) {
		if (reply != null) {
			var hasChanged = false;
			list = JSON.parse(reply.toString());

			itemsToAdd.forEach (function (val, index, array) {
				if (!inArray (val, list)) {
					if (verbose) console.log ("pushing " + val + " to array");

					list.push (val);
					hasChanged = true;
				} else {
					if (verbose) console.log ("'" + val + "' already in " + listname);
				}
			});

			if (hasChanged) {
				if (verbose) console.log ("pushing " + listname + " back to redis");
				client.set (listname, JSON.stringify (list), function (err, reply) {
					if (err != null) console.log ("ERROR:" + err);
					client.quit();
				});
			} else {
				client.quit();
			}
		} else {
			if (verbose) console.log ("adding first item(s) " + itemsToAdd + " to " + listname);
			client.set (listname, JSON.stringify (itemsToAdd), function (err, reply) {
				client.quit();
			});
		}
	});
};

if (inArray (listname, validLists)) {
	addToList (listname, itemsToAdd, true);
} else {
	console.log ("Error: '" + listname + "' is not a valid list name please use " + validLists);
	client.quit();
}
@@ -293,16 +293,86 @@
 );%0A%7D);%0A%0A
+// TODO: add/remove both use this same function, need to abstract out%0A
 // taken
@@ -1113,24 +1113,25 @@
 g (%22pushing 
+'
 %22 + val + %22 
@@ -1133,18 +1133,25 @@
  + %22
+'
  to 
-array%22
+%22 + listname
 );%0A%0A

add comment about common function in 2 files, update copy