#!/usr/bin/ruby

# wavcddb by jared jennings
# liver@soon.com
# under Ruby's license

require '/home/jaredj/ruby-lib/cddb'

if ARGV.length == 0
    print "\nUsage: wavcddb *.wav\n"
    print "--------------------\n"
    print "wavcddb takes the lengths of the wav files handed to it as\n"
    print "parameters, hands them to a cddb server, and puts the results\n"
    print "in an easily editable file called wavcddb.txt.\n"
    print "to encode ogg files based on this info, use batch-oggenc\n\n"
    exit
end

#db = CddbConnection.new "localhost",8880
db = Cddb::Connection.new "freedb.freedb.de",8880
toc = Cddb::Toc.new
toc.prepare ARGV
result = db.query toc

if result.length > 0

    result.each_index do |ind|
        print "#{ind+1}. "
        result[ind].each do |inf|
            print inf, " "
        end
        print "\n"
    end

    print "------------------\n"
    if result.length > 1
        print "More than one close match found.\n"
        print "Which should I use?\n"
        reply = $stdin.gets.to_i - 1
    else 
        reply = 0
    end

    lines = db.read result[reply][0], result[reply][1]
    toc.setFromCddbInfo lines
    print "Writing data to file wavcddb.txt.\n"

    print "Done. Edit to your heart's content, then run batch-oggenc\n"
else
    print "Sorry, no matches found.\n"
end
    wavcddb = File.new("wavcddb.txt","w")
    wavcddb.print toc.inspect,"\n"
    wavcddb.close
