#!/usr/bin/ruby

# batch-oggenc
# jared jennings
# liver@soon.com
# under Ruby's license

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

wavcddb = File.open("wavcddb.txt")
begin
    switchfile = File.open("#{ENV[\"HOME\"]}/.batch-oggenc-switches");
    swstring = ""
    switchfile.each {|line|
        swstring += line + " "
    }
    switches = swstring.split
rescue Errno::ENOENT
    print "No ~/.batch-oggenc-switches file found. Using defaults\n";
    switches = ["-b","192"]
end

tocString = ""
wavcddb.each { |line|
    tocString += line
}
toc = Cddb::Toc.new
toc.uninspect tocString

paramLines = []
playlist = File.open("#{toc.discArtist} - #{toc.discTitle}.m3u","w");
toc.titles.each_index do |ind|
    if toc.artists[ind] == nil then
        toc.artists[ind] = toc.discArtist
    end
    serial = `getNextSerial`
    serialHex = sprintf("%05x",serial)
    filename = "#{toc.artists[ind]} - #{toc.titles[ind]} % #{serialHex}.ogg"
    line = [ switches,
           "-s",serial,
           "-o",filename,
           "-N","#{ind+1}",
           "-a",toc.artists[ind],
           "-t",toc.titles[ind],
           "-l",toc.discTitle,
                toc.filenames[ind]
           ]
    paramLines.push line.flatten
    playlist.print filename,"\n"
end
playlist.close

paramLines.each do |line|
    system "oggenc",*line
end

