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



 /*
   To compile - javac mainprog.java fractmorse.java
   To run - java mainprog

   This main program uses the functionality provided by
   fractmorse.java to encrypt plaintext using the method
   of fractionated morse or to decrypt ciphertext that has
   been encrypted with the same method.
   The user can enter the ciphertext or plaintext from the
   command line or designate an input file and has the option
   of printing the results to an output file.
 */



public class mainprog
{

 public static void main(String argv[]) throws FileNotFoundException,
                                               IOException
                                               
  {
   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));


   while(true)
    {
   System.out.println();
   System.out.println("Fractionated Morse Code Encryption/Decryption");
   System.out.println("1) Encrypt plaintext");
   System.out.println("2) Decrypt ciphertext");
   System.out.println("3) Exit");
   System.out.print("Enter your choice: ");
   String line = in.readLine();
   int choice = Integer.parseInt(line);

   String plaintext = new String();
   String ciphertext = new String();
   String keyword = new String();

   if(choice == 1)
    {
     System.out.println();
     System.out.print("Read plaintext from an input file (y/n)? ");
     line = in.readLine();
     System.out.println();
     if(line.equals("y"))
      {
       System.out.print("Enter input file name: ");
       line = in.readLine();
       File inputfile = new File(line);
       FileInputStream from = new FileInputStream(inputfile);
       byte [] buffer = new byte[4096];
       int charsread;
       while((charsread = from.read(buffer)) != -1)
        ;
       from.close();
       String text = new String(buffer);
       text = text.trim();
       plaintext = text.toLowerCase();
      }
     else
      {
       System.out.print("Enter plaintext: ");
       plaintext = new String(in.readLine());
      }
     System.out.println();
     System.out.print("Enter keyword for encryption: ");
     keyword = new String(in.readLine());

     fractmorse temp = new fractmorse(plaintext,"",keyword);
     temp.PlaintextToMorse();
     temp.MorseCodeToFractionatedMorse();
     temp.AssignKeywordTableConversions();
     temp.FractionatedMorseToCiphertext();
     ciphertext = temp.getCiphertext();
     System.out.println();

     System.out.print("Print ciphertext to an output file (y/n)? ");
     line = in.readLine();
     if(line.equals("y"))
      {
       System.out.println();
       System.out.print("Enter output file name: ");
       line = in.readLine();
       File outputfile = new File(line);
       FileOutputStream to = new FileOutputStream(outputfile);
       byte [] buffer = ciphertext.getBytes();
       to.write(buffer,0,buffer.length);
       to.close();
      }
     else
      {
       System.out.println();
       System.out.println("Ciphertext: "+ciphertext);
      }

    }


   else if(choice == 2)
    {
     System.out.println();
     System.out.print("Read ciphertext from an input file (y/n)? ");
     line = in.readLine();
     
     if(line.equals("y"))
      {
       System.out.println();
       System.out.print("Enter input file name: ");
       line = in.readLine();
       File inputfile = new File(line);
       FileInputStream from = new FileInputStream(inputfile);
       byte [] buffer = new byte[4096];
       int charsread;
       while((charsread = from.read(buffer)) != -1)
        ;
       from.close();
       String text = new String(buffer);
       text = text.trim();
       ciphertext = text.toLowerCase();
      }
     else
      {
       System.out.println();
       System.out.print("Enter ciphertext: ");
       ciphertext = new String(in.readLine());
      }
     System.out.println();
     System.out.print("Enter keyword for decryption: ");
     keyword = new String(in.readLine());

     fractmorse temp = new fractmorse("",ciphertext,keyword);

     temp.AssignKeywordTableConversions();
     temp.CiphertextToFractionatedMorse();
     temp.FractionatedMorseToMorseCode();
     temp.MorseCodeToPlaintext();
     System.out.println();
     System.out.println();
     plaintext = temp.getPlaintext();
     System.out.println();

     System.out.print("Print plaintext to an output file (y/n)? ");
     line = in.readLine();
     if(line.equals("y"))
      {
       System.out.println();
       System.out.print("Enter output file name: ");
       line = in.readLine();
       File outputfile = new File(line);
       FileOutputStream to = new FileOutputStream(outputfile);
       byte [] buffer = plaintext.getBytes();
       to.write(buffer,0,buffer.length);
       to.close();
      }
     else
      {
       System.out.println();
       System.out.println("Plaintext: "+plaintext);
      }

    }
   else System.exit(0);
    }  

  }
}

