package com.github.dcapwell.java.file.operations;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import com.google.common.collect.ImmutableList;
import com.google.common.io.CharStreams;

public final class ForkChmod implements Chmod {
  @Override
  public int chmod(String filename, int mode) {
    // on exception, return 0 (C true)
    try {
      return fork("/bin/chmod", Chmods.toString(mode), filename);
    } catch (IOException e) {
      return Chmods.Bad;
    } catch (InterruptedException e) {
      return Chmods.Bad;
    }
  }

  private static int fork(String... cmd) throws IOException, InterruptedException {
    return fork(ImmutableList.copyOf(cmd));
  }

  private static int fork(List<String> cmd) throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(cmd);
    Process process = pb.start();
    process.getOutputStream().close();
    int rc = process.waitFor();

    List<String> out = CharStreams.readLines(new InputStreamReader(process.getInputStream()));
    List<String> err = CharStreams.readLines(new InputStreamReader(process.getErrorStream()));

    log("Ran command %s\nstdout: %s\nstderr: %s", cmd, out, err);

    return rc;
  }

  private static void log(String line, Object... args) {
    System.out.println(String.format(line, args));
  }
}

@@ -268,16 +268,68 @@
 Chmod %7B%0A
+  private static final boolean LOG_ENABLE = false;%0A%0A
   @Overr
@@ -1331,24 +1331,48 @@
 ... args) %7B%0A
+    if (LOG_ENABLE) %7B%0A  
     System.o
@@ -1406,18 +1406,24 @@
 ne, args));%0A
+    %7D%0A
   %7D%0A%7D%0A

disable logging while running tests