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