# File setup.rb, line 986
  def parsearg_global
    valid_task = /\A(?:#{TASKS.map {|task,desc| task }.join '|'})\z/

    while arg = ARGV.shift
      case arg
      when /\A\w+\z/
        raise InstallError, "invalid task: #{arg}" unless valid_task =~ arg
        return arg

      when '-q', '--quiet'
        @options['verbose'] = false

      when       '--verbose'
        @options['verbose'] = true

      when '-h', '--help'
        print_usage $stdout
        exit 0

      when '-v', '--version'
        puts "#{File.basename($0)} version #{Version}"
        exit 0
      
      when '--copyright'
        puts Copyright
        exit 0

      else
        raise InstallError, "unknown global option '#{arg}'"
      end
    end

    raise InstallError, "No task or global option given.\nTypical installation procedure is:\n    $ ruby \#{File.basename($0)} config\n    $ ruby \#{File.basename($0)} setup\n    \# ruby \#{File.basename($0)} install  (may require root privilege)\nEOS\n  end\n\n\n  def parsearg_no_options\n    raise InstallError, \"\#{task}:  unknown options: \#{ARGV.join ' '}\"\\\n        unless ARGV.empty?\n  end\n\n  alias parsearg_show       parsearg_no_options\n  alias parsearg_setup      parsearg_no_options\n  alias parsearg_clean      parsearg_no_options\n  alias parsearg_distclean  parsearg_no_options\n\n  def parsearg_config\n    re = /\\A--(\#{ConfigTable.keys.join '|'})(?:=(.*))?\\z/\n    @options['config-opt'] = []\n\n    while i = ARGV.shift\n      if /\\A--?\\z/ =~ i\n        @options['config-opt'] = ARGV.dup\n        break\n      end\n      m = re.match(i) or raise InstallError, \"config: unknown option \#{i}\"\n      name, value = m.to_a[1,2]\n      if value\n        if ConfigTable.bool_config?(name)\n          raise InstallError, \"config: --\#{name} allows only yes/no for argument\"\\\n              unless /\\A(y(es)?|n(o)?|t(rue)?|f(alse))\\z/i =~ value\n          value = (/\\Ay(es)?|\\At(rue)/i =~ value) ? 'yes' : 'no'\n        end\n      else\n        raise InstallError, \"config: --\#{name} requires argument\"\\\n            unless ConfigTable.bool_config?(name)\n        value = 'yes'\n      end\n      @config[name] = value\n    end\n  end\n\n  def parsearg_install\n    @options['no-harm'] = false\n    @options['install-prefix'] = ''\n    while a = ARGV.shift\n      case a\n      when /\\A--no-harm\\z/\n        @options['no-harm'] = true\n      when /\\A--prefix=(.*)\\z/\n        path = $1\n        path = File.expand_path(path) unless path[0,1] == '/'\n        @options['install-prefix'] = path\n      else\n        raise InstallError, \"install: unknown option \#{a}\"\n      end\n    end\n  end\n\n  def print_usage(out)\n    out.puts 'Typical Installation Procedure:'\n    out.puts \"  $ ruby \#{File.basename $0} config\"\n    out.puts \"  $ ruby \#{File.basename $0} setup\"\n    out.puts \"  \# ruby \#{File.basename $0} install (may require root privilege)\"\n    out.puts\n    out.puts 'Detailed Usage:'\n    out.puts \"  ruby \#{File.basename $0} <global option>\"\n    out.puts \"  ruby \#{File.basename $0} [<global options>] <task> [<task options>]\"\n\n    fmt = \"  %-20s %s\\n\"\n    out.puts\n    out.puts 'Global options:'\n    out.printf fmt, '-q,--quiet',   'suppress message outputs'\n    out.printf fmt, '   --verbose', 'output messages verbosely'\n    out.printf fmt, '-h,--help',    'print this message'\n    out.printf fmt, '-v,--version', 'print version and quit'\n    out.printf fmt, '   --copyright',  'print copyright and quit'\n\n    out.puts\n    out.puts 'Tasks:'\n    TASKS.each do |name, desc|\n      out.printf \"  %-10s  %s\\n\", name, desc\n    end\n\n    out.puts\n    out.puts 'Options for config:'\n    ConfigTable.each_definition do |name, (default, arg, desc, default2)|\n      out.printf \"  %-20s %s [%s]\\n\",\n                 '--'+ name + (ConfigTable.bool_config?(name) ? '' : '='+arg),\n                 desc,\n                 default2 || default\n    end\n    out.printf \"  %-20s %s [%s]\\n\",\n        '--rbconfig=path', 'your rbconfig.rb to load', \"running ruby's\"\n\n    out.puts\n    out.puts 'Options for install:'\n    out.printf \"  %-20s %s [%s]\\n\",\n        '--no-harm', 'only display what to do if given', 'off'\n    out.printf \"  %-20s %s [%s]\\n\",\n        '--prefix',  'install path prefix', '$prefix'\n\n    out.puts\n  end\n\n  \#\n  \# Task Handlers\n  \#\n\n  def exec_config\n    @installer.exec_config\n    @config.save   \# must be final\n  end\n\n  def exec_setup\n    @installer.exec_setup\n  end\n\n  def exec_install\n    @installer.exec_install\n  end\n\n  def exec_show\n    ConfigTable.each_name do |k|\n      v = @config.get_raw(k)\n      if not v or v.empty?\n        v = '(not specified)'\n      end\n      printf \"%-10s %s\\n\", k, v\n    end\n  end\n\n  def exec_clean\n    @installer.exec_clean\n  end\n\n  def exec_distclean\n    @installer.exec_distclean\n  end\n\nend\n\n\nclass ToplevelInstallerMulti < ToplevelInstaller\n\n  include HookUtils\n  include HookScriptAPI\n  include FileOperations\n\n  def initialize(ardir)\n    super\n    @packages = all_dirs_in(\"\#{@ardir}/packages\")\n    raise 'no package exists' if @packages.empty?\n  end\n\n  def run_metaconfigs\n    eval_file_ifexist \"\#{@ardir}/metaconfig\"\n    @packages.each do |name|\n      eval_file_ifexist \"\#{@ardir}/packages/\#{name}/metaconfig\"\n    end\n  end\n\n  def init_installers\n    @installers = {}\n    @packages.each do |pack|\n      @installers[pack] = Installer.new(@config, @options,\n                                       \"\#{@ardir}/packages/\#{pack}\",\n                                       \"packages/\#{pack}\")\n    end\n\n    with    = extract_selection(config('with'))\n    without = extract_selection(config('without'))\n    @selected = @installers.keys.select {|name|\n                  (with.empty? or with.include?(name)) \\\n                      and not without.include?(name)\n                }\n  end\n\n  def extract_selection(list)\n    a = list.split(/,/)\n    a.each do |name|\n      raise InstallError, \"no such package: \#{name}\" \\\n              unless @installers.key?(name)\n    end\n    a\n  end\n\n  def print_usage(f)\n    super\n    f.puts 'Inluded packages:'\n    f.puts '  ' + @packages.sort.join(' ')\n    f.puts\n  end\n\n  \#\n  \# multi-package metaconfig API\n  \#\n\n  attr_reader :packages\n\n  def declare_packages(list)\n    raise 'package list is empty' if list.empty?\n    list.each do |name|\n      raise \"directory packages/\#{name} does not exist\"\\\n              unless File.dir?(\"\#{@ardir}/packages/\#{name}\")\n    end\n    @packages = list\n  end\n\n  \#\n  \# Task Handlers\n  \#\n\n  def exec_config\n    run_hook 'pre-config'\n    each_selected_installers {|inst| inst.exec_config }\n    run_hook 'post-config'\n    @config.save   \# must be final\n  end\n\n  def exec_setup\n    run_hook 'pre-setup'\n    each_selected_installers {|inst| inst.exec_setup }\n    run_hook 'post-setup'\n  end\n\n  def exec_install\n    run_hook 'pre-install'\n    each_selected_installers {|inst| inst.exec_install }\n    run_hook 'post-install'\n  end\n\n  def exec_clean\n    rm_f 'config.save'\n    run_hook 'pre-clean'\n    each_selected_installers {|inst| inst.exec_clean }\n    run_hook 'post-clean'\n  end\n\n  def exec_distclean\n    rm_f 'config.save'\n    run_hook 'pre-distclean'\n    each_selected_installers {|inst| inst.exec_distclean }\n    run_hook 'post-distclean'\n  end\n\n  \#\n  \# lib\n  \#\n\n  def each_selected_installers\n    Dir.mkdir 'packages' unless File.dir?('packages')\n    @selected.each do |pack|\n      $stderr.puts \"Processing the package `\#{pack}' ...\" if @options['verbose']\n      Dir.mkdir \"packages/\#{pack}\" unless File.dir?(\"packages/\#{pack}\")\n      Dir.chdir \"packages/\#{pack}\"\n      yield @installers[pack]\n      Dir.chdir '../..'\n    end\n  end\n\n  def verbose?\n    @options['verbose']\n  end\n\n  def no_harm?\n    @options['no-harm']\n  end\n\nend\n\nif $0 == __FILE__\n  begin\n    if multipackage_install?\n      ToplevelInstallerMulti.invoke\n    else\n      ToplevelInstaller.invoke\n    end\n  rescue\n    raise if $DEBUG\n    $stderr.puts $!.message\n    $stderr.puts \"Try 'ruby \#{$0} --help' for detailed usage.\"\n    exit 1\n  end\nend\n"