| 1 |
# This file, debianize-fmtutil, is meant to be sourced by fmtutil and |
| 2 |
# enhance the --enable and --disable options with the functionality |
| 3 |
# needed to deal with Debian's generated fmtutil.cnf. |
| 4 |
# $Id$ |
| 5 |
|
| 6 |
# This is the planned scheme how it works |
| 7 |
# |
| 8 |
# A for disablefmt: |
| 9 |
# 1. create a list of files to act on (either in sysconfdir only, or in user dirs too) |
| 10 |
# 2. grep for not-commented lines in these files that contain $fmt |
| 11 |
# 3. if in syswide-mode, for each of these files, run configReplace appropriately; |
| 12 |
# if not in syswide mode, for each of these files, |
| 13 |
# test whether it is in the syswide dir |
| 14 |
# 3.a if not, run configReplace appropriately |
| 15 |
# 3b. if yes, create copy in user dir and run configReplace appropriately |
| 16 |
# |
| 17 |
# B for enableMap: |
| 18 |
# 1. create a list of files to act on (either in sysconfdir only, or in user dirs too) |
| 19 |
# 2. grep for commented lines in these files that contain $map |
| 20 |
# 2.1. if any are found, check whether it is only one |
| 21 |
# 2.1.1 if there is only one, run configReplace on that file appropriately |
| 22 |
# 2.1.2 if there is more than one, the precedence is |
| 23 |
# 20tetex-extra.cfg > XX*local*.cfg > 10tetex-base.cfg > any |
| 24 |
# (extra has maximum priority because |
| 25 |
# 2.1.2.1 if it's clear on which file to act, run configReplace on that file |
| 26 |
# appropriately |
| 27 |
# 2.1.2.2 if unclear (i.e. only "any"), exit with an error |
| 28 |
# 2.2 if none are found, act on 10local.cfg (could be made configurable) |
| 29 |
# - if 10local.cfg does not exist, create it (with an explanatory comment) |
| 30 |
# - run configReplace on it appropriately |
| 31 |
# |
| 32 |
# C for syncwithtrees (and even more?) |
| 33 |
# ? |
| 34 |
# |
| 35 |
# After that, update-fmtutil must be run (in the appropriate mode!), and then fmtutil |
| 36 |
# must reexecute itself. Since disableMap is called by enableMap, we need a wrapper |
| 37 |
# for it. |
| 38 |
|
| 39 |
############################################################################### |
| 40 |
# verboseMsg(msg) |
| 41 |
# print `msg' to stderr is $verbose is true |
| 42 |
############################################################################### |
| 43 |
verboseMsg() { |
| 44 |
$verboseFlag && verbose echo ${1+"$@"} >&2 |
| 45 |
} |
| 46 |
|
| 47 |
############################################################################### |
| 48 |
# enablefmt(format) |
| 49 |
# enables format in configuration file |
| 50 |
############################################################################### |
| 51 |
enablefmt() |
| 52 |
{ |
| 53 |
enableFmtInner $1 |
| 54 |
runUpdate |
| 55 |
} |
| 56 |
|
| 57 |
############################################################################### |
| 58 |
# disablefmt(format) |
| 59 |
# disables format in configuration file |
| 60 |
############################################################################### |
| 61 |
disablefmt() |
| 62 |
{ |
| 63 |
disableFmtInner $1 |
| 64 |
runUpdate |
| 65 |
} |
| 66 |
|
| 67 |
############################################################################### |
| 68 |
# debianEditWarnAndShow() |
| 69 |
# warn that this won't work |
| 70 |
############################################################################### |
| 71 |
debianEditWarnAndShow(){ |
| 72 |
echo "Warning!" |
| 73 |
echo "" |
| 74 |
echo "In Debian, fmtutil.cnf, the format configuration file, is a generated file." |
| 75 |
echo "The actual configuration files are in /etc/texmf/fmt.d." |
| 76 |
echo "Please read the update-fmtutil(1) manpage and edit those files." |
| 77 |
echo "" |
| 78 |
echo "Instead of editing, you will now be shown fmtutil.cnf in a pager." |
| 79 |
echo "" |
| 80 |
echo "Press enter to continue." |
| 81 |
read dummy |
| 82 |
|
| 83 |
sensible-pager $cnf_file |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
############################################################################### |
| 88 |
# DebianSyswideMode () |
| 89 |
# determine whether we are running in syswide mode (or user-specific) |
| 90 |
############################################################################### |
| 91 |
DebianSyswideMode(){ |
| 92 |
test "$(id -u)" -eq 0 && return 0 || return 1 |
| 93 |
} |
| 94 |
|
| 95 |
############################################################################### |
| 96 |
# FindDebianUserdir () |
| 97 |
# find the directory containing user-specific fmt.d snippets |
| 98 |
############################################################################### |
| 99 |
FindDebianUserdir(){ |
| 100 |
# verboseMsg "entering FindDebianUserdir" |
| 101 |
|
| 102 |
texmfconfig=$(kpsewhich --expand-path '$TEXMFCONFIG') |
| 103 |
OLDIFS="$IFS" |
| 104 |
IFS=: |
| 105 |
count=0 |
| 106 |
for d in "$texmfconfig"; do |
| 107 |
count=$(($count+1)) |
| 108 |
: ${cnfdir:=$d} |
| 109 |
if [ -d "$d/fmt.d" ]; then |
| 110 |
defaultuserdir="$d/fmt.d" |
| 111 |
break |
| 112 |
fi |
| 113 |
done |
| 114 |
IFS="$OLDIFS" |
| 115 |
# still empty? Pick one |
| 116 |
if [ -z "$defaultuserdir" ]; then |
| 117 |
if [ $count -eq 1 ]; then |
| 118 |
defaultuserdir="$cnfdir/fmt.d" |
| 119 |
else |
| 120 |
echo "TEXMFCONFIG contains more than one directory, and none of them" >&2 |
| 121 |
echo "already contains a fmt.d/ subdirectory." >&2 |
| 122 |
echo "I don't know how to proceed here, stopping." >&2 |
| 123 |
return |
| 124 |
fi |
| 125 |
fi |
| 126 |
|
| 127 |
/usr/share/texmf/web2c/mktexdir $defaultuserdir |
| 128 |
if [ -d $defaultuserdir ]; then |
| 129 |
echo $defaultuserdir |
| 130 |
fi |
| 131 |
} |
| 132 |
|
| 133 |
############################################################################### |
| 134 |
# createDebianConffilelist |
| 135 |
# create a list of configuration files containing fmtutil snippets; |
| 136 |
# we are going to act on these |
| 137 |
############################################################################### |
| 138 |
createDebianConffilelist(){ |
| 139 |
# verboseMsg "entering createDebianConffilelist" |
| 140 |
|
| 141 |
userFiles="" |
| 142 |
local DebianConffilelist="" |
| 143 |
syswideFiles=`find /etc/texmf/fmt.d -maxdepth 1 -type f -name '*.cnf'` |
| 144 |
if ! DebianSyswideMode; then |
| 145 |
debianuserdir=`FindDebianUserdir` |
| 146 |
if [ -n "$debianuserdir" ]; then |
| 147 |
userFiles=`find "$debianuserdir" -maxdepth 1 -type f -name '*.cnf'` |
| 148 |
fi |
| 149 |
fi |
| 150 |
if [ -n "$userFiles" ]; then |
| 151 |
for sysfile in $syswideFiles; do |
| 152 |
for userfile in $userFiles; do |
| 153 |
if [ ! "`basename $userfile`" = "`basename $sysfile`" ]; then |
| 154 |
DebianConffilelist="$DebianConffilelist $sysfile" |
| 155 |
fi |
| 156 |
done |
| 157 |
done |
| 158 |
DebianConffilelist="$DebianConffilelist $userFiles" |
| 159 |
else |
| 160 |
DebianConffilelist="$syswideFiles" |
| 161 |
fi |
| 162 |
echo $DebianConffilelist |
| 163 |
} |
| 164 |
|
| 165 |
############################################################################### |
| 166 |
# snippetFileIsWritable (debCnfFile) |
| 167 |
# checks whether we are (supposed to be) able to write to a file |
| 168 |
############################################################################### |
| 169 |
snippetFileIsWritable(){ |
| 170 |
local file=$1 |
| 171 |
|
| 172 |
if DebianSyswideMode; then |
| 173 |
return 0 |
| 174 |
else |
| 175 |
# /etc/texmf/fmt.d shouldn't be hardcoded |
| 176 |
if [ "${file#/etc/texmf}" = "${file}" ]; then |
| 177 |
# file is not in systemwide dir |
| 178 |
return 0 |
| 179 |
else |
| 180 |
return 1 |
| 181 |
fi |
| 182 |
fi |
| 183 |
} |
| 184 |
|
| 185 |
############################################################################### |
| 186 |
# makeUserCopy (filename) |
| 187 |
# create a copy in the user dir |
| 188 |
############################################################################### |
| 189 |
makeUserCopy(){ |
| 190 |
local debCnfFile="$1" |
| 191 |
|
| 192 |
# create a copy of that file in the user dir |
| 193 |
debianuserdir=`FindDebianUserdir` |
| 194 |
newdebCnfFile="$debianuserdir/`basename $debCnfFile`" |
| 195 |
verboseMsg "I'm not allowed to handle $fmt in $debCnfFile." >&2 |
| 196 |
verboseMsg "Creating copy in $debianuserdir instead." >&2 |
| 197 |
cp $debCnfFile $newdebCnfFile |
| 198 |
echo "$newdebCnfFile" |
| 199 |
} |
| 200 |
|
| 201 |
############################################################################### |
| 202 |
# findUserfiles (filelist) |
| 203 |
# pick user-specific files out of a filelist of configuration files |
| 204 |
############################################################################### |
| 205 |
findUserfiles(){ |
| 206 |
userfiles="" |
| 207 |
while [ $# -gt 0 ]; do |
| 208 |
file="$1" |
| 209 |
if [ "$file" = "${file#/etc/texmf/}" ]; then |
| 210 |
# doesn't start with /etc/texmf/: must be user-specific |
| 211 |
userfiles="$userfiles $file" |
| 212 |
fi |
| 213 |
shift; |
| 214 |
done |
| 215 |
echo $userfiles |
| 216 |
} |
| 217 |
|
| 218 |
############################################################################### |
| 219 |
# findRightMatchfile (format,filelist) |
| 220 |
# find the right file from filelist to enable mapname |
| 221 |
############################################################################### |
| 222 |
findRightMatchfile(){ |
| 223 |
fmt=$1; shift |
| 224 |
matchfiles="$@" |
| 225 |
local debCnfFile="" |
| 226 |
|
| 227 |
if [ $# -eq 1 ]; then |
| 228 |
# it is only one, enable map in it. May we write it? |
| 229 |
debCnfFile=$matchfiles |
| 230 |
if ! snippetFileIsWritable $debCnfFile; then |
| 231 |
debCnfFile=`makeUserCopy $debCnfFile` |
| 232 |
fi |
| 233 |
echo $debCnfFile |
| 234 |
verboseMsg "Enabling format in $debCnfFile." |
| 235 |
return 0 |
| 236 |
else |
| 237 |
# there are more than one; |
| 238 |
if ! DebianSyswideMode; then |
| 239 |
# can only change files in the user's dir |
| 240 |
usermatchfiles=`findUserfiles $matchfiles` |
| 241 |
if [ `(set $usermatchfiles; echo $#)` -eq 1 ]; then |
| 242 |
# exactly one of them is a user-specific file |
| 243 |
echo $usermatchfiles |
| 244 |
verboseMsg "Enabling format in user-specific file $usermatchfiles." |
| 245 |
return 0 |
| 246 |
else |
| 247 |
# two files in the user directory (as bad as two in the site-wide |
| 248 |
# dir), or no user-specific files. If there are two user-specific |
| 249 |
# files, report about them: |
| 250 |
test -n "$usermatchfiles" && matchfiles="$usermatchfiles" || true |
| 251 |
fi |
| 252 |
fi |
| 253 |
# for now, we exit with an error. Any sensible choice would have to |
| 254 |
# check whether teTeX or texlive is installed, and I want to save me |
| 255 |
# that hazzle. |
| 256 |
cat >&2 <<EOF |
| 257 |
Entries for format $fmt found in several files: |
| 258 |
$matchfiles |
| 259 |
|
| 260 |
Since fmtutil.cnf is a generated file in Debian, I don't know how to proceed. |
| 261 |
Please refer to the manpage of update-fmtutil(1) |
| 262 |
|
| 263 |
EOF |
| 264 |
fi |
| 265 |
} |
| 266 |
|
| 267 |
############################################################################### |
| 268 |
# pickLocalFile (mapname,directory) |
| 269 |
# pick the right local file in directory |
| 270 |
############################################################################### |
| 271 |
pickLocalFile(){ |
| 272 |
fmt=$1 |
| 273 |
debDirname=$2 |
| 274 |
debDirname=${debDirname%/} |
| 275 |
# Is there a file with "local" in the name? |
| 276 |
localfile="`ls $debDirname/*local*cfg 2>/dev/null`" |
| 277 |
if [ -n "$localfile" ]; then |
| 278 |
# there is at least one |
| 279 |
if [ `(set $localfile; echo $#)` -eq 1 ]; then |
| 280 |
# exactly one |
| 281 |
verboseMsg "Using local configuration file $localfile" |
| 282 |
echo $localfile |
| 283 |
return 0 |
| 284 |
else |
| 285 |
# more than one local file? Brrrr. |
| 286 |
cat >&2 <<EOF |
| 287 |
While trying to enable $fmt locally, I found several user-specific config files: |
| 288 |
$localfile |
| 289 |
|
| 290 |
I don't know how to proceed here. Please refer to the manpage of update-fmtutil(1) |
| 291 |
|
| 292 |
EOF |
| 293 |
fi |
| 294 |
else |
| 295 |
# no user config file yet |
| 296 |
echo $debDirname/10local.cfg |
| 297 |
return 0 |
| 298 |
fi |
| 299 |
} |
| 300 |
|
| 301 |
############################################################################### |
| 302 |
# findRightLocalfile (fmt) |
| 303 |
# find the right file to enable fmt, site-wide or user-specific |
| 304 |
############################################################################### |
| 305 |
findRightLocalfile(){ |
| 306 |
fmt=$1 |
| 307 |
# Do we know about a user-specific directory? |
| 308 |
if DebianSyswideMode; then |
| 309 |
DebCnfFile=`pickLocalFile $fmt /etc/texmf/fmt.d` |
| 310 |
else |
| 311 |
debianuserdir=`FindDebianUserdir` |
| 312 |
test -w $debianuserdir || ( |
| 313 |
echo "Target directory $debianuserdir not writable." >&2 |
| 314 |
echo "Exiting" >&2 |
| 315 |
exit 0 |
| 316 |
) |
| 317 |
DebCnfFile=`pickLocalFile $fmt $debianuserdir` |
| 318 |
fi |
| 319 |
# # configReplace will try to grep in the file: touch it to prevent a error message |
| 320 |
# touch $DebCnfFile |
| 321 |
verboseMsg "Using local configuration file $DebCnfFile" |
| 322 |
echo $DebCnfFile |
| 323 |
} |
| 324 |
|
| 325 |
############################################################################### |
| 326 |
# runUpdate |
| 327 |
# run update-fmtutil |
| 328 |
############################################################################### |
| 329 |
runUpdate(){ |
| 330 |
updOptions="" |
| 331 |
if [ -n "$cfgparam" ]; then |
| 332 |
# --conffile option was given on the command line |
| 333 |
updOptions="--output-file $orig" |
| 334 |
fi |
| 335 |
# here we could check for an alternative user dir, or output file |
| 336 |
# but as long as FindDebianUserdir only reports the default location, |
| 337 |
# it doesn't make sense |
| 338 |
|
| 339 |
verboseMsg "Running update-fmtutil to merge the changed files" |
| 340 |
update-fmtutil --quiet $updOptions |
| 341 |
} |
| 342 |
|
| 343 |
############################################################################### |
| 344 |
# disableFmtInner (map) |
| 345 |
# disables fmt in config file (any type) (real command) |
| 346 |
############################################################################### |
| 347 |
disableFmtInner() |
| 348 |
{ |
| 349 |
fmt=$1 |
| 350 |
noverbose="$2" |
| 351 |
|
| 352 |
local debCnfFile |
| 353 |
|
| 354 |
oldverbose="$verbose" |
| 355 |
if [ -n "$noverbose" ]; then |
| 356 |
# don't confuse users with disable messages when they called --enable |
| 357 |
# verboseMsg "disabling verbosity" |
| 358 |
verbose=false |
| 359 |
fi |
| 360 |
|
| 361 |
|
| 362 |
if [ -z "$DebianConffilelist" ]; then |
| 363 |
DebianConffilelist=`createDebianConffilelist` |
| 364 |
fi |
| 365 |
|
| 366 |
# create list of all files that contain an entry |
| 367 |
for debCnfFile in $DebianConffilelist; do |
| 368 |
if grep -q "^$fmt" $debCnfFile; then |
| 369 |
if ! snippetFileIsWritable $debCnfFile; then |
| 370 |
debCnfFile=`makeUserCopy $debCnfFile` |
| 371 |
fi |
| 372 |
verboseMsg "Disabling $fmt in $debCnfFile." |
| 373 |
ed $debCnfFile >/dev/null 2>&1 <<-eof |
| 374 |
g/^$fmt[ ]/s/^/#! / |
| 375 |
w |
| 376 |
q |
| 377 |
eof |
| 378 |
fi |
| 379 |
done |
| 380 |
|
| 381 |
verbose="$oldverbose" |
| 382 |
return 0 |
| 383 |
} |
| 384 |
|
| 385 |
############################################################################### |
| 386 |
# enableFmtInner (fmt) |
| 387 |
# enables fmt in config file (any type) (real command) |
| 388 |
############################################################################### |
| 389 |
enableFmtInner(){ |
| 390 |
fmt=$1 |
| 391 |
local debCnfFile="" |
| 392 |
|
| 393 |
if [ -z "$DebianConffilelist" ]; then |
| 394 |
DebianConffilelist=`createDebianConffilelist` |
| 395 |
fi |
| 396 |
|
| 397 |
# a format should only be defined once, so we carefully disable |
| 398 |
# everything about it here: |
| 399 |
disableFmtInner "$fmt" noverbose |
| 400 |
|
| 401 |
# now enable with the right type. |
| 402 |
# But we have to determine where to enable. |
| 403 |
# is there already a file that contains a (commented) entry? |
| 404 |
matchfiles="" |
| 405 |
for file in $DebianConffilelist; do |
| 406 |
if grep -q "^#![ ]*$fmt[ ]" "$file"; then |
| 407 |
matchfiles="$matchfiles $file" |
| 408 |
fi |
| 409 |
done |
| 410 |
if [ -n "$matchfiles" ]; then |
| 411 |
# there is already (at least) one file that has a commented entry for our map |
| 412 |
debCnfFile=`findRightMatchfile $fmt $matchfiles` |
| 413 |
test -w "$debCnfFile" || debCnfFile="" |
| 414 |
fi |
| 415 |
if [ -z "$debCnfFile" ]; then |
| 416 |
# still none, there is no (commented) entry in any writable file yet. |
| 417 |
debCnfFile=`findRightLocalfile $fmt` |
| 418 |
test -n "$debCnfFile" || abort "Cannot proceed. Exiting" |
| 419 |
verboseMsg "Creating new entry for format $fmt in $debCnfFile" |
| 420 |
fi |
| 421 |
|
| 422 |
ed $debCnfFile >/dev/null 2>&1 <<-eof |
| 423 |
g/^#![ ]*$fmt[ ]/s/..[ ]*// |
| 424 |
w |
| 425 |
q |
| 426 |
eof |
| 427 |
} |
| 428 |
|
| 429 |
|