package se.soy.gpg;

import java.util.List;
import java.util.ArrayList;

public class GPG {
  // FIXME Remove when done
  static<T> void println(T arg) { System.out.println(arg); }

  /*
  FIXME: Add as tests
  GPG.decrypt(java.io.File).output();
  GPG.decrypt("string").output(System.out, "println");
  GPG.decrypt("string").output(java.io.File);
  */

  private final List<String> pre_command = new ArrayList<String>(Arrays.asList(
    "gpg",
    "--default-recipient-self",
    "--no-tty",
    "--batch",
    "--yes"
    )
  );
  private List<String> command = new ArrayList<String>();
  private static GPG gpg = null;
  private String data;

  public void output() {
    println("Command: " + pre_command + command);
    println("----------");
    gpg = null;
  }

  public <T> void output(T file) {
    println("Outputing to: " + file);
    output();
  }

  public static GPG encrypt(String data) {
    gpg = (null == gpg) ? new GPG() : gpg;
    gpg.command.add("--encrypt");
    // FIXME secureErase()
    gpg.data = data;
    return gpg;
  }

  public static GPG decrypt(String data) {
    gpg = (null == gpg) ? new GPG() : gpg;
    gpg.command.add("--decrypt");
    // FIXME secureErase()
    gpg.data = data;
    return gpg;
  }

  public static GPG sign() {
    gpg = (null == gpg) ? new GPG() : gpg;
    gpg.command.add("--sign");
    return gpg;
  }

  public GPG armor() {
    command.add("--armor");
    return this;
  }

  // TODO: Add recipients(List<String> recipients)
  public GPG recipient(String recipient) {
    command.add("--recipient");
    command.add(recipient);
    return this;
  }

  public GPG localUser(String localUser) {
    command.add("--local-user");
    command.add(localUser);
    return this;
  }

  public GPG home(String home) {
    command.add("--home");
    command.add(home);
    return this;
  }
}

@@ -1035,32 +1035,1246 @@
 eturn gpg;%0A  %7D%0A%0A
+  public static GPG decrypt(File file) %7B%0A    gpg = (null == gpg) ? new GPG() : gpg;%0A    gpg.command.add(%22--decrypt%22);%0A    // FIXME secureErase()%0A    try %7B%0A      gpg.data = readFileAsString(file.toString(), null);%0A    %7D%0A    catch (IOException e) %7B%0A      gpg.data = null;%0A    %7D%0A    return gpg;%0A  %7D%0A%0A  // http://ptspts.blogspot.com/2009/11/how-to-read-whole-file-to-string-in.html%0A  public static String readFileAsString(String fileName, String charsetName)%0A    throws java.io.IOException %7B%0A    java.io.InputStream is = new java.io.FileInputStream(fileName);%0A    try %7B%0A      final int bufsize = 4096;%0A      int available = is.available();%0A      byte data%5B%5D = new byte%5Bavailable %3C bufsize ? bufsize : available%5D;%0A      int used = 0;%0A      while (true) %7B%0A        if (data.length - used %3C bufsize) %7B%0A          byte newData%5B%5D = new byte%5Bdata.length %3C%3C 1%5D;%0A          System.arraycopy(data, 0, newData, 0, used);%0A          data = newData;%0A        %7D%0A        int got = is.read(data, used, data.length - used);%0A        if (got %3C= 0) break;%0A        used += got;%0A      %7D%0A      return charsetName != null ? new String(data, 0, used, charsetName)%0A        : new String(data, 0, used);%0A    %7D finally %7B%0A      is.close();%0A    %7D%0A  %7D%0A%0A%0A
   public static 

Add the possibility to decrypt from a file