r2999: *** 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.2 2002/10/14 03:25:05 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 (declaim (type (fixnum *section-indent*)))
28 (defvar *signed* nil)
29
30 (defun list-files (files)
31   "List files in a directory for downloading"
32   ;;files.sort()
33   (mapcar #'print-file files))
34
35 (defun strip-ftp-base (file)
36   (let ((fdir (pathname-directory file))
37         (bdir (pathname-directory *ftp-base*)))
38     (make-pathname
39      :name (pathname-name file)
40      :type (pathname-type file)
41      :directory 
42      (when (> (length bdir) (length fdir))
43        (append '(:absolute) 
44                (subseq fdir (length bdir) (length fdir)))))))
45      
46 (defun print-file (file)
47   (let ((size 0)
48         (modtime (date-string (file-write-date file)))
49         (basename (namestring
50                    (make-pathname :name (pathname-name file)
51                                   :type (pathname-type file))))
52         (ftp-name (strip-ftp-base file))
53         (sig-path (concatenate 'string (namestring file) ".asc")))
54     (when (plusp (length basename))
55       (with-open-file (strm file :direction :input)
56                       (setq size (round (/ (file-length strm) 1024))))
57       (lml-print "<a href=\"~A~A\">~A</a>" *ftp-url* ftp-name basename)
58       (lml-print "<span class=\"modtime\">")
59       (lml-print " (~A, <b>~:D <span style=\"font-size:90%;\">KB</span></b>)</span>" modtime size)
60       (when (probe-file sig-path)
61         (setq *signed* t)
62         (lml-print " [<a href=\"~A~A.asc\">Signature</a>]" *ftp-url* ftp-name))
63       (br))))
64
65 (defun display-header (name url)
66   (lml-print "<h1>Download</h1>")
67   (lml-print "<div class=\"mainbody\">")
68   (lml-print "<h3>Browse ~A FTP Site</h3>" name)
69   (lml-print "<a style=\"padding-left:20pt;\" href=\"~A\">~A</a>" url url))
70
71 (defun display-footer ()
72   (when *signed*
73     (lml-print "<h3>GPG Public Key</h3>")
74     (lml-print "Use this <a href=\"https://www.b9.com/key.asc\">key</a> to verify file signtatures"))
75   (lml-print "</div>"))
76   
77 (defun print-sect-title (title)
78   (lml-print "<h~D>~A</h~D>" *section-indent* title *section-indent*))
79
80 (defun match-base-name? (name)
81   (let ((len-base-name (length *base-name*)))
82     (when (>= (length name) len-base-name)
83       (dotimes (i len-base-name)
84         (declare (fixnum i))
85         (unless (char= (char *base-name* i)
86                        (char name i))
87           (return-from match-base-name? nil)))))
88   t)
89
90 (defun filter-against-base (files)
91   (let ((filtered '()))
92     (dolist (f files)
93       (let ((name (pathname-name f)))
94         (when (match-base-name? name)
95           (push f filtered))))
96     (when filtered
97       (sort filtered #'(lambda (a b) (when (and a b)
98                                        (string<
99                                         (namestring a)
100                                         (namestring b))))))))
101
102 (defun display-one-section (title pat)
103   (let ((files (filter-against-base (directory pat))))
104     (when files
105       (print-sect-title title)
106       (lml-print "<div style=\"padding-left: 20pt;\">")
107       (list-files files)
108       (lml-print"</div>"))))
109
110
111 (defun display-sections (sects)
112   (when sects
113     (let ((title (car sects))
114           (value (cadr sects)))
115       (if (consp title)
116           (mapcar #'display-sections sects)
117         (if (consp  value)
118             (progn
119               (print-sect-title title)
120               (incf *section-indent*)
121               (display-sections value)
122               (decf *section-indent*))
123           (display-one-section title value))))))
124       
125 (defun display-page (pkg-name pkg-base ftp-base ftp-url sects)
126   (let ((*section-indent* 3)
127         (*ftp-base* ftp-base)
128         (*ftp-url* ftp-url)
129         (*base-name* pkg-base)
130         (*signed* nil))
131     (display-header pkg-name ftp-url)
132     (mapcar #'display-sections sects)
133     (display-footer)))
134
135 (defun std-dl-page (pkg-name pkg-base ftp-base ftp-url)
136   (let ((base (parse-namestring ftp-base)))
137     (let ((tgz-path (make-pathname :defaults base :type "gz" :name :wild))
138           (zip-path (make-pathname :defaults base :type "zip" :name :wild))
139           (doc-path (make-pathname :defaults base :type "pdf" :name :wild)))
140       (display-page pkg-name pkg-base ftp-base ftp-url
141                     `(("Manual" ,doc-path)
142                       ("Source Code"
143                        (("Unix (.tar.gz)" ,tgz-path)
144                         ("Windows (.zip)" ,zip-path))))))))
145   
146 (defun full-dl-page (pkg-name pkg-base ftp-base ftp-url)
147   (let ((base (parse-namestring ftp-base)))
148     (let ((tgz-path (make-pathname :defaults base :type "gz" :name :wild))
149           (zip-path (make-pathname :defaults base :type "zip" :name :wild))
150           (doc-path (make-pathname :defaults base :type "pdf" :name :wild))
151           (deb-path (merge-pathnames
152                      (make-pathname :directory '(:relative "linux-debian")
153                                     :type :wild :name :wild)
154                      base))
155           (rpm-path (merge-pathnames
156                      (make-pathname :directory '(:relative "linux-rpm")
157                                     :type :wild :name :wild)
158                      base))
159           (w32-path (merge-pathnames
160                      (make-pathname :directory '(:relative "w32")
161                                     :type :wild :name :wild)
162                      base)))
163       (display-page pkg-name pkg-base ftp-base ftp-url
164                     `(("Manual" ,doc-path)
165                       ("Source Code"
166                        (("Unix (.tar.gz)" ,tgz-path)
167                         ("Windows (.zip)" ,zip-path)))
168                       ("Binaries" 
169                        (("Linux Binaries"
170                          (("Debian Linux" ,deb-path)
171                           ("RedHat Linux" ,rpm-path)))
172                         ("Windows Binaries" ,w32-path))))))))