#!/usr/bin/env python
#
# This script creates new configuration files
# called system_defaults.xml and user_defaults.xml
# from an existing dna_config file.
#
# The script accesses the dna_config file from $DNACONFIG if
# defined, otherwise from $DNAHOME/config/$DNANAME.
#
# It then tries to save the system_defaults.xml file in
# the same location as $DNACONFIG if defined, otherwise
# in $DNAHOME/config/$DNANAME.
#
# The user_defaults.xml file is saved in $HOME/.dna/user_defaults.xml.
#
# If the files already exists the scripts asks for confirmation
# before overwriting them.
#

import os, sys

print "---------------------------------------"
print "old2new_dnaconfig"
print ""

DNAHOME = None
DNANAME = None
if not "DNAHOME" in os.environ.keys():
  raise "ERROR in ESConfig: DNAHOME not defined"
else:
  DNAHOME = os.environ["DNAHOME"]
  print "DNAHOME = %s" % DNAHOME

sys.path.append(os.path.join(DNAHOME, "xsd", "python"))
import XSD
from XML_utils import *

if not "DNANAME" in os.environ.keys():
  raise "ERROR in ESConfig: DNANAME not defined"
else:
  DNANAME = os.environ["DNANAME"]
  print "DNANAME = %s" % DNANAME

print ""

DNACONFIG = None
if "DNACONFIG" in os.environ.keys():
  dna_config_file_name = os.environ["DNACONFIG"]
  print "DNACONFIG is defined to %s" % dna_config_file_name
else:
  dna_config_file_name = os.path.join(os.environ["DNAHOME"], \
                                      "config",
                                      os.environ["DNANAME"],
                                      "dna_config")
  print "DNACONFIG is not defined, path to old configuration file:"
  print dna_config_file_name

print ""

if not os.access(dna_config_file_name, os.F_OK):
  print "The old configuration file can not be found!"
  print "Please check the path to the old configuration file."
  sys.exit(1)

print "Reading the old configration file."
#
file = open(dna_config_file_name)
dna_config_xml = file.read()
dna_configuration = XSD.Dna_configuration()
dna_configuration.unmarshal(dna_config_xml)
file.close()

#
# Save the configuration in the new scheme
#

system_defaults = XSD.System_defaults()
user_defaults = XSD.User_defaults()

server_data = dna_configuration.getServer_data()
system_defaults.setServer_data(server_data)
local_info = dna_configuration.getLocal_info()
if not local_info is None:
  system_defaults.setLocal_info(local_info)
default_values = dna_configuration.getDefault_values()
user_defaults.setDefault_values(default_values)
index_parameters = dna_configuration.getIndex_parameters()
user_defaults.setIndex_parameters(index_parameters)

system_defaults_xml = XML_utils.xml_with_indentations(system_defaults.marshal())
user_defaults_xml =    XML_utils.xml_with_indentations(user_defaults.marshal())

# print system_defaults_xml, user_defaults_xml

#
# Build filename for system_defaults.xml
#

if not DNACONFIG is None:
  config_dir = os.path.dirname(DNACONFIG)
  system_defaults_filename = os.path.join(config_dir, "system_defaults.xml")
else:
  system_defaults_filename = os.path.join(DNAHOME, "config", \
                                          DNANAME, "system_defaults.xml")

print
print "System defaults file name:"
print system_defaults_filename
  
answer = ""
if os.access(system_defaults_filename, os.F_OK):
  #
  # File exists - confirm overwrite
  #
  while (answer != "y" and answer != "n"):
    answer = raw_input("File exists - overwrite? (y/n):")
else:
  answer = "y"

if answer == "y":
  print "Writing %s" % system_defaults_filename
  file = open(system_defaults_filename, "w")
  file.write(system_defaults_xml)
  file.close()

    
user_defaults_filename = os.path.join(os.environ["HOME"], ".dna", \
                                      "user_defaults.xml")

print
print "User defaults file name:"
print user_defaults_filename

#
# Check that $HOME/.dna exists
#

user_defaults_dir = os.path.join(os.environ["HOME"], ".dna")
if not os.access(user_defaults_dir, os.F_OK):
  print
  print "Creating %s directory." % user_defaults_dir
  os.system("mkdir -p %s" % user_defaults_dir)


answer = ""
if os.access(user_defaults_filename, os.F_OK):
  #
  # File exists - confirm overwrite
  #
  while (answer != "y" and answer != "n"):
    answer = raw_input("File exists - overwrite? (y/n):")
else:
  answer = "y"

if answer == "y":
  print "Writing %s" % user_defaults_filename
  file = open(user_defaults_filename, "w")
  file.write(user_defaults_xml)
  file.close()
