beta-0.89.2
[luatex.git] / source / texk / kpathsea / mktexlsr
blob3161a5317ec1065387ce415b7ba618c1165c7861
1 #!/bin/sh
2 # original mktexlsr -- create or rebuild ls-R.
3 #
4 # (If you change or delete the word `original' on the previous line,
5 # installation won't write this script over yours.)
7 # Suitable for calling from cron, as in:
8 # 0 * * * * cd /your/texmf/root && /usr/local/texlive/bin/mktexlsr
9 #
10 # Originally written as `texhash' by Thomas Esser, Okt., 1994.
11 # Public domain.
13 version='$Id: mktexlsr 36855 2015-04-14 12:15:08Z preining $'
14 progname=`echo $0 | sed 's%.*/%%'`
16 usage="Usage: $progname [OPTION]... [DIR]...
18 Rebuild ls-R filename databases used by TeX. If one or more arguments
19 DIRS are given, these are used as the directories in which to build
20 ls-R. Else all directories in the search path for ls-R files
21 (\$TEXMFDBS) are used.
23 Options:
24 --dry-run do not actually update anything
25 --help display this help and exit
26 --quiet cancel --verbose
27 --silent same as --quiet
28 --verbose explain what is being done
29 --version output version information and exit
31 If standard input is a terminal, --verbose is on by default.
33 For more information, see the \`Filename database' section of
34 Kpathsea manual available at http://tug.org/kpathsea.
36 Report bugs to: tex-k@tug.org
37 TeX Live home page: <http://tug.org/texlive/>
40 # The simple PATH-prepending approach we take in other scripts doesn't
41 # handle DOS/Windows, but we don't use the shell scripts on Windows
42 # anyway, and does anyone want current TL on DOS or DJGPP? Maybe they
43 # can maintain their own changes if so. Leaving this code though, since
44 # it's always been here ...
46 # MS-DOS and MS-Windows define $COMSPEC or $ComSpec and use `;' to separate
47 # directories in path lists whereas Unix uses `:'. Make an exception for
48 # Cygwin, which pretends to be UNIX.
49 # Create a variable that holds the right character to be used by the scripts.
50 DOSISH=no
51 case `uname -s` in
52 CYGWIN*|Cygwin*|cygwin*) ;;
53 *) if test -n "$COMSPEC" || test -n "$ComSpec"; then DOSISH=yes; fi
54 esac
55 if test "$DOSISH" = "no"; then SEP=':'; else SEP=';';fi
57 # Add the location of the script to the PATH if necessary. This must
58 # be done before kpsewhich can be called, and thus cannot be put into
59 # mktex.opt.
60 dirname=`echo $0 | sed 's%/*[^/][^/]*$%%'`
61 case $dirname in
62 "") # Do nothing
64 /* | [A-z]:/*) # Absolute name
65 PATH="$dirname$SEP$PATH"
66 export PATH ;;
67 *) # Relative name
68 PATH="`pwd`/$dirname$SEP$PATH"
69 export PATH ;;
70 esac
72 if tty -s; then verbose=true; else verbose=false; fi
73 dry_run=false
74 trees=
76 treefile="${TMPDIR-/tmp}/mktexlsrtrees$$.tmp"
77 trap 'cd /; rm -f $treefile; test -z "$db_dir_tmp" || rm -rf "$db_dir_tmp";
78 exit' 0 1 2 3 7 13 15
80 # A copy of some stuff from mktex.opt, so we can run in the presence of
81 # terminally damaged ls-R files.
82 while test $# -gt 0; do
83 if test "x$1" = x--help || test "x$1" = x-help; then
84 echo "$usage"
85 exit 0
86 elif test "x$1" = x--version || test "x$1" = x-version; then
87 echo "`basename $0` $version"
88 kpsewhich --version
89 exit 0
90 elif test "x$1" = x--verbose || test "x$1" = x-verbose; then
91 verbose=true
92 elif test "x$1" = x--dry-run || test "x$1" = x-n; then
93 dry_run=true
94 elif test "x$1" = x--quiet || test "x$1" = x--silent \
95 || test "x$1" = x-quiet || test "x$1" = x-silent ; then
96 verbose=false
97 elif test "x$1" = x--; then
99 elif echo "x$1" | grep '^x-' >/dev/null; then
100 echo "$progname: unknown option \`$1', try --help if you need it." >&2
101 exit 1
102 else
103 if test ! -d "$1"; then
104 echo "$progname: $1: not a directory, skipping." >&2
105 shift
106 continue
108 # By saving the argument in a file, we can later get it back while
109 # supporting spaces in the name. This still doesn't support
110 # newlines in the directory names, but nobody ever complains about
111 # that, and it seems much too much trouble to use \0 terminators.
112 (umask 077
113 if echo "$1" >>"$treefile"; then :; else
114 echo "$progname: $treefile: could not append to arg file, goodbye." >&2
115 exit 1
119 shift
120 done
122 # mktexupd and mktexlsr make sure they're coordinated via this. A copy
123 # is found mktex.opt.
124 ls_R_magic='% ls-R -- filename database for kpathsea; do not change this line.'
125 # The old string, which should continue to work.
126 old_ls_R_magic='% ls-R -- maintained by MakeTeXls-R; do not change this line.'
129 # Get list of directories from the explicit arguments (now saved in
130 # $treefile), or $TEXMFDBS if no explicit args. Eliminate duplicates.
131 saveIFS=$IFS # break only at newline
132 IFS='
134 if test -s "$treefile"; then
135 set x `sort "$treefile" | uniq`
136 else
137 set x `kpsewhich --show-path=ls-R | tr : '
138 ' | sort | uniq`
140 IFS=$saveIFS
141 shift
144 for TEXMFLS_R in "$@"; do
145 # Prepend cwd if the directory was relative.
146 case "$TEXMFLS_R" in
147 "") continue ;; # Strictly speaking, it is an error if this case is taken.
148 /* | [A-z]:/*) ;;
149 *) TEXMFLS_R="`pwd`/$TEXMFLS_R"
150 esac
151 # Allow for either ls-R and ls-r to exist. But create ls-R if we're
152 # working from scratch.
153 if test -f "$TEXMFLS_R/ls-R"; then
154 db_file="$TEXMFLS_R/ls-R"
155 elif test -f "$TEXMFLS_R/ls-r"; then
156 db_file="$TEXMFLS_R/ls-r"
157 else
158 db_file="$TEXMFLS_R/ls-R"
160 # Follow a possible symlink to get the right filesystem.
161 # The '|| true' construct prevents an sh -e aborting.
162 db_readlink=`kpsereadlink "$TEXMFLS_R/ls-R" 2>/dev/null` || true
163 case "$db_readlink" in
164 "") ;;
165 /* | [A-z]:/*) db_file="$db_readlink" ;;
166 *) db_file="$TEXMFLS_R/$db_readlink"
167 esac
168 db_dir=`echo "$db_file" | sed 's%/[^/][^/]*$%%'` # can't rely on dirname
170 # want to be silent if the directory doesn't exist, since the ls-R
171 # path ordinarily contains many nonexistent directories.
172 test -d "$db_dir" || continue
173 test -w "$db_dir" || { echo "$progname: $db_dir: directory not writable. Skipping..." >&2; continue; }
175 if test ! -f "$db_file"; then
176 cp /dev/null "$db_file"
177 # Use same permissions as parent directory, minus x,s, or t bits.
178 chmod `kpsestat -xst "$db_dir"` "$db_file"
179 elif test -s "$db_file" \
180 && test "x`sed '1s/ $//;1q' \"$db_file\"`" != "x$ls_R_magic" \
181 && test "x`sed '1s/ $//;1q' \"$db_file\"`" != "x$old_ls_R_magic"; then
182 echo "$progname: $db_file: no magic string, skipping..." >&2
183 continue
186 # Skip if we cannot write the file:
187 kpseaccess -w "$db_file" || { echo "$progname: $db_file: no write permission, skipping..." >&2; continue; }
189 db_dir_tmp="$db_dir/lsR$$.tmp"
190 (umask 077 && mkdir "$db_dir_tmp" ) \
191 || { echo "$progname: $db_dir_tmp: could not create directory, skipping..." >&2; continue; }
192 db_file_tmp="$db_dir_tmp/lsR$$.tmp"
193 rm -f "$db_file_tmp"
195 $verbose && echo "$progname: Updating $db_file... "
196 $dry_run && continue
198 echo "$ls_R_magic" >"$db_file_tmp"
200 # The main task. We put ./: in the output, so top-level files can be
201 # found via ls-R. Probably irrelevant in practice. The sed command
202 # inserts the leading ./ for directory names, and removes ., .., and
203 # version control entries from the list. Also omit contents of any
204 # the version directories; sed apparently requires that we do that
205 # operation in a separate invocation. We do not try to support colons
206 # in directory names.
208 echo "./:" >>"$db_file_tmp"
209 vc_dirs='\.\(bzr\|git\|hg\|svn\)\|_darcs'
210 (cd "$TEXMFLS_R" && \ls -LRa 2>/dev/null) \
211 | sed -e '/^$/{n;s%^\./%%;s%^%./%;}; /^\.$/d; /^\.\.$/d; /^'$vc_dirs'$/d;' \
212 -e '/^[\.\/]*lsR[0-9]*\.tmp:*$/d' \
213 | sed -e /$vc_dirs'.*:$/,/^$/d' \
214 >>"$db_file_tmp"
216 # To be really safe, a loop.
217 until PERMS=`kpsestat = "$db_file"`; do sleep 1; done
218 chmod $PERMS "$db_file_tmp"
219 rm -f "$db_file"
220 mv "$db_file_tmp" "$db_file"
221 rm -rf "$db_dir_tmp"
222 done
224 $verbose && echo "$progname: Done."
225 exit 0