package com.eactive.eai.rms.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.apache.log4j.Logger;

public final class Version {

    private static String title;
    private static String version;
    private static String buildOn;
    private static final String MANIFEST_PATH = "/META-INF/MANIFEST.MF";
    private static final String VERSION_PATH = "/"
            + Version.class.getName().replace('.', '/') + ".class";
    
    private static Logger logger = Logger.getLogger(Version.class);

    static {
        InputStream is = null;

        try {
            Manifest m = null;
            URL url = Version.class.getResource(VERSION_PATH);

            if (url != null && (url != null && "jar".equals(url.getProtocol())
            		 || "zip".equals(url.getProtocol())|| "vfszip".equals(url.getProtocol()))) {
            	  if(url != null) { //취약점소스 수정 2020.08.25
            		  m = getManifestByJar(url);
            	  }
            } else {
            	 if(url != null) { //취약점소스 수정 2020.08.25
            		 m = getManifestByFile(url);
            	 }
            }

            Attributes attributes = m.getMainAttributes();

            version = attributes.getValue("Implementation-Version");
            buildOn = attributes.getValue("Implementation-Date");
            title = attributes.getValue("Implementation-Title");

        } catch (Exception e) {
        	//취약점소스 수정 2020.08.24
        	logger.error(" error", e); 
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    //e.printStackTrace();
                }
            }
        }

        if (version == null) {
            version = "UNKNOWN";
        }

        if (buildOn == null) {
            version = "UNKNOWN";
        }
    }

    private static Manifest getManifestByJar(URL url) throws Exception {
    	String jarFilePath = url.getFile().replaceFirst(VERSION_PATH,
            "").substring(1);
        
        return new JarFile(new File(jarFilePath)).getManifest();
    }

    private static Manifest getManifestByFile(URL url) throws Exception {
        String path = url.getFile().replaceFirst(VERSION_PATH, MANIFEST_PATH);
        FileInputStream fis = null;
        Manifest m = null;
        
        try {
			fis = new FileInputStream(path);
			m = new Manifest(fis);
		}
		catch(Exception ex) {
			logger.error(" error", ex);
		}
		finally {
			if(fis != null) try { fis.close(); } catch(Exception ee) {logger.error("FileInputStream error");}
		}
        
        return m;
    }

    public static String getVersionInfomation() {
        return "eLink RMS Version " + version + " build on " + buildOn +", Title " + title;
    }

    public static String getBuildOn() {
        return buildOn;
    }

    public static String getVersion() {
        return version;
    }

//    public static void main(String[] args) { //취약점소스 수정 2020.08.24
//        System.out.println(getVersionInfomation());
//    }

}
