package edu.pdx.cs410J.grader;

import edu.pdx.cs410J.ParserException;

import java.io.*;

public class FetchAndProcessGraderEmail {

  public static void main(String[] args) {
    String passwordFile = null;
    String directoryName = null;
    String gradeBookFile = null;

    for (String arg : args) {
      if (passwordFile == null) {
        passwordFile = arg;


      } else if (directoryName == null) {
        directoryName = arg;

      } else if (gradeBookFile == null) {
        gradeBookFile = arg;

      } else {
        usage("Extraneous argument: " + arg);
      }
    }

    if (passwordFile == null) {
      usage("Missing password file");
    }

    if (directoryName == null) {
      usage("Missing directory name");
    }

    if (gradeBookFile == null) {
      usage("Missing grade book file");
    }

    String password = null;
    try {
      password = readGraderEmailPasswordFromFile(passwordFile);

    } catch (IOException e) {
      usage("Exception while reading password file");
    }

    assert directoryName != null;
    final File directory = new File(directoryName);
    if (!directory.exists()) {
      usage("Download directory \"" + directoryName + "\" does not exist");
    }

    if (!directory.isDirectory()) {
      usage("Download directory \"" + directoryName + "\" is not a directory");
    }

    GradeBook gradeBook = null;
    try {
      gradeBook = readGradeBookFromFile(gradeBookFile);

    } catch (IOException ex) {
      usage("Couldn't read grade book file \"" + gradeBookFile + "\"");

    } catch (ParserException ex) {
      usage("Couldn't parse grade book file \"" + gradeBookFile + "\"");
    }

    GraderEmailAccount account = new GraderEmailAccount(password);
    ProjectSubmissionsProcessor processor = new ProjectSubmissionsProcessor(directory, gradeBook);
    account.fetchAttachmentsFromUnreadMessagesInFolder(processor.getEmailFolder(), processor);
  }

  private static GradeBook readGradeBookFromFile(String fileName) throws IOException, ParserException {
    XmlGradeBookParser parser = new XmlGradeBookParser(fileName);
    return parser.parse();
  }

  private static String readGraderEmailPasswordFromFile(String passwordFile) throws IOException {
    File file = new File(passwordFile);
    if (!file.exists()) {
      usage("Password file \"" + passwordFile + "\" does not exist");
    }

    BufferedReader br = new BufferedReader(new FileReader(file));
    return br.readLine();
  }

  private static void usage(String message) {
    PrintStream err = System.err;
    err.println("** " + message);
    err.println();
    err.println("FetchAndProcessGraderEmail passwordFile directory");
    err.println("  passwordFile    File containing the password for the Grader's email account");
    err.println("  directory       The directory in which to download attachments");
    err.println("  gradeBookFile   Grade Book XML file related to this submission");
    err.println();

    System.exit(1);
  }
}

@@ -267,16 +267,47 @@
  = null;
+%0A    String whatToFetch = null;
 %0A%0A    fo
@@ -393,17 +393,16 @@
 = arg;%0A%0A
-%0A
       %7D 
@@ -529,32 +529,100 @@
 ookFile = arg;%0A%0A
+      %7D else if (whatToFetch == null) %7B%0A        whatToFetch = arg;%0A%0A
       %7D else %7B%0A 
@@ -925,196 +925,157 @@
     
-String password
+if (whatToFetch
  =
+=
  null
-;%0A    try %7B%0A      password = readGraderEmailPasswordFromFile(passwordFile);%0A%0A    %7D catch (IOException e) %7B%0A      usage(%22Exception while reading password file%22);%0A    %7D
+) %7B%0A      usage(%22Missing kind of email to fetch%22);%0A    %7D%0A%0A    String password = readGraderEmailPasswordFromFile(passwordFile);
 %0A%0A  
@@ -2257,35 +2257,16 @@
 ordFile)
- throws IOException
  %7B%0A    F
@@ -2328,24 +2328,31 @@
 ()) %7B%0A      
+return 
 usage(%22Passw
@@ -2406,24 +2406,36 @@
 t%22);%0A    %7D%0A%0A
+    try %7B%0A  
     Buffered
@@ -2488,24 +2488,26 @@
 file));%0A    
+  
 return br.re
@@ -2511,24 +2511,122 @@
 .readLine();
+%0A%0A    %7D catch (IOException e) %7B%0A      return usage(%22Exception while reading password file%22);%0A    %7D
 %0A  %7D%0A%0A  priv
@@ -2636,20 +2636,21 @@
  static 
-void
+%3CT%3E T
  usage(S
@@ -2811,32 +2811,58 @@
 rdFile directory
+ gradeBookFile whatToFetch
 %22);%0A    err.prin
@@ -3137,29 +3137,287 @@
 tln(
-);%0A%0A    System.exit(1
+%22  whatToFetch     What kind of student emails should be fetched%22);%0A    err.println(%22      projects    Project submissions%22);%0A    err.println(%22      surveys     Student surveys%22);%0A    err.println();%0A%0A    System.exit(1);%0A%0A    throw new IllegalStateException(%22Shouldn't reach here%22
 );%0A 

Add a required "what to fetch" command line argument. Also started refactoring some code to make the main() method a little shorter.