#!/usr/bin/env ruby

require 'rrb/default_value'

if $0 == __FILE__ then
  
  def print_usage
    print "\
Usage: rrb_default_value PATH LINENO INFOTYPE IOTYPE

  INFOTYPE
    * --method
    * --class
  IOTYPE
    * --stdin-stdout
    * --filein-stdout FILES..
    * --marshalin-stdout
"
  end
  
  if ARGV.size == 0 || ARGV[0] == '--help' then
    print_usage
    exit
  end

  path = ARGV.shift
  lineno = ARGV.shift.to_i
  infotype = ARGV.shift

  case ARGV.shift
  when '--stdin-stdout'
    script = RRB::Script.new_from_io( STDIN )
  when '--filein-stdout'
    script = RRB::Script.new_from_filenames( *ARGV )
  when '--marshalin-stdout'
    script = RRB::Script.new_from_marshal(ARGV.shift)
  end

  case infotype
  when '--class'
    namespace = script.get_class_on_cursor(path, lineno)
    if namespace 
      print namespace.name
    end
  when '--method'
    method = script.get_method_on_cursor(path, lineno)
    if method
      print method.name
    end
  when '--bare-method'
    method = script.get_method_on_cursor(path, lineno)
    if method
      print method.name.split('#')[-1]
    end
  end  
end
