summaryrefslogtreecommitdiff
path: root/debian/local/dexconf
blob: 8261cad7afba2f77380d024c2ac3ee1a65a24f35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/sh

# dexconf: Debian X server configuration file writer
#
# This tool is a backend which uses debconf database values.  It writes an
# XFree86 X server configuration file based on the information in the database.
#
# Author: Branden Robinson

# Copyright 2000--2004 Progeny Linux Systems, Inc.
#
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
#
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License with
# the Debian operating system, in /usr/share/common-licenses/GPL;  if
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA

set -e

# source debconf library
. /usr/share/debconf/confmodule

# display a usage message
usage () {
  cat <<EOF
Usage: $PROGNAME [OPTION ...]
  write an Xorg X server configuration file based on debconf database values
Options:
  -h, --help                                 display this usage message and exit
  -o FILE, --output=FILE                        write configuration file to FILE
This help message is intended only as a quick reference.  For a description of
the usage of $PROGNAME, see the $PROGNAME(1) manual page.
EOF
}

# the error-out function
bomb () {
  echo "$PROGNAME: error: $*" | fold -s -w "${COLUMNS:-80}" >&2
  exit 1
}

# wrapper around db_get to ensure that the info we try to retrieve exists; it
# is (almost) always a fatal error for the values to be null
fetch () {
  db_get "$1" || true
  if [ -z "$RET" ]; then
    ERRMSG="cannot generate configuration file; $1 not set.  Aborting."
    ERRMSG="$ERRMSG  Reconfigure the X server with \"dpkg-reconfigure"
    ERRMSG="$ERRMSG xserver-xorg\" to correct this problem."
    bomb "$ERRMSG"
  fi
}

# convert a debconf comma-delimited list to a shell whitespace-delimited list
list_convert () {
  echo $(IFS=", "; set -- $RET; while [ $# -gt 0 ]; do echo \"$1\"; shift; done)
}

SERVER="xorg"
XF86CONFIG=/etc/X11/xorg.conf
PROGNAME=${0##*/}
SHOWHELP=
EARLYEXIT=

GETOPT_OUTPUT=$(getopt --options ho: \
                       --longoptions help,output: \
                       -n "$PROGNAME" -- "$@")

if [ $? -ne 0 ]; then
    bomb "error while getting options; use \"$PROGNAME --help\" for help"
fi

eval set -- "$GETOPT_OUTPUT"

while :; do
    case "$1" in
        -f|--format)
          bomb "This option, and XFree86 3.x output, are no longer supported."
          ;;
        -h|--help) SHOWHELP=yes EARLYEXIT=yes ;;
        -o|--output) XF86CONFIG="$2"; shift ;;
        --) shift; break ;;
        *)
          bomb "unrecognized option \"$1\"; use \"$PROGNAME --help\" for help"
          ;;
    esac
    shift
done

if [ -n "$SHOWHELP" ]; then
    usage
fi

if [ -n "$EARLYEXIT" ]; then
    exit 0
fi

DEXCONFTMPDIR=

trap 'if [ -e "$DEXCONFTMPDIR/backup" ] && [ -n "$XF86CONFIG" ]; then \
        cat "$DEXCONFTMPDIR/backup" >"$XF86CONFIG"; \
      fi; \
      exec 4<&-; \
      rm -rf "$DEXCONFTMPDIR"; \
      bomb "received signal; aborting"' HUP INT QUIT TERM


# Set up a temporary directory for the files we'll be writing.
TDIR_PARENT="${TMPDIR:-/tmp}"
TDIR="${TMPDIR:-/tmp}/dexconf-tmp-$$"

if [ ! -d "$TDIR_PARENT" ]; then
  bomb "cannot create temporary work directory; \"$TDIR_PARENT\" does not" \
    "exist or is not a directory"
fi

if [ ! -w "$TDIR_PARENT" ]; then
  bomb "cannot create temporary work directory in \"$TDIR_PARENT\"; directory" \
    "not writable"
