package com.camunda.bpm.engine.rest;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class TemplateParser {

  public static void main(String[] args) throws ParseException, IOException, TemplateException {

    if (args.length != 3) {
      throw new RuntimeException("Must provide two arguments: <source template directory> <main template> <output directory>");
    }

    String sourceDirectory = args[0];
    String mainTemplate = args[1];
    String outputFile = args[2];

    Configuration cfg = new Configuration();

    cfg.setDirectoryForTemplateLoading(new File(sourceDirectory));
    cfg.setDefaultEncoding("UTF-8");

    Template template = cfg.getTemplate(mainTemplate);

    Map<String, Object> templateData = createTemplateData();

    try (StringWriter out = new StringWriter()) {

      template.process(templateData, out);

      // format json with Gson
      String jsonString = out.getBuffer().toString();
      String formattedJson = formatJsonString(jsonString);

      File outFile = new File(outputFile);
      FileUtils.forceMkdir(outFile.getParentFile());
      Files.write(outFile.toPath(), formattedJson.getBytes());
    }
  }

  private static Map<String, Object> createTemplateData() {
    Map<String, Object> templateData = new HashMap<>();

    String version = TemplateParser.class.getPackage().getImplementationVersion();

    // docsVersion = 7.X.Y
    templateData.put("cambpmVersion", version);

    if (version.contains("SNAPSHOT")) {
      templateData.put("docsVersion", "develop");
    } else {
      // docsVersion = 7.X
      templateData.put("docsVersion", version.substring(0, version.lastIndexOf(".")));
    }
    return templateData;
  }

  private static String formatJsonString(String jsonString) {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    JsonParser jsonParser = new JsonParser();
    JsonElement json = jsonParser.parse(jsonString);
    String formattedJson = gson.toJson(json);

    return formattedJson;
  }

}

@@ -1,8 +1,832 @@
+/*%0D%0A * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH%0D%0A * under one or more contributor license agreements. See the NOTICE file%0D%0A * distributed with this work for additional information regarding copyright%0D%0A * ownership. Camunda licenses this file to you under the Apache License,%0D%0A * Version 2.0; you may not use this file except in compliance with the License.%0D%0A * You may obtain a copy of the License at%0D%0A *%0D%0A *     http://www.apache.org/licenses/LICENSE-2.0%0D%0A *%0D%0A * Unless required by applicable law or agreed to in writing, software%0D%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0D%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0D%0A * See the License for the specific language governing permissions and%0D%0A * limitations under the License.%0D%0A */%0D%0A
 package 

add license header