/* ====================================================================
  Copyright 2017 Quanticate Ltd

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
==================================================================== */
package com.quanticate.opensource.pdftkbox;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionGroup;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 * PDFtk bookmark replacement, powered by Apache PDFBox
 */
public class PDFtkBox {
   public static void main(String[] args) throws Exception {
      // For printing help
      Options optsHelp = new Options();
      optsHelp.addOption(
            Option.builder("help")
            .required()
            .desc("print this message")
            .build()
      );
      
      // Normal-style import/export
      Options optsNormal = new Options();
      OptionGroup normal = new OptionGroup();
      Option optExport =
            Option.builder("export")
            .required()
            .hasArg()
            .desc("export bookmarks from pdf" )
            .argName("pdf").build();
      normal.addOption(optExport);
      Option optImport = 
            Option.builder("import")
            .required()
            .hasArg()
            .desc("import bookmarks to pdf" )
            .argName("pdf").build();
      normal.addOption(optImport);
      optsNormal.addOptionGroup(normal);
      Option optBookmarks = 
            Option.builder("bookmarks")
            .hasArg()
            .desc("bookmarks definition file" )
            .argName("bookmarks").build();
      optsNormal.addOption(optBookmarks);
      
      // TODO PDFtk style options

      
      // What are we doing?
      CommandLineParser parser = new DefaultParser();
      
      // Did they want help?
      try {
         parser.parse(optsHelp, args);
         
         // If we get here, they asked for help
         doPrintHelp(optsHelp, optsNormal);
         return;
      } catch (ParseException pe) {}
      
      // Normal-style import/export?
      try {
         CommandLine line = parser.parse(optsNormal, args);
         
         if (line.hasOption(optExport.getOpt())) {
            doExport( line.getOptionValue(optExport.getOpt()), 
                      line.getOptionValue(optBookmarks.getOpt()), 
                      line.getArgs() );
            return;
         }
         if (line.hasOption(optImport.getOpt()) && line.getArgs().length > 0) {
            doImport( line.getOptionValue(optImport.getOpt()),
                      line.getOptionValue(optBookmarks.getOpt()), 
                      line.getArgs() );
            return;
         }
      } catch (ParseException pe) {}
      
      // TODO PDFtk style options
      
      doPrintHelp(optsHelp, optsNormal);
   }
   
   protected static void doPrintHelp(Options optsHelp, Options optsNormal) {
      // TODO More helpful help
      HelpFormatter formatter = new HelpFormatter();
      formatter.printHelp("PDFtkBox", optsNormal);
      formatter.printHelp("PDFtkBox", optsHelp);
   }
   
   protected static void doExport(String pdf, String bookmarks, String[] args) throws IOException {
      Bookmarks bm = new Bookmarks(new File(pdf));
      
      File bmf = null;
      if (bookmarks != null) {
         bmf = new File(bookmarks);
      } else if (args.length > 0) {
         bmf = new File(args[0]);
      }
      
      PrintWriter output;
      if (bmf == null) {
         output = new PrintWriter(System.out);
      } else {
         output = new PrintWriter(bmf, "UTF-8");
      }
      
      bm.exportBookmarks(output);
      output.close();
      bm.close();
   }
   protected static void doImport(String pdf, String bookmarks, String[] args) throws IOException {
      Bookmarks bm = new Bookmarks(new File(pdf));

      InputStream istream;
      if (bookmarks != null) {
         istream = new FileInputStream(new File(bookmarks));
      } else {
         istream = System.in;
      }
      BufferedReader input = new BufferedReader(new InputStreamReader(istream,"UTF-8"));

      bm.importBookmarks(input, new File(args[0]));
      
      input.close();
      bm.close();
   }
}
@@ -2538,37 +2538,32 @@
 %0A      %0A      //
- TODO
  PDFtk style opt
@@ -2566,16 +2566,810 @@
  options
+%0A      Options optsPDFtk = new Options();%0A      OptionGroup pdftk = new OptionGroup();%0A      Option optDumpData =%0A            Option.builder(%22dump_data%22)%0A            .required()%0A            .hasArg()%0A            .desc(%22dump bookmarks from pdf%22 )%0A            .argName(%22pdf%22).build();%0A      pdftk.addOption(optDumpData);%0A      Option optUpdateInfo = %0A            Option.builder(%22update_info%22)%0A            .required()%0A            .hasArg()%0A            .desc(%22update bookmarks in pdf%22 )%0A            .argName(%22pdf%22).build();%0A      pdftk.addOption(optUpdateInfo);%0A      optsPDFtk.addOptionGroup(pdftk);%0A      Option optOutput = %0A            Option.builder(%22output%22)%0A            .hasArg()%0A            .desc(%22output to file%22 )%0A            .argName(%22file%22).build();%0A      optsPDFtk.addOption(optOutput);
 %0A%0A      
@@ -3629,32 +3629,43 @@
 Help, optsNormal
+, optsPDFtk
 );%0A         retu
@@ -4484,24 +4484,35 @@
 , optsNormal
+, optsPDFtk
 );%0A   %7D%0A   %0A
@@ -4584,16 +4584,35 @@
 tsNormal
+, Options optsPDFtk
 ) %7B%0A    
@@ -4739,24 +4739,112 @@
 ptsNormal);%0A
+      // TODO Nobble output for pdftk%0A      formatter.printHelp(%22PDFtkBox%22, optsPDFtk);%0A
       format

Prepare for pdftk-style arguments