import java.io.*;
import java.text.Format;
import java.util.*;
import java.sql.*;
import java.net.*;
import java.util.zip.*;

public class Tool
{

    
    public static char choose(String msg, String choices, String def)
    throws IOException
    {
        while(true)
        {
            System.out.print(msg);
            String ans=new BufferedReader(new InputStreamReader(System.in)).readLine();
            if(ans.length()==0)
            {
                if(def.length()==0)
                    continue;
                return def.toUpperCase().charAt(0);
            }
            else
            if(choices.indexOf(ans.toUpperCase().charAt(0))>=0)
                return ans.toUpperCase().charAt(0);
        }
    }
    
    public static Vector parseQuotes(String s)
    {
        Vector V=new Vector();
        if((s==null)||(s.length()==0)) return V;
        int x=s.indexOf("\"");
        while(x>=0)
        {
            if((x==0)||(s.charAt(x-1)=='\\'))
                x=s.indexOf("\"",x+1);
            else
            {
                String s2=null;
                if(s.startsWith("\""))
                {
                    s2=s.substring(0,x+1);
                    s=s.substring(x+1);
                }
                else
                {
                    s2=s.substring(0,x);
                    s=s.substring(x);
                }
                if(s2.length()>0)
                    V.addElement(s2);
                x=s.indexOf("\"");
            }
        }
        if(s.trim().length()>0)
            V.addElement(s.trim());
        return V;
    }
    
