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