/*
 *   
 *
 * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation. 
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt). 
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA 
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions. 
 */

package tests;

import java.io.*;
import java.util.*;
import util.*;

/**
 * Find the .java and .jasm source files for the unix tests.
 */
public class CollectTestCases {
    static void usage() {
        System.out.println("Usage: java -jar buildtool.jar " +
                           "testcases <workspacedir> <outputdir>");
    }
    static File workspacedir;
    static File outputdir;
    static Vector sourcefiles;
    static PrintWriter pw;
    static Hashtable env;

    public static void main(String args[]) throws Throwable {
        if (args.length != 2) {
            usage();
            System.exit(1);
        }
        workspacedir = new File(args[0]);
        outputdir = new File(args[1]);

	env = Util.getenv();

        if (!workspacedir.isDirectory()) {
            usage();
            System.exit(1);
        }

        sourcefiles = listSourceFiles(workspacedir);
        FileOutputStream out = 
            new FileOutputStream(new File(outputdir, "testcases.make"));
        pw = new PrintWriter(new OutputStreamWriter(out));

        writeHeader();
        writeList("TEST_JAVA_SRCS",    null,  null, ".java");
        writeList("TEST_JASM_SRCS",    null,  null, ".jasm");

        pw.close();
    }

    static Vector listSourceFiles(File dir) {
        Vector v = new Vector();
        listSourceFiles(v, new File(dir, "src/tests"));
        return v;
    }

    static void listSourceFiles(Vector list, File dir) {
        String children[] = dir.list();
        if (children == null) {
            return;
        }
        for (int i=0; i<children.length; i++) {
            String child = children[i];
            if (child.equals("SCCS")) {
                continue;
            }
            if (child.equals("CVS")) {
                continue;
            }
            if (child.equals(".")) {
                continue;
            }
            if (child.equals("..")) {
                continue;
            }
	    if (checkSkip(child)) {
		continue;
	    }
            File c = new File(dir, child);
            if (c.isDirectory()) {
                listSourceFiles(list, c);
            } else {
                list.addElement(c.getAbsolutePath());
            }
        }
    }

    static boolean checkSkip(String s) {
        String value = (String)env.get("ENABLE_ISOLATES");
	if (value == null || !value.equals("true")) {
	    if (s.indexOf("isolate") != -1) {
		return true;
	    }
	    if (s.indexOf("dynamic_natives") != -1) {
		return true;
	    }
	    if (s.indexOf("Isolate") != -1) {
		return true;
	    }
	    if (s.indexOf("MShLib") != -1) {
		return true;
	    }
	} else {
            if (s.indexOf("SShLib") != -1) {
		return true;
	    }
        }

        value = (String)env.get("ENABLE_LIB_IMAGES");
	if (value == null || !value.equals("true")) {
	    if (s.indexOf("ShLib") != -1) {
		return true;
	    }
	}

        value = (String)env.get("ENABLE_MULTIPLE_PROFILES_SUPPORT");
	if (value == null || !value.equals("true")) {
	    if (s.indexOf("MVMProfileTest") != -1) {
                return true;
	    }
	    if (s.indexOf("MVMRestrictedTest") != -1) {
                return true;
	    }
        }
	return false;
    }

    static void writeHeader() {
        pw.println("# This file is auto-generated by ");
        pw.println("# $(JVMWorkSpace)/src/tools/buildtool/tests/" +
                      "CollectTestCases.java.");
        pw.println("# Do not edit!");
        pw.println();
        pw.println();
        pw.println();
    }
    
    /**
     * Write a list of files in Makefile format. An example output is
     * <pre>
     * TESTS_JAVA_SRCS = \
     *     e:/cldcvm/src/tests/Sanity.java \
     *     e:/cldcvm/src/tests/Foo.java
     * </pre>
     *
     * @param includePattern if non-null, a file in the <i>list</i>
     *                       won't be printed if the file's pathname
     *                       does not contain <i>includePattern</i> as
     *                       a substring.
     * @param excludePattern if non-null, a file in the <i>list</i>
     *                       won't be printed if the file's pathname
     *                       contains <i>excludePattern</i> as
     *                       a substring.
     * @param suffix         if non-null, a file in the <i>list</i>
     *                       won't be printed if the suffix of the file
     *                       is not <i>suffix</i>.
     */
    static void writeList(String varName, 
                          String includePattern, String excludePattern,
                          String suffix) throws Throwable {
        /*
         * Note: the list of source files are written into two places:
         *
         * [1] in testcases.make, as a Makefile variable
         * [2] in the file <varName>.lst, such that the list of source files
         *     may be passed to javac as @<varName>.lst.
         *
         * The main reason for having <varName>.lst is Windows may not
         * be able to handle too many source files if we list all of them
         * separately in the command-line.
         */
        FileOutputStream out = 
            new FileOutputStream(new File(outputdir, varName + ".lst"));
        PrintWriter pw2 = new PrintWriter(new OutputStreamWriter(out));

        pw.print(varName + " =");
        for (int i=0; i<sourcefiles.size(); i++) {
            String path = (String)sourcefiles.elementAt(i);

            if (includePattern != null) {
                if (path.indexOf(includePattern) == -1) {
                    continue;
                }
            }
            if (excludePattern != null) {
                if (path.indexOf(excludePattern) != -1) {
                    continue;
                }
            }
            if (suffix != null) {
                if (!path.endsWith(suffix)) {
                    continue;
                }
            }
            pw.print(" \\");
            pw.println();
            pw.print(" ");
            for (int j=0; j<path.length(); j++) { 
                char c = path.charAt(j);
                if (c == '\\') {
                    pw.print("/");
                    pw2.print("/");
                } else {
                    pw.print(c);
                    pw2.print(c);
                }
            }
            pw2.println();
        }
        pw.println();
        pw.println();
        pw2.close();
    }
}
