/* Copyrights and Licenses
 *
 * This product includes Hypersonic SQL.
 * Originally developed by Thomas Mueller and the Hypersonic SQL Group.
 *
 * Copyright (c) 1995-2000 by the Hypersonic SQL Group. All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 *     -  Redistributions of source code must retain the above copyright notice, this list of conditions
 *         and the following disclaimer.
 *     -  Redistributions in binary form must reproduce the above copyright notice, this list of
 *         conditions and the following disclaimer in the documentation and/or other materials
 *         provided with the distribution.
 *     -  All advertising materials mentioning features or use of this software must display the
 *        following acknowledgment: "This product includes Hypersonic SQL."
 *     -  Products derived from this software may not be called "Hypersonic SQL" nor may
 *        "Hypersonic SQL" appear in their names without prior written permission of the
 *         Hypersonic SQL Group.
 *     -  Redistributions of any form whatsoever must retain the following acknowledgment: "This
 *          product includes Hypersonic SQL."
 * This software is provided "as is" and any expressed or implied warranties, including, but
 * not limited to, the implied warranties of merchantability and fitness for a particular purpose are
 * disclaimed. In no event shall the Hypersonic SQL Group or its contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential damages (including, but
 * not limited to, procurement of substitute goods or services; loss of use, data, or profits;
 * or business interruption). However caused any on any theory of liability, whether in contract,
 * strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
 * software, even if advised of the possibility of such damage.
 * This software consists of voluntary contributions made by many individuals on behalf of the
 * Hypersonic SQL Group.
 *
 *
 * For work added by the HSQL Development Group:
 *
 * Copyright (c) 2001-2002, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer, including earlier
 * license statements (above) and comply with all above license conditions.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution, including earlier
 * license statements (above) and comply with all above license conditions.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.apache.maven.codeswitcher;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.LineNumberReader;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;

import org.apache.tools.ant.DirectoryScanner;

/**
 * changes the source code to support different JDK or profile settings
 *
 * @version 1.7.0
 */
public class CodeSwitcher
{
    private final static String ls = System.getProperty("line.separator", "\n");
    private List onSwitches;
    private List offSwitches;
    private List switches;
    private final static int MAX_LINELENGTH = 82;
    
    private String[] files;
    
    /**
     * Constructor declaration
     */
    public CodeSwitcher()
    {
        onSwitches = new ArrayList();
        offSwitches = new ArrayList();
        switches = new ArrayList();
    }

    /**
     * Method declaration
     *
     * @param a
     */
    public void execute()
    {
        System.out.println("On Switches");
        System.out.println(onSwitches);        

        System.out.println("Off Switches");
        System.out.println(offSwitches);        

        process();
    }
    
    /**
     * Set the on switches
     */
    public void setOnSwitches(String onSwitchesCSV)
    {
        String[] onSwitchesArray = StringUtils.split(onSwitchesCSV, ",");
        
        for (int i = 0; i < onSwitchesArray.length; i++)
        {
            onSwitches.add(onSwitchesArray[i]);
        }
    }
    
    /**
     * Set the off switches
     */
    public void setOffSwitches(String offSwitchesCSV)
    {
        String[] offSwitchesArray = StringUtils.split(offSwitchesCSV, ",");
        
        for (int i = 0; i < offSwitchesArray.length; i++)
        {
            offSwitches.add(offSwitchesArray[i]);
        }
    }

    /**
     * Set the directory of sources to preprocess
     */
    public void setDirectory(String directory)
    {
        // Grab all the java source files in the listed
        // directory.
        files = getFiles(new File(directory), "**/*.java");        
    }

    /**
     * Method declaration
     */
    private void process()
    {
        for (int i = 0; i < files.length; i++)
        {
            System.out.print(".");
            
            String file = files[i];

            if (!processFile(file))
            {
                System.out.println("in file " + file + " !");
            }
        }

        System.out.println("");
    }

    /**
     * Method declaration
     *
     * @param s
     * @return
     */
    private boolean isLongline(String s)
    {
        char c = s.charAt(s.length() - 1);

        if (",(+-&|".indexOf(c) >= 0)
        {
            return true;
        }

        return false;
    }

