#!/usr/bin/ruby

message = STDIN.readlines.map { |x|
	x.gsub!(/=\n$/,'')
	x.gsub!(/=20/, ' ')
	x.gsub!(/=3D/, '=')
	x
}

x = `date "+irc.%m.%d"`
$stdout = File.open(x.chomp, "w+")

x = `date`.chomp
print x

attendees = Hash.new
joined = Hash.new
message.to_s.each { |x|
	next unless (x =~ /^\[[0-9][0-9]:[0-9][0-9]\]/)
	x.gsub!(/^\[..:..\] \* /, "  * ")

	if (/subject to (.*)/.match(x))
		s = "SUBJECT: #{$1}"
		print "\n\n#{s}\n#{"=" * s.size}\n\n"
	elsif (/^\[..:..\] ([^ ]*) has joined /.match(x))
		joined[$1] = 1
		print x.gsub(/...:... /, "  ")
	elsif (/^\[..:..\] ([^: ]*) /.match(x))
		print x.gsub(/...:... /, "  ")
	else
		print x
#		print x.gsub(/^...:... /, "        ")
	end

	if (/^\[..:..\] ([^ :]*): /.match(x))
		attendees[$1] = 1
	end


}

total = (attendees.keys + joined.keys).sort.uniq
print "\n\nAttendees (#{total.size}): \n"

total.each { |x|
	print "\t", x
	if (joined.member?(x) && ! attendees.member?(x))
		print " (nonspeaking)"
	end
	print "\n"
}

#message.each { |line|
#	line.chomp!
#	if (line =~ /=$/)
#		line.chop!
#		line += readline.chomp!
#	end
#
#	if (line =~ /set the subject to (.*)/)
#		subj = $1;
#		print "\n", subj, "\n\n";
#	else
#		print line, "\n";
#	end
#}


