#!/usr/bin/env ruby

require 'cgi'
cgi = CGI::new( "html3" )

stock = ''

TMPFILE = "/tmp/stock.cgi"

if cgi.key?( 'clear' )
  File::delete( TMPFILE ) if File::exist?( TMPFILE )
else
  if cgi.key?( 'name' ) && cgi.key?( 'title' )
    open( TMPFILE, "a" ){ |f|
      f.puts "#{cgi.params['name'][0]},#{cgi.params['title'][0]}"
    }
  end
  if File::exist?( TMPFILE )
    open( TMPFILE ).each do |line|
      line.chop!
      stock << cgi.text_field( 'name', line.split( ',' )[0] )
      stock << cgi.text_field( 'title', line.split( ',' )[1] )
      stock << "<br>\n"
    end
  end
end

cgi.out(){
  "<html><head><title>stock.cgi</title></head><body>" +
  "<form method='POST'>" +
  cgi.text_field( 'name' ) + cgi.text_field( 'title' ) +
  cgi.submit( 'add' ) + "<br>\n" +
  stock +
  "</form></body></html>"
}