    /**
     * Method declaration
     *
     * @param name
     * @return
     */
    private boolean processFile(String name)
    {
        File f = new File(name);
        File fnew = new File(name + ".new");
        int state = 0;// 0=normal 1=inside_if 2=inside_else
        boolean switchoff = false;
        boolean working = false;

        try
        {
            LineNumberReader read = new LineNumberReader(new FileReader(f));
            FileWriter write = new FileWriter(fnew);

            while (true)
            {
                String line = read.readLine();

                if (line == null)
                {
                    break;
                }

                if (working)
                {
                    if (line.equals("/*") || line.equals("*/"))
                    {
                        continue;
                    }
                }

                if (!line.startsWith("//#"))
                {
                    write.write(line + ls);
                }
                else
                {
                    if (line.startsWith("//#ifdef "))
                    {
                        if (state != 0)
                        {
                            printError(
                                "'#ifdef' not allowed inside '#ifdef'");

                            return false;
                        }

                        write.write(line + ls);

                        state = 1;

                        String s = line.substring(9);

                        if (onSwitches.indexOf(s) != -1)
                        {
                            working = true;
                            switchoff = false;
                        }
                        else if (offSwitches.indexOf(s) != -1)
                        {
                            working = true;

                            write.write("/*" + ls);

                            switchoff = true;
                        }

                        if (switches.indexOf(s) == -1)
                        {
                            switches.add(s);
                        }
                    }
                    else if (line.startsWith("//#else"))
                    {
                        if (state != 1)
                        {
                            printError("'#else' without '#ifdef'");

                            return false;
                        }

                        state = 2;

                        if (!working)
                        {
                            write.write(line + ls);
                        }
                        else if (switchoff)
                        {
                            write.write("*/" + ls);
                            write.write(line + ls);

                            switchoff = false;
                        }
                        else
                        {
                            write.write(line + ls);
                            write.write("/*" + ls);

                            switchoff = true;
                        }
                    }
                    else if (line.startsWith("//#endif"))
                    {
                        if (state == 0)
                        {
                            printError("'#endif' without '#ifdef'");

                            return false;
                        }

                        state = 0;

                        if (working && switchoff)
                        {
                            write.write("*/" + ls);
                        }

                        write.write(line + ls);

                        working = false;
                    }
                    else
                    {
                        write.write(line + ls);
                    }
                }
            }

            if (state != 0)
            {
                printError("'#endif' missing");

                return false;
            }

            read.close();
            write.flush();
            write.close();

            File fbak = new File(name + ".bak");

            fbak.delete();
            f.renameTo(fbak);

            File fcopy = new File(name);

            fnew.renameTo(fcopy);
            fbak.delete();

            return true;
        }
        catch (Exception e)
        {
            printError(e.getMessage());

            return false;
        }
    }

    /**
     * Method declaration
     *
     * @param name
     * @return
     */
    int testFile(String name)
    {

        File f = new File(name);

        try
        {
            LineNumberReader read = new LineNumberReader(new FileReader(f));
            int l = 1;
            int
                maxline = 0;
            boolean longline = false;

            while (true)
            {
                String line = read.readLine();

                if (line == null)
                {
                    break;
                }

                if (line.length() > MAX_LINELENGTH
                     && !line.startsWith("org.hsqldb.test.Profile."))
                {
                    System.out.println("long line in " + name + " at line "
                         + l);
                }

                if (line.startsWith(" "))
                {
                    int spaces = 0;

                    for (; spaces < line.length(); spaces++)
                    {
                        if (line.charAt(spaces) != ' ')
                        {
                            break;
                        }
                    }

                    if (spaces > 3 && testLine(line) && !longline)
                    {
                        maxline++;
                    }
                    else if (isLongline(line))
                    {
                        longline = true;
                    }
                    else
                    {
                        longline = false;
                    }

                    String s = line.substring(spaces);

                    if (s.startsWith("if("))
                    {
                        if (!s.endsWith(" {"))
                        {
                            System.out.println("if( without { in " + name
                                 + " at line " + l);
                        }
                    }
                    else if (s.startsWith("} else if("))
                    {
                        if (!s.endsWith(" {"))
                        {
                            System.out.println("} else if without { in "
                                 + name + " at line " + l);
                        }
                    }
                    else if (s.startsWith("while("))
                    {
                        if (!s.endsWith(" {"))
                        {
                            System.out.println("while( without { in " + name
                                 + " at line " + l);
                        }
                    }
                    else if (s.startsWith("switch("))
                    {
                        if (!s.endsWith(" {"))
                        {
                            System.out.println("switch( without { in " + name
                                 + " at line " + l);
                        }
                    }
                    else if (s.startsWith("do "))
                    {
                        if (!s.endsWith(" {"))
                        {
                            System.out.println("do without { in " + name
                                 + " at line " + l);
                        }
                    }
                }

                l++;
            }

            read.close();

            return maxline;
        }
        catch (Exception e)
        {
            printError(e.getMessage());
        }

        return -1;
    }

    /**
     * Method declaration
     *
     * @param line
     * @return
     */
    private boolean testLine(String line)
    {

        if (!line.endsWith(";"))
        {
            return false;
        }

        if (line.trim().startsWith("super("))
        {
            return false;
        }

        return true;
    }

    /**
     * Method declaration
     *
     * @param error
     */
    private void printError(String error)
    {
        System.out.println("");
        System.out.println("ERROR: " + error);
    }

    /**
     * Get a set of files from a specifed directory with a set of includes.
     *
     * @param directory Directory to scan
     * @param includes Comma separated list of includes
     * @return files
     */
    public String[] getFiles(File directory, String includes)
    {
        String[] includePatterns = null;
        if (includes != null)
        {
            includePatterns = StringUtils.split(includes, ",");
        }
        DirectoryScanner directoryScanner = new DirectoryScanner();
        directoryScanner.setBasedir(directory);
        directoryScanner.setIncludes(includePatterns);
        directoryScanner.scan();
        String[] files = directoryScanner.getIncludedFiles();

        for (int i = 0; i < files.length; i++)
        {
            files[i] = new File(directory, files[i]).getAbsolutePath();
        }

        return files;
    }

}
