package edu.pdx.cs410J.grader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class GwtZipFixer {

  private static final Logger logger = LoggerFactory.getLogger("edu.pdx.cs410J.grader");

  public static void main(String[] args) {
    String outputDirectoryName = null;
    List<String> zipFileNames = new ArrayList<>();

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

      } else {
        zipFileNames.add(arg);
      }
    }

    if (outputDirectoryName == null) {
      usage("Missing output directory");
    }

    if (zipFileNames.isEmpty()) {
      usage("Missing zip file");
    }
  }

  private static void usage(String message) {
    PrintStream err = System.err;

    err.println("+++ " + message);
    err.println();
    err.println("usage: java GwtZipFixer outputDirectory zipFile+");
    err.println("    outputDirectory     Name of direct into which fixed zip files should be written");
    err.println("    zipFile             Name of zip file to be fixed");
    err.println();
    err.println("Fixes the contents of a zip file submitted for the GWT ");
    err.println("project so that it will work with the grading scripts");
    err.println();

    System.exit(1);
  }
}

@@ -25,16 +25,57 @@
 rader;%0A%0A
+import com.google.common.io.ByteStreams;%0A
 import o
@@ -91,16 +91,16 @@
 Logger;%0A
-
 import o
@@ -139,27 +139,17 @@
 java.io.
-PrintStream
+*
 ;%0Aimport
@@ -192,16 +192,122 @@
 il.List;
+%0Aimport java.util.zip.ZipEntry;%0Aimport java.util.zip.ZipInputStream;%0Aimport java.util.zip.ZipOutputStream;
 %0A%0Apublic
@@ -796,24 +796,38 @@
 irectory%22);%0A
+      return;%0A
     %7D%0A%0A    i
@@ -892,17 +892,1310 @@
 %22);%0A    
-%7D
+  return;%0A    %7D%0A%0A    File outputDirectory = new File(outputDirectoryName);%0A%0A    for (String zipFileName : zipFileNames) %7B%0A      File zipFile = new File(zipFileName);%0A      try %7B%0A        fixZipFile(zipFile, outputDirectory);%0A%0A      %7D catch (IOException e) %7B%0A        usage(%22While fixing zip file: %22 + e);%0A      %7D%0A%0A    %7D%0A  %7D%0A%0A  private static void fixZipFile(File zipFile, File outputDirectory) throws IOException %7B%0A    File fixedZipFile = getFixedZipFile(zipFile, outputDirectory);%0A%0A    try (%0A      ZipInputStream input = new ZipInputStream(new FileInputStream(zipFile));%0A      ZipOutputStream output = new ZipOutputStream(new FileOutputStream(fixedZipFile));%0A    ) %7B%0A      for (ZipEntry entry = input.getNextEntry(); entry != null; entry = input.getNextEntry()) %7B%0A        String fixedEntryName = getFixedEntryName(entry.getName());%0A%0A        if (fixedEntryName != null) %7B%0A          ZipEntry fixedEntry = new ZipEntry(fixedEntryName);%0A          output.putNextEntry(fixedEntry);%0A%0A          ByteStreams.copy(input, output);%0A        %7D%0A%0A        output.flush();%0A      %7D%0A    %7D%0A  %7D%0A%0A  private static String getFixedEntryName(String entryName) %7B%0A    return entryName;%0A  %7D%0A%0A  private static File getFixedZipFile(File zipFile, File outputDirectory) %7B%0A    return new File(outputDirectory, zipFile.getName());
 %0A  %7D%0A%0A  

Copy the contents of a zip file into another.