#! /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 "badpage.ss"))

(define more-specs
  (not (null? (extract-bindings 'morespecs bindings))))

(define extra-specs
  (map (lambda (n)
	 (let ([s (number->string n)])
	   (input-line (sa "Specification " s ": ") 
		       (sa "specification_" s))))
       (list 6 7 8 9 10)))

(define wish-page
  (apply-html-template
   `((TITLE "Create a wish")
     (META ((http-equiv "Content-Type")
	    (content "text/html; charset=iso-8859-1")))
     (META ((NAME "description")
	    (CONTENT "WishForge: The Software Completion Bond Market"))))
   `((DIV
      (P)
      (FORM ((NAME "new_wish")
	     (METHOD "POST")
	     (ACTION "/cgi-bin/commit-wish.sh"))
	    (H2 "Enter the details for your wish:") (BR)
	    (TABLE
	     ,(input-line "Symbol: " "symbol")
	     ,(input-line "Summary: " "summary")
	     ,(input-text "Description: " "description")
	     ,(input-line "Specification 1: " "specification_1")
	     ,(input-line "Specification 2: " "specification_2")
	     ,(input-line "Specification 3: " "specification_3")
	     ,(input-line "Specification 4: " "specification_4")
	     ,(input-line "Specification 5: " "specification_5")
	     ,@(if more-specs
		  extra-specs
		  `((TR (TD 'nbsp) (TD (A ((HREF "http://localhost/cgi-bin/create-wish.sh?morespecs=t")) "Add specifications")))))
	     ,(input-line "Judge: " "judge")
	;     ,(mk-date-selection) ,(mk-time-selection)
	     ,(input-line "Bond amount:" "bond_amount")
	     ,(input-line "Number of bonds:" "number_of_bonds")
	     ,(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 wish-page))))
    ; missing cookies
    (write-xml/content 
     (xexpr->xml bad-login-page)))









		  
