r3258: *** empty log message ***
[lml.git] / downloads.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          downloads.cl
6 ;;;; Purpose:       Generate downloads page
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Aug 2002
9 ;;;;
10 ;;;; $Id: downloads.lisp,v 1.6 2002/10/31 18:52:23 kevin Exp $
11 ;;;;
12 ;;;; This file, part of LML, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; LML users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the GNU General Public License v2
16 ;;;; (http://www.gnu.org/licenses/gpl.html)
17 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :lml)
21
22
23 (defvar *ftp-base*)
24 (defvar *ftp-url*)
25 (defvar *base-name*)
26 (defvar *section-indent* 0)
27 (defvar *signed* nil)
28
29 (defun list-files (files)
30   "List files in a directory for downloading"
31   ;;files.sort()
32   (mapcar #'print-file files))
33
34 (defun strip-ftp-base (file)
35   (let ((fdir (pathname-directory file))
36         (bdir (pathname-directory *ftp-base*)))
37     (make-pathname
38      :name (pathname-name file)
39      :type (pathname-type file)
40      :directory 
41      (when (> (length fdir) (length bdir))
42        (append '(:absolute) 
43                (subseq fdir (length bdir) (length fdir)))))))
44      
45 (defun print-file (file)
46   (let ((size 0)
47         (modtime (date-string (file-write-date file)))
48         (basename (namestring
49                    (make-pathname :name (pathname-name file)
50                                   :type (pathname-type file))))
51         (ftp-name (strip-ftp-base file))
52         (sig-path (concatenate 'string (namestring file) ".asc")))
53     (when (plusp (length basename))
54       (with-open-file (strm file :direction :input)
55                       (setq size (round (/ (file-length strm) 1024))))
56       (lml-print "<a href=\"~A~A\">~A</a>" *ftp-url* ftp-name basename)
57       (lml-print "<span class=\"modtime\">")
58       (lml-print " (~A, <b>~:D <span style=\"font-size:90%;\">KB</span></b>)</span>" modtime size)
59       (when (probe-file sig-path)
60         (setq *signed* t)
61         (lml-print " [<a href=\"~A~A.asc\">Signature</a>]" *ftp-url* ftp-name))
62       (br))))
63
64 (defun display-header (name url)
65   (lml-print "<h1>Download</h1>")
66   (lml-print "<div class=\"mainbody\">")
67   (lml-print "<h3>Browse ~A FTP Site</h3>" name)
68   (lml-print "<a style=\"padding-left:20pt;\" href=\"~A\">~A</a>" url url))
69
70 (defun display-footer ()
71   (when *signed*
72     (lml-print "<h3>GPG Public Key</h3>")
73     (lml-print "Use this <a href=\"https://www.b9.com/key.asc\">key</a> to verify file signtatures"))
74   (lml-print "</div>"))
75   
76 (defun print-sect-title (title)
77   (lml-print "<h~D>~A</h~D>" *section-indent* title *section-indent*))
78
79 (defun match-base-name? (name)
80   (let ((len-base-name (length *base-name*)))
81     (when (>= (length name) len-base-name)
82       (dotimes (i len-base-name)
83         (declare (fixnum i))
84         (unless (char= (char *base-name* i)
85                        (char name i))
86           (return-from match-base-name? nil)))))
87   t)
88
89 (defun filter-against-base (files)
90   (let ((filtered '()))
91     (dolist (f files)
92       (let ((name (pathname-name f)))
93         (when (match-base-name? name)
94           (push f filtered))))
95     (when filtered
96       (sort filtered #'(lambda (a b) (when (and a b)
97                                        (string<
98                                         (namestring a)
99                                         (namestring b))))))))
100
101 (defun display-one-section (title pat)
102   (let ((files (filter-against-base (directory pat))))
103     (when files
104       (print-sect-title title)
105       (lml-print "<div style=\"padding-left: 20pt;\">")
106       (list-files files)
107       (lml-print"</div>"))))
108
109
110 (defun display-sections (sects)
111   (when sects
112     (let ((title (car sects))
113           (value (cadr sects)))
114       (if (consp title)
115           (map nil #'display-sections sects)
116         (if (consp value)
117             (progn
118               (print-sect-title title)
119               (incf *section-indent*)
120               (display-sections value)
121               (decf *section-indent*))
122           (display-one-section title value))))))
123       
124 (defun display-page (pkg-name pkg-base ftp-base ftp-url sects)
125   (let ((*section-indent* 3)
126         (*ftp-base* ftp-base)
127         (*ftp-url* ftp-url)
128         (*base-name* pkg-base)
129         (*signed* nil))
130     (display-header pkg-name ftp-url)
131     (map nil #'display-sections sects)
132     (display-footer)))
133
134 (defun std-dl-page (pkg-name pkg-base ftp-base ftp-url)
135   (let ((base (parse-namestring ftp-base)))
136     (let ((tgz-path (make-pathname :defaults base :type "gz" :name :wild))
137           (zip-path (make-pathname :defaults base :type "zip" :name :wild))
138           (doc-path (make-pathname :defaults base :type "pdf" :name :wild)))
139       (display-page pkg-name pkg-base ftp-base ftp-url
140                     `(("Manual" ,doc-path)
141                       ("Source Code"
142                        (("Unix (.tar.gz)" ,tgz-path)
143                         ("Windows (.zip)" ,zip-path))))))))
144   
145 (defun full-dl-page (pkg-name pkg-base ftp-base ftp-url)
146   (let ((base (parse-namestring ftp-base)))
147     (let ((tgz-path (make-pathname :defaults base :type "gz" :name :wild))
148           (zip-path (make-pathname :defaults base :type "zip" :name :wild))
149           (doc-path (make-pathname :defaults base :type "pdf" :name :wild))
150           (deb-path (merge-pathnames
151                      (make-pathname :directory '(:relative "linux-debian")
152                                     :type :wild :name :wild)
153                      base))
154           (rpm-path (merge-pathnames
155                      (make-pathname :directory '(:relative "linux-rpm")
156                                     :type :wild :name :wild)
157                      base))
158           (w32-path (merge-pathnames
159                      (make-pathname :directory '(:relative "win32")
160                                     :type :wild :name :wild)
161                      base)))
162       (display-page pkg-name pkg-base ftp-base ftp-url
163                     `(("Manual" ,doc-path)
164                       ("Source Code"
165                        (("Unix (.tar.gz)" ,tgz-path)
166                         ("Windows (.zip)" ,zip-path)))
167                       ("Binaries" 
168                        (("Linux Binaries"
169                          (("Debian Linux" ,deb-path)
170                           ("RedHat Linux" ,rpm-path)))
171                         ("Windows Binaries" ,w32-path))))))))