#!/usr/bin/ruby

require 'cgi'

//
// Find out number of times this has been executed (or at least displayed)
//
file = File.new( [REDACTED!], "r" )
line = file.gets
file.close
mCount = line.to_i
mCount += 1
file = File.new( [REDACTED!], "w" )
file.write mCount.to_s
file.close

//
// Create the page and send over the css
//
cgi = CGI.new
puts cgi.header
puts "<html><head>"
puts "<title>Pagerizer</title>"
puts "<style type='text/css'>"


puts "@font-face {"
puts "    font-family: Helvetica_Neue_LT_Std;"
puts "    src: local(Helvetica_Neue_LT_Std),
          url( [REDACTED!] )
          format('opentype');"
puts "}"

puts "h2 {"
puts "    font-weight: 200;"
puts "    color:       #888;"
puts "    color: orange;"
puts "    font-family: Helvetica_Neue_LT_Std, Helvetica Neue LT Std,helvetica neue,helvetica,sans;"
puts "   font-size: 2em;"
puts "}"



puts "p"
puts "{"
puts "    font-family: Helvetica_Neue_LT_Std, Helvetica Neue LT Std, helv, sans;"
puts "    font-size: 1em;"
puts "    color:#222;"
puts "}"

puts "p.note"
puts "{"
puts "   font-size: .5em;"
puts "   color    : #000;"
puts "   font-style: italic;"
puts "   font-weight: bold;"
puts "   margin-bottom: 0px;"
puts "}"


puts "</style></head><body>"


puts "<h2>Pagerizer</h2>"

puts "<p>Pagerizer will figure out how to make a folded, stapled "
puts "booklet with the number of pages you specify.</p>"
puts "<p>It will give you a chart of where each page goes on which sheet.</p>"

//
// Build the interface where the user can interact:
//
puts "<div style='border: 1px solid black; padding:5px; width: 200px;'>"

// Reload this page, but pass the data if the user entered any:
puts "<form method='POST' action='http://www.jeffholton.com/cgi-bin/pagerizer.cgi'>"
puts "<p><b>Number of pages:</b><br /><input type='text' name='NumPages'"

// Here's the magic. If this page was just accessed by typing in the
// base URI, there's no data to display here and there's nothing to
// compute. But if this page just got reloaded because of a SUBMIT
// click, then extract the number here. Doesn't matter if it's a legit
// number or not. Just display whatever the value is:
puts " value='"
if cgi.has_key?('NumPages') 
   puts cgi['NumPages'].to_i.to_s
end 
puts "' /><br /><br />"

puts "<input type='submit' value='Pagerize' /></p>"

// Leave the user a note to astound him or her with how amazingly
// popular this applet is:
puts "<p class='note'>Pagerizer has been executed " << mCount.to_s
puts " times.</p>"
puts "</form>"
puts "</div>"

//
// If there was input, compute it:
//
if cgi.has_key?('NumPages') 
   puts "<p>User entered " << cgi['NumPages'] << "</p>"
   mNumPages = cgi['NumPages'].to_i

// The booklet is being printed on pages that are folded in half. This
// means that four pages are on each sheet. Make sure that the number
// we're processing is a multiple of four. If the user was off by a
// little bit, fix it:
   while( ( mNumPages % 4 ) != 0 )
      mNumPages += 1
   end

   puts "<p>Actual number of pages to process: " << mNumPages.to_s 
   puts "<br />Number of sheets: " << (mNumPages/4).to_s
   puts "</p>"

   puts "<div style='border: 1px solid black; padding:5px; width: 200px;'>"

   for mNumSheet in 1..(mNumPages/4) 
      puts "<p>Sheet " << mNumSheet.to_s << "a: "
      puts (mNumPages - (2*(mNumSheet-1))).to_s << ", "
      puts ( ( mNumSheet * 2 ) - 1 ).to_s << "<br />"
      puts "Sheet " << mNumSheet.to_s << "b: "
      puts (mNumSheet*2).to_s << ", "
      puts (mNumPages - (2*(mNumSheet-1) + 1 )).to_s
      puts "</p>"
   end

   puts "</div> "
end

puts "</body>"
puts "</html>"