/*
 履歴:
 NO   日付        内容
 001  2021.04.26  新規作成
 */

package com.galaxygoby ;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * <DL>
 * <DT>クラス概要:
 * <DD>共有メモリ作業領域サイズ算出
 * </DL>
 */
public class ShmSizeSet {

	/** タブコード */
	private static final String TAB_CODE = "\t";

	/** コメント行 */
	private static final String COMMENT_LINE = ";";

	/** 環境名 */
	private static final String SHM_ENV_NAME = "SHM_ENV_NAME";

	/** 置換文字列環境名 */
	private static final String REPLACE_SHM_ENV_NAME = "\\$\\{SHM_ENV_NAME\\}";

	/**
	 * <DL>
	 * <DT>共有メモリ作業領域サイズ算出メイン
	 * </DL>
	 * @param args 起動時パラメータ [0]:Table.defパス名 [1]:共有メモリID
	 */
	public static void main(String[] args) {

		int status = 0;
		// パラメータ指定なしの検索確認
		if (args.length == 2) {
			ShmSizeSet instance = new ShmSizeSet();
			status = instance.checkWorkSize(args);
		}
		else {
			status = 100;
		}
		System.exit(status);
	}

	/**
	 * <DL>
	 * <DT>共有メモリ作業領域サイズチェック
	 * </DL>
	 * @param args 起動時パラメータ [0]:Table.defパス名 [1]:共有メモリID
	 * @return チェック結果
	 */
	private int checkWorkSize(String[] args) {

		String fileName = args[0];
		String shmgId = args[1];
		BufferedReader brTableDef = null;
		BufferedReader brTsv = null;
		int maxRows = 0;
		int maxColumns = 0;
		int maxColSize = 0;
		int ret = 0;
		try {
			String envWork = System.getenv("GG_CHARSET");
			String ShmEnvName = System.getenv(SHM_ENV_NAME);
			if (envWork == null) {
				brTableDef = new BufferedReader(
						new InputStreamReader(new FileInputStream(fileName)));
			}
			else {
				brTableDef = new BufferedReader(new InputStreamReader(
						new FileInputStream(fileName), envWork));
			}
			String inBufTableDef = "";
			String inBufTsv = "";
			String[] sptabStr = null;
			for (; (inBufTableDef = brTableDef.readLine()) != null;) {
				sptabStr = inBufTableDef.split(",");
				// コメント行のチェック
				if (COMMENT_LINE.equals(sptabStr[0].substring(0, 1))) {
					continue;
				}
				// 共有メモリグループのチェック
				if (!shmgId.equals(sptabStr[2])) {
					continue;
				}
				brTsv = null;
				if (ShmEnvName != null && ShmEnvName.length() > 0) {
					// 環境名で置換
					sptabStr[4] = sptabStr[4].replaceFirst(REPLACE_SHM_ENV_NAME, ShmEnvName);
				}
				if (envWork == null) {
					brTsv = new BufferedReader(
							new InputStreamReader(new FileInputStream(sptabStr[4])));
				}
				else {
					brTsv = new BufferedReader(new InputStreamReader(
							new FileInputStream(sptabStr[4]), envWork));
				}
				// 列数
				int row = 0;
				// 行数
				int col = 0;
				// データサイズ
				int colSize = 0;
				String workStr = "";
				for (; (inBufTsv = brTsv.readLine()) != null;) {
					row++;
					sptabStr = inBufTsv.split(TAB_CODE);
					// 列数の算出
					if (sptabStr.length > col) {
						col = sptabStr.length;
					}
					// データサイズの算出
					for (int i = 0; i < sptabStr.length; i++) {
						int length = getByteLength(sptabStr[i], envWork);
						if (length > colSize) {
							colSize = length;
//							workStr = sptabStr[i];
						}
					}
				}
				if (brTsv != null) {
					brTsv.close();
					brTsv = null;
				}
				if (row > maxRows) {
					maxRows = row;
				}
				if (col > maxColumns) {
					maxColumns = col;
				}
				if (colSize > maxColSize) {
					maxColSize = colSize;
				}
			}
			System.out.println(shmgId + " " + maxRows + " " + maxColumns + " " + maxColSize);
		} catch (Exception e) {
			e.printStackTrace();
			ret = 100;
		} finally {
			if (brTsv != null) {
				try {
					brTsv.close();
					brTsv = null;
				} catch (IOException e) {
				}
			}
			if (brTableDef != null) {
				try {
					brTableDef.close();
					brTableDef = null;
				} catch (IOException e) {
				}
			}
		}
		return ret;
	}

	/**
	 * <DL>
	 * <DT>バイト数取得
	 * </DL>
	 * @param str 対象文字列
	 * @param charset キャラセット
	 * @return バイト数
	 */
	private int getByteLength(String str, String charset) {
		if (charset == null) {
			return str.getBytes().length;
		}
		else {
			return str.getBytes(Charset.forName(charset)).length;
		}
	}
}
