require 'rubygems'
require_gem 'Mxx_ru', '>= 1.3.1'

require 'fileutils'

require 'mxx_ru/cpp'

module	Oess_1
	VERSION = ".1.4.0"

	# Выполнить настройку цели в зависимости от платформы.
	def	Oess_1.setup_platform( a_target )
		a_target.implib_path( "lib" )
		if "tandem_oss" == a_target.toolset.tag( "target_os" )
			a_target.define( "__unix__" )
		end
	end

	class LibraryMode
		include Singleton

		MARK_FILE = '.oess.static'

		def initialize
			@mode_fixed = false
			@libs_init_blocks = {}
		end

		def force_static
			fail 'ObjESSty library mode already determined!' if @mode_fixed
			MxxRu.show_brief( "forcing STATIC mode for ObjESSty" )
			FileUtils.touch( MARK_FILE )
		end

		def force_shared
			fail 'ObjESSty library mode already determined!' if @mode_fixed
			MxxRu.show_brief( "forcing SHARED mode for ObjESSty" )
			File.delete( MARK_FILE ) if File.exists?( MARK_FILE )
		end

		def static?
			static_mark_exists = File.exists?( '.oess.static' )
			MxxRu.show_brief( "detecting ObjESSty library mode: " +
					"#{static_mark_exists ? 'STATIC' : 'SHARED'}" )
			@mode_fixed = true
			self.class.class_eval %Q{ def static?; #{static_mark_exists}; end }
			static_mark_exists
		end

		def define_library( prj_alias, &block )
			klass = static? ? MxxRu::Cpp::LibTarget : MxxRu::Cpp::DllTarget
			@libs_init_blocks[ prj_alias ] = block

			target = MxxRu.setup_target(
				klass.new( prj_alias, &proc {
					target_root( 'lib' ) if Oess_1::LibraryMode.instance.static?
					instance_eval &Oess_1::LibraryMode.instance.library_init_block(
							prj_alias )
				}
				)
			)
		end

		def library_init_block( prj_alias )
			@libs_init_blocks.fetch( prj_alias )
		end

		def ace_prj
			"ace/#{static? ? 'lib' : 'dll'}.rb"
		end
	end
end

