#!/opt/bin/ruby

require 'socket'

$SAFE = 1

host = "localhost"
port = 8000

online_image = "tastycastsmall_on.gif"
offline_image = "tastycastsmall_off.gif"


# The shoutcast HTTP server seems VERY picky about the request used to access it.

resp = ""
TCPSocket.open(host, port) do |sock|
  sock.print "GET /7.html HTTP/1.0\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n\r\n"
  while dat = sock.gets
    resp << dat
  end
end


# the HTML returned is something like:
# <HTML><meta http-equiv="Pragma" content="no-cache"></head><body>0,0,8,32,0,128,Explosions In The Sky - once more to the afterlife</body></html>

online = false
if resp =~ %r{<body>(.*?)</body}i
  fields = $1.split(",")
  online = fields[1] == "1"
end

img_fname = online ? online_image : offline_image
img_data = File.read(img_fname)
img_data.untaint

print "Content-type: image/gif\r\n\r\n"
print img_data

