"""
     python/ui/resources.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2008 University of York, CCLRC
     Copyright (C) 2009-2010 University of York

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     This program 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.
"""

from utils import *
import os
from string import *

from mgapp import *
from mmdb2 import *
from  mmut import *
import ccp4srs
import mmut
import mmdb2

#------------------------------------------------------------------
# Interface with the C/C++ resources for handling models
#------------------------------------------------------------------
class CResources:

  CMolBondParams = ''
  # The monomer database distributed from EBI
  sbase = ''
  monomers_dir = ''
  ener_lib = ''
  mon_lib = ''
  ele_lib = ''
  
  def __del__(self):
    if os.environ.has_key("CCP4MG_DEBUG_MEMORY"):
      print "resources __del__"

#------------------------------------------------------------------
  def __init__(self):
#------------------------------------------------------------------
    # Initialise mmdb
    InitMatType()

#--------------------------------------------------------------------------------
  def load_resources(self):
#--------------------------------------------------------------------------------
    import utils
    CResources.mon_lib = os.path.join(os.environ["CCP4_MGDATA"] , "mon_lib_list")
    # Is the Refmac Monomer library accessible
    if os.environ.has_key("MONOMER_LIBRARY") and os.path.exists(os.environ['MONOMER_LIBRARY']):
      CResources.monomers_dir =  os.environ['MONOMER_LIBRARY']
      #Make sure there is a terminating slash
      if not ['/','\\'].count(CResources.monomers_dir[-1]):
        md = os.path.join(CResources.monomers_dir,'a')
        CResources.monomers_dir = md[0:-1]
    #elif os.environ.has_key("CCP4"):
    #  mon_dir =   os.path.join(os.environ['CCP4'],'lib','data','monomers','a')
    #  CResources.monomers_dir = mon_dir[0:len(mon_dir)-1]
    if  CResources.monomers_dir and not os.path.exists(CResources.monomers_dir):
      CResources.monomers_dir = ""

    CResources.user_monomers_dir = os.path.join(utils.get_CCP4MG(),'data','monomer_library')
    #print "user_monomers_dir", CResources.user_monomers_dir
    # Is the SBase stuff needed and/or present
    if not CResources.monomers_dir:
      sbase =  os.path.join(os.environ["CCP4_MGDATA"] ,"sbase")
      if os.path.exists(sbase) and os.path.exists(os.path.join(sbase,'index.sbase')):
        CResources.sbase = sbase


    self.SRS = ccp4srs.Manager()
    self.SRS.loadIndex(os.path.join(os.environ["CCP4"],"share","ccp4srs"))
    for i in range(mmdb2.nAminoacidNames):
      self.SRS.loadStructure(mmdb2.getAAProperty(mmdb2.AAProperties,i).name)
    for i in range(mmdb2.nNucleotideNames):
      self.SRS.loadStructure(mmdb2.getPstr(mmdb2.NucleotideName,i))

    self.CMolBondParams = mmut.CMolBondParams()
    mmut.CMMUTSRS.LoadCovalentDistances(os.path.join(os.environ["CCP4MG"],"data","covalent_distances"))
    mmut.CMMUTSRS.LoadEnerLib(os.path.join(os.environ["CCP4MG"],"data","ener_lib.cif"))
    mmut.CMMUTSRS.LoadEleLib(os.path.join(os.environ["CCP4MG"],"data","elements.cif"))

    return 0
  
