# -*-ruby-*-

module RubyCocoaConfig
  eval File.open('VERSION'){|f|f.read}

  # "10.9.3"
  SYSTEM_VERSION = `/usr/bin/sw_vers -productVersion`.chomp

  module_function

  def libruby_path
    path = File.join(RbConfig::CONFIG['libdir'], RbConfig::CONFIG['LIBRUBY'])
    unless File.exist?( path ) then
      path = File.join(RbConfig::CONFIG['archdir'], RbConfig::CONFIG['LIBRUBY_A'])
    end
    return path
  end

  # "10.9"
  def os_version
    SYSTEM_VERSION.split('.')[0..1].join('.')
  end

  # "10.9" >= "10.7"
  def os_version_ge?(other)
    self.os_version.split('.').zip(other.split('.')) do |v1, v2|
      v1 ||= 0
      v2 ||= 0
      if v1.to_i < v2.to_i
        return false
      end
    end
    return true
  end

  def framework_version
    # "#{RubyCocoaConfig::VERSION}_RUBY-#{RUBY_VERSION}"
    "A"
  end

  def archs
    # collect ruby's -arch flags from RbConfig::CONFIG
    flags = [RbConfig::CONFIG['CFLAGS'], RbConfig::CONFIG['LDFLAGS'],
            RbConfig::CONFIG['ARCH_FLAG']].join(' ')
    archs = flags.scan(/(?:\s?-arch\s+(\w+))/).flatten.uniq
    archs.join(' ')
  end
end

### install destination ### 

add_path_config 'install-root', '',
  'path to the root directory for Frameworks and "ProjectBuilder Extras"'

add_path_config 'frameworks', '/Library/Frameworks',
  'path to the directory for installing RubyCocoa.framework'

if File.exist?('/Developer/Applications/Xcode.app') or File.exist?('/usr/bin/xcrun') then
  if RubyCocoaConfig.os_version_ge?('10.7')
    xcode_templates_dir = nil
  else
    xcode_templates_dir = '/Library/Application Support/Developer/Shared/Xcode/'
  end
  add_path_config 'xcode-extras', 
    xcode_templates_dir,
    'path to the directory for "Xcode Extras"'
end 

add_path_config 'examples', '/Developer/Examples',
  'path to the directory for Examples'

add_path_config 'documentation', '/Developer/Documentation',
  'path to the directory for Documentation'

add_bool_config 'build-as-embeddable', true,
  'yes, build the framework as embeddable (with INSTALL_PATH pointing to the current application\'s Frameworks directory)'

### build configuration ### 

add_path_config 'ruby-header-dir', RbConfig::CONFIG['rubyhdrdir'] || RbConfig::CONFIG['archdir'],
  'path to the ruby ruby.h header directory'

add_path_config 'ruby-archheader-dir', RbConfig::CONFIG['rubyarchhdrdir'] || RbConfig::CONFIG['archdir'],
  'path to the ruby config.h header directory'

add_path_config 'libruby-path', RubyCocoaConfig.libruby_path,
  'path to the libruby file'

add_path_config 'ri-dir',
  "#{RbConfig::CONFIG['datadir']}/ri/#{RbConfig::CONFIG['ruby_version']}/site",
  'the directory for ri files'

add_config 'macosx-deployment-target', 'version', 
  RubyCocoaConfig.os_version,
  'target Mac OS X version'

sdkroot =
  if RubyCocoaConfig.os_version_ge?('10.8')
    `xcrun --sdk macosx --show-sdk-path`.chomp
  else
    ''
  end
add_config 'sdkroot', 'sdkroot-path', sdkroot, '$SDKROOT of Xcode.'

add_config 'target-archs', 'arch(s)', RubyCocoaConfig.archs,
  '$ARCHS of Xcode. target architecture type(s)'

### versioning ### 

add_config 'rubycocoa-version', 'name', 
  RubyCocoaConfig::VERSION + RubyCocoaConfig::STAGE,
  'RubyCocoa version'

add_config 'rubycocoa-version-short', 'name', 
  RubyCocoaConfig::VERSION,
  'RubyCocoa version short'

add_config 'rubycocoa-release-date', 'name', 
  RubyCocoaConfig::RELEASE_DATE, 'RubyCocoa release date'

add_config 'rubycocoa-svn-revision', 'name', 
  RubyCocoaConfig::SVN_REVISION, 'RubyCocoa subversion revision number'

add_config 'rubycocoa-framework-version', 'name',
  RubyCocoaConfig.framework_version, 'RubyCocoa Framework version name'
