package tools;

import java.net.InetAddress;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * A program that fixes all the localhost indexes in the rtb4free,com web site
 * 
 * @author Ben M. Faul
 *
 */
public class WebPatch {
	/** List of files to modify */
	static List<String> files = new ArrayList();
	static {
		files.add("database.json");
		files.add("stub.json");
		files.add("Campaigns/extendedDevice-test.json");
		files.add("Campaigns/payday.json");
		files.add("Campaigns/README.md");
		files.add("Campaigns/rtbfree-payday.json");
		files.add("Campaigns/Source.txt");
		files.add("web/login.html");
		files.add("web/exchange.html");
		files.add("XXXwww/admarkup.html");
		files.add("XXXwww/blog_link.html");
		files.add("XXXwww/index.html");
		files.add("XXXwww/video-sample.html");
		files.add("XXXwww/banner-sample.html");
		files.add("XXXwww/details.html");
		files.add("XXXwww/faq.html");
		files.add("XXXwww/vast.html");
		files.add("XXXwww/elastic.html");
		files.add("XXXwww/integration.html");
		files.add("XXXwww/privatex/x_index.html");
		files.add("XXXwww/privatex/x_details.html");
	}

	public static void main(String[] args) throws Exception {
		boolean write = false;
		WebPatch p = new WebPatch();
		String fix = "";
		String address = "rtb4free.com";

		int i = 0;
		while (i < 0) {
			switch (args[i]) {
			case "-www":
				fix = args[++i];
				i++;
				break;
			case "-address":
				address = args[++i];
				i++;
				break;
			default:
				System.err.println("Huh? " + args[i]);
				;
				return;
			}
		}

		String computername = InetAddress.getLocalHost().getHostName();
		System.out.println("System Name = " + computername);
		if (computername.contains("ben") == false) {
			write = true;
			System.out.println("*** NOTE *** FILES WILL BE MODIFIED ***");
		} else {
			System.out.println("*** NO FILES WILL BE MODIFIED HERE ***");
		}
		for (String file : files) {
			file = file.replace("XXX", fix);
			String content = null;
			try {
				content = new String(Files.readAllBytes(Paths.get(file)));
				StringBuilder sb = new StringBuilder(content);
				int k = p.perform("localhost", address, sb);
				if (write)
					Files.write(Paths.get(file), sb.toString().getBytes());
				System.out.println(file + " had " + k + " replacements.");
			} catch (Exception error) {
				System.out.println(file + " does not exist, SKIPPED...");
			}
		}

	}

	public int perform(String from, String to, StringBuilder sb) {
		int k = 0;
		while (patch(from, to, sb)) {
			k++;
		}
		return k;
	}

	public boolean patch(String from, String to, StringBuilder sb) {
		int index = sb.indexOf(from);
		if (index > -1) {

			sb.replace(index, index + from.length(), to);
			return true;

		}
		return false;
	}
}

@@ -1327,20 +1327,8 @@
  = %22
-rtb4free.com
 %22;%0A%0A
@@ -1561,22 +1561,16 @@
 %5D);%0A%09%09%09%09
-;%0A%09%09%09%09
 return;%0A
@@ -1569,24 +1569,133 @@
 return;%0A%09%09%09%7D
+%0A%09%09%7D%0A%09%09%0A%09%09if (address.length() ==0) %7B%0A%09%09%09System.err.println(%22You must specify at least -address%22);%0A%09%09%09return;
 %0A%09%09%7D%0A%0A%09%09Stri

Remove default address