Debian Codegraph @

debian/paperconfig

raw
1 #!/bin/sh
2
3 # paperconfig: configuration of system paper name
4 #
5 # Copyright (C) 1996, Yves Arrouye <[email protected]>
6 # 2001, Adrian Bunk <[email protected]>
7 # 2021-2022, Reuben Thomas <[email protected]>
8 #
9 # This file is part of the libpaper Debian package.
10 #
11 # This program is free software: you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License version 2 as published
13 # by the Free Software Foundation.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see
22 # <https://www.gnu.org/licenses/gpl-2.0.html>.
23
24 sysconfdir="/etc"
25 PAPERSIZE="$sysconfdir/papersize"
26 PAPERRUNPARTSDIR="$sysconfdir/paper.d"
27 PAPER_PROGRAM="/usr/bin/paper"
28
29 usage() {
30 echo "Usage: `basename $0` [--version|--help|--paper NAME|--choose]"
31
32 cat <<EOF
33
34 Options: -V, --version print version information and exit
35 -h, --help print this help and exit
36 -p, --paper NAME specify the paper to use
37 -c, --choose choose paper interactively
38 EOF
39 exit $1
40 }
41
42 choose=0
43
44 if [ "$#" -eq 0 ]; then
45 exec "$PAPER_PROGRAM" --no-size
46 fi
47
48 while [ $# -ne 0 ]; do
49 case "$1" in
50 -V|--version)
51 echo "`basename $0` version 2" \
52 "by Yves Arrouye <[email protected]>"
53 exit 0
54 ;;
55 -h|--help)
56 usage 0
57 ;;
58 -p|--paper)
59 test $# -gt 1 || usage
60 paper="$2"
61 shift
62 ;;
63 -c|--choose)
64 choose=1
65 ;;
66 *)
67 usage 1
68 ;;
69 esac
70 shift
71 done
72
73 invalidpaper() {
74 ! "$PAPER_PROGRAM" --no-size "$1" 2>/dev/null 1>&2
75 }
76
77 setpaper() {
78 paperright=`"$PAPER_PROGRAM" --no-size "$1" 2>/dev/null`
79
80 ok=0
81
82 if 2>/dev/null echo "$paperright" >${PAPERSIZE}.new; then
83 if 2>/dev/null mv -f ${PAPERSIZE}.new $PAPERSIZE; then
84 if 2>/dev/null chmod 644 $PAPERSIZE; then
85 ok=1
86 fi
87 fi
88 fi
89
90 if [ $ok -eq 0 ]; then
91 echo `basename $0`: cannot create $PAPERSIZE
92 exit 2
93 else
94 if [ -d $PAPERRUNPARTSDIR ]; then
95 run-parts $PAPERRUNPARTSDIR
96 fi
97 fi
98 }
99
100 if [ ! -z "$paper" ]; then
101 if invalidpaper $paper; then
102 >&2 echo `basename $0`: \"$paper\" is not a known paper name
103 exit 3
104 fi
105 setpaper "$paper"
106 exit 0
107 fi
108
109 knownpapers="`"$PAPER_PROGRAM" --all --no-size`"
110
111 prompt=
112 width=72
113
114 paper=`"$PAPER_PROGRAM" --no-size 2>/dev/null || true`
115 dftpaper=`"$PAPER_PROGRAM" --no-size`
116
117 if [ $choose -eq 1 ]; then
118 echo
119
120 LESS="-X -E $LESS"
121 export LESS
122
123 (
124 cat <<EOF
125 The system paper can be chosen from many known papers that are
126 currently recognized:
127
128 EOF
129 echo "$knownpapers" | sed 's/^/ /'
130 echo
131 ) | /usr/bin/sensible-pager
132
133 : ${paper:=$dftpaper}
134
135 echo -n "Default paper name? [$paper] "
136 read ans
137 : ${ans:=$paper}
138 paper=$ans
139
140 while invalidpaper "$paper"; do
141 if [ -z "$paper" ]; then
142 echo "Please choose a paper from the available papers list."
143 else
144 echo "Unknown paper \"$paper\"," \
145 "please choose one from the available papers list."
146 fi
147
148 paper=$dftpaper
149
150 echo -n "Default paper name? [$paper] "
151 read ans
152 paper=$ans
153 done
154 fi
155
156 if invalidpaper "$paper"; then
157 echo "Invalid paper \"$paper\": not setting the system default."
158 exit 4
159 elif [ "`"$PAPER_PROGRAM" --no-size 2>/dev/null`" != "$paper" ] || grep -q "[# ]" $PAPERSIZE; then
160 setpaper "$paper"
161 fi