summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/texconfig/tcfmgr
blob: 08dddbd4d1a71e5be749e58b6dba37efade96b9e (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/bin/sh
# $Id: tcfmgr 34641 2014-07-17 16:38:18Z karl $
# tcfmgr - TeX configuration file manager (sort of).
# Public domain.  Originally written by Thomas Esser.

# invoke the right shell:

test -f /bin/ksh && test -z "$RUNNING_KSH" \
  && { UNAMES=`uname -s`; test "x$UNAMES" = xULTRIX; } 2>/dev/null \
  && { RUNNING_KSH=true; export RUNNING_KSH; exec /bin/ksh $0 ${1+"$@"}; }
unset RUNNING_KSH

test -f /bin/bsh && test -z "$RUNNING_BSH" \
  && { UNAMES=`uname -s`; test "x$UNAMES" = xAIX; } 2>/dev/null \
  && { RUNNING_BSH=true; export RUNNING_BSH; exec /bin/bsh $0 ${1+"$@"}; }
unset RUNNING_BSH

# preferentially use subprograms from our own directory.
mydir=`echo "$0" | sed 's,/[^/]*$,,'`
mydir=`cd "$mydir" && pwd`
PATH="$mydir:$PATH"; export PATH

# hack around a bug in zsh:
test -n "${ZSH_VERSION+set}" && alias -g '${1+"$@"}'='"$@"'

progname=tcfmgr

abort()
{
  echo "$progname: $@." >&2
  (exit 1); exit 1
}

mktexupd()
{
  setupMktexupd
  "$TFC_MKTEXUPD" "$@" >&2
}

# call mktexdir script, disable all features (to prevent sticky directories)
mktexdir()
{
  setupMktexDir
  MT_FEATURES=none "$TFC_MKTEXDIR" "$@" >&2
}

setupMap()
{
  case $setupMapOK in
    "") : ;;
    *) (exit 0); return 0;;
  esac
  
  case $TFC_MAP in
    "") TFC_MAP=`(kpsewhich -progname=texconfig -format='other text files' tcfmgr.map) 2>/dev/null`;;
  esac

  case $TFC_MAP in
    "") abort "config file \`tcfmgr.map' (usually in \$TEXMFMAIN/texconfig) not found (ls-R missing?)";;
  esac

  if test ! -f "$TFC_MAP"; then
    abort "config file \`TFC_MAP' does not exist"
  fi
  setupMapOK=true
}

setupTemp()
{
  case $setupTempOK in
    "") : ;;
    *) (exit 0); return 0;;
  esac

  case $TFC_TEMP in
    "") TFC_TEMP=${TEMPDIR-/tmp};;
  esac

  if test ! -d "$TFC_TEMP"; then
    abort "temp directory \`$TFC_TEMP' does not exist"
  fi

  if test ! -w "$TFC_TEMP"; then
    abort "temp directory \`$TFC_TEMP' is not writable"
  fi
  setupTempOK=true
}

setupTexmfConfig()
{
  case $TFC_TEXMFCONFIG in
    "") TFC_TEXMFCONFIG=`(kpsewhich -var-value=TEXMFCONFIG) 2>/dev/null`;;
    *) (exit 0); return 0;;
  esac
  case $TFC_TEXMFCONFIG in
    "") abort "variable \$TEXMFCONFIG is not defined in texmf.cnf";;
  esac
}

setupMktexDir()
{
  case $TFC_MKTEXDIR in
    "") TFC_MKTEXDIR=`(kpsewhich --format='web2c files' mktexdir) 2>/dev/null`;;
  esac

  case $TFC_MKTEXDIR in
    "") abort "program mktexdir (usually in \$TEXMFMAIN/web2c) not found";;
  esac
}

setupMktexupd()
{
  case $TFC_MKTEXUPD in
    "") TFC_MKTEXUPD=`(kpsewhich --format='web2c files' mktexupd) 2>/dev/null`;;
  esac

  case $TFC_MKTEXUPD in
    "") abort "program mktexupd (usually in \$TEXMFMAIN/web2c) not found";;
  esac
}

writableCheck()
{
  setupTexmfConfig
  if test ! -d "$TFC_TEXMFCONFIG"; then
    mktexdir "$TFC_TEXMFCONFIG" >/dev/null 2>&1
    if test ! -d "$TFC_TEXMFCONFIG"; then
      msg="Directory TEXMFCONFIG=\`$TFC_TEXMFCONFIG' does not exist"
      (exit 1); return 1
    fi
  fi

  case "$relDir" in
    "") get_info;;
  esac
  if test -f "$TFC_TEXMFCONFIG/$relDir/$TFC_FILE" \
     && test -w "$TFC_TEXMFCONFIG/$relDir/$TFC_FILE"
  then
    (exit 0); return 0
  fi

  if test -d "$TFC_TEXMFCONFIG/$relDir"; then
    if test -w "$TFC_TEXMFCONFIG/$relDir"; then
      (exit 0); return 0
    else
      msg="Directory \`$TFC_TEXMFCONFIG/$relDir' is not writable"
      (exit 1); return 1
    fi
  else
    # we assume that a "mkdir -p" would work if the top-level
    # $TFC_TEXMFCONFIG is writable
    if test -w "$TFC_TEXMFCONFIG"; then
      (exit 0); return 0
    else
      msg="Directory \`$TFC_TEXMFCONFIG' is not writable"
      (exit 1); return 1
    fi
  fi
}

get_info()
{
  case "$TFC_FILE" in
    "") abort "missing --file option or empty argument";;
  esac
  setupMap
  # prepare + get info
  IFS=$TAB
  # set -- x `(awk '$1 == file' file=$TFC_FILE <$TFC_MAP) 2>/dev/null`; shift
  set -- x `(grep "^$TFC_FILE[ 	]" $TFC_MAP) 2>/dev/null`; shift
  IFS=$STD_IFS

  # assign results
  file="$1"
  relDir="$2"
  kpseFormat="$3"
  kpseProgArg="$4"

  # sanity checks
  case $file in
    "") abort "no info for file \`$TFC_FILE' in map \`$TFC_MAP'";;
  esac

  case $relDir in
    -) abort "no known standard location for file \`$TFC_FILE' in map \`$TFC_MAP'";;
  esac

  case $kpseProgArg in
    -) kpseProgArg=kpsewhich;;
  esac
}

check_out()
{
  if $createEmptyFile; then
    :
  else
    case "$orig" in
      "")
          get_info
          orig=`(kpsewhich -format="$kpseFormat" -progname="$kpseProgArg" "$TFC_FILE") 2>/dev/null`
          ;;
      *)
          test -f "$orig" || orig=
    esac

    case $orig in
      "") abort "file \`$TFC_FILE' not found";;
    esac
  fi

  # writableCheck needs to get a valid relDir, so it calls get_info if necessary
  if writableCheck; then
   : ok
  else
    cat >&2 <<eof

There was a problem finding a proper place to store configuration data.
The message was:
  $msg

To overcome such problems, set up the environment variable TEXMFCONFIG to
point to a directory tree that is writable to you, e.g.
  TEXMFCONFIG=\$HOME/texmf; export TEXMFCONFIG
or
  setenv TEXMFCONFIG \$HOME/texmf
eof
    (exit 1); exit 1
  fi

  setupTemp
  : ${TFC_ID=tfc$$}
  for d in "$TFC_TEMP/$TFC_ID" "$TFC_TEMP/$TFC_ID/in" "$TFC_TEMP/$TFC_ID/out"; do
    (umask 077; mkdir "$d") || abort "could not create directory \`$d'"
  done

  if $createEmptyFile; then
    (umask 022
     > "$TFC_TEMP/$TFC_ID/in/$TFC_FILE"
     > "$TFC_TEMP/$TFC_ID/out/$TFC_FILE")
  else
    cp "$orig" "$TFC_TEMP/$TFC_ID/in/$TFC_FILE" \
      || abort "copy \`$orig' -> \`$TFC_TEMP/$TFC_ID/in/$TFC_FILE' failed"

    cp "$orig" "$TFC_TEMP/$TFC_ID/out/$TFC_FILE" \
      || abort "copy \`$orig' -> \`$TFC_TEMP/$TFC_ID/out/$TFC_FILE' failed"

    chmod u+w "$TFC_TEMP/$TFC_ID/out/$TFC_FILE"
  fi

  cat >"$TFC_TEMP/$TFC_ID/relDir" <<eof
$relDir
eof

  cat > "$TFC_TEMP/$TFC_ID/file" <<eof
$TFC_FILE
eof

  echo "$TFC_ID $TFC_TEMP/$TFC_ID/in/$TFC_FILE $TFC_TEMP/$TFC_ID/out/$TFC_FILE $orig"
}

