import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/**
   This program encrypts a file using the Caesar cipher.
*/
public class CaesarCipher
{  
   public static void main(String[] args) throws FileNotFoundException
   {  
      final int DEFAULT_KEY = 3;
      int key = DEFAULT_KEY;
      String inFile = null;
      String outFile = null;

      for (int i = 0; i < args.length; i++)
      {  
         String arg = args[i];
         if (arg.charAt(0) == '-')
         {  
            // It is a command line option

            char option = arg.charAt(1);
            if (option == 'd') { key = -key; }
            else { usage(); return; }
         }
         else
         {  
            // It is a file name

            if (inFile == null) { inFile = arg; }
            else if (outFile == null) { outFile = arg; }
            else { usage(); return; }
         }
      }
      if (inFile == null || outFile == null) { usage(); return; }

      Scanner in = new Scanner(new File(inFile));
      in.useDelimiter(""); // Process individual characters
      PrintWriter out = new PrintWriter(outFile);
      while (in.hasNext())
      {
         char from = in.next().charAt(0);
         char to = (char) (from + key);
         out.print(to);
      }
      in.close();
      out.close();               
   }   

   /**
      Prints a message describing proper usage.
   */
   public static void usage()
   {  
      System.out.println("Usage: java CaesarCipher [-d] infile outfile");
   }
}
