#!/usr/bin/ruby -w

require 'optparse'
require 'ostruct'
require 'xmms'

class Optparse

  PROGRAM = $0.sub(/^.*\//, '')
  USAGE_BANNER = "Usage: #{PROGRAM} [options]"

  def self.parse(args)
    options = OpenStruct.new
    options.case = false

    opts = OptionParser.new do |opts|
      opts.banner = USAGE_BANNER

      opts.on("-i", "--ignore-case", "Ignore case distinctions.") do
	options.case = true
      end

      opts.on_tail("-h", "--help", "Show this message.") do
	puts opts
	exit
      end
    end

    begin
      opts.parse!(args)
      options
    rescue OptionParser::InvalidOption
      puts opts
      exit 1
    end

  end
end

options = Optparse.parse(ARGV)

remote = Xmms::Remote.new

count = 1
remote.playlist.each do |song|
  name, path = song
  if Regexp.new(ARGV[0], options.case).match(name)
    puts "#{count}: #{name}", path
  end

  count += 1
end
