| 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.) |
| 6 | # |
| 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. |
| 12 | |
| 13 | version='$Id: mktexlsr 56842 2020-11-02 23:00:54Z karl $' |
| 14 | progname=`echo $0 | sed 's%.*/%%'` |
| 15 | |
| 16 | usage="Usage: $progname [OPTION]... [DIR]... |
| 17 | |
| 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. |
| 22 | |
| 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 |
| 30 | |
| 31 | If standard input is a terminal, --verbose is on by default. |
| 32 | |
| 33 | For more information, see the \`Filename database' section of |
| 34 | Kpathsea manual available at http://tug.org/kpathsea. |
| 35 | |
| 36 | Report bugs to: [email protected] |
| 37 | TeX Live home page: <http://tug.org/texlive/> |
| 38 | " |
| 39 | |
| 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 ... |
| 45 | |
| 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 |
| 56 | |
| 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 |
| 63 | ;; |
| 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 |
| 71 | |
| 72 | if tty -s >/dev/null 2>&1; then verbose=true; else verbose=false; fi |
| 73 | dry_run=false |
| 74 | trees= |
| 75 | |
| 76 | # initialize treefile by either mktemp or some random name |
| 77 | # code taken from pdfjam and adjusted |
| 78 | { |
| 79 | treefile=` |
| 80 | (umask 077 && mktemp "${TMPDIR-/tmp}/mktexlsrtrees.XXXXXXXXXX") 2>/dev/null |
| 81 | ` && test -n "$treefile" && test -f "$treefile" |
| 82 | } || { |
| 83 | ## We'll use awk to make random number, for portability |
| 84 | random=`awk 'END { srand(); printf ("%d\n", rand()*1000000); }' /dev/null` |
| 85 | treefile="${TMPDIR-/tmp}/mktexlsrtrees$$.$random" |
| 86 | } || exit $? |
| 87 | |
| 88 | trap 'cd /; rm -f $treefile; test -z "$db_dir_tmp" || rm -rf "$db_dir_tmp"; |
| 89 | exit' 0 1 2 3 7 13 15 |
| 90 | |
| 91 | # A copy of some stuff from mktex.opt, so we can run in the presence of |
| 92 | # terminally damaged ls-R files. |
| 93 | while test $# -gt 0; do |
| 94 | if test "x$1" = x--help || test "x$1" = x-help; then |
| 95 | echo "$usage" |
| 96 | exit 0 |
| 97 | elif test "x$1" = x--version || test "x$1" = x-version; then |
| 98 | echo "`basename $0` $version" |
| 99 | kpsewhich --version |
| 100 | exit 0 |
| 101 | elif test "x$1" = x--verbose || test "x$1" = x-verbose; then |
| 102 | verbose=true |
| 103 | elif test "x$1" = x--dry-run || test "x$1" = x-n; then |
| 104 | dry_run=true |
| 105 | elif test "x$1" = x--quiet || test "x$1" = x--silent \ |
| 106 | || test "x$1" = x-quiet || test "x$1" = x-silent ; then |
| 107 | verbose=false |
| 108 | elif test "x$1" = x--; then |
| 109 | : |
| 110 | elif echo "x$1" | grep '^x-' >/dev/null; then |
| 111 | echo "$progname: unknown option \`$1', try --help if you need it." >&2 |
| 112 | exit 1 |
| 113 | else |
| 114 | if test ! -d "$1"; then |
| 115 | echo "$progname: $1: not a directory, skipping." >&2 |
| 116 | shift |
| 117 | continue |
| 118 | fi |
| 119 | # By saving the argument in a file, we can later get it back while |
| 120 | # supporting spaces in the name. This still doesn't support |
| 121 | # newlines in the directory names, but nobody ever complains about |
| 122 | # that, and it seems much too much trouble to use \0 terminators. |
| 123 | (umask 077 |
| 124 | if echo "$1" >>"$treefile"; then :; else |
| 125 | echo "$progname: $treefile: could not append to arg file, goodbye." >&2 |
| 126 | exit 1 |
| 127 | fi |
| 128 | ) |
| 129 | fi |
| 130 | shift |
| 131 | done |
| 132 | |
| 133 | # mktexupd and mktexlsr make sure they're coordinated via this. A copy |
| 134 | # is found mktex.opt. |
| 135 | ls_R_magic='% ls-R -- filename database for kpathsea; do not change this line.' |
| 136 | # The old string, which should continue to work. |
| 137 | old_ls_R_magic='% ls-R -- maintained by MakeTeXls-R; do not change this line.' |
| 138 | |
| 139 | { |
| 140 | # Get list of directories from the explicit arguments (now saved in |
| 141 | # $treefile), or $TEXMFDBS if no explicit args. Eliminate duplicates. |
| 142 | saveIFS=$IFS # break only at newline |
| 143 | IFS=' |
| 144 | ' |
| 145 | if test -s "$treefile"; then |
| 146 | set x `sort "$treefile" | uniq` |
| 147 | else |
| 148 | set x `kpsewhich --show-path=ls-R | tr : ' |
| 149 | ' | sort | uniq` |
| 150 | fi |
| 151 | IFS=$saveIFS |
| 152 | shift |
| 153 | } |
| 154 | |
| 155 | for TEXMFLS_R in "$@"; do |
| 156 | # Prepend cwd if the directory was relative. |
| 157 | case "$TEXMFLS_R" in |
| 158 | "") continue ;; # Strictly speaking, it is an error if this case is taken. |
| 159 | /* | [A-z]:/*) ;; |
| 160 | *) TEXMFLS_R="`pwd`/$TEXMFLS_R" |
| 161 | esac |
| 162 | # Allow for either ls-R and ls-r to exist. But create ls-R if we're |
| 163 | # working from scratch. |
| 164 | if test -f "$TEXMFLS_R/ls-R"; then |
| 165 | db_file="$TEXMFLS_R/ls-R" |
| 166 | elif test -f "$TEXMFLS_R/ls-r"; then |
| 167 | db_file="$TEXMFLS_R/ls-r" |
| 168 | else |
| 169 | db_file="$TEXMFLS_R/ls-R" |
| 170 | fi |
| 171 | # Follow a possible symlink to get the right filesystem. |
| 172 | # The '|| true' construct prevents an sh -e aborting. |
| 173 | db_readlink=`kpsereadlink "$TEXMFLS_R/ls-R" 2>/dev/null` || true |
| 174 | case "$db_readlink" in |
| 175 | "") ;; |
| 176 | /* | [A-z]:/*) db_file="$db_readlink" ;; |
| 177 | *) db_file="$TEXMFLS_R/$db_readlink" |
| 178 | esac |
| 179 | db_dir=`echo "$db_file" | sed 's%/[^/][^/]*$%%'` # can't rely on dirname |
| 180 | |
| 181 | # want to be silent if the directory doesn't exist, since the ls-R |
| 182 | # path ordinarily contains many nonexistent directories. |
| 183 | test -d "$db_dir" || continue |
| 184 | test -w "$db_dir" || { echo "$progname: $db_dir: directory not writable. Skipping..." >&2; continue; } |
| 185 | |
| 186 | if test ! -f "$db_file"; then |
| 187 | cp /dev/null "$db_file" |
| 188 | # Use same permissions as parent directory, minus x,s, or t bits. |
| 189 | chmod `kpsestat -xst "$db_dir"` "$db_file" |
| 190 | elif test -s "$db_file" \ |
| 191 | && test "x`sed '1s/ $//;1q' \"$db_file\"`" != "x$ls_R_magic" \ |
| 192 | && test "x`sed '1s/ $//;1q' \"$db_file\"`" != "x$old_ls_R_magic"; then |
| 193 | echo "$progname: $db_file: no magic string, skipping..." >&2 |
| 194 | continue |
| 195 | fi |
| 196 | |
| 197 | # Skip if we cannot write the file: |
| 198 | kpseaccess -w "$db_file" || { echo "$progname: $db_file: no write permission, skipping..." >&2; continue; } |
| 199 | |
| 200 | db_dir_tmp="$db_dir/lsR$$.tmp" |
| 201 | (umask 077 && mkdir "$db_dir_tmp" ) \ |
| 202 | || { echo "$progname: $db_dir_tmp: could not create directory, skipping..." >&2; continue; } |
| 203 | db_file_tmp="$db_dir_tmp/lsR$$.tmp" |
| 204 | rm -f "$db_file_tmp" |
| 205 | |
| 206 | $verbose && echo "$progname: Updating $db_file... " |
| 207 | $dry_run && continue |
| 208 | |
| 209 | echo "$ls_R_magic" >"$db_file_tmp" |
| 210 | |
| 211 | # The main task. We put ./: in the output, so top-level files can be |
| 212 | # found via ls-R. Probably irrelevant in practice. The sed command |
| 213 | # inserts the leading ./ for directory names, and removes ., .., and |
| 214 | # version control entries from the list. Also omit contents of any |
| 215 | # the version directories; sed apparently requires that we do that |
| 216 | # operation in a separate invocation. We do not try to support colons |
| 217 | # in directory names. |
| 218 | # |
| 219 | echo "./:" >>"$db_file_tmp" |
| 220 | vc_dirs='\.\(bzr\|git\|hg\|svn\)\|_darcs' |
| 221 | (cd "$TEXMFLS_R" && \ls -LRa 2>/dev/null) \ |
| 222 | | sed -e '/^$/{n;s%^\./%%;s%^%./%;}; /^\.$/d; /^\.\.$/d; /^'$vc_dirs'$/d;' \ |
| 223 | -e '/^[\.\/]*lsR[0-9]*\.tmp:*$/d' \ |
| 224 | | sed -e /$vc_dirs'.*:$/,/^$/d' \ |
| 225 | >>"$db_file_tmp" |
| 226 | |
| 227 | # To be really safe, a loop. |
| 228 | until PERMS=`kpsestat = "$db_file"`; do sleep 1; done |
| 229 | chmod $PERMS "$db_file_tmp" |
| 230 | rm -f "$db_file" |
| 231 | mv "$db_file_tmp" "$db_file" |
| 232 | rm -rf "$db_dir_tmp" |
| 233 | done |
| 234 | |
| 235 | $verbose && echo "$progname: Done." |
| 236 | exit 0 |