; baseball

(define (tempname)
  (let loop ((i 0))
    (let ((f (string-append "temp" (number->string i))))
      (if (file-exists? f) (loop (+ i 1)) f))))

(define (with-input-from-url url thunk)
  (let ((f (tempname)))
    (if (zero? (system (string-append wget " " f " " url)))
        (begin (with-input-from-file f thunk) (delete-file f #t))
        (error 'with-input-from-url "system error in wget"))))

; these use the letter oh, not the digit zero
(define wget "c:\\cygwin\\bin\\wget -qO") ; windows/cygwin
(define wget "/usr/local/bin/wget -qO") ; unix/hp
(define wget "/usr/bin/wget -qO") ; linux/ubuntu

(define (string-split sep str)
  (define (f cs xs) (cons (list->string (reverse cs)) xs))
  (let loop ((ss (string->list str)) (cs '()) (xs '()))
    (cond ((null? ss) (reverse (if (null? cs) xs (f cs xs))))
          ((char=? (car ss) sep) (loop (cdr ss) '() (f cs xs)))
          (else (loop (cdr ss) (cons (car ss) cs) xs)))))

(define (string-find pat str . s)
  (let* ((plen (string-length pat))
         (slen (string-length str))
         (skip (make-vector plen 0)))
    (let loop ((i 1) (j 0))
      (cond ((= i plen))
            ((char=? (string-ref pat i) (string-ref pat j))
              (vector-set! skip i (+ j 1))
              (loop (+ i 1) (+ j 1)))
            ((< 0 j) (loop i (vector-ref skip (- j 1))))
            (else (vector-set! skip i 0)
                  (loop (+ i 1) j))))
    (let loop ((p 0) (s (if (null? s) 0 (car s))))
      (cond ((= s slen) #f)
            ((char=? (string-ref pat p) (string-ref str s))
              (if (= p (- plen 1))
                  (- s plen -1)
                  (loop (+ p 1) (+ s 1))))
            ((< 0 p) (loop (vector-ref skip (- p 1)) s))
            (else (loop p (+ s 1)))))))

; replace all occurrences of pat in str with rep
(define (string-replace str pat rep)
  (let ((x (string-find pat str)))
    (if (not x) str
      (string-append (substring str 0 x) rep
        (string-replace (substring str (+ x (string-length pat))
          (string-length str)) pat rep)))))

(define (parse-mlb-lines)
  (let ((lines #f))
    (with-input-from-url
      "http://s...content-available-to-author-only...o.com/mlb/bottomline/scores"
      (lambda ()
        (let loop ((xs (list)))
          (if (not (eof-object? (peek-char)))
              (loop (cons (read-char) xs))
              (set! lines (list->string (reverse xs)))))))
    (map (lambda (xs) (list (car xs) (string-replace (cadr xs) "%20" " ")))
      (map (lambda (s) (string-split #\= s))
         (map (lambda (s) (substring s 6 (string-length s)))
              (cdr (string-split #\& lines)))))))

(define (cardinals)
  (let loop ((lines (parse-mlb-lines)))
    (when (pair? lines)
      (if (not (string-find "St. Louis" (cadar lines)))
          (loop (cdr lines))
          (let ((num (substring (caar lines) 4
                       (string-length (caar lines)))))
            (let loop ((lines lines))
              (when (not (string=? (caar lines)
                      (string-append "right" num "_count")))
                (display (cadar lines)) (newline)
                (loop (cdr lines)))))))))

(cardinals)