package com.szabowexler.cookbook;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

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

import com.google.inject.Guice;
import com.szabowexler.cookbook.recipes.RecipeParser;
import com.szabowexler.cookbook.recipes.RecipeTexGenerator;

public class CookbookRunner {
  private final static Logger LOG = LoggerFactory.getLogger(CookbookRunner.class);

  private void run(List<File> files) {
    LOG.info("Running: {} files", files.size());
    files.forEach(this::texify);
  }

  private void texify(File f) {
    if (!f.exists()) {
      LOG.error("{}: does not exist, ignoring", f.getAbsolutePath());
      return;
    }

    if (!f.getName().endsWith(".recipe")) {
      LOG.error("{}: does not end with .recipe, ignoring", f.getAbsolutePath());
      return;
    }

    String tex = RecipeTexGenerator.generateRecipeTex(RecipeParser.parse(f));
    LOG.info("Generated:\n{}", tex);
  }

  public static void main(String[] args) {
    if (args.length == 0) {
      System.out.println("Usage: give us a list of (absolute) *.recipe files, and we'll make 'em pretty!");
      System.exit(0);
    }

    Guice.createInjector(new CookbookModule()).getInstance(CookbookRunner.class)
         .run(Arrays.asList(args).stream()
                    .map(File::new)
                    .collect(Collectors.toList()));
  }
}

@@ -49,16 +49,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -220,16 +220,51 @@
 ctory;%0A%0A
+import com.google.common.io.Files;%0A
 import c
@@ -1020,38 +1020,510 @@
     
-LOG.info(%22Generated:%5Cn%7B%7D
+try %7B%0A      writeTex(f, tex);%0A    %7D catch (IOException ex) %7B%0A      LOG.error(%22Unable to write tex file for %7B%7D%22, f.getAbsolutePath(), ex);%0A    %7D%0A  %7D%0A%0A  private static void writeTex(File originalRecipeFile, String tex) throws IOException %7B%0A    File texFile = new File(%0A        originalRecipeFile.getAbsolutePath()%0A                          .replace(%22/recipies/
 %22, 
+%22/
 tex
+/%22)%0A                          .replace(%22.recipe%22, %22.tex%22));%0A    Files.createParentDirs(texFile);%0A    Files.write(tex.getBytes(), texFile
 );%0A 

Write tex out to parallel dir structure