/**
 * Copyright (c) 2015, Lucee Assosication Switzerland. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package lucee;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import lucee.loader.TP;
import lucee.loader.engine.CFMLEngineFactory;

/**
 * returns th current built in version
 */
public class VersionInfo {

	private static org.osgi.framework.Version version = null;
	private static long created = -1;

	/**
	 * @return returns the current version
	 */
	public static org.osgi.framework.Version getIntVersion() {
		init();
		return version;
	}

	/**
	 * return creattion time of this version
	 * 
	 * @return creattion time
	 */
	public static long getCreateTime() {
		init();
		return created;
	}

	private static void init() {
		if (version != null) return;
		String content = "9000000:" + System.currentTimeMillis();
		try {
			content = getContentAsString(new TP().getClass().getClassLoader()
					.getResourceAsStream("lucee/version"), "UTF-8");

		} catch (IOException e) {
		}

		int index = content.indexOf(':');
		version = CFMLEngineFactory.toVersion(content.substring(0, index),CFMLEngineFactory.VERSION_ZERO);
		created = Long.parseLong(content.substring(index + 1));

	}

	private static String getContentAsString(InputStream is, String charset)
			throws IOException {

		BufferedReader br = (charset == null) ? new BufferedReader(
				new InputStreamReader(is)) : new BufferedReader(
				new InputStreamReader(is, charset));
		StringBuffer content = new StringBuffer();

		String line = br.readLine();
		if (line != null) {
			content.append(line);
			while ((line = br.readLine()) != null) {
				content.append("\n" + line);
			}
		}
		br.close();
		return content.toString();
	}
}
/**
 * Copyright (c) 2015, Lucee Assosication Switzerland. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package lucee;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import lucee.loader.TP;
import lucee.loader.engine.CFMLEngineFactory;

/**
 * returns th current built in version
 */
public class VersionInfo {

	private static org.osgi.framework.Version version = null;
	private static long created = -1;

	/**
	 * @return returns the current version
	 */
	public static org.osgi.framework.Version getIntVersion() {
		init();
		return version;
	}

	/**
	 * return creattion time of this version
	 * 
	 * @return creattion time
	 */
	public static long getCreateTime() {
		init();
		return created;
	}

	private static void init() {
		if (version != null) return;
		String content = "9000000:" + System.currentTimeMillis();
		try {
			content = getContentAsString(new TP().getClass().getClassLoader()
					.getResourceAsStream("lucee/version"), "UTF-8");

		} catch (IOException e) {
		}

		int index = content.indexOf(':');
		version = CFMLEngineFactory.toVersion(content.substring(0, index),CFMLEngineFactory.VERSION_ZERO);
		
		String d=content.substring(index + 1);
		try{
			created = Long.parseLong(d);
		}
		catch(NumberFormatException nfe){
			try {
				created=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z").parse(d).getTime();
			} catch (ParseException pe) {
				pe.printStackTrace();
				created=0;
			}
		}


	}

	private static String getContentAsString(InputStream is, String charset)
			throws IOException {

		BufferedReader br = (charset == null) ? new BufferedReader(
				new InputStreamReader(is)) : new BufferedReader(
				new InputStreamReader(is, charset));
		StringBuffer content = new StringBuffer();

		String line = br.readLine();
		if (line != null) {
			content.append(line);
			while ((line = br.readLine()) != null) {
				content.append("\n" + line);
			}
		}
		br.close();
		return content.toString();
	}
}
make the parsing of the version file more flexible