#!/usr/bin/env gosh

;; download the given files to ~/.cache/feedsync under hashed filenames
;; if the versions there are older than two hours

(use util.match)
(use gauche.process)


(define targets
  (cdr (command-line)))

(define cachedir
  (string-append (sys-getenv "HOME") "/.cache/feedsync"))

(do-process `("mkdir" "-p" ,cachedir))

(map (lambda (target)
       (let ([cachefile (format "~a/~a.ics" cachedir (portable-hash target 0))])
         (cond
           [(not (file-exists? cachefile))
            (do-process `("curl" ,target "-o" ,cachefile))]
           [(< 7200 (- (sys-time) (sys-stat->mtime (sys-stat cachefile))))
            (print (format "fetching ~a" target))
            (do-process `("curl" ,target "-o" ,cachefile))]
           [else
            (display "time limit not yet exceeded")])))
     targets)