#!/usr/bin/env ruby
#-*-ruby-*-
require "dpklib/command"
require "dpklib/parsearg"
require "rbconfig"

class DpkrbRbconfigProgram < Dpklib::CommandLineProgram
  def start
    @rbconfig = Config::CONFIG
    opts = Dpklib.parse_args(argv, "el", "list", "env")
    if opts[:list] || opts[:l]
      stdout.puts @rbconfig.keys
    else
      key = opts.shift || usage
      value = @rbconfig[key] || die("Unknown rbconfig key: #{key}")
      if opts[:env] || opts[:e]
        value = ENV["RBCONFIG_#{key.upcase}"] || value
      end

      stdout.puts(value)
    end
  end

  def usage
    die(<<-EOF)
usage: #{progname} [OPTIONS] RBCONFIG_KEY
OPTIONS:
  -l, --list:
    Lists available strings for RBCONFIG_KEY.
  -e, --env:
    Replaces rbconfig values with environment variable RBCONFIG_* if
    it is set. For example, '#{progname} --env bindir' refers variable
    'RBCONFIG_BINDIR'.
    EOF
  end

  execute
end #/DpkrbRbconfigProgram
