/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.tools.headers;

import java.io.BufferedWriter;
import java.io.File;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.tools.FileProcessor;

public class HeaderFixer {
	static class HeaderFileProcessor extends FileProcessor {
		final String header;

		public HeaderFileProcessor () {
			header = new FileHandle("assets/licence-header.txt").readString();
			addInputSuffix(".java");
			setFlattenOutput(false);
			setRecursive(true);
		}

		@Override
		protected void processFile (Entry inputFile) throws Exception {
			String content = new FileHandle(inputFile.inputFile).readString();
			content = content.trim();
			if (content.startsWith("package")) {
				System.out.println("File '" + inputFile.inputFile + "' header fixed");
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileHandle(inputFile.outputFile).write(false)));
				writer.write(header + "\n\n" + content);
				writer.close();
			}
		}

		@Override
		protected void processDir (Entry inputDir, ArrayList<Entry> value) throws Exception {
		}
	}

	public static void process (String directory) throws Exception {
		HeaderFileProcessor processor = new HeaderFileProcessor();
		processor.process(new File(directory), new File(directory));
	}

	public static void main (String[] args) throws Exception {
		if (args.length != 1) {
			HeaderFixer.process("../../gdx/");
			HeaderFixer.process("../../backends/");
			HeaderFixer.process("../../tests/");
			HeaderFixer.process("../../extensions/");
		} else {
			HeaderFixer.process(args[0]);
		}
	}
}

@@ -1050,16 +1050,70 @@
 ixer %7B%0D%0A
+%09static int filesScanned;%0D%0A%09static int filesChanged;%0D%0A
 %09static 
@@ -1163,16 +1163,16 @@
 ssor %7B%0D%0A
-
 %09%09final 
@@ -1464,16 +1464,36 @@
 tion %7B%0D%0A
+%09%09%09filesScanned++;%0D%0A
 %09%09%09Strin
@@ -1558,26 +1558,20 @@
 ();%0D%0A%09%09%09
-content = 
+if (
 content.
@@ -1576,33 +1576,16 @@
 t.trim()
-;%0D%0A%09%09%09if (content
 .startsW
@@ -1675,24 +1675,45 @@
 r fixed%22);%0D%0A
+%09%09%09%09filesChanged++;%0D%0A
 %09%09%09%09Buffered
@@ -1820,24 +1820,26 @@
 (false)));%0D%0A
+%0D%0A
 %09%09%09%09writer.w
@@ -2522,20 +2522,105 @@
 s%5B0%5D);%0D%0A
-
 %09%09%7D%0D%0A
+%09%09System.out.println(%22Changed %22 + filesChanged + %22 / %22 + filesScanned + %22 files.%22);%0D%0A
 %09%7D%0D%0A%7D%0D%0A

update headerfixer