tentative fix for issue 3 (ex 53)
[mplib] / src / texk / kpathsea / mktexlsr
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/bin/mktexlsr
9 # unless the scripts live in a different directory than the 'kpseaccess',
10 # 'kpsestat', 'kpsereadlink', and 'kpsewhich' binaries.
11
12 # Originally written as `texhash' by Thomas Esser
13 # <te@dbs.uni-hannover.de>, Okt., 1994.
14 # Public domain.
15
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]...
19
20 Only options are --help, --version, and --verbose.  If standard input is
21 a terminal, --verbose is on by default.
22
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."
27
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.
32 DOSISH=no
33 case `uname -s` in
34   CYGWIN*|Cygwin*|cygwin*) ;;
35   *) if test -n "$COMSPEC" || test -n "$ComSpec"; then DOSISH=yes; fi
36 esac
37 if test "$DOSISH" = "no"; then SEP=':'; else SEP=';';fi
38
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
41 # mktex.opt.
42 dirname=`echo $0 | sed 's%/*[^/][^/]*$%%'`
43 case $dirname in
44   "") # Do nothing
45       ;;
46   /* | [A-z]:/*) # Absolute name
47       PATH="$dirname$SEP$PATH"
48       export PATH ;;
49    *)  # Relative name
50       PATH="`pwd`/$dirname$SEP$PATH"
51       export PATH ;;
52 esac
53
54 if tty -s; then verbose=true; else verbose=false; fi
55
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
59   echo "$usage"
60   exit 0
61 elif test "x$1" = x--version || test "x$1" = x-version; then
62   echo "`basename $0` $version"
63   kpsewhich --version
64   exit 0
65 elif test "x$1" = x--verbose || test "x$1" = x-verbose; then
66   verbose=true
67   shift
68 fi
69
70 # mktexupd and mktexlsr make sure they're coordinated via this.  A copy
71 # is found mktex.opt.
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.'
75
76 trap 'cd / ; test -z "$db_dir_tmp" || rm -rf "$db_dir_tmp"; exit' 0 1 2 3 7 13 15
77
78 # Get list of directories from $TEXMFDBS; eliminate duplicates.
79 test $# = 0 && {
80   OIFS=$IFS
81   IFS='
82 '
83   set x `kpsewhich --show-path=ls-R | tr : '
84 ' | sort | uniq`; shift
85   IFS=$OIFS
86 }
87
88 for TEXMFLS_R in "$@"; do
89   # Prepend cwd if the directory was relative.
90   case "$TEXMFLS_R" in
91   "") continue ;;  # Strictly speaking, it is an error if this case is taken.
92   /* | [A-z]:/*) ;;
93   *)  TEXMFLS_R="`pwd`/$TEXMFLS_R"
94   esac
95   # Allow for ls-R and ls-r to exist.  But create ls-R if we're working
96   # from scratch.
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"
101   else
102     db_file="$TEXMFLS_R/ls-R"
103   fi
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
108   "") ;;
109   /* | [A-z]:/*) db_file="$db_readlink" ;;
110   *)  db_file="$TEXMFLS_R/$db_readlink"
111   esac
112   db_dir=`echo "$db_file" | sed 's%/[^/][^/]*$%%'` # can't rely on dirname
113
114   test -d "$db_dir" || continue
115   test -w "$db_dir" || { echo "$progname: $db_dir: directory not writable. Skipping..." >&2; continue; }
116
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
125     continue
126   fi
127
128   # Skip if we cannot write the file:
129   kpseaccess -w "$db_file" || { echo "$progname: $db_file: no write permission. Skipping..." >&2; continue; }
130
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"
135   rm -f "$db_file_tmp"
136
137   $verbose && echo "$progname: Updating $db_file... " >&2
138   echo "$ls_R_magic" >"$db_file_tmp"
139
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.
147   # 
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' \
153    >>"$db_file_tmp"
154
155   # To be really safe, a loop.
156   until PERMS=`kpsestat = "$db_file"`; do sleep 1; done
157   chmod $PERMS "$db_file_tmp"
158   rm -f "$db_file"
159   mv "$db_file_tmp" "$db_file"
160   rm -rf "$db_dir_tmp"
161 done
162 $verbose && echo "$progname: Done." >&2
163 exit 0