#! /usr/bin/env ruby

# Ruby program to generate config.rb

default_architecture = ENV['DEFAULT_ARCHITECTURE']
default_format = ENV['DEFAULT_FORMAT']
gas_command = ENV['GAS'] || 'as'
nasm_command = ENV['NASM'] || ENV['YASM'] || 'nasm'

print <<EOT
module Voodoo
  # Methods to get and set configuration parameters
  module Config
    # Class that holds configuration parameters
    class Configuration
      def initialize
        @default_architecture = #{default_architecture.to_sym.inspect}
        @default_format = #{default_format.to_sym.inspect}
        @gas_command = #{gas_command.inspect}
        @nasm_command = #{nasm_command.inspect}
      end

      attr_accessor :default_format,
                    :gas_command,
                    :nasm_command

      attr_writer :default_architecture

      # Returns the default architecture for this compiler
      def default_architecture
        if @default_architecture == :auto
          # If @default_architecture has been set to :auto,
          # return the host architecture, falling back to :i386
          # if the host architecture cannot be determined
          host_architecture || :i386
        else
          @default_architecture
        end
      end

      # Returns the architecture of the machine running the program,
      # or nil if that architecture cannot be determined
      def host_architecture
        arch, = RUBY_PLATFORM.split '-'
        case arch
        when 'x86_64', 'amd64'
          :amd64
        when 'i386', 'i686'
          :i386
        when 'mips'
          :mips
        when 'mipsel'
          :mipsel
        else
          nil
        end
      end
    end

    DEFAULT_CONFIGURATION = Configuration.new

    module_function

EOT

[:default_architecture,
 :default_format,
 :gas_command,
 :nasm_command
].each do |symbol|
    print <<EOT
    def #{symbol}
      DEFAULT_CONFIGURATION.#{symbol}
    end

    def #{symbol}= value
      DEFAULT_CONFIGURATION.#{symbol} = value
    end
EOT
end

print <<EOT
  end
end
EOT
