#! /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 "cookie.ss" "net"))
(require (lib "cgi.ss" "net"))
(require (lib "xml.ss" "xml"))
(require (file "util.ss"))
(require (file "form.ss"))
(require (file "html.ss"))
(require (file "db.ss"))
(require (file "cookies.ss"))
(require (file "bindings.ss"))
(require (file "badpage.ss"))

; (define login (get-one-cookie "login"))

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

(define bid-page
  (let* ([bidder (ebs "bidder")]
	 [wish-login (ebs "wish-login")]
	 [symbol (ebs "symbol")]
	 [wish (get-wish-bonds wish-login symbol)] ; TODO - make a wish structure?
	 [num_bonds (car wish)]
	 [bond_amount (cadr wish)])
    (apply-html-template
     `((TITLE "Bid for wish" ,symbol)
       (META ((http-equiv "Content-Type")
	      (content "text/html; charset=iso-8859-1")))
       (META ((NAME "description")
	      (CONTENT "WishForge: The Software Completion Bond Market"))))
     `((DIV
	(P)
	"You are bidding on the wish " ,symbol " created by user " ,wish-login (P)
	"There are " ,(format "~a" num_bonds) " available bonds, each paying $" ,(format "~a" bond_amount) (BR)
	"You may bid on some or all of the bonds, up to their face amount" (BR)
	(FORM ((NAME "new_bid")
	       (METHOD "POST")
	       (ACTION "/cgi-bin/commit-bid.sh"))
	      (TABLE
	       ,(input-line "Number of bonds: " "num_bonds")
	       ,(input-line "Amount per bond: " "bond_amount")
	       ,(mk-hidden "bidder" bidder)
	       ,(mk-hidden "wish-login" wish-login)
	       ,(mk-hidden "symbol" symbol)
	       ,(submit-button))))))))

; TODO : store session id on server instead of login/passwd
(if cookies 
    (let* ([login (get-one-cookie "login")]
	   [passhash (get-one-cookie "passhash")]
	   [stored-passhash (get-user-passhash login)])
      (if (and login passhash (not (equal? passhash stored-passhash)))
	  (begin
	    (write-xml/content 
	     (xexpr->xml bad-session-page))
	    (exit))
	  (write-xml/content 
	   (xexpr->xml bid-page))))
    ; missing cookies
    (write-xml/content 
     (xexpr->xml bad-login-page)))










		  
