#	license: "MIT license (See: en.wikipedia.org/wiki/MIT_License)"
#	date: "10 Dec 2012"
#	revision: "0.1"

import subprocess, sys, os, stat

from distutils import dir_util
from os import path

from eiffel_loop.eiffel import project


def convert_pyxis_to_xml (pecf_path):
	subprocess.call (['el_toolkit2', '-pyxis_to_xml', '-no_highlighting', '-in', pecf_path])

project.set_build_environment (project.read_project_py ())

for arg in sys.argv [1:]:
	if not arg.startswith ('cpu='):
		project_path = arg

pecf_path = None
parts = path.splitext (project_path)
if parts [1] == '.pecf':
	pecf_path = project_path
	project_path = parts [0] + '.ecf'

	if os.stat (pecf_path)[stat.ST_MTIME] > os.stat (project_path)[stat.ST_MTIME]:
		convert_pyxis_to_xml (pecf_path)
		
eifgen_path = path.join ('build', os.environ ['ISE_PLATFORM'])	
#	contact: "finnian at eiffel hyphen loop dot com"
#	license: "MIT license (See: en.wikipedia.org/wiki/MIT_License)"
#	date: "26 Oct 2016"
#	revision: "0.2"

import subprocess, sys, os, stat

from distutils import dir_util
from os import path

from eiffel_loop.eiffel import project

def convert_pyxis_to_xml (pecf_path):
	subprocess.call (['el_toolkit', '-pyxis_to_xml', '-no_highlighting', '-in', pecf_path])

project_py = project.read_project_py ()

MSC_options = project_py.MSC_options
cpu_option = 'x64'

if not '/x64' in MSC_options:
	MSC_options.append ('/x64')

for arg in sys.argv [1:]:
	if arg.startswith ('cpu='):
		cpu_option = arg.split ('=')[-1]
		if cpu_option in ['x86', 'x64']:
			MSC_options.append ('/' + cpu_option)
		else:
			raise Exception('Invalid argument', arg)
	else:
from eiffel_loop.eiffel.ecf import EIFFEL_CONFIG_FILE
from eiffel_loop.eiffel.ecf import FREEZE_BUILD

from SCons.Environment import Base
from SCons.Variables import Variables
from glob import glob

var = Variables ()
var.Add ('cpu', '', 'x64')
var.Add ('project', '', glob ('*.ecf')[0])

os.environ ['ISE_LIBRARY'] = os.environ ['ISE_EIFFEL']

env = Base ()

var.Update (env)

env.Append (ENV = os.environ)
env.Append (ISE_PLATFORM = os.environ ['ISE_PLATFORM'])

project_py = project.read_project_py ()
project_py.set_build_environment (env.get ('cpu'))

ecf_path = env.get ('project')
config = EIFFEL_CONFIG_FILE (ecf_path)

build = FREEZE_BUILD (config, project_py)
build.install_resources (build.resources_destination ())