
import os, glob

from ..runtimetool import RuntimeTool
from .javatoolmixin import JavaToolMixIn

class javaTool( RuntimeTool, JavaToolMixIn ):
    """Java Runtime Environment.
    
Home: http://openjdk.java.net/

Due to issues with Oracle's licensing, cid
supports only automatic installation of OpenJDK.

javaVer supports:
- only one digit like 6, 7, 8, 9 (Zulu JDK (http://zulu.org)
- use javaBin for custom JRE
"""
    def envNames( self ) :
        return ['javaBin', 'javaVer']
    
    def getVersionParts( self ):
        return 1
    
    def _installTool( self, env ):
        if env.get('javaBin', None):
            return

        ver = env['javaVer']
        
        if self._isMacOS():
            if ver == '8':
                self._requireDmg('https://cdn.azul.com/zulu/bin/zulu8.20.0.5-jdk8.0.121-macosx_x64.dmg')
            elif ver == '7':
                self._requireDmg('https://cdn.azul.com/zulu/bin/zulu7.17.0.5-jdk7.0.131-macosx_x64.dmg')
            return
        
        self._addAptRepo('zulu', 'deb http://repos.azulsystems.com/debian stable main', self._ZULU_GPG_KEY)
        self._addYumRepo('zulu', 'http://repos.azulsystems.com/rhel/zulu.repo', self._ZULU_GPG_KEY, releasevermax=7)
        self._addZypperRepo('zulu', 'http://repos.azulsystems.com/sles/latest', self._ZULU_GPG_KEY)
        
        # leaving here for possible future use
        #self._requireDeb(['openjdk-{0}-jre-headless'.format(ver)])
        #self._requireYum(['java-1.{0}.0-openjdk'.format(ver)])
        #self._requireZypper(['java-1_{0}_0-openjdk'.format(ver)])
        self._requirePackages(['zulu-{0}'.format(ver)])
        
        self._requirePacman(['jre{0}-openjdk'.format(ver)])
        self._requireEmerge(['=dev-java/oracle-jre-bin-1.{0}*'.format(ver)])

    
    def uninstallTool( self, env ):
        pass

    def initEnv( self, env ) :
        if env.get('javaBin', None):
            bin_dir = os.path.dirname(env['javaBin'])
            java_home = os.path.dirname(env['javaBin'])
            
            os.environ['JAVA_HOME'] = java_home
            os.environ['JRE_HOME'] = java_home
            self._addBinPath(bin_dir, True)
            
            super(javaTool, self).initEnv( env, 'java' )
            return
        
        env.setdefault('javaVer', self._LATEST_JAVA)
        ver = env['javaVer']
        
        candidates = [
            # Zulu
            '/usr/lib/jvm/zulu-{0}*/jre/bin/java'.format(ver),
            # Debian / Ubuntu
            #"/usr/lib/jvm/java-{0}-openjdk*/jre/bin/java".format(ver),
            # RedHat
            #"/usr/lib/jvm/jre-1.{0}.0/bin/java".format(ver),
            # OpenSuse
            #"/usr/lib*/jvm/jre-1.{0}.0/bin/java".format(ver),
            # Default oracle
            #"/opt/jdk/jdk1.{0}*/bin/java".format(ver),
        ]

        if self._isGentoo() or self._isArchLinux():
            candidates += [
                "/usr/lib/jvm/java-{0}-openjdk*/jre/bin/java".format(ver),
            ]
        
        for c in candidates:
            bin_name = glob.glob(c)
            
            if bin_name:
                bin_name = bin_name[0]
                bin_dir = os.path.dirname(bin_name)
                java_home = os.path.dirname(bin_dir)
                
                env['javaBin'] = bin_name
                os.environ['JAVA_HOME'] = java_home
                os.environ['JRE_HOME'] = java_home
                self._addBinPath(bin_dir, True)
                self._have_tool = True
                break
                
    def onRun( self, config, file, args, tune ):
        env = config['env']
        self._callInteractive([
            env[self._name + 'Bin'], '-jar', file
        ] + args)
            
    _ZULU_GPG_KEY = '''
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1

mQINBFNgFa8BEADTL/REB10M+TfiZOtFHqL5LHKkzTMn/O2r5iIqXGhi6iwZazFs
9S5g1eU7WMen5Xp9AREs+OvaHx91onPZ7ZiP7VpZ6ZdwWrnVk1Y/HfI59tWxmNYW
DmKYBGMj4EUpFPSE9EnFj7dm1WdlCvpognCwZQl9D3BseGqN7OLHfwqqmOlbYN9h
HYkT+CaqOoWDIGMB3UkBlMr0GuujEP8N1gxg7EOcSCsZH5aKtXubdUlVSphfAAwD
z4MviB39J22sPBnKmaOT3TUTO5vGeKtC9BAvtgA82jY2TtCEjetnfK/qtzj/6j2N
xVUbHQydwNQVRU92A7334YvCbn3xUUNI0WOscdmfpgCU0Z9Gb2IqDb9cMjgUi8F6
MG/QY9/CZjX62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