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/bin/mktexlsr
9 # unless the scripts live in a different directory than the 'kpseaccess',
10 # 'kpsestat', 'kpsereadlink', and 'kpsewhich' binaries.
12 # Originally written as `texhash' by Thomas Esser
13 # <te@dbs.uni-hannover.de>, Okt., 1994.
16 version='$Id: mktexlsr,v 1.46 2005/06/21 14:32:26 olaf Exp $'
17 progname=`echo $0 | sed 's%.*/%%'`
18 usage="Usage: $progname [DIR]...
20 Only options are --help, --version, and --verbose. If standard input is
21 a terminal, --verbose is on by default.
23 Rebuild all necessary ls-R filename databases completely. If one or
24 more arguments DIRS are given, these are used as texmf directories to
25 build ls-R for. Else all directories in the search path for ls-R files
26 (\$TEXMFDBS) are used."
28 # MS-DOS and MS-Windows define $COMSPEC or $ComSpec and use `;' to separate
29 # directories in path lists whereas Unix uses `:'. Make an exception for
30 # Cygwin, which pretends to be UNIX.
31 # Create a variable that holds the right character to be used by the scripts.
34 CYGWIN*|Cygwin*|cygwin*) ;;
35 *) if test -n "$COMSPEC" || test -n "$ComSpec"; then DOSISH=yes; fi
37 if test "$DOSISH" = "no"; then SEP=':'; else SEP=';';fi
39 # Add the location of the script to the PATH if necessary. This must
40 # be done before kpsewhich can be called, and thus cannot be put into
42 dirname=`echo $0 | sed 's%/*[^/][^/]*$%%'`
46 /* | [A-z]:/*) # Absolute name
47 PATH="$dirname$SEP$PATH"
50 PATH="`pwd`/$dirname$SEP$PATH"
54 if tty -s; then verbose=true; else verbose=false; fi
56 # A copy of some stuff from mktex.opt, so we can run in the presence of
57 # terminally damaged ls-R files.
58 if test "x$1" = x--help || test "x$1" = x-help; then
61 elif test "x$1" = x--version || test "x$1" = x-version; then
62 echo "`basename $0` $version"
65 elif test "x$1" = x--verbose || test "x$1" = x-verbose; then
70 # mktexupd and mktexlsr make sure they're coordinated via this. A copy
72 ls_R_magic='% ls-R -- filename database for kpathsea; do not change this line.'
73 # The old string, which should continue to work.
74 old_ls_R_magic='% ls-R -- maintained by MakeTeXls-R; do not change this line.'
76 trap 'cd / ; test -z "$db_dir_tmp" || rm -rf "$db_dir_tmp"; exit' 0 1 2 3 7 13 15
78 # Get list of directories from $TEXMFDBS; eliminate duplicates.
83 set x `kpsewhich --show-path=ls-R | tr : '
84 ' | sort | uniq`; shift
88 for TEXMFLS_R in "$@"; do
89 # Prepend cwd if the directory was relative.
91 "") continue ;; # Strictly speaking, it is an error if this case is taken.
93 *) TEXMFLS_R="`pwd`/$TEXMFLS_R"
95 # Allow for ls-R and ls-r to exist. But create ls-R if we're working
97 if test -f "$TEXMFLS_R/ls-R"; then
98 db_file="$TEXMFLS_R/ls-R"
99 elif test -f "$TEXMFLS_R/ls-r"; then
100 db_file="$TEXMFLS_R/ls-r"
102 db_file="$TEXMFLS_R/ls-R"
104 # Follow a possible symlink to get the right filesystem.
105 # The '|| true' construct prevents an sh -e aborting.
106 db_readlink=`kpsereadlink "$TEXMFLS_R/ls-R" 2>/dev/null` || true
107 case "$db_readlink" in
109 /* | [A-z]:/*) db_file="$db_readlink" ;;
110 *) db_file="$TEXMFLS_R/$db_readlink"
112 db_dir=`echo "$db_file" | sed 's%/[^/][^/]*$%%'` # can't rely on dirname
114 test -d "$db_dir" || continue
115 test -w "$db_dir" || { echo "$progname: $db_dir: directory not writable. Skipping..." >&2; continue; }
117 if test ! -f "$db_file"; then
118 cp /dev/null "$db_file"
119 # Use same permissions as parent directory, minus x,s, or t bits.
120 chmod `kpsestat -xst "$db_dir"` "$db_file"
121 elif test -s "$db_file" \
122 && test "x`sed '1s/
\r$//;1q' \"$db_file\"`" != "x$ls_R_magic" \
123 && test "x`sed '1s/
\r$//;1q' \"$db_file\"`" != "x$old_ls_R_magic"; then
124 echo "$progname: $db_file lacks magic string. Skipping..." >&2
128 # Skip if we cannot write the file:
129 kpseaccess -w "$db_file" || { echo "$progname: $db_file: no write permission. Skipping..." >&2; continue; }
131 db_dir_tmp="$db_dir/lsR$$.tmp"
132 (umask 077 && mkdir "$db_dir_tmp" ) \
133 || { echo "$progname: could not create directory '$db_dir_tmp'. Skipping..." >&2; continue; }
134 db_file_tmp="$db_dir_tmp/lsR$$.tmp"
137 $verbose && echo "$progname: Updating $db_file... " >&2
138 echo "$ls_R_magic" >"$db_file_tmp"
140 # The main task. We put ./: in the output, so top-level files can be
141 # found via ls-R. Probably irrelevant in practice.
142 # The sed command inserts the leading ./ for directory names, and
143 # removes ., .., and .svn entries from the list. Also omit contents
144 # of any .svn directories; sed apparently requires that we do that
145 # operation in a separate invocation.
146 # We do not try to support colons in directory names.
148 echo "./:" >>"$db_file_tmp"
149 (cd "$TEXMFLS_R" && \ls -LRa 2>/dev/null) \
150 | sed -e '/^$/{n;s%^\./%%;s%^%./%;}; /^\.$/d; /^\.\.$/d; /^\.svn$/d;' \
151 -e '/^[\.\/]*lsR[0-9]*\.tmp:*$/d' \
152 | sed -e '/\.svn.*:$/,/^$/d' \
155 # To be really safe, a loop.
156 until PERMS=`kpsestat = "$db_file"`; do sleep 1; done
157 chmod $PERMS "$db_file_tmp"
159 mv "$db_file_tmp" "$db_file"
162 $verbose && echo "$progname: Done." >&2