package com.jiayue.ipfcst.console.util;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;

/**
 * 生成console的linux启动脚本
 *
 * @author xsl
 * @version 3.0
 */
public class CreateConsoleSh {
  public static void main(String[] args) {
    System.out.println("==============执行console的启动sh文件生成=============");
    FileOutputStream os = null;
    try {
      VelocityEngine ve = new VelocityEngine();
      ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
      ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
      ve.init();
      Template t = ve.getTemplate("start-console.vm");
      VelocityContext ctx = new VelocityContext();
      ctx.put("version", args[0]);
      StringWriter sw = new StringWriter();
      t.merge(ctx, sw);
      os = new FileOutputStream("../syjy/ipfcstV3/bin/start-console.sh");
      // 采用UTF-8字符集
      os.write(sw.toString().getBytes("UTF-8"));
      os.flush();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (os != null) {
        try {
          os.close();
        } catch (IOException e) {
          System.out.println(e);
        }
      }
    }
  }
}
