chiark / gitweb /
cvs-takeover: Clone-and-hack of svn-takeover for CVS.
[bin.git] / debman
1 #! /bin/sh -e
2
3 # debman - read a man page from an uninstalled Debian package file (.deb)
4
5 # Copyright (C) 2003 Colin Watson <cjwatson@debian.org>
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
10 # any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 # 02111-1307, USA.
21
22 usage () {
23     if [ "$1" -eq 1 ]; then
24         FD=2
25     else
26         FD=1
27     fi
28     cat >&$FD <<EOF
29 Usage: debman [options] [-- man(1) options] <man page name> ...
30
31 Options should be exactly one of:
32         -f package.deb          read pages from package.deb archive
33         -p package              download .deb for package and read pages
34                                 from there
35 EOF
36     exit "$1"
37 }
38
39 ARGS=`getopt -l help,filename:,package: -o hf:p: -n debman -- "$@"`
40 eval set -- "$ARGS"
41
42 FILENAME=
43 PACKAGE=
44
45 while :; do
46     case "$1" in
47         -h|--help)
48             usage 0
49             ;;
50         -f|--filename)
51             FILENAME="$2"
52             shift 2
53             ;;
54         -p|--package)
55             PACKAGE="$2"
56             shift 2
57             ;;
58         --)
59             shift
60             break
61             ;;
62         *)
63             echo "debman: Internal error in option parsing" >&2
64             exit 1
65             ;;
66     esac
67 done
68
69 if ( [ -n "$FILENAME" ] && [ -n "$PACKAGE" ] ) || \
70    ( [ -z "$FILENAME" ] && [ -z "$PACKAGE" ] ); then
71     usage 1
72 fi
73
74 if test $# -lt 1; then
75     usage 1
76 fi
77
78 # Directory names are duplicated with and without ./ in order to cope with
79 # old .debs whose data.tar.gz components had a slightly different format.
80 MANDIRS='usr/share/man usr/X11R6/man ./usr/share/man ./usr/X11R6/man'
81
82 TEMPDIR=`mktemp -dt debman.XXXXXXXXXX`
83 trap 'rm -rf "$TEMPDIR"' EXIT ERR HUP INT QUIT TERM
84
85 if [ -n "$PACKAGE" ]; then
86     (cd "$TEMPDIR" && debget "$PACKAGE")
87     # There should be at most one file in $TEMPDIR now.
88     FILENAME="`find \"$TEMPDIR\" -name \*.deb -print`"
89     if [ -z "$FILENAME" ]; then
90         echo "Failed to fetch package $PACKAGE; exiting." >&2
91         exit 1
92     fi
93 fi
94
95 # Ignore errors from tar (though not dpkg). They'll generally just be of the
96 # form "tar: usr/share/man: Not found in archive". If they're something
97 # else, then man will fail to find the page anyway.
98
99 dpkg --fsys-tarfile "$FILENAME" | \
100     (tar -C "$TEMPDIR" -xf - $MANDIRS 2>/dev/null || true)
101
102 MANPATH="$TEMPDIR/usr/share/man:$TEMPDIR/usr/X11R6/man" man "$@"
103
104 # vi: expandtab