check_in()
{
  setupTemp

  if test ! -d "$TFC_TEMP/$TFC_ID"; then
    abort "directory \`$TFC_TEMP/$TFC_ID' does not exist. Perhaps missing arg --id"
  fi

  case "$TFC_FILE" in
    "") TFC_FILE=`(cat "$TFC_TEMP/$TFC_ID/file") 2>/dev/null`;;
  esac

  for f in "$TFC_TEMP/$TFC_ID/relDir" "$TFC_TEMP/$TFC_ID/file" "$TFC_TEMP/$TFC_ID/in/$TFC_FILE" "$TFC_TEMP/$TFC_ID/out/$TFC_FILE"; do
    if test ! -f "$f"; then
      abort "Oops! file \`$f' does not exist"
    fi
  done

  if (cmp "$TFC_TEMP/$TFC_ID/in/$TFC_FILE" "$TFC_TEMP/$TFC_ID/out/$TFC_FILE") >/dev/null 2>&1; then
    (exit 0); return 0
  fi

  setupTexmfConfig
  relDir=`cat "$TFC_TEMP/$TFC_ID/relDir"`
  if test ! -d "$TFC_TEXMFCONFIG/$relDir"; then
    setupMktexDir
    mktexdir "$TFC_TEXMFCONFIG/$relDir" || abort "could not create directory \`$TFC_TEXMFCONFIG/$relDir'"
  fi

  if test -f "$TFC_TEXMFCONFIG/$relDir/$TFC_FILE"; then
    needsLSRUpdate=false
    test -w "$TFC_TEXMFCONFIG/$relDir/$TFC_FILE" || rm -f "$TFC_TEXMFCONFIG/$relDir/$TFC_FILE"
  else
    needsLSRUpdate=true
  fi

  if test -f "$TFC_TEXMFCONFIG/$relDir/$TFC_FILE"; then
    cat <"$TFC_TEMP/$TFC_ID/out/$TFC_FILE" >"$TFC_TEXMFCONFIG/$relDir/$TFC_FILE" ||
      abort "failed to write new file \`$TFC_TEXMFCONFIG/$relDir/$TFC_FILE'"
  else
    cp "$TFC_TEMP/$TFC_ID/out/$TFC_FILE" "$TFC_TEXMFCONFIG/$relDir/$TFC_FILE" ||
      abort "failed to copy \`$TFC_TEMP/$TFC_ID/out/$TFC_FILE' to \`$TFC_TEXMFCONFIG/$relDir/$TFC_FILE'"
    chmod 644 "$TFC_TEXMFCONFIG/$relDir/$TFC_FILE" 2>/dev/null
  fi
  if $needsLSRUpdate; then
    mktexupd "$TFC_TEXMFCONFIG/$relDir" "$TFC_FILE"
  fi
  echo "$TFC_TEXMFCONFIG/$relDir/$TFC_FILE"
  (exit 0); return 0
}

find_file()
{
  get_info
  kpsewhich -format="$kpseFormat" -progname="$kpseProgArg" "$TFC_FILE"
}

help()
{
  cat <<eof
Usage: $progname [option] ...

Valid options:
    --reldir       specify directory relative to texmf root
    --infile       specify input file
    --emptyinfile  create an empty input file
    --map          specify map file
    --texmfconfig  texmf tree for config files
    --mktexdir     full path of mktexdir
    --mktexupd     full path of mktexupd
    --tmp          temp directory
    --id           identity string (generated when running command co)
    --file         specify configuration entry from map file
    --cmd          command to execute
    --help         display this help

Valid commands (--cmd) are:
    co             check out
    ci             check in
    find           look up source for file
    mapfile        display path name of map file
eof
}

show_map()
{
  setupMap
  echo "$TFC_MAP"
}

TAB='	'
SPACE=' '
NL='
'
createEmptyFile=false

STD_IFS="$TAB$SPACE$NL"
while
  case $1 in
    --reldir) relDir=$2; shift;;
    --infile) orig=$2; shift;;
    --emptyinfile) createEmptyFile=true;;
    --map) TFC_MAP=$2; shift;;
    --texmfconfig)
           TFC_TEXMFCONFIG=$2; shift;;
    --mktexdir)
           TFC_MKTEXDIR=$2;;
    --mktexupd)
           TFC_MKTEXUPD=$2;;
    --tmp) TFC_TEMP=$2; shift;;
    --id)  TFC_ID=$2; shift;;
    --file)TFC_FILE=$2; shift;;
    --cmd) TFC_CMD=$2; shift;;
    --help) TFC_CMD=help;;
    "") break;;
    *) abort "unknown option \`$1'; try $progname --help"; (exit 1); exit 1;;
  esac
do test $# -gt 0 && shift; done

case $TFC_CMD in
  ci) check_in;;
  co) check_out;;
  find) find_file;;
  mapfile) show_map;;
  help) help;;
  "")
    abort "no command; try $progname --help"; (exit 1); exit 1;;
  *)
    abort "unknown command \`$TFC_CMD'; try $progname --help"; (exit 1); exit 1;;
esac