2 # original mktexlsr -- create or rebuild ls-R.
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
10 # Originally written as `texhash' by Thomas Esser
11 # <te@dbs.uni-hannover.de>, Okt., 1994.
14 version='$Id: mktexlsr 9164 2008-07-02 17:42:11Z karl $'
15 progname=`echo $0 | sed 's%.*/%%'`
16 usage="Usage: $progname [DIR]...
18 Rebuild ls-R filename databases. If one or more arguments DIRS are
19 given, these are used as the directories in which to build ls-R. Else
20 all directories in the search path for ls-R files (\$TEXMFDBS) are used.
23 --dry-run do not actually update anything
24 --help display this help and exit
25 --quiet cancel --verbose
26 --silent same as --quiet
27 --verbose explain what is being done
28 --version output version information and exit
30 If standard input is a terminal, --verbose is on by default.
32 Report bugs to tex-k@tug.org.
35 # MS-DOS and MS-Windows define $COMSPEC or $ComSpec and use `;' to separate
36 # directories in path lists whereas Unix uses `:'. Make an exception for
37 # Cygwin, which pretends to be UNIX.
38 # Create a variable that holds the right character to be used by the scripts.
41 CYGWIN*|Cygwin*|cygwin*) ;;
42 *) if test -n "$COMSPEC" || test -n "$ComSpec"; then DOSISH=yes; fi
44 if test "$DOSISH" = "no"; then SEP=':'; else SEP=';';fi
46 # Add the location of the script to the PATH if necessary. This must
47 # be done before kpsewhich can be called, and thus cannot be put into
49 dirname=`echo $0 | sed 's%/*[^/][^/]*$%%'`
53 /* | [A-z]:/*) # Absolute name
54 PATH="$dirname$SEP$PATH"
57 PATH="`pwd`/$dirname$SEP$PATH"
61 if tty -s; then verbose=true; else verbose=false; fi
65 # A copy of some stuff from mktex.opt, so we can run in the presence of
66 # terminally damaged ls-R files.
67 while test $# -gt 0; do
68 if test "x$1" = x--help || test "x$1" = x-help; then
71 elif test "x$1" = x--version || test "x$1" = x-version; then
72 echo "`basename $0` $version"
75 elif test "x$1" = x--verbose || test "x$1" = x-verbose; then
77 elif test "x$1" = x--dry-run || test "x$1" = x-n; then
79 elif test "x$1" = x--quiet || test "x$1" = x--silent \
80 || test "x$1" = x-quiet || test "x$1" = x-silent ; then
82 elif test "x$1" = x--; then
84 elif echo "x$1" | grep '^x-' >/dev/null; then
85 echo "$progname: unknown option $1, try --help if you need it." >&2
88 test -d "$1" || echo "$progname: $1 not a directory." >&2
89 trees="$trees $1" # don't want to update system dirs
94 # mktexupd and mktexlsr make sure they're coordinated via this. A copy
96 ls_R_magic='% ls-R -- filename database for kpathsea; do not change this line.'
97 # The old string, which should continue to work.
98 old_ls_R_magic='% ls-R -- maintained by MakeTeXls-R; do not change this line.'
100 trap 'cd /; test -z "$db_dir_tmp" || rm -rf "$db_dir_tmp"; exit' 0 1 2 3 7 13 15
102 # Get list of directories from $TEXMFDBS; eliminate duplicates.
104 if test -n "$trees"; then
107 OIFS=$IFS # want just a newline
110 set x `kpsewhich --show-path=ls-R | tr : '
117 for TEXMFLS_R in "$@"; do
118 # Prepend cwd if the directory was relative.
120 "") continue ;; # Strictly speaking, it is an error if this case is taken.
122 *) TEXMFLS_R="`pwd`/$TEXMFLS_R"
124 # Allow for either ls-R and ls-r to exist. But create ls-R if we're
125 # working from scratch.
126 if test -f "$TEXMFLS_R/ls-R"; then
127 db_file="$TEXMFLS_R/ls-R"
128 elif test -f "$TEXMFLS_R/ls-r"; then
129 db_file="$TEXMFLS_R/ls-r"
131 db_file="$TEXMFLS_R/ls-R"
133 # Follow a possible symlink to get the right filesystem.
134 # The '|| true' construct prevents an sh -e aborting.
135 db_readlink=`kpsereadlink "$TEXMFLS_R/ls-R" 2>/dev/null` || true
136 case "$db_readlink" in
138 /* | [A-z]:/*) db_file="$db_readlink" ;;
139 *) db_file="$TEXMFLS_R/$db_readlink"
141 db_dir=`echo "$db_file" | sed 's%/[^/][^/]*$%%'` # can't rely on dirname
143 # want to be silent if the directory doesn't exist, since the ls-R
144 # path ordinarily contains many nonexistent directories.
145 test -d "$db_dir" || continue
146 test -w "$db_dir" || { echo "$progname: $db_dir: directory not writable. Skipping..." >&2; continue; }
148 if test ! -f "$db_file"; then
149 cp /dev/null "$db_file"
150 # Use same permissions as parent directory, minus x,s, or t bits.
151 chmod `kpsestat -xst "$db_dir"` "$db_file"
152 elif test -s "$db_file" \
153 && test "x`sed '1s/
\r$//;1q' \"$db_file\"`" != "x$ls_R_magic" \
154 && test "x`sed '1s/
\r$//;1q' \"$db_file\"`" != "x$old_ls_R_magic"; then
155 echo "$progname: $db_file lacks magic string. Skipping..." >&2
159 # Skip if we cannot write the file:
160 kpseaccess -w "$db_file" || { echo "$progname: $db_file: no write permission. Skipping..." >&2; continue; }
162 db_dir_tmp="$db_dir/lsR$$.tmp"
163 (umask 077 && mkdir "$db_dir_tmp" ) \
164 || { echo "$progname: could not create directory '$db_dir_tmp'. Skipping..." >&2; continue; }
165 db_file_tmp="$db_dir_tmp/lsR$$.tmp"
168 $verbose && echo "$progname: Updating $db_file... " >&2
171 echo "$ls_R_magic" >"$db_file_tmp"
173 # The main task. We put ./: in the output, so top-level files can be
174 # found via ls-R. Probably irrelevant in practice. The sed command
175 # inserts the leading ./ for directory names, and removes ., .., and
176 # version control entries from the list. Also omit contents of any
177 # the version directories; sed apparently requires that we do that
178 # operation in a separate invocation. We do not try to support colons
179 # in directory names.
181 echo "./:" >>"$db_file_tmp"
182 vc_dirs='\.\(bzr\|git\|hg\|svn\)\|CVS\|RCS\|_darcs'
183 (cd "$TEXMFLS_R" && \ls -LRa 2>/dev/null) \
184 | sed -e '/^$/{n;s%^\./%%;s%^%./%;}; /^\.$/d; /^\.\.$/d; /^'$vc_dirs'$/d;' \
185 -e '/^[\.\/]*lsR[0-9]*\.tmp:*$/d' \
186 | sed -e /$vc_dirs'.*:$/,/^$/d' \
189 # To be really safe, a loop.
190 until PERMS=`kpsestat = "$db_file"`; do sleep 1; done
191 chmod $PERMS "$db_file_tmp"
193 mv "$db_file_tmp" "$db_file"
196 $verbose && echo "$progname: Done." >&2