#!/usr/bin/env python
#
# Python script for checking versions of python, java and mosflm
#

#
# Welcome message
#

print "-------------------------------------------------"
print
print "Test of the DNA installation"
print

import os, sys, string
if not "DNAHOME" in os.environ.keys():
    print "ERROR! DNAHOME not defined."
    sys.exit(1)
DNAHOME=os.getenv("DNAHOME")

print "DNAHOME currently set to:"
print DNAHOME
print

sys.path.append(os.path.join(DNAHOME, "xsd/python"))
try:
    import XSD
except:
    print "ERROR when trying to import XSD - please check the"
    print "DNAHOME variable!"
    sys.exit(1)

if not "DNANAME" in os.environ.keys():
    print "ERROR! DNANAME not defined."
    sys.exit(1)
DNANAME=os.getenv("DNANAME")

print "DNANAME currently set to:"
print DNANAME
print

VERBOSE = 0

if len(sys.argv) > 1:
    # changed the check to allow more command line options
    if "-v" in sys.argv:
        VERBOSE = 1

#
# Check that the system defaults configuration is there
#

if "DNA_SYSTEM_DEFAULTS" in os.environ.keys():
    DNA_SYSTEM_DEFAULTS = os.environ["DNA_SYSTEM_DEFAULTS"]
else: 
    DNA_SYSTEM_DEFAULTS = os.path.join(DNAHOME, "config", DNANAME, "system_defaults.xml")
if not os.access(DNA_SYSTEM_DEFAULTS, os.F_OK):
    print "ERROR! The system defaults configuration file is not available/accessible:"
    print DNA_SYSTEM_DEFAULTS
    sys.exit(1)

if VERBOSE:
    print "Reading system defaults configuration from:"
    print DNA_SYSTEM_DEFAULTS
    print

try:
    system_defaults = XSD.System_defaults()
    sd_file=open(DNA_SYSTEM_DEFAULTS,"r")
    system_defaults_xml = sd_file.read()
    sd_file.close()
    system_defaults.unmarshal(system_defaults_xml)
except:
    print "Error when parsing the system defaults configuration!"
    print
    raise

print "System defaults configuration file seems to be ok..."
print

#
# Check that the java properties are there.
#

if "DNAJAVAPROPERTIES" in os.environ.keys():
    DNAJAVAPROPERTIES = os.environ["DNAJAVAPROPERTIES"]
else:
    DNAJAVAPROPERTIES = os.path.join(DNAHOME, "config", DNANAME, "java.properties")

if VERBOSE:
    print "Reading java properties from:"
    print DNAJAVAPROPERTIES
    print

if not os.access(DNAJAVAPROPERTIES, os.F_OK):
    print "ERROR! The java properties file is not available/accessible:"
    print DNAJAVAPROPERTIES
    sys.exit(1)


#
# Start with python, check that we have version >= 2.3.0
#

try:
    print "Checking the Python version:"
    print sys.version
    print
    version_info = sys.version_info
    if version_info[0] < 2:
        print "Wrong version of python: %s" % sys.version
        print "DNA requires version 2.3.0 or higher"
        sys.exit(1)
    elif version_info[1] < 3:
            print "Wrong version of python: %s" % sys.version
            print "DNA requires version 2.3.0 or higher"
            sys.exit(1)
    if VERBOSE:
        print "This version of Python ok."
        print
except:
    print "ERROR during python version check."
    raise

#
# Now check the version of java
#

if "DNAJDK" in os.environ.keys():
    DNAJDK=os.getenv("DNAJDK")
    JAVA_PATH=os.path.join(DNAJDK,"bin","java")
else:
    JAVA_PATH="java"

try:
    print "Checking the java version:"
    os.system("rm -rf java.tmp")
    os.system("bash -c \"%s -version > java.tmp 2>&1\"" % JAVA_PATH)
    if VERBOSE:
        os.system("cat java.tmp")
        print
    f = open("java.tmp", "r")
    java_version = f.readlines()
    f.close()
    if string.find(java_version[0], "command not found") != -1:
        print "ERROR - no java command available!"
        print "Make sure that java version 1.4.0 or higher is installed"
        print "and that the path is correctly configured."
        sys.exit(1)
    print java_version[0]
    print
    version_string = string.split(java_version[0])[2]
    version_string = string.replace(version_string, '"', '')
    version_list = string.split(version_string, ".")
    major_version = string.atoi(version_list[0])
    minor_version = string.atoi(version_list[1])
    if major_version != 1 or minor_version < 4:
        print "You have an obsolete version of java - please install"
        print "java version 1.4.0 or higher."
        sys.exit(1)
    if VERBOSE:
        print "This version of java is ok."
        print
except:
    print "ERROR during java version check."
    raise

#
# Checking the version of MOSFLM
#

try:
    print "Checking the MOSFLM version:"
    mosflm_executable = system_defaults.getServer_data().getMosflm_executable()
    if VERBOSE:
        print "MOSFLM executable: %s" % mosflm_executable
        print
    os.system("rm -rf mosflm.tmp")
    os.system("bash -c \"echo \"END\" | %s > mosflm.tmp 2>&1\"" % mosflm_executable)
    if VERBOSE:
        os.system("cat mosflm.tmp")
        print
    f = open("mosflm.tmp", "r")
    mosflm_version = f.readlines()
    f.close()
    if string.find(mosflm_version[0], "command not found") != -1:
        print "ERROR - no %s command available!" % mosflm_executable
        print "Make sure that mosflm version 6.2.3 or higher is installed"
        print "and that the path is correctly configured."
        sys.exit(1)
    #
    # Extract the version number
    #
    for line in mosflm_version:
        if string.find(line, "Version") != -1:
            break
    print line
    print 
    version_string = string.split(line)[2]
    version_string = string.replace(version_string, '"', '')
    version_list = string.split(version_string, ".")
    major_version = string.atoi(version_list[0])
    mid_version = string.atoi(version_list[1])
    minor_version = string.atoi(version_list[2])
    version_flag = 1
    if major_version < 6:
        #
        # Unlikely, but one never knows!
        #
        version_flag = 0
    if major_version == 6 and mid_version < 2:
        #
        # Wrong version...
        #
        version_flag = 0
    if major_version == 6 and mid_version == 2 and minor_version < 5:
        #
        # Also wrong version...
        #
        version_flag = 0
    if not version_flag:
        print "You have configured DNA to use an obsolete version of mosflm."
        print "Please install and configure DNA to use mosflm version 6.2.5 or higher."
        sys.exit(1)
    if VERBOSE:
        print "This version of mosflm is ok."
        print
except:
    print "ERROR during MOSFLM version check."
    raise
   
