import java.io.*;
import java.util.*;
import java.net.*;
//_____________________________________________________________
public class mission
{ 
   boolean show1 = false;
   String[] translate;
   StringBuffer getittxt = new StringBuffer();
   boolean allold = true;
   boolean justbook = false;
   int bookstart = 4893;
   int bookend = 5853;
   int max = 1000;
   byte[] bb;
   int[] bbpos;
   HashMap hash = new HashMap();
   HashMap hash2 = new HashMap();
   HashMap hash3 = new HashMap();
   int hashpos = 0;
   int hashpos2 = 0;
   int hashpos3 = 0;
   int longest = 0;
   int length = 490;
   String the490;
   StringBuffer bufout1 = new StringBuffer();
   StringBuffer bufout2 = new StringBuffer();
   StringBuffer bufout3 = new StringBuffer();
   String[] hub;
   String green = "";
   String orange = "";
   String blue = "";
   String red = "";
   String purple = "";
   String green2 = "";
   String orange2 = "";
   String blue2 = "";
   String red2 = "";
   String purple2 = "";
   String loins2 = "";
   String out = "";
   String[] look = new String[1000000];
   StringBuffer fw = new StringBuffer();
   String numbs = "VJMI ABYH AO NDRH VASRH AJR ASRH IL NFJH VHCRYJ LH ABYH VQMV KL NDRYH VKL ASR AJR ASRH IL NFJH YQVM";

//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   mission bnk = new mission();
   bnk.start(args);
}
//_____________________________________________________________
mission() throws Exception 
{
}
//___________________________________________________________________________
void start(String[] args)
{
   String loins = "";
   String in = "DFVSY";
   if (args.length > 0) in = args[0];
   loadTranslate();
   startit(args);
   System.out.println("_____________________________________");
   StringBuffer bufbb = new StringBuffer();
   int which = 0;
   for (int i = 0; i < the490.length(); i++)
   {
      byte[] be = new byte[1];
      String s = the490.substring(i,i+1);
      int ps2 = i+3;
      if (ps2 > length) ps2 = length;
      String sb = the490.substring(i, ps2);
      int ps = i+5;
      if (ps > length) ps = length;
      String sc = the490.substring(i, ps);
      if (bbpos[i] == 0)
      {
         s = s.toLowerCase();
      }
      else
      {
         System.out.print(s);
      }
      int b = bbpos[i];
      String color = "black";
      String ss = s.toUpperCase();
      if (b > 0) 
      {
         which++;
         if (!allold)
         {
            if (b == 9) 
            {
               color = "green";
               green = convert(in);
               green2 = "patterns";
            }
            else if ((b == 2) | (b == 6)) 
            {
               color = "orange";
               if (which == 1)
               {
                  orange += convert(sc) + " ";
                  orange2 += findword(sc) + " ";
               }
            }
            else if ((b == 3) | (b == 5) | (b == 7)) 
            {
               color = "blue";
               if (which == 1)
               {
                  blue += convert(sb) + " ";
                  blue2 += findword(sb) + " ";
               }
            }
            else if ((b == 1) | (b == 4) | (b == 8)) 
            {
                color = "red";
               if (which == 1)
               {
                  red += convert(sb) + " ";
                  red2 += findword(sb) + " ";
               }
            }
         }
         else
         {
            if (b == 17) 
            {
               color = "green";
               green = convert(in);
               green2 = "patterns";
            }
            else if ((b == 3) | (b == 12)) 
            {
               color = "orange";
               if (which == 1)
               {
                  orange += convert(sc) + " ";
                  orange2 += findword(sc) + " ";
               }
            }
            else if ((b == 4) | (b == 8) | (b == 13)) 
            {
               color = "blue";
               if (which == 1)
               {
                  blue += convert(sb) + " ";
                  blue2 += findword(sb) + " ";
               }
            }
            else if ((b == 1) | (b == 7) | (b == 16)) 
            {
               color = "red";
               if (which == 1)
               {
                  red += convert(sb) + " ";
                  red2 += findword(sb) + " ";
               }
            }
            else if ((b == 2) | (b == 5) | (b == 6) | (b == 9) | (b == 10) | (b == 11) | (b == 14) | (b == 15))
            {
               color = "purple";
               if ((which == 1) | (which == 0))
               {
                  String sbu = sc;
                  if (sc.substring(0,3).equals("BCY")) sbu = sc.substring(0,3);
                  else if (sc.substring(0,3).equals("OEL")) sbu = sc.substring(0,3);
                  else if (sc.substring(0,2).equals("NB")) sbu = sc.substring(0,2);
                  purple += convert(sbu) + " ";
                  purple2 += findword2(sbu) + " ";
loins += sbu + " ";
               }
            }
            if ((i == 13) | (i == 479))
            {
               if (i != 479) color = "purple";
               String sbu = sc;
               if (sc.substring(0,3).equals("BCY")) sbu = sc.substring(0,3);
               else if (sc.substring(0,3).equals("OEL")) sbu = sc.substring(0,3);
               else if (sc.substring(0,2).equals("NB")) sbu = sc.substring(0,2);
               else if (sc.substring(0,3).equals("AYV")) sbu = sc.substring(0,3);
               System.out.println("\r\n\rPURPLE PURPLE PURPLE " + sbu + "\r\n");
               purple += convert(sbu) + " ";
               purple2 += findword2(sbu) + " ";
loins += sbu + " ";
            }
         }
         bufout3.append("<font size='7' color='" + color + "'>");
      }
      else
      {
         which = 0;
      }
      bufout3.append(convert(s.toUpperCase()));
      if (bbpos[i] > 0) bufout3.append("</font>");
      if (isdiv(i,7)) bufout3.append("");
      if (isdiv(i,70)) bufout3.append("<br>");
      bufbb.append(s);
   }
   System.out.println("\r\n\r\n{" + purple + "}\r\n\r\n");
   System.out.println("\r\n\r\n{" + purple2 + "}\r\n\r\n");
   System.out.println("\r\n");
   System.out.println(bufbb.toString());
   String out = "";
   out += "<hr>";
   out += bufout3.toString() + "<hr>\r\n";
   out += "";
   saveFile("mission.htm",out,false);
   System.out.println("\r\n" + loins + " " + value(loins) + "\r\n");
   System.out.println(loins2 + " | Hebrew numeric value = " + value(loins) + "\r\n");
   System.out.println("\r\nresults in 'mission.htm'");
}
//___________________________________________________________________________
void startit(String[] args)
{
   if (args.length == 0)
   {
      System.out.println("java mission first last 1000 490 2");
   }
   String inlk = "DFVSY";
   if (args.length > 0) inlk = args[0];
   String in = transcribe(inlk).split("\r\n")[0];
   String in2lk = "B";
   if (args.length > 1) in2lk = args[1];
   String in2 = transcribe(in2lk).split("\r\n")[0];
   int max = 1000;
   if (args.length > 2) max = Integer.parseInt(args[2]);
   int length = 490;
   if (args.length > 3) length = Integer.parseInt(args[3]);
   int thestart = 2;
   if (args.length > 4) thestart = Integer.parseInt(args[4]);
   System.out.println(inlk + " " + in + " " + in2lk + " " + in2 + " " + max + " " + length);
   bbpos = new int[length];
   if (allold) setpos(0,in,"b",17);
   else setpos(0,in,"b", 9);
   String[] file = getFile("torah.txt").split("\r\n");
   hub = getFile("desc1.txt").split("\r\n");
   StringBuffer buf = new StringBuffer();
   int thepos = 0;
   for (int i = 0; i < hub.length; i++)
   {
      String[] words = hub[i].split(" ");
      String wrd = words[0].trim();
      if (hash2.get(wrd) == null)
      {
         hash2.put(wrd,Integer.toString(hashpos2++));
      }
   }
   int end2 = bookend;
   if (end2 > file.length) end2 = file.length;
   for (int i = bookstart; i < end2; i++)
   {
      String[] words = file[i].split(" ");
      for (int j = 0; j < words.length; j++)
      {
         String wrd = words[j];
         if (hash3.get(wrd) == null)
         {
            hash3.put(wrd,Integer.toString(hashpos3++));
         }
      }
   }
   for (int i = 0; i < file.length; i++)
   {
      String[] words = file[i].split(" ");
      for (int j = 0; j < words.length; j++)
      {
         if (words[j].length() > longest) longest = words[j].length();
         if (hash.get(words[j]) == null)
         {
            hash.put(words[j],Integer.toString(hashpos++));
         }
         buf.append(words[j]);
      }
   }
   bb = buf.toString().getBytes();
   byte[] bc = in.getBytes();
   for (int i = thestart; i < max; i++)
   {
      for (int j = 0; j < bb.length; j++)
      {
         boolean found = true;
         for (int k = 0; k < bc.length; k++)
         {
            int pos = (j + (i * k)) % bb.length;
            if (bb[pos] != bc[k])
            {
               found = false;
               break;
            }
         }
         if (found)
         {
            System.out.println(in + " " + j + " " + i);
            the490 = get490(j,i);
            if (the490.indexOf(in2) >= 0)
            {
               System.out.println(the490);
               String output = getwords(the490,j);
               System.out.println(output);
               return;
            }
            else
            {
               System.out.print(".");
            }
         }
      }
   }
}
//_____________________________________________________________
String get490(int ii, int jj)
{
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < length; i++)
   {
      int pos = (ii + (jj * i)) % bb.length;
      buf.append((char)bb[pos]);
   }
   return(buf.toString());
}
//_____________________________________________________________
String getwords(String the490, int kk)
{
   int position = 0;
   int count = 0;
   byte[] bd = the490.getBytes();
   for (int i = 0; i < bd.length; i++)
   {
      StringBuffer buf = new StringBuffer();
      for (int j = 0; j < longest; j++)
      {
         int pos = (i + j) % bd.length;
         buf.append((char)bd[pos]);
         String str = buf.toString();
         if (j == 3)
         {
            for (int k = 1; k <= 3; k++)
            {
               String look = "AAAA".substring(k,4);
               if (str.substring(k,4).equals(look))
               {
                   String str2 = str.substring(0,k);
                   String eng = (String)hash.get(str2);
                   if (allold) eng = (String)hash2.get(str2);
                   if (justbook) eng = (String)hash3.get(str2);
                   if (eng != null)
                   {
                      count++;
                      System.out.print(++position + " " + i + " " + str2 + " " + eng + " " + findword(str2));
                      System.out.println(" " + convert(str2));
                      setpos(i,str2, eng, position);
                   }
               }
            }
         }
         else if (j > 3)
         {
            String eng = (String)hash.get(str);
            if (allold) eng = (String)hash2.get(str);
            if (justbook) eng = (String)hash3.get(str);
            if (eng != null)
            {
               count++;
               System.out.println(++position + " " + i + " " + str + " " + eng + " " + findword(str));
               setpos(i,str, eng, position);
            }
         }
      }
   }
   System.out.println(count);
   return("");
}
//_____________________________________________________________
void setpos(int ii, String str, String eng, int position)
{
   bufout1.append(str + " ");
   bufout2.append(eng + " ");
   for (int i = 0; i < str.length(); i++)
   {
      bbpos[ii+i] = position;
   }
}
//_____________________________________________________________
String findword2(String str)
{
   String s = findword(str);
   String[] ss = s.split(" ");
   for (int i = 1; i < ss.length; i++)
   {
      loins2 += ss[i] + " ";
   }
   System.out.println("[[[" + str + "|||" + s + "]]]");
   return(s);
}
//_____________________________________________________________
String findword(String str)
{
   System.out.print("[" + str + " ");
   fw.append("[" + str + "]\r\n");
   for (int i = 1; i < hub.length; i++)
   {
      String[] words = hub[i].split(" ");
      if (words[0].trim().equals(str.trim()))
      {
         fw.append("\t" + hub[i].trim() + "\r\n");
         System.out.println(hub[i].trim() + "]");
         return(hub[i].trim());
      }
   }
   System.out.println("*]");
   return("");
}
//_____________________________________________________
String convert(String tr)
{
   String out = "";
   byte[] bb = tr.getBytes();
   for (int i = 0; i < bb.length; i++)
   {
      //out = "";
      //int val = tr.charCodeAt(i);
      int val = bb[i];
      if (val < 0) val += 256;
      if (val == 65) out += "&#x05d0";
      else if (val == 66) out += "&#x05d1";
      else if (val == 71) out += "&#x05d2";
      else if (val == 68) out += "&#x05d3";
      else if (val == 72) out += "&#x05d4";
      else if (val == 86) out += "&#x05d5";
      else if (val == 90) out += "&#x05d6";
      else if (val == 67) out += "&#x05d7";
      else if (val == 84) out += "&#x05d8";
      else if (val == 89) out += "&#x05d9";
      else if (val == 75) out += "&#x05da";
      else if (val == 75) out += "&#x05db";
      else if (val == 76) out += "&#x05dc";
      else if (val == 77) out += "&#x05dd";
      else if (val == 77) out += "&#x05de";
      else if (val == 78) out += "&#x05df";
      else if (val == 78) out += "&#x05e0";
      else if (val == 83) out += "&#x05e1";
      else if (val == 73) out += "&#x05e2";
      else if (val == 70) out += "&#x05e3";
      else if (val == 70) out += "&#x05e4";
      else if (val == 69) out += "&#x05e5";
      else if (val == 69) out += "&#x05e6";
      else if (val == 81) out += "&#x05e7";
      else if (val == 82) out += "&#x05e8";
      else if (val == 74) out += "&#x05e9";
      else if (val == 79) out += "&#x05ea";
      else out += (char)val;
   }
   return(out);
}
//__________________________________________________
public static boolean isdiv(int val, int div)
{
   if (val == 0) return(false);
   double val2 = (double)val / (double)div;
   int val3 = (int)val2;
   if (val2 == val3) 
   {
      return(true);
   }
   return(false);
}
//___________________________________________________________________________
public static String getFile(String file) 
{
   StringBuffer buf=new StringBuffer();String str;
  try
  {
   BufferedReader in = new BufferedReader (new FileReader (file));
   while((str=in.readLine())!=null)
   {
      buf.append(str+"\r\n");
   }
   in.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return (buf.toString());
}
//___________________________________________________________________________
public static void saveFile(String file, String str, boolean append) 
{
  try
  {
   BufferedWriter out = new BufferedWriter(new FileWriter(file, append));
   out.write(str);
   out.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
}
//___________________________________________________________________________
void loadTranslate()
{
   translate = new String[128];
   String[] tt = getFile("translate.txt").split("\r\n");
   for (int i = 0; i < tt.length; i++)
   {
      String s = tt[i].substring(0,1);
      byte[] b = s.getBytes();
      translate[b[0]] = tt[i].substring(2);
   }
}
//___________________________________________________________________________
String transcribe(String look)
{
   //StringBuffer buf = new StringBuffer();
   look = look.toUpperCase();
   look = look.replace("CH", "a");
   look = look.replace("QU", "b");
   look = look.replace("SH", "c");
   look = look.replace("TH", "d");
   look = look.replace("AI", "e");
   look = look.replace("AU", "f");
   look = look.replace("AY", "g");
   look = look.replace("EA", "h");
   look = look.replace("EE", "i");
   look = look.replace("EI", "j");
   look = look.replace("EO", "k");
   look = look.replace("EU", "l");
   look = look.replace("EY", "m");
   look = look.replace("IA", "n");
   look = look.replace("IE", "o");
   look = look.replace("OA", "p");
   look = look.replace("OI", "q");
   look = look.replace("OO", "r");
   look = look.replace("OU", "s");
   look = look.replace("OY", "t");
   look = look.replace("UI", "u");
   look = look.replace("UO", "v");
   look = look.replace("UY", "w");
   if (look.length() > 1)
   {
      if (look.substring(look.length()-2).equals("AH"))
      {
         look = look.substring(0,look.length()-2) + "x";
      }
   }
   if (show1) System.out.println(look);
   byte[] bb = look.getBytes();
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < bb.length; i++)
   {
      buf.append(translate[bb[i]] + "\r\n");
   }
   if (show1) System.out.println(buf.toString());
   if (show1) System.out.println("-----------");
   getittxt = new StringBuffer();
   getit(buf.toString(), 0, "");
   if (show1) System.out.println(getittxt.toString());
   String[] lines = getittxt.toString().split("\r\n");
   buf = new StringBuffer();
   for (int i = 0; i < lines.length; i++)
   {
      String str = lines[i].replace('.',',');
      String[] words = str.split(",");
      for (int j = 0; j < words.length-0; j++)
      {
         if (words[j].equals("")) continue;
         int val = Integer.parseInt(words[j]);

         buf.append((char)val);
      }
      buf.append("\r\n");
   }
   if (show1) System.out.println(buf.toString());
   return(buf.toString());
}
//___________________________________________________________________________
String getit(String array, int val, String str)
{
   String str2 = str;
   String[] words = array.split("\r\n");
   String[] words2 = words[val].split(",");
   for (int j = 0; j < words2.length; j++)
   {
      if (j > 0)
      {
         String str3 = str2.substring(0, str2.length()-1);
         int pos = str3.lastIndexOf(",");
         str2 = str3.substring(0,pos+1);
      }
      str2 += words2[j] + ",";
      if (val < words.length - 1)
      {
         getit(array,val+1,str2);
      }
      else
      {
         getittxt.append(str2 + "\r\n");
      }
   }
   return(str2);
}
//__________________________________________________
int value(String str)
{
   int total = 0;
   out = "";
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 20;
      else if (st == 'l') v = 30;    
      else if (st == 'm') v = 40;
      else if (st == 'n') v = 50;
      else if (st == 'o') v = 60;
      else if (st == 'p') v = 70;
      else if (st == 'q') v = 80;
      else if (st == 'r') v = 90;
      else if (st == 's') v = 100;
      else if (st == 't') v = 200;
      else if (st == 'u') v = 300;
      else if (st == 'v') v = 400;
      else if (st == 'w') v = 500;
      else if (st == 'x') v = 600;
      else if (st == 'y') v = 700;
      else if (st == 'z') v = 800;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 20;
      else if (st == 'L') v = 30;
      else if (st == 'M') v = 40;
      else if (st == 'N') v = 50;
      else if (st == 'S') v = 60;
      else if (st == 'I') v = 70;
      else if (st == 'F') v = 80;
      else if (st == 'E') v = 90;
      else if (st == 'Q') v = 100;
      else if (st == 'R') v = 200;
      else if (st == 'J') v = 300;
      else if (st == 'O') v = 400;
      total += v;
      if (i > 0) out += " + ";
      out += Integer.toString(v);
   }
   return(total);
}
//__________________________________________________
int place(String str)
{
   int total = 0;
   out = "";
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 11;
      else if (st == 'l') v = 12;    
      else if (st == 'm') v = 13;
      else if (st == 'n') v = 14;
      else if (st == 'o') v = 15;
      else if (st == 'p') v = 16;
      else if (st == 'q') v = 17;
      else if (st == 'r') v = 18;
      else if (st == 's') v = 19;
      else if (st == 't') v = 20;
      else if (st == 'u') v = 21;
      else if (st == 'v') v = 22;
      else if (st == 'w') v = 23;
      else if (st == 'x') v = 24;
      else if (st == 'y') v = 25;
      else if (st == 'z') v = 26;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 11;
      else if (st == 'L') v = 12;
      else if (st == 'M') v = 13;
      else if (st == 'N') v = 14;
      else if (st == 'S') v = 15;
      else if (st == 'I') v = 16;
      else if (st == 'F') v = 17;
      else if (st == 'E') v = 18;
      else if (st == 'Q') v = 19;
      else if (st == 'R') v = 20;
      else if (st == 'J') v = 21;
      else if (st == 'O') v = 22;
      total += v;
      if (i > 0) out += " + ";
      out += Integer.toString(v);
   }
   return(total);
}
}//__________________________________________________________________________
