dvitomp fix from Akira
[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/texlive/bin/mktexlsr
9
10 # Originally written as `texhash' by Thomas Esser
11 # <te@dbs.uni-hannover.de>, Okt., 1994.
12 # Public domain.
13
14 version='$Id: mktexlsr 9164 2008-07-02 17:42:11Z karl $'
15 progname=`echo $0 | sed 's%.*/%%'`
16 usage="Usage: $progname [DIR]...
17
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.
21
22 Options:
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
29   
30 If standard input is a terminal, --verbose is on by default.
31
32 Report bugs to tex-k@tug.org.
33 "
34
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.
39 DOSISH=no
40 case `uname -s` in
41   CYGWIN*|Cygwin*|cygwin*) ;;
42   *) if test -n "$COMSPEC" || test -n "$ComSpec"; then DOSISH=yes; fi
43 esac
44 if test "$DOSISH" = "no"; then SEP=':'; else SEP=';';fi
45
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
48 # mktex.opt.
49 dirname=`echo $0 | sed 's%/*[^/][^/]*$%%'`
50 case $dirname in
51   "") # Do nothing
52       ;;
53   /* | [A-z]:/*) # Absolute name
54       PATH="$dirname$SEP$PATH"
55       export PATH ;;
56    *)  # Relative name
57       PATH="`pwd`/$dirname$SEP$PATH"
58       export PATH ;;
59 esac
60
61 if tty -s; then verbose=true; else verbose=false; fi
62 dry_run=false
63 trees=
64
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
69     echo "$usage"
70     exit 0
71   elif test "x$1" = x--version || test "x$1" = x-version; then
72     echo "`basename $0` $version"
73     kpsewhich --version
74     exit 0
75   elif test "x$1" = x--verbose || test "x$1" = x-verbose; then
76     verbose=true
77   elif test "x$1" = x--dry-run || test "x$1" = x-n; then
78     dry_run=true
79   elif test "x$1" = x--quiet || test "x$1" = x--silent \
80        || test "x$1" = x-quiet || test "x$1" = x-silent ; then
81     verbose=false
82   elif test "x$1" = x--; then
83     :
84   elif echo "x$1" | grep '^x-' >/dev/null; then
85     echo "$progname: unknown option $1, try --help if you need it." >&2
86     exit 1
87   else
88     test -d "$1" || echo "$progname: $1 not a directory." >&2
89     trees="$trees $1"  # don't want to update system dirs
90   fi
91   shift
92 done
93
94 # mktexupd and mktexlsr make sure they're coordinated via this.  A copy
95 # is found mktex.opt.
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.'
99
100 trap 'cd /; test -z "$db_dir_tmp" || rm -rf "$db_dir_tmp"; exit' 0 1 2 3 7 13 15
101
102 # Get list of directories from $TEXMFDBS; eliminate duplicates.
103 {
104   if test -n "$trees"; then
105     set x $trees
106   else
107     OIFS=$IFS  # want just a newline
108     IFS='
109 '
110     set x `kpsewhich --show-path=ls-R | tr : '
111 ' | sort | uniq`
112     IFS=$OIFS
113   fi
114   shift
115 }
116
117 for TEXMFLS_R in "$@"; do
118   # Prepend cwd if the directory was relative.
119   case "$TEXMFLS_R" in
120   "") continue ;;  # Strictly speaking, it is an error if this case is taken.
121   /* | [A-z]:/*) ;;
122   *)  TEXMFLS_R="`pwd`/$TEXMFLS_R"
123   esac
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"
130   else
131     db_file="$TEXMFLS_R/ls-R"
132   fi
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
137   "") ;;
138   /* | [A-z]:/*) db_file="$db_readlink" ;;
139   *)  db_file="$TEXMFLS_R/$db_readlink"
140   esac
141   db_dir=`echo "$db_file" | sed 's%/[^/][^/]*$%%'` # can't rely on dirname
142
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; }
147
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
156     continue
157   fi
158
159   # Skip if we cannot write the file:
160   kpseaccess -w "$db_file" || { echo "$progname: $db_file: no write permission. Skipping..." >&2; continue; }
161
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"
166   rm -f "$db_file_tmp"
167
168   $verbose && echo "$progname: Updating $db_file... " >&2
169   $dry_run && continue
170   
171   echo "$ls_R_magic" >"$db_file_tmp"
172
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.
180   # 
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' \
187    >>"$db_file_tmp"
188
189   # To be really safe, a loop.
190   until PERMS=`kpsestat = "$db_file"`; do sleep 1; done
191   chmod $PERMS "$db_file_tmp"
192   rm -f "$db_file"
193   mv "$db_file_tmp" "$db_file"
194   rm -rf "$db_dir_tmp"
195 done
196 $verbose && echo "$progname: Done." >&2
197 exit 0