#!/usr/local/bin/ruby

Basedir = File.dirname($0)
ipv4_image = File.join(Basedir,"access_over_ipv4.png")
ipv4_image_data = open(ipv4_image).read
ipv6_image = File.join(Basedir,"access_over_ipv6.png")
ipv6_image_data = open(ipv6_image).read

app_uri = "checkipv6"
Rev = "1.0"
ENV['GEM_HOME'] = "/app/www.yadiary.net/lib/#{app_uri}/#{Rev}/gems"
require 'rubygems'
require 'fcgi'

FCGI.each { |request|
  out = request.out
  out.print "Content-Type: image/png\r\n\r\n"

  ip_addr = request.env['REMOTE_ADDR']
  
  if ip_addr =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/
    out.print ipv4_image_data
  else
    out.print ipv6_image_data
  end

  request.finish
}