fi

rm -rf "$TDIR"

if mkdir -m 0700 "$TDIR"; then
  DEXCONFTMPDIR="$TDIR"
else
  bomb "creation of temporary work directory \"$TDIR\" failed"
fi

# xorg.conf sections:
#   Files          File pathnames                    NOT USED BY DEXCONF
#   ServerFlags    Server flags                      NOT USED BY DEXCONF
#   Module         Dynamic module loading            NOT USED BY DEXCONF
#   InputDevice    Input device description          NOT USED BY DEXCONF
#   Device         Graphics device description
#   VideoAdaptor   Xv video adaptor description      NOT USED BY DEXCONF
#   Monitor        Monitor description               NOT USED BY DEXCONF
#   Modes          Video modes descriptions          NOT USED BY DEXCONF
#   Screen         Screen configuration              NOT USED BY DEXCONF
#   ServerLayout   Overall layout                    NOT USED BY DEXCONF
#   DRI            DRI-specific configuration        NOT USED BY DEXCONF
#   Vendor         Vendor-specific configuration     NOT USED BY DEXCONF

### HEADER

# Because debconf hijacks standard output and its confmodule uses file
# descriptor 3 for its own purposes, we will write our output to file descriptor
# 4 instead of standard output.

exec 4>"$DEXCONFTMPDIR/Header"
cat >&4 <<SECTION
# xorg.conf (X.Org X Window System server configuration file)
#
# This file was generated by dexconf, the Debian X Configuration tool, using
# values from the debconf database.
#
# Edit this file with caution, and see the xorg.conf manual page.
# (Type "man xorg.conf" at the shell prompt.)
#
# This file is automatically updated on xserver-xorg package upgrades *only*
# if it has not been modified since the last upgrade of the xserver-xorg
# package.
#
# If you have edited this file but would like it to be automatically updated
# again, run the following command:
#   sudo dpkg-reconfigure -phigh xserver-xorg
SECTION

### DEVICE

db_get xserver-$SERVER/config/device/bus_id
DEVICE_BUSID="$RET"
exec 4>"$DEXCONFTMPDIR/Device"
cat >&4 <<SECTION
Section "Device"
	Identifier	"Configured Video Device"
SECTION
if [ -n "$DEVICE_BUSID" ]; then
  printf "\tBusID\t\t\"$DEVICE_BUSID\"\n" >&4
fi
printf "EndSection\n" >&4

# Close file descriptor 4 before we delete temporary files
exec 4<&-

# Tell debconf to stop listening to us.
db_stop

# Write the configuration file.  Put a blank line before every section we write
# except the first.

OUTFILE="$DEXCONFTMPDIR/dexconf-out"
umask 022
: >"$OUTFILE"

SPACER=
for SECTION in Header Device ; do
  if [ -e "$DEXCONFTMPDIR/$SECTION" ]; then
    eval $SPACER
    cat "$DEXCONFTMPDIR/$SECTION" >>"$OUTFILE"
    SPACER='echo "" >>"$OUTFILE"'
  fi
done

# Ensure we can write to our destination if it already exits.
if [ -e "$XF86CONFIG" ]; then
  if [ ! -w "$XF86CONFIG" ]; then
    bomb "unable to write to \"$XF86CONFIG\""
  fi
fi

BACKUP=
# Create a backup of the existing configuration file if it already exists.
if [ -e "$XF86CONFIG" ]; then
  cat "$XF86CONFIG" >"$DEXCONFTMPDIR/backup"
  BACKUP=true
fi

# Move the new file into place.
if ! cat "$OUTFILE" >"$XF86CONFIG"; then
  # Failed; try to restore the backup.
  if [ -n "$BACKUP" ]; then
    cat "$DEXCONFTMPDIR/backup" >"$XF86CONFIG"
  fi
fi

rm -rf "$DEXCONFTMPDIR"

exit 0

# vim:set ai et sts=2 sw=2 tw=80: