#! /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 mk-bid-row
  (lambda (pledge)
    (let ([pledger (car pledge)]
	  [sym (cadr pledge)]
	  [summary (caddr pledge)])
    `(TR (TD ,pledger) (TD ,sym) (TD ,summary) 
	 (TD (FORM ((NAME "new_bid") (METHOD "POST") (ACTION "/cgi-bin/new-bid.sh"))
		   ,(mk-hidden "bidder" login)
		   ,(mk-hidden "wish-login" pledger)
		   ,(mk-hidden "symbol" sym)
		   ,(mk-hidden "summary" summary)
		   (TD ,(mk-form-button "bid" "Bid" 12))))))))

(define bid-page
  (let* ([login (get-one-cookie "login")]
	 [pledges (get-others-pledges login)]
	 [bid-rows (map mk-bid-row pledges)])
    (apply-html-template
     `((TITLE "Bid on a wish")
       (META ((http-equiv "Content-Type")
	      (content "text/html; charset=iso-8859-1")))
       (META ((NAME "description")
	      (CONTENT "WishForge: The Software Completion Bond Market"))))
     ; list all pledges not belonging to current user
     `((DIV
	(P)
	,(if (null? bid-rows)
	    "No pledges available to bid on"
	    `(DIV
	      "Pledges created by other users" (BR)
	      (TABLE ((FRAME "box") (CELLPADDING "4") (CELLSPACING "2") (RULES "rows"))
		     (TR ((BGCOLOR "lightblue")) (TD "Pledger") (TD "Symbol") (TD "Summary"))
		     ,@bid-rows))))))))

; 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)))









		  
