/**
 * BEGIN_COPYRIGHT
 *
 * Copyright (C) 2014-2015 CRS4.
 *
 * 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.
 *
 * END_COPYRIGHT
 */

package it.crs4.features;

import java.io.File;

import loci.formats.ImageReader;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.ParseException;

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


public final class ImageToAvro {

  private static final Logger LOGGER = LoggerFactory.getLogger(
      ImageToAvro.class);

  private ImageToAvro() {}

  private static CommandLine parseCmdLine(Options opts, String[] args)
      throws ParseException {
    opts.addOption("o", "outdir", true, "write avro files to this dir");
    CommandLineParser parser = new GnuParser();
    return parser.parse(opts, args);
  }

  public static void main(String[] args) throws Exception {
    Options opts = new Options();
    CommandLine cmd = null;
    try {
      cmd = parseCmdLine(opts, args);
    } catch (ParseException e) {
      System.err.println("ERROR: " + e.getMessage());
      System.exit(1);
    }
    String fn = null;
    try {
      fn = cmd.getArgs()[0];
    } catch (ArrayIndexOutOfBoundsException e) {
      HelpFormatter fmt = new HelpFormatter();
      fmt.printHelp("java ImageToAvro IMG_FILE", opts);
      System.exit(2);
    }
    String outDirName = null;
    if (cmd.hasOption("outdir")) {
      outDirName = cmd.getOptionValue("outdir");
    }

    String name = PathTools.stripext(PathTools.basename(fn));
    ImageReader reader = new ImageReader();
    reader.setId(fn);
    LOGGER.info("Reading from {}", fn);
    BioImgFactory factory = new BioImgFactory(reader);
    int seriesCount = factory.getSeriesCount();

    // FIXME: add support for XY slicing
    String seriesName;
    String outFn;
    for (int i = 0; i < seriesCount; i++) {
      if (seriesCount <= 1) {
        seriesName = name;
      } else {
        seriesName = String.format("%s_%d", name, i);
      }
      outFn = new File(outDirName, seriesName + ".avro").getPath();
      factory.setSeries(i);
      factory.writeSeries(seriesName, outFn);
      LOGGER.info("Writing to {}", outFn);
    }
    reader.close();
    LOGGER.info("All done");
  }

}

@@ -2090,24 +2090,268 @@
 (%22outdir%22);%0A
+      File outDir = new File(outDirName);%0A      if (!outDir.exists()) %7B%0A        boolean ret = outDir.mkdirs();%0A        if (!ret) %7B%0A          System.err.format(%22ERROR: can't create %25s%5Cn%22, outDirName);%0A          System.exit(3);%0A        %7D%0A      %7D%0A
     %7D%0A%0A    S
@@ -2747,82 +2747,8 @@
 ) %7B%0A
-      if (seriesCount %3C= 1) %7B%0A        seriesName = name;%0A      %7D else %7B%0A  
     
@@ -2795,24 +2795,16 @@
 me, i);%0A
-      %7D%0A
       ou

create out dir if nonexistent; always add series idx tag