    public static void dodir(String dir)
    throws IOException
    {
        File DIR=new File(dir);
        if(!DIR.isDirectory())
        { System.out.println(dir+" is not a directory!"); return;}
        System.out.println("New Directory: "+DIR.getName()+".");
        char c=choose("P)rocess, S)kip, or C)ancel (RETURN=P):","PSC","P");
        if(c=='C') throw new IOException("Cancelled By User");
        if(c=='S') return;
        
        String[] files=DIR.list();
        for(int f=0;f<files.length;f++)
        {
            File F=new File(DIR.getAbsolutePath()+File.separatorChar+files[f]);
            if(F.isDirectory())
            {
                try
                {
                    dodir(F.getAbsolutePath());
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
            else
            if(F.getName().toUpperCase().endsWith(".JAVA"))
            {
                System.out.println("****"+F.getName()+"*****");
                Vector FV=new Vector();
                HashSet tagNamesUsed=new HashSet();
                
                StringBuffer buf=getFileRaw(F.getAbsolutePath());
                if(buf==null){ System.out.println("** Could not read "+F.getAbsolutePath()); continue;}
                Vector bufV=getFileLineVector(buf);
                for(int b=0;b<bufV.size();b++)
                {
                    String line=(String)bufV.elementAt(b);
                    int start=line.indexOf(".tell(");
                    int len=6;
                    int end=line.lastIndexOf(");");
                    if(start<0)
                    {
                        start=line.indexOf(".append(");
                        len=8;
                    }
                    if((start>=0)&&(end>start)&&(line.substring(start+6,end).indexOf("getScr(")<0))
                    {
                        start+=len;
                        Vector V=parseQuotes(line.substring(start,end));
                        StringBuffer newBuf=new StringBuffer("");
                        if((V.size()>0)&&(line.substring(start).startsWith(".tell(")));
                        {
                            String s=(String)V.firstElement();
                            if(s.endsWith(","))
                            {
                                start+=s.length();
                                V.removeElementAt(0);
                            }
                        }
                        boolean noQuotes=true;
                        for(int i=0;i<V.size();i++)
                            if(((String)V.elementAt(i)).indexOf("\"")>=0)
                                noQuotes=false;
                        if(noQuotes)
                            continue;
                        Vector TV=new Vector();
                        for(int v=0;v<V.size();v++)
                        {
                            String s=(String)V.elementAt(v);
                            if(s.startsWith("\"")&&(s.endsWith("\""))&&(s.length()>2))
                                newBuf.append(s.substring(1,s.length()-1));
                            else
                            {
                                if(s.startsWith("+"))
                                    s=s.substring(1);
                                if(s.endsWith("+"))
                                    s=s.substring(0,s.length()-1);
                                newBuf.append("@x"+(TV.size()+1));
                                TV.addElement(s);
                            }
                        }
                        String newLine1=line.substring(0,start)+"getScr(\""+F.getName().substring(0,F.getName().length()-5)+"\",\"";
                        String newLine2="\"";
                        for(int i=0;i<TV.size();i++)
                            newLine2+=","+TV.elementAt(i);
                        newLine2+=")"+line.substring(end);
                        System.out.println("");
                        System.out.println("");
                        System.out.println(line.trim());
                        System.out.println((newLine1.toString()+"?"+newLine2.toString()).trim());
                        System.out.print("Tag name for the text in this line or ENTER to skip: ");
                        String tagName=null;
                        while((tagName==null)
                        ||(tagNamesUsed.contains(tagName.toUpperCase()))
                        ||(tagName.indexOf(" ")>=0))
                        {
                            tagName=new BufferedReader(new InputStreamReader(System.in)).readLine();
                            if(tagName.length()==0)
                            {
                                System.out.println("Cancelled.");
                                break;
                            }
                            else
                            if(!tagNamesUsed.contains(tagName.toUpperCase())&&(tagName.indexOf(" ")<0))
                            {
                                FV.addElement(tagName+"="+newBuf.toString());
                                bufV.setElementAt(newLine1.toString()+tagName+newLine2.toString(),b);
                            }
                        }
                    }
                    else
                    {
                        start=line.indexOf("private String[] access={");
                        end=line.lastIndexOf("};");
                        if((start>=0)&&(end>start)&&(line.substring(start,end).indexOf("getScr(")<0))
                        {
                            start+=25;
                            Vector V=parseQuotes(line.substring(start,end));
                            Vector TV=new Vector();
                            for(int v=0;v<V.size();v++)
                            {
                                String s=(String)V.elementAt(v);
                                if(s.startsWith("\"")&&(s.endsWith("\"")))
                                    TV.addElement(s.substring(1,s.length()-1));
                            }
                            String newLine=line.substring(0,start);
                            for(int i=0;i<TV.size();i++)
                            {
                               newLine+="getScr(\""+F.getName().substring(0,F.getName().length()-5)+"\"";
                               newLine+=",\"cmd"+((i>0)?(""+i):"")+"\")";
                               if((i<(TV.size()-1)))
                                   newLine+=",";
                            }
                            newLine+=line.substring(end);
                            System.out.println("");
                            System.out.println(line.trim());
                            System.out.println((newLine.toString()).trim());
                            String resp=null;
                            while(resp==null)
                            {
                                System.out.print("Does this look OK? (Y/N)?");
                                resp=new BufferedReader(new InputStreamReader(System.in)).readLine();
                                if((!resp.toUpperCase().equals("Y"))&&(!resp.toUpperCase().equals("Y")))
                                    resp=null;
                            }
                            if((resp!=null)&&(resp.toUpperCase().equalsIgnoreCase("Y")))
                            {
                                for(int i=0;i<TV.size();i++)
                                    FV.addElement("cmd"+((i>0)?(""+i):"")+"="+(String)TV.elementAt(i));
                                bufV.setElementAt(newLine.toString(),b);
                            }
                        }
                    }
                }
                System.out.println("");
                if(FV.size()>0)
                {
                    buf=new StringBuffer("");
                    for(int i=0;i<bufV.size();i++)
                        buf.append(((String)bufV.elementAt(i))+"\n");
                    saveFile(F.getAbsolutePath(),buf);
                    buf=new StringBuffer("");
                    for(int i=0;i<FV.size();i++)
                        buf.append(((String)FV.elementAt(i))+"\n");
                    System.out.println(buf.toString());
                    saveFile(F.getParentFile().getAbsolutePath()+File.separatorChar+F.getName().substring(0,F.getName().length()-5)+"_en_TX.properties",buf);
                }
                FV.clear();
            }
        }
    }
                
    public static StringBuffer getFileRaw(String filename)
    {
        StringBuffer buf=new StringBuffer("");
        try
        {
            FileReader F=new FileReader(filename);
            char c=' ';
            while(F.ready())
            {
                c=(char)F.read();
                if(c<0) break;
                buf.append(c);
            }
            F.close();
        }
        catch(Exception e)
        {
            return null;
        }
        return buf;
    }
    
    public static Vector getFileLineVector(StringBuffer buf)
    {
        Vector V=new Vector();
        
        StringBuffer str=new StringBuffer("");
        for(int i=0;i<buf.length()-1;i++)
        {
            if(((buf.charAt(i)=='\n')&&(buf.charAt(i+1)=='\r'))
               ||((buf.charAt(i)=='\r')&&(buf.charAt(i+1)=='\n')))
            {
                i++;
                V.addElement(str.toString());
                str.setLength(0);
            }
            else
            if(((buf.charAt(i)=='\r'))
            ||((buf.charAt(i)=='\n')))
            {
                V.addElement(str.toString());
                str.setLength(0);
            }
            else
                str.append(buf.charAt(i));
        }
        if(str.length()>0)
            V.addElement(str.toString());
        return V;
    }
    
    public static int[] makeInt(int i)
    {
        int[] ii=new int[1];
        ii[0]=i;
        return ii;
    }
    
    public static boolean saveFile(String filename, StringBuffer myRsc)
    {
        if(myRsc==null)
        {
            return false;
        }
        try
        {
            File F=new File(filename);
            FileWriter FW=new FileWriter(F);
            FW.write(myRsc.toString());
            FW.close();
            return true;
        }
        catch(IOException e)
        {
            System.out.println(filename+": "+e.getMessage());
        }
        return false;
    }
    
            
    public static void main(String[] args)
    {
        try
        {
            Tool.dodir(".");
        }
        catch(Exception e){e.printStackTrace();}
    }

}
