import java.lang.*;
import java.io.*;

/*
 * change java file name by the right file name described in the java file
 * example)
 *              in java file
 *                      public class ExactFileName {
 *              but in the window explore if you can see the file name as EXACTFILENAME.JAVA
 *              this program will change the name to ExactFileName.
 *
 * usage : java RenameBy <directory>
 * 2003.05.12
 * made by Park,Sidong
 */

public class RenameBy
{
        public static void main( String args[] )
        {
                if ( args.length != 1 )
                {
                        System.err.println( "usage : java RenameBy <directory>" );
                        return;
                }
                
                RenameBy rb = new RenameBy();
                rb.recursiveExploreDirectory( args[0] );
        }

        void recursiveExploreDirectory( String directory )
        {
                String list[];
                File f;

                f = new File( directory );
                if ( f.isDirectory() )
                {
                        list = f.list();
                        for ( int i=0; i<list.length; i++ )
                                recursiveExploreDirectory( directory + File.separatorChar + list[i] );
                }
                else
                {
                        String fname = f.getName();
                        if ( fname.endsWith( "java" ) || fname.endsWith("JAVA") )
                        {
                                changeFileName( directory, f );
                        }
                }
        }

        int split( String line, String ret[] )
        {
                int len = line.length();
                int index = 0;

                ret[0] = "";
                for ( int i=0; i<len; i++ )
                {
                        char ch = line.charAt(i);
                        if ( ch == ' ' )
                        {
                                if ( ret[index].length() != 0 )
                                {
                                        index ++;
                                        ret[index] = "";
                                }
                        }
                        else
                        {
                                ret[index] += ch;
                        }
                }
                return index;
        }

        void changeFileName( String directory, File f )
        {
                FileInputStream fis;
                BufferedReader in;
                
                try {
                        fis  = new FileInputStream( f );
                in = new BufferedReader(new InputStreamReader(fis));
                } catch ( FileNotFoundException ee ) {
                        System.out.println( "file not found" );
                        return;
                }
        String thisLine = "";
        try {
                        System.out.print( "found java file :" + f.toString() );
                        while ( (thisLine = in.readLine()) != null )
                        {
                                String buf[] = new String[50];
                

                int len = split( thisLine.trim(), buf );
                                if ( len >= 2 && buf[0].equals("public") && buf[1].equals("class") )
                                {
                                        in.close();
                                        String newFileName = buf[2].trim();
                                        if ( f.getName().equals( newFileName+".java" ) )
                                                System.out.println( " --> skip by the same file name" );
                                        else
                                        {
                                                System.out.println( "  --> change file name : " + newFileName );
                                                f.renameTo( new File( f.getParent() + File.separatorChar +  newFileName + ".java" ) );
                

                        }
                                        return;
                                }
                }
            } catch ( IOException ie ) {
            }
        }
}