package org.apache.maven.javacc;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   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.
 * ====================================================================
 */

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

import org.javacc.jjtree.JJTree;

/**
 * 
 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a> 
 * @version $Id$
 */
public class JJTreeBean extends BaseBean
{
    
    /**
     * Generates AST and javacc grammar
     * @return
     * @throws Exception
     */
    public int generate() throws Exception
    {                                               
        final ArrayList params = new ArrayList();
        
        final File outputDir = getJJTreeOutputDir();
        
        System.err.println("-OUTPUT_DIRECTORY=" + outputDir.getCanonicalPath());
        
        final long timestamp = System.currentTimeMillis();
        
        System.err.println( "Creating directory: " + outputDir.getCanonicalPath() );
        
        if ( !outputDir.exists() )
        {                
            
            System.out.println( "Creating directorty: " + outputDir.getAbsolutePath() );
            
            outputDir.mkdirs();            
        }
        else
        {
            if ( !checkTimestamp() )
            {
                return 0;
            }    
        }           
        params.add( "-OUTPUT_DIRECTORY=" + outputDir.getCanonicalPath() );        
        
        params.add( getGrammar() );
        
        String[] args = ( String[] ) params.toArray( new String[ params.size() ] );        
        
        final JJTree jjtree = new JJTree();
        
        final int retValue = jjtree.main( args );  
        
        moveJJTJavaParserState( );
        
        //addHeader( outputDir );
        
        createTimestampFile( timestamp, outputDir );
        
        return retValue;
    }
    
    /**
     * 
     */
    private void moveJJTJavaParserState( )
    {     
        final File  jjtJavaParserStateFileOrg = new File( getJJTreeOutputDir() , "JJTJavaParserState.java" );
        
        final File  jjtJavaParserStateFileNew = new File( getJavaccOutputDir() , "JJTJavaParserState.java" );
        
        if ( ! jjtJavaParserStateFileOrg.equals( jjtJavaParserStateFileNew ) )
        {    
	    final File dir  = jjtJavaParserStateFileNew.getParentFile(); 
	    
	    if ( !dir.exists() )
	    {
	       dir.mkdirs();
	    }    
	    
	    System.out.println("Moving: " + jjtJavaParserStateFileOrg +" to: " + jjtJavaParserStateFileNew );
	    
	    jjtJavaParserStateFileOrg.renameTo( jjtJavaParserStateFileNew );
        }
    }

    /**
     * Returns the path to generated javacc grammar
     * @return the path to generated javacc grammar
     */
    public String getJavaccGrammar()
    {                 
         final File grammarFile = new File ( getGrammar() );   
               
         
         String grammarName =  grammarFile.getName();
         
         grammarName =  grammarName.substring( 0, grammarName.length()-1 );        
         
         final String retValue = getJJTreeOutputDir() + File.separator +  grammarName ;        
         
         return retValue;
    }
    
    
        
}
