r5100: Auto commit for Debian build
[lml.git] / downloads.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          downloads.lisp
6 ;;;; Purpose:       Generate downloads page
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Aug 2002
9 ;;;;
10 ;;;; $Id: downloads.lisp,v 1.10 2003/06/07 22:46:04 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 (in-package #:lml)
20
21
22 (defvar *dl-base*)
23 (defvar *dl-url*)
24 (defvar *base-name*)
25 (defvar *section-indent* 0)
26 (defvar *signed* nil)
27
28 (defun list-files (files)
29   "List files in a directory for downloading"
30   ;;files.sort()
31   (mapcar #'print-file files))
32
33 (defun strip-dl-base (file)
34   (let ((fdir (pathname-directory file))
35         (bdir (pathname-directory *dl-base*)))
36     (make-pathname
37      :name (pathname-name file)
38      :type (pathname-type file)
39      :directory 
40      (when (> (length fdir) (length bdir))
41        (append '(:absolute) 
42                (subseq fdir (length bdir) (length fdir)))))))
43      
44 (defun print-file (file)
45   (let ((size 0)
46         (modtime (date-string (file-write-date file)))
47         (basename (namestring
48                    (make-pathname :name (pathname-name file)
49                                   :type (pathname-type file))))
50         (dl-name (strip-dl-base file))
51         (sig-path (concatenate 'string (namestring file) ".asc")))
52     (when (plusp (length basename))
53       (with-open-file (strm file :direction :input)
54                       (setq size (round (/ (file-length strm) 1024))))
55       (lml-format "<a href=\"~A~A\">~A</a>" *dl-url* dl-name basename)
56       (lml-princ "<span class=\"modtime\">")
57       (lml-format " (~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-format " [<a href=\"~A~A.asc\">Signature</a>]" *dl-url* dl-name))
61       (br))))
62
63 (defun display-header (name url)
64   (lml-princ "<h1>Download</h1>")
65   (lml-princ "<div class=\"mainbody\">")
66   (lml-format "<h3>Browse ~A Download Site</h3>" name)
67   (lml-format "<a style=\"padding-left:20pt;\" href=\"~A\">~A</a>" url url))
68
69 (defun display-footer ()
70   (when *signed*
71     (lml-princ "<h3>GPG Public Key</h3>")
72     (lml-princ "Use this <a href=\"https://www.b9.com/kevin.gpg.asc\">key</a> to verify file signtatures"))
73   (lml-princ "</div>"))
74   
75 (defun print-sect-title (title)
76   (lml-format "<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       (string= name *base-name* :end1 len-base-name :end2 len-base-name))))
82
83 (defun filter-against-base (files)
84   (delete-if-not #'(lambda (f) (match-base-name? (pathname-name f))) files))
85
86 (defun sort-pathnames (list)
87   (sort list #'(lambda (a b) (string< (namestring a) (namestring b)))))
88
89 (defun display-one-section (title pat)
90   (let ((files (sort-pathnames (filter-against-base (directory pat)))))
91     (when files
92       (print-sect-title title)
93       (lml-princ "<div style=\"padding-left: 20pt;\">")
94       (list-files files)
95       (lml-princ "</div>"))))
96
97 (defun display-sections (sects)
98   (when sects
99     (let ((title (car sects))
100           (value (cadr sects)))
101       (if (consp title)
102           (dolist (sect sects) (display-sections sect))
103         (if (consp value)
104             (progn
105               (print-sect-title title)
106               (incf *section-indent*)
107               (display-sections value)
108               (decf *section-indent*))
109           (display-one-section title value))))))
110       
111 (defun display-page (pkg-name pkg-base dl-base dl-url sects)
112   (let ((*section-indent* 3)
113         (*dl-base* dl-base)
114         (*dl-url* dl-url)
115         (*base-name* pkg-base)
116         (*signed* nil))
117     (display-header pkg-name dl-url)
118     (map nil #'display-sections sects)
119     (display-footer)))
120
121 (defun std-dl-page (pkg-name pkg-base dl-base dl-url)
122   (let ((base (parse-namestring dl-base)))
123     (let ((tgz-path (make-pathname :defaults base :type "gz" :name :wild))
124           (zip-path (make-pathname :defaults base :type "zip" :name :wild))
125           (doc-path (make-pathname :defaults base :type "pdf" :name :wild)))
126       (display-page pkg-name pkg-base dl-base dl-url
127                     `(("Manual" ,doc-path)
128                       ("Source Code"
129                        (("Unix (.tar.gz)" ,tgz-path)
130                         ("Windows (.zip)" ,zip-path))))))))
131   
132 (defun full-dl-page (pkg-name pkg-base dl-base dl-url)
133   (let ((base (parse-namestring dl-base)))
134     (let ((tgz-path (make-pathname :defaults base :type "gz" :name :wild))
135           (zip-path (make-pathname :defaults base :type "zip" :name :wild))
136           (doc-path (make-pathname :defaults base :type "pdf" :name :wild))
137           (deb-path (merge-pathnames
138                      (make-pathname :directory '(:relative "linux-debian")
139                                     :type :wild :name :wild)
140                      base))
141           (rpm-path (merge-pathnames
142                      (make-pathname :directory '(:relative "linux-rpm")
143                                     :type :wild :name :wild)
144                      base))
145           (w32-path (merge-pathnames
146                      (make-pathname :directory '(:relative "win32")
147                                     :type :wild :name :wild)
148                      base)))
149       (display-page pkg-name pkg-base dl-base dl-url
150                     `(("Manual" ,doc-path)
151                       ("Source Code"
152                        (("Unix (.tar.gz)" ,tgz-path)
153                         ("Windows (.zip)" ,zip-path)))
154                       ("Binaries" 
155                        (("Linux Binaries"
156                          (("Debian Linux" ,deb-path)
157                           ("RedHat Linux" ,rpm-path)))
158                         ("Windows Binaries" ,w32-path))))))))