#!/usr/bin/env ruby
require 'rubygems'
require 'id3lib'

def usage
	puts "podtagger 1.0 ( Robin Wood - http://www.digininja.org )\n\n"
	puts "Usage: podtagger.rb [Options]"
	puts "  -w <show code> <episode number> <filename>: Write the tags for the specified code"
	puts "  -s: Show the available codes"
	puts "  -?: This help screen"
end

show_details = {
				"rabit" => {
							"artist" => "Mubix",
							"show" => "Securabit",
						},
				"pdc" => {
							"artist" => "Pauldotcom and Larry",
							"show" => "Pauldotcom",
						},
				"rb" => {
							"artist" => "Patrick Gray",
							"show" => "Risky Business",
						}
			}

if (ARGV.size > 0):
	case ARGV[0]
		when "-s":
			show_details.each {|code,show|print "Code: ", code, "\n\tShow: ", show["show"], "\n\tArtist: ", show["artist"], "\n\n"}
			exit
		when "-?","-h":
			usage
			exit
		when "-w":
			if (ARGV.size != 4):
				usage
				exit
			end

			podcast_code = ARGV[1]
			episode = ARGV[2]
			filename = ARGV[3]

			if !show_details.include?(podcast_code):
				puts "Podcast code not found"
				exit
			end

			show=show_details[podcast_code]

			if !FileTest.exists?(filename):
				puts "mp3 not found"
				exit
			end

			puts "Show: " + show['show']
			puts "Artist: " + show['artist']
			puts "Episode: " + episode
			puts "Year: " + Time.now.year.to_s

			tag = ID3Lib::Tag.new(filename)

			#tag.delete_if{ |frame| frame[:id] == :APIC }
			tag.title=episode
			# Assume that the year is this year
			tag.year=Time.now.year
			tag.album=show['show']
			tag.genre="podcast"
			tag.artist=show['artist']

			puts "\nWriting"
			tag.update!
			puts "Finished"

			exit
	end
else
	usage
	exit
end
