#!/usr/bin/env python
#
# Python script for checking the DNA configuration and version of MOSFLM
#

import os, sys, string

VERBOSE = 0
current_mosflm_version_line = " ************ Version 6.2.5 for Image plate and CCD data 1st October 2004  ***********"

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

def vprint(message=""):
    if VERBOSE:
        print message

#
# Welcome message
#

vprint("-------------------------------------------------")
vprint
vprint("Test of the DNA installation - checking configuration")
vprint("and version of MOSFLM")
vprint

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

vprint("DNAHOME currently set to: %s" % DNAHOME)

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")

vprint("DNANAME currently set to: %s" % DNANAME)
vprint


#
# Check that the configuration files are there:
#

#
# DNA_SYSTEM_DEFAULTS
#
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)

vprint("Reading system defaults configuration from:")
vprint(DNA_SYSTEM_DEFAULTS)

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

vprint("System defaults configuration file seems to be ok.")
vprint

#
# DNA_USER_DEFAULTS
#
if "DNA_USER_DEFAULTS" in os.environ.keys():
    DNA_USER_DEFAULTS = os.environ["DNA_USER_DEFAULTS"]
else: 
    DNA_USER_DEFAULTS = os.path.join(os.environ["HOME"], ".dna", "user_defaults.xml")
if not os.access(DNA_USER_DEFAULTS, os.F_OK):
    print("ERROR! The user defaults configuration file is not available/accessible:")
    print(DNA_USER_DEFAULTS)
    sys.exit(1)

vprint("Reading user defaults configuration from:")
vprint(DNA_USER_DEFAULTS)

try:
    user_defaults = XSD.User_defaults()
    sd_file=open(DNA_USER_DEFAULTS,"r")
    user_defaults_xml = sd_file.read()
    sd_file.close()
    user_defaults.unmarshal(user_defaults_xml)
except:
    print("Error when parsing the user defaults configuration!")
    print
    raise

vprint("User defaults configuration file seems to be ok.")
vprint


#
# 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")

vprint("Reading java properties from:")
vprint(DNAJAVAPROPERTIES)

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

vprint("Java properties file seems to be ok.")
vprint

#
# Before checking the version of MOSFLM, make sure that
# CCP4 is configured
#

if not os.environ.has_key("CCP4"):
    print "ERROR - couldn't find CCP4 installation."
    print "In order for DNA to work you must have CCP4 version 5.0 or higher"
    print "installed and configured."
    sys.exit(1)


#
# Checking the version of MOSFLM
#

try:
    vprint("Checking the MOSFLM version:")
    mosflm_executable = system_defaults.getServer_data().getMosflm_executable()
    vprint("MOSFLM executable: %s" % mosflm_executable)
    vprint()
    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)
    #
    # Check the version line
    #
    for line in mosflm_version:
        if string.find(line, "Version") != -1:
            break
    line = line[:-1]
    vprint(line)
    if line != current_mosflm_version_line :
        print
        print "DNA is using the wrong version of MOSFLM!"
        print
        print "This is the version used:"
        print line
        print
        print "DNA should be using this version:"
        print current_mosflm_version_line
        print
        print "Please check that your PATH variable is correctly set."
        print "The correct version of MOSFLM for Linux should be available"
        print "in $DNAHOME/third_party/mosflm, i.e. in"
        print os.path.join(DNAHOME, "third_party/mosflm")
        sys.exit(1)
    if VERBOSE:
        vprint("This version of mosflm is ok.")
        vprint
except:
    vprint("ERROR during MOSFLM version check.")
    raise
   
