#! /bin/sh

#|
 echo Content-type: text/html
 echo ""
 ":";exec /usr/local/bin/mzscheme -r "$0" "$@"
|#

; Copyright 2008 Christopher Michael Rasch
;
; This file is part of Wishforge.

; Wishforge is free software: you can redistribute it and/or modify
; it under the terms of the GNU Affero General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.

; Wishforge is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU Affero General Public License for more details.

; You should have received a copy of the GNU Affero General Public License
; along with Wishforge.  If not, see <http://www.gnu.org/licenses/>.

(require (lib "etc.ss"))
(require (lib "cgi.ss" "net"))
(require (lib "xml.ss" "xml"))

(define bindings (get-bindings))

(define ebs 
  (lambda (s)
    (extract-binding/single s bindings)))

(define mk-line
  (lambda (prompt field)
    `(TR (TD ,prompt) (TD ,field))))

; TODO - factor out HTML building code, import it here

(define mk-form-button ; copied from elsewhere -- should import
  (lambda (s label len)
    `(INPUT ((TYPE "submit") (VALUE ,label) 
	     (NAME ,s) (MAXLENGTH ,(number->string len))))))

(define submit-button
  (lambda ()
    `(TR (TD ((COLSPAN "2") (ALIGN "center"))
	     ,(mk-form-button "submit login" "Submit" 20)))))

(define mk-hidden
  (lambda (key val)
    `(INPUT ((TYPE "hidden")
	     (NAME ,key)
	     (VALUE ,val)))))

(let ([login (ebs "login")]
      [password (ebs "password")]
      [last (ebs "last_name")]
      [first (ebs "first_name")]
      [email (ebs "email_address")]
      [phone (ebs "telephone")]
      [addr1 (ebs "address1")]
      [addr2 (ebs "address2")]
      [city (ebs "city")]
      [state (ebs "state")]
      [country (ebs "country")]
      [postcode (ebs "postcode")])
  (write-xml/content 
   (xexpr->xml 
    `(HTML
      (HEAD 
       (TITLE "Wishforge account"))
      (BODY
       (H2 "Here are the details you entered:")
       (TABLE
	,(mk-line "Login:" login) 
	,(mk-line "Password:" 
	  (build-string (string-length password) (lambda _ #\*)))
	,(mk-line "Last name:" last)
	,(mk-line "First name: " first)
	,(mk-line "Email: " email)
	,(mk-line "Telephone: " phone)
	,(mk-line "Address 1: " addr1)
	,(mk-line "Address 2: " addr2)
	,(mk-line "City: " city)
	,(mk-line "State: " state)
	,(mk-line "Country: " country)
	,(mk-line "Zip/Postcode: " postcode))
	(P))
       (H2 "If this is correct, click the submit button")
       (FORM ((NAME "commit_account")
	      (METHOD "POST")
	      (ACTION "/cgi-bin/commit-account.sh"))
	     ,(mk-hidden "login" login)
	     ,(mk-hidden "password" password)
	     ,(mk-hidden "last_name" last)
	     ,(mk-hidden "first_name" first)
	     ,(mk-hidden "email_address" email)
	     ,(mk-hidden "telephone" phone)
	     ,(mk-hidden "address1" addr1)
	     ,(mk-hidden "address2" addr2)
	     ,(mk-hidden "city" city)
	     ,(mk-hidden "state" state)
	     ,(mk-hidden "country" country)
	     ,(mk-hidden "postcode" postcode)
	     ,(submit-button))
       (P)
       (H2 "Otherwise, " (A ((HREF "/sign-in.html")) "go back")
	   " to the sign in page")
       ))))

(newline)
 

                
