install-sh revision a005d216
1ff7e0accSmrg#!/bin/sh
2ff7e0accSmrg# install - install a program, script, or datafile
3ff7e0accSmrg
4a005d216Smrgscriptversion=2006-12-25.00
5ff7e0accSmrg
6ff7e0accSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7ff7e0accSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8ff7e0accSmrg# following copyright and license.
9ff7e0accSmrg#
10ff7e0accSmrg# Copyright (C) 1994 X Consortium
11ff7e0accSmrg#
12ff7e0accSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13ff7e0accSmrg# of this software and associated documentation files (the "Software"), to
14ff7e0accSmrg# deal in the Software without restriction, including without limitation the
15ff7e0accSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16ff7e0accSmrg# sell copies of the Software, and to permit persons to whom the Software is
17ff7e0accSmrg# furnished to do so, subject to the following conditions:
18ff7e0accSmrg#
19ff7e0accSmrg# The above copyright notice and this permission notice shall be included in
20ff7e0accSmrg# all copies or substantial portions of the Software.
21ff7e0accSmrg#
22ff7e0accSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23ff7e0accSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24ff7e0accSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25ff7e0accSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26ff7e0accSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27ff7e0accSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28ff7e0accSmrg#
29ff7e0accSmrg# Except as contained in this notice, the name of the X Consortium shall not
30ff7e0accSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31ff7e0accSmrg# ings in this Software without prior written authorization from the X Consor-
32ff7e0accSmrg# tium.
33ff7e0accSmrg#
34ff7e0accSmrg#
35ff7e0accSmrg# FSF changes to this file are in the public domain.
36ff7e0accSmrg#
37ff7e0accSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38ff7e0accSmrg# `make' implicit rules from creating a file called install from it
39ff7e0accSmrg# when there is no Makefile.
40ff7e0accSmrg#
41ff7e0accSmrg# This script is compatible with the BSD install script, but was written
42ff7e0accSmrg# from scratch.
43ff7e0accSmrg
44ff7e0accSmrgnl='
45ff7e0accSmrg'
46ff7e0accSmrgIFS=" ""	$nl"
47ff7e0accSmrg
48ff7e0accSmrg# set DOITPROG to echo to test this script
49ff7e0accSmrg
50ff7e0accSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51a005d216Smrgdoit=${DOITPROG-}
52ff7e0accSmrgif test -z "$doit"; then
53ff7e0accSmrg  doit_exec=exec
54ff7e0accSmrgelse
55ff7e0accSmrg  doit_exec=$doit
56ff7e0accSmrgfi
57ff7e0accSmrg
58ff7e0accSmrg# Put in absolute file names if you don't have them in your path;
59ff7e0accSmrg# or use environment vars.
60ff7e0accSmrg
61a005d216Smrgchgrpprog=${CHGRPPROG-chgrp}
62a005d216Smrgchmodprog=${CHMODPROG-chmod}
63a005d216Smrgchownprog=${CHOWNPROG-chown}
64a005d216Smrgcmpprog=${CMPPROG-cmp}
65a005d216Smrgcpprog=${CPPROG-cp}
66a005d216Smrgmkdirprog=${MKDIRPROG-mkdir}
67a005d216Smrgmvprog=${MVPROG-mv}
68a005d216Smrgrmprog=${RMPROG-rm}
69a005d216Smrgstripprog=${STRIPPROG-strip}
70a005d216Smrg
71a005d216Smrgposix_glob='?'
72a005d216Smrginitialize_posix_glob='
73a005d216Smrg  test "$posix_glob" != "?" || {
74a005d216Smrg    if (set -f) 2>/dev/null; then
75a005d216Smrg      posix_glob=
76a005d216Smrg    else
77a005d216Smrg      posix_glob=:
78a005d216Smrg    fi
79a005d216Smrg  }
80a005d216Smrg'
81ff7e0accSmrg
82ff7e0accSmrgposix_mkdir=
83ff7e0accSmrg
84ff7e0accSmrg# Desired mode of installed file.
85ff7e0accSmrgmode=0755
86ff7e0accSmrg
87a005d216Smrgchgrpcmd=
88ff7e0accSmrgchmodcmd=$chmodprog
89ff7e0accSmrgchowncmd=
90a005d216Smrgmvcmd=$mvprog
91ff7e0accSmrgrmcmd="$rmprog -f"
92a005d216Smrgstripcmd=
93a005d216Smrg
94ff7e0accSmrgsrc=
95ff7e0accSmrgdst=
96ff7e0accSmrgdir_arg=
97a005d216Smrgdst_arg=
98a005d216Smrg
99a005d216Smrgcopy_on_change=false
100ff7e0accSmrgno_target_directory=
101ff7e0accSmrg
102a005d216Smrgusage="\
103a005d216SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104ff7e0accSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105ff7e0accSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106ff7e0accSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107ff7e0accSmrg
108ff7e0accSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109ff7e0accSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110ff7e0accSmrgIn the 4th, create DIRECTORIES.
111ff7e0accSmrg
112ff7e0accSmrgOptions:
113a005d216Smrg     --help     display this help and exit.
114a005d216Smrg     --version  display version info and exit.
115a005d216Smrg
116a005d216Smrg  -c            (ignored)
117a005d216Smrg  -C            install only if different (preserve the last data modification time)
118a005d216Smrg  -d            create directories instead of installing files.
119a005d216Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120a005d216Smrg  -m MODE       $chmodprog installed files to MODE.
121a005d216Smrg  -o USER       $chownprog installed files to USER.
122a005d216Smrg  -s            $stripprog installed files.
123a005d216Smrg  -t DIRECTORY  install into DIRECTORY.
124a005d216Smrg  -T            report an error if DSTFILE is a directory.
125ff7e0accSmrg
126ff7e0accSmrgEnvironment variables override the default commands:
127a005d216Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128a005d216Smrg  RMPROG STRIPPROG
129ff7e0accSmrg"
130ff7e0accSmrg
131ff7e0accSmrgwhile test $# -ne 0; do
132ff7e0accSmrg  case $1 in
133a005d216Smrg    -c) ;;
134a005d216Smrg
135a005d216Smrg    -C) copy_on_change=true;;
136ff7e0accSmrg
137a005d216Smrg    -d) dir_arg=true;;
138ff7e0accSmrg
139ff7e0accSmrg    -g) chgrpcmd="$chgrpprog $2"
140a005d216Smrg	shift;;
141ff7e0accSmrg
142ff7e0accSmrg    --help) echo "$usage"; exit $?;;
143ff7e0accSmrg
144ff7e0accSmrg    -m) mode=$2
145ff7e0accSmrg	case $mode in
146ff7e0accSmrg	  *' '* | *'	'* | *'
147ff7e0accSmrg'*	  | *'*'* | *'?'* | *'['*)
148ff7e0accSmrg	    echo "$0: invalid mode: $mode" >&2
149ff7e0accSmrg	    exit 1;;
150ff7e0accSmrg	esac
151a005d216Smrg	shift;;
152ff7e0accSmrg
153ff7e0accSmrg    -o) chowncmd="$chownprog $2"
154a005d216Smrg	shift;;
155ff7e0accSmrg
156a005d216Smrg    -s) stripcmd=$stripprog;;
157ff7e0accSmrg
158a005d216Smrg    -t) dst_arg=$2
159a005d216Smrg	shift;;
160ff7e0accSmrg
161a005d216Smrg    -T) no_target_directory=true;;
162ff7e0accSmrg
163ff7e0accSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164ff7e0accSmrg
165ff7e0accSmrg    --)	shift
166ff7e0accSmrg	break;;
167ff7e0accSmrg
168ff7e0accSmrg    -*)	echo "$0: invalid option: $1" >&2
169ff7e0accSmrg	exit 1;;
170ff7e0accSmrg
171ff7e0accSmrg    *)  break;;
172ff7e0accSmrg  esac
173a005d216Smrg  shift
174ff7e0accSmrgdone
175ff7e0accSmrg
176a005d216Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177ff7e0accSmrg  # When -d is used, all remaining arguments are directories to create.
178ff7e0accSmrg  # When -t is used, the destination is already specified.
179ff7e0accSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180ff7e0accSmrg  for arg
181ff7e0accSmrg  do
182a005d216Smrg    if test -n "$dst_arg"; then
183ff7e0accSmrg      # $@ is not empty: it contains at least $arg.
184a005d216Smrg      set fnord "$@" "$dst_arg"
185ff7e0accSmrg      shift # fnord
186ff7e0accSmrg    fi
187ff7e0accSmrg    shift # arg
188a005d216Smrg    dst_arg=$arg
189ff7e0accSmrg  done
190ff7e0accSmrgfi
191ff7e0accSmrg
192ff7e0accSmrgif test $# -eq 0; then
193ff7e0accSmrg  if test -z "$dir_arg"; then
194ff7e0accSmrg    echo "$0: no input file specified." >&2
195ff7e0accSmrg    exit 1
196ff7e0accSmrg  fi
197ff7e0accSmrg  # It's OK to call `install-sh -d' without argument.
198ff7e0accSmrg  # This can happen when creating conditional directories.
199ff7e0accSmrg  exit 0
200ff7e0accSmrgfi
201ff7e0accSmrg
202ff7e0accSmrgif test -z "$dir_arg"; then
203ff7e0accSmrg  trap '(exit $?); exit' 1 2 13 15
204ff7e0accSmrg
205ff7e0accSmrg  # Set umask so as not to create temps with too-generous modes.
206ff7e0accSmrg  # However, 'strip' requires both read and write access to temps.
207ff7e0accSmrg  case $mode in
208ff7e0accSmrg    # Optimize common cases.
209ff7e0accSmrg    *644) cp_umask=133;;
210ff7e0accSmrg    *755) cp_umask=22;;
211ff7e0accSmrg
212ff7e0accSmrg    *[0-7])
213ff7e0accSmrg      if test -z "$stripcmd"; then
214ff7e0accSmrg	u_plus_rw=
215ff7e0accSmrg      else
216ff7e0accSmrg	u_plus_rw='% 200'
217ff7e0accSmrg      fi
218ff7e0accSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219ff7e0accSmrg    *)
220ff7e0accSmrg      if test -z "$stripcmd"; then
221ff7e0accSmrg	u_plus_rw=
222ff7e0accSmrg      else
223ff7e0accSmrg	u_plus_rw=,u+rw
224ff7e0accSmrg      fi
225ff7e0accSmrg      cp_umask=$mode$u_plus_rw;;
226ff7e0accSmrg  esac
227ff7e0accSmrgfi
228ff7e0accSmrg
229ff7e0accSmrgfor src
230ff7e0accSmrgdo
231ff7e0accSmrg  # Protect names starting with `-'.
232ff7e0accSmrg  case $src in
233a005d216Smrg    -*) src=./$src;;
234ff7e0accSmrg  esac
235ff7e0accSmrg
236ff7e0accSmrg  if test -n "$dir_arg"; then
237ff7e0accSmrg    dst=$src
238ff7e0accSmrg    dstdir=$dst
239ff7e0accSmrg    test -d "$dstdir"
240ff7e0accSmrg    dstdir_status=$?
241ff7e0accSmrg  else
242ff7e0accSmrg
243ff7e0accSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244ff7e0accSmrg    # might cause directories to be created, which would be especially bad
245ff7e0accSmrg    # if $src (and thus $dsttmp) contains '*'.
246ff7e0accSmrg    if test ! -f "$src" && test ! -d "$src"; then
247ff7e0accSmrg      echo "$0: $src does not exist." >&2
248ff7e0accSmrg      exit 1
249ff7e0accSmrg    fi
250ff7e0accSmrg
251a005d216Smrg    if test -z "$dst_arg"; then
252ff7e0accSmrg      echo "$0: no destination specified." >&2
253ff7e0accSmrg      exit 1
254ff7e0accSmrg    fi
255ff7e0accSmrg
256a005d216Smrg    dst=$dst_arg
257ff7e0accSmrg    # Protect names starting with `-'.
258ff7e0accSmrg    case $dst in
259a005d216Smrg      -*) dst=./$dst;;
260ff7e0accSmrg    esac
261ff7e0accSmrg
262ff7e0accSmrg    # If destination is a directory, append the input filename; won't work
263ff7e0accSmrg    # if double slashes aren't ignored.
264ff7e0accSmrg    if test -d "$dst"; then
265ff7e0accSmrg      if test -n "$no_target_directory"; then
266a005d216Smrg	echo "$0: $dst_arg: Is a directory" >&2
267ff7e0accSmrg	exit 1
268ff7e0accSmrg      fi
269ff7e0accSmrg      dstdir=$dst
270ff7e0accSmrg      dst=$dstdir/`basename "$src"`
271ff7e0accSmrg      dstdir_status=0
272ff7e0accSmrg    else
273ff7e0accSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274ff7e0accSmrg      dstdir=`
275ff7e0accSmrg	(dirname "$dst") 2>/dev/null ||
276ff7e0accSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277ff7e0accSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
278ff7e0accSmrg	     X"$dst" : 'X\(//\)$' \| \
279ff7e0accSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280ff7e0accSmrg	echo X"$dst" |
281ff7e0accSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282ff7e0accSmrg		   s//\1/
283ff7e0accSmrg		   q
284ff7e0accSmrg		 }
285ff7e0accSmrg		 /^X\(\/\/\)[^/].*/{
286ff7e0accSmrg		   s//\1/
287ff7e0accSmrg		   q
288ff7e0accSmrg		 }
289ff7e0accSmrg		 /^X\(\/\/\)$/{
290ff7e0accSmrg		   s//\1/
291ff7e0accSmrg		   q
292ff7e0accSmrg		 }
293ff7e0accSmrg		 /^X\(\/\).*/{
294ff7e0accSmrg		   s//\1/
295ff7e0accSmrg		   q
296ff7e0accSmrg		 }
297ff7e0accSmrg		 s/.*/./; q'
298ff7e0accSmrg      `
299ff7e0accSmrg
300ff7e0accSmrg      test -d "$dstdir"
301ff7e0accSmrg      dstdir_status=$?
302ff7e0accSmrg    fi
303ff7e0accSmrg  fi
304ff7e0accSmrg
305ff7e0accSmrg  obsolete_mkdir_used=false
306ff7e0accSmrg
307ff7e0accSmrg  if test $dstdir_status != 0; then
308ff7e0accSmrg    case $posix_mkdir in
309ff7e0accSmrg      '')
310ff7e0accSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
311ff7e0accSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
312ff7e0accSmrg	umask=`umask`
313ff7e0accSmrg	case $stripcmd.$umask in
314ff7e0accSmrg	  # Optimize common cases.
315ff7e0accSmrg	  *[2367][2367]) mkdir_umask=$umask;;
316ff7e0accSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317ff7e0accSmrg
318ff7e0accSmrg	  *[0-7])
319ff7e0accSmrg	    mkdir_umask=`expr $umask + 22 \
320ff7e0accSmrg	      - $umask % 100 % 40 + $umask % 20 \
321ff7e0accSmrg	      - $umask % 10 % 4 + $umask % 2
322ff7e0accSmrg	    `;;
323ff7e0accSmrg	  *) mkdir_umask=$umask,go-w;;
324ff7e0accSmrg	esac
325ff7e0accSmrg
326ff7e0accSmrg	# With -d, create the new directory with the user-specified mode.
327ff7e0accSmrg	# Otherwise, rely on $mkdir_umask.
328ff7e0accSmrg	if test -n "$dir_arg"; then
329ff7e0accSmrg	  mkdir_mode=-m$mode
330ff7e0accSmrg	else
331ff7e0accSmrg	  mkdir_mode=
332ff7e0accSmrg	fi
333ff7e0accSmrg
334ff7e0accSmrg	posix_mkdir=false
335ff7e0accSmrg	case $umask in
336ff7e0accSmrg	  *[123567][0-7][0-7])
337ff7e0accSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338ff7e0accSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339ff7e0accSmrg	    ;;
340ff7e0accSmrg	  *)
341ff7e0accSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342ff7e0accSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343ff7e0accSmrg
344ff7e0accSmrg	    if (umask $mkdir_umask &&
345ff7e0accSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346ff7e0accSmrg	    then
347ff7e0accSmrg	      if test -z "$dir_arg" || {
348ff7e0accSmrg		   # Check for POSIX incompatibilities with -m.
349ff7e0accSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350ff7e0accSmrg		   # other-writeable bit of parent directory when it shouldn't.
351ff7e0accSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352ff7e0accSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353ff7e0accSmrg		   case $ls_ld_tmpdir in
354ff7e0accSmrg		     d????-?r-*) different_mode=700;;
355ff7e0accSmrg		     d????-?--*) different_mode=755;;
356ff7e0accSmrg		     *) false;;
357ff7e0accSmrg		   esac &&
358ff7e0accSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359ff7e0accSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360ff7e0accSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361ff7e0accSmrg		   }
362ff7e0accSmrg		 }
363ff7e0accSmrg	      then posix_mkdir=:
364ff7e0accSmrg	      fi
365ff7e0accSmrg	      rmdir "$tmpdir/d" "$tmpdir"
366ff7e0accSmrg	    else
367ff7e0accSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
368ff7e0accSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369ff7e0accSmrg	    fi
370ff7e0accSmrg	    trap '' 0;;
371ff7e0accSmrg	esac;;
372ff7e0accSmrg    esac
373ff7e0accSmrg
374ff7e0accSmrg    if
375ff7e0accSmrg      $posix_mkdir && (
376ff7e0accSmrg	umask $mkdir_umask &&
377ff7e0accSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378ff7e0accSmrg      )
379ff7e0accSmrg    then :
380ff7e0accSmrg    else
381ff7e0accSmrg
382ff7e0accSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383ff7e0accSmrg      # or it failed possibly due to a race condition.  Create the
384ff7e0accSmrg      # directory the slow way, step by step, checking for races as we go.
385ff7e0accSmrg
386ff7e0accSmrg      case $dstdir in
387a005d216Smrg	/*) prefix='/';;
388a005d216Smrg	-*) prefix='./';;
389a005d216Smrg	*)  prefix='';;
390ff7e0accSmrg      esac
391ff7e0accSmrg
392a005d216Smrg      eval "$initialize_posix_glob"
393ff7e0accSmrg
394ff7e0accSmrg      oIFS=$IFS
395ff7e0accSmrg      IFS=/
396a005d216Smrg      $posix_glob set -f
397ff7e0accSmrg      set fnord $dstdir
398ff7e0accSmrg      shift
399a005d216Smrg      $posix_glob set +f
400ff7e0accSmrg      IFS=$oIFS
401ff7e0accSmrg
402ff7e0accSmrg      prefixes=
403ff7e0accSmrg
404ff7e0accSmrg      for d
405ff7e0accSmrg      do
406ff7e0accSmrg	test -z "$d" && continue
407ff7e0accSmrg
408ff7e0accSmrg	prefix=$prefix$d
409ff7e0accSmrg	if test -d "$prefix"; then
410ff7e0accSmrg	  prefixes=
411ff7e0accSmrg	else
412ff7e0accSmrg	  if $posix_mkdir; then
413ff7e0accSmrg	    (umask=$mkdir_umask &&
414ff7e0accSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415ff7e0accSmrg	    # Don't fail if two instances are running concurrently.
416ff7e0accSmrg	    test -d "$prefix" || exit 1
417ff7e0accSmrg	  else
418ff7e0accSmrg	    case $prefix in
419ff7e0accSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420ff7e0accSmrg	      *) qprefix=$prefix;;
421ff7e0accSmrg	    esac
422ff7e0accSmrg	    prefixes="$prefixes '$qprefix'"
423ff7e0accSmrg	  fi
424ff7e0accSmrg	fi
425ff7e0accSmrg	prefix=$prefix/
426ff7e0accSmrg      done
427ff7e0accSmrg
428ff7e0accSmrg      if test -n "$prefixes"; then
429ff7e0accSmrg	# Don't fail if two instances are running concurrently.
430ff7e0accSmrg	(umask $mkdir_umask &&
431ff7e0accSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432ff7e0accSmrg	  test -d "$dstdir" || exit 1
433ff7e0accSmrg	obsolete_mkdir_used=true
434ff7e0accSmrg      fi
435ff7e0accSmrg    fi
436ff7e0accSmrg  fi
437ff7e0accSmrg
438ff7e0accSmrg  if test -n "$dir_arg"; then
439ff7e0accSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440ff7e0accSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441ff7e0accSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442ff7e0accSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443ff7e0accSmrg  else
444ff7e0accSmrg
445ff7e0accSmrg    # Make a couple of temp file names in the proper directory.
446ff7e0accSmrg    dsttmp=$dstdir/_inst.$$_
447ff7e0accSmrg    rmtmp=$dstdir/_rm.$$_
448ff7e0accSmrg
449ff7e0accSmrg    # Trap to clean up those temp files at exit.
450ff7e0accSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451ff7e0accSmrg
452ff7e0accSmrg    # Copy the file name to the temp name.
453ff7e0accSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454ff7e0accSmrg
455ff7e0accSmrg    # and set any options; do chmod last to preserve setuid bits.
456ff7e0accSmrg    #
457ff7e0accSmrg    # If any of these fail, we abort the whole thing.  If we want to
458ff7e0accSmrg    # ignore errors from any of these, just make sure not to ignore
459ff7e0accSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460ff7e0accSmrg    #
461a005d216Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462a005d216Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463a005d216Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464a005d216Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465a005d216Smrg
466a005d216Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467a005d216Smrg    if $copy_on_change &&
468a005d216Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469a005d216Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470a005d216Smrg
471a005d216Smrg       eval "$initialize_posix_glob" &&
472a005d216Smrg       $posix_glob set -f &&
473a005d216Smrg       set X $old && old=:$2:$4:$5:$6 &&
474a005d216Smrg       set X $new && new=:$2:$4:$5:$6 &&
475a005d216Smrg       $posix_glob set +f &&
476a005d216Smrg
477a005d216Smrg       test "$old" = "$new" &&
478a005d216Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479a005d216Smrg    then
480a005d216Smrg      rm -f "$dsttmp"
481a005d216Smrg    else
482a005d216Smrg      # Rename the file to the real destination.
483a005d216Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484a005d216Smrg
485a005d216Smrg      # The rename failed, perhaps because mv can't rename something else
486a005d216Smrg      # to itself, or perhaps because mv is so ancient that it does not
487a005d216Smrg      # support -f.
488a005d216Smrg      {
489a005d216Smrg	# Now remove or move aside any old file at destination location.
490a005d216Smrg	# We try this two ways since rm can't unlink itself on some
491a005d216Smrg	# systems and the destination file might be busy for other
492a005d216Smrg	# reasons.  In this case, the final cleanup might fail but the new
493a005d216Smrg	# file should still install successfully.
494a005d216Smrg	{
495a005d216Smrg	  test ! -f "$dst" ||
496a005d216Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497a005d216Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498a005d216Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499a005d216Smrg	  } ||
500a005d216Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
501a005d216Smrg	    (exit 1); exit 1
502a005d216Smrg	  }
503a005d216Smrg	} &&
504a005d216Smrg
505a005d216Smrg	# Now rename the file to the real destination.
506a005d216Smrg	$doit $mvcmd "$dsttmp" "$dst"
507a005d216Smrg      }
508a005d216Smrg    fi || exit 1
509ff7e0accSmrg
510ff7e0accSmrg    trap '' 0
511ff7e0accSmrg  fi
512ff7e0accSmrgdone
513ff7e0accSmrg
514ff7e0accSmrg# Local variables:
515ff7e0accSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516ff7e0accSmrg# time-stamp-start: "scriptversion="
517ff7e0accSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518ff7e0accSmrg# time-stamp-end: "$"
519ff7e0accSmrg# End:
520