install-sh revision 2e2dd055
1a966c04fSmrg#!/bin/sh
2a966c04fSmrg# install - install a program, script, or datafile
3a966c04fSmrg
42e2dd055Smrgscriptversion=2009-04-28.21; # UTC
5a966c04fSmrg
6a966c04fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a966c04fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8a966c04fSmrg# following copyright and license.
9a966c04fSmrg#
10a966c04fSmrg# Copyright (C) 1994 X Consortium
11a966c04fSmrg#
12a966c04fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a966c04fSmrg# of this software and associated documentation files (the "Software"), to
14a966c04fSmrg# deal in the Software without restriction, including without limitation the
15a966c04fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a966c04fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17a966c04fSmrg# furnished to do so, subject to the following conditions:
18a966c04fSmrg#
19a966c04fSmrg# The above copyright notice and this permission notice shall be included in
20a966c04fSmrg# all copies or substantial portions of the Software.
21a966c04fSmrg#
22a966c04fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a966c04fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a966c04fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a966c04fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a966c04fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a966c04fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a966c04fSmrg#
29a966c04fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30a966c04fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a966c04fSmrg# ings in this Software without prior written authorization from the X Consor-
32a966c04fSmrg# tium.
33a966c04fSmrg#
34a966c04fSmrg#
35a966c04fSmrg# FSF changes to this file are in the public domain.
36a966c04fSmrg#
37a966c04fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38a966c04fSmrg# `make' implicit rules from creating a file called install from it
39a966c04fSmrg# when there is no Makefile.
40a966c04fSmrg#
41a966c04fSmrg# This script is compatible with the BSD install script, but was written
42a966c04fSmrg# from scratch.
43a966c04fSmrg
44a966c04fSmrgnl='
45a966c04fSmrg'
46a966c04fSmrgIFS=" ""	$nl"
47a966c04fSmrg
48a966c04fSmrg# set DOITPROG to echo to test this script
49a966c04fSmrg
50a966c04fSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
512e2dd055Smrgdoit=${DOITPROG-}
52a966c04fSmrgif test -z "$doit"; then
53a966c04fSmrg  doit_exec=exec
54a966c04fSmrgelse
55a966c04fSmrg  doit_exec=$doit
56a966c04fSmrgfi
57a966c04fSmrg
58a966c04fSmrg# Put in absolute file names if you don't have them in your path;
59a966c04fSmrg# or use environment vars.
60a966c04fSmrg
612e2dd055Smrgchgrpprog=${CHGRPPROG-chgrp}
622e2dd055Smrgchmodprog=${CHMODPROG-chmod}
632e2dd055Smrgchownprog=${CHOWNPROG-chown}
642e2dd055Smrgcmpprog=${CMPPROG-cmp}
652e2dd055Smrgcpprog=${CPPROG-cp}
662e2dd055Smrgmkdirprog=${MKDIRPROG-mkdir}
672e2dd055Smrgmvprog=${MVPROG-mv}
682e2dd055Smrgrmprog=${RMPROG-rm}
692e2dd055Smrgstripprog=${STRIPPROG-strip}
702e2dd055Smrg
712e2dd055Smrgposix_glob='?'
722e2dd055Smrginitialize_posix_glob='
732e2dd055Smrg  test "$posix_glob" != "?" || {
742e2dd055Smrg    if (set -f) 2>/dev/null; then
752e2dd055Smrg      posix_glob=
762e2dd055Smrg    else
772e2dd055Smrg      posix_glob=:
782e2dd055Smrg    fi
792e2dd055Smrg  }
802e2dd055Smrg'
81a966c04fSmrg
82a966c04fSmrgposix_mkdir=
83a966c04fSmrg
84a966c04fSmrg# Desired mode of installed file.
85a966c04fSmrgmode=0755
86a966c04fSmrg
872e2dd055Smrgchgrpcmd=
88a966c04fSmrgchmodcmd=$chmodprog
89a966c04fSmrgchowncmd=
902e2dd055Smrgmvcmd=$mvprog
91a966c04fSmrgrmcmd="$rmprog -f"
922e2dd055Smrgstripcmd=
932e2dd055Smrg
94a966c04fSmrgsrc=
95a966c04fSmrgdst=
96a966c04fSmrgdir_arg=
972e2dd055Smrgdst_arg=
982e2dd055Smrg
992e2dd055Smrgcopy_on_change=false
100a966c04fSmrgno_target_directory=
101a966c04fSmrg
1022e2dd055Smrgusage="\
1032e2dd055SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104a966c04fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105a966c04fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106a966c04fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107a966c04fSmrg
108a966c04fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109a966c04fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110a966c04fSmrgIn the 4th, create DIRECTORIES.
111a966c04fSmrg
112a966c04fSmrgOptions:
1132e2dd055Smrg     --help     display this help and exit.
1142e2dd055Smrg     --version  display version info and exit.
1152e2dd055Smrg
1162e2dd055Smrg  -c            (ignored)
1172e2dd055Smrg  -C            install only if different (preserve the last data modification time)
1182e2dd055Smrg  -d            create directories instead of installing files.
1192e2dd055Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1202e2dd055Smrg  -m MODE       $chmodprog installed files to MODE.
1212e2dd055Smrg  -o USER       $chownprog installed files to USER.
1222e2dd055Smrg  -s            $stripprog installed files.
1232e2dd055Smrg  -t DIRECTORY  install into DIRECTORY.
1242e2dd055Smrg  -T            report an error if DSTFILE is a directory.
125a966c04fSmrg
126a966c04fSmrgEnvironment variables override the default commands:
1272e2dd055Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1282e2dd055Smrg  RMPROG STRIPPROG
129a966c04fSmrg"
130a966c04fSmrg
131a966c04fSmrgwhile test $# -ne 0; do
132a966c04fSmrg  case $1 in
1332e2dd055Smrg    -c) ;;
1342e2dd055Smrg
1352e2dd055Smrg    -C) copy_on_change=true;;
136a966c04fSmrg
1372e2dd055Smrg    -d) dir_arg=true;;
138a966c04fSmrg
139a966c04fSmrg    -g) chgrpcmd="$chgrpprog $2"
1402e2dd055Smrg	shift;;
141a966c04fSmrg
142a966c04fSmrg    --help) echo "$usage"; exit $?;;
143a966c04fSmrg
144a966c04fSmrg    -m) mode=$2
145a966c04fSmrg	case $mode in
146a966c04fSmrg	  *' '* | *'	'* | *'
147a966c04fSmrg'*	  | *'*'* | *'?'* | *'['*)
148a966c04fSmrg	    echo "$0: invalid mode: $mode" >&2
149a966c04fSmrg	    exit 1;;
150a966c04fSmrg	esac
1512e2dd055Smrg	shift;;
152a966c04fSmrg
153a966c04fSmrg    -o) chowncmd="$chownprog $2"
1542e2dd055Smrg	shift;;
155a966c04fSmrg
1562e2dd055Smrg    -s) stripcmd=$stripprog;;
157a966c04fSmrg
1582e2dd055Smrg    -t) dst_arg=$2
1592e2dd055Smrg	shift;;
160a966c04fSmrg
1612e2dd055Smrg    -T) no_target_directory=true;;
162a966c04fSmrg
163a966c04fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164a966c04fSmrg
165a966c04fSmrg    --)	shift
166a966c04fSmrg	break;;
167a966c04fSmrg
168a966c04fSmrg    -*)	echo "$0: invalid option: $1" >&2
169a966c04fSmrg	exit 1;;
170a966c04fSmrg
171a966c04fSmrg    *)  break;;
172a966c04fSmrg  esac
1732e2dd055Smrg  shift
174a966c04fSmrgdone
175a966c04fSmrg
1762e2dd055Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177a966c04fSmrg  # When -d is used, all remaining arguments are directories to create.
178a966c04fSmrg  # When -t is used, the destination is already specified.
179a966c04fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180a966c04fSmrg  for arg
181a966c04fSmrg  do
1822e2dd055Smrg    if test -n "$dst_arg"; then
183a966c04fSmrg      # $@ is not empty: it contains at least $arg.
1842e2dd055Smrg      set fnord "$@" "$dst_arg"
185a966c04fSmrg      shift # fnord
186a966c04fSmrg    fi
187a966c04fSmrg    shift # arg
1882e2dd055Smrg    dst_arg=$arg
189a966c04fSmrg  done
190a966c04fSmrgfi
191a966c04fSmrg
192a966c04fSmrgif test $# -eq 0; then
193a966c04fSmrg  if test -z "$dir_arg"; then
194a966c04fSmrg    echo "$0: no input file specified." >&2
195a966c04fSmrg    exit 1
196a966c04fSmrg  fi
197a966c04fSmrg  # It's OK to call `install-sh -d' without argument.
198a966c04fSmrg  # This can happen when creating conditional directories.
199a966c04fSmrg  exit 0
200a966c04fSmrgfi
201a966c04fSmrg
202a966c04fSmrgif test -z "$dir_arg"; then
203a966c04fSmrg  trap '(exit $?); exit' 1 2 13 15
204a966c04fSmrg
205a966c04fSmrg  # Set umask so as not to create temps with too-generous modes.
206a966c04fSmrg  # However, 'strip' requires both read and write access to temps.
207a966c04fSmrg  case $mode in
208a966c04fSmrg    # Optimize common cases.
209a966c04fSmrg    *644) cp_umask=133;;
210a966c04fSmrg    *755) cp_umask=22;;
211a966c04fSmrg
212a966c04fSmrg    *[0-7])
213a966c04fSmrg      if test -z "$stripcmd"; then
214a966c04fSmrg	u_plus_rw=
215a966c04fSmrg      else
216a966c04fSmrg	u_plus_rw='% 200'
217a966c04fSmrg      fi
218a966c04fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219a966c04fSmrg    *)
220a966c04fSmrg      if test -z "$stripcmd"; then
221a966c04fSmrg	u_plus_rw=
222a966c04fSmrg      else
223a966c04fSmrg	u_plus_rw=,u+rw
224a966c04fSmrg      fi
225a966c04fSmrg      cp_umask=$mode$u_plus_rw;;
226a966c04fSmrg  esac
227a966c04fSmrgfi
228a966c04fSmrg
229a966c04fSmrgfor src
230a966c04fSmrgdo
231a966c04fSmrg  # Protect names starting with `-'.
232a966c04fSmrg  case $src in
2332e2dd055Smrg    -*) src=./$src;;
234a966c04fSmrg  esac
235a966c04fSmrg
236a966c04fSmrg  if test -n "$dir_arg"; then
237a966c04fSmrg    dst=$src
238a966c04fSmrg    dstdir=$dst
239a966c04fSmrg    test -d "$dstdir"
240a966c04fSmrg    dstdir_status=$?
241a966c04fSmrg  else
242a966c04fSmrg
243a966c04fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244a966c04fSmrg    # might cause directories to be created, which would be especially bad
245a966c04fSmrg    # if $src (and thus $dsttmp) contains '*'.
246a966c04fSmrg    if test ! -f "$src" && test ! -d "$src"; then
247a966c04fSmrg      echo "$0: $src does not exist." >&2
248a966c04fSmrg      exit 1
249a966c04fSmrg    fi
250a966c04fSmrg
2512e2dd055Smrg    if test -z "$dst_arg"; then
252a966c04fSmrg      echo "$0: no destination specified." >&2
253a966c04fSmrg      exit 1
254a966c04fSmrg    fi
255a966c04fSmrg
2562e2dd055Smrg    dst=$dst_arg
257a966c04fSmrg    # Protect names starting with `-'.
258a966c04fSmrg    case $dst in
2592e2dd055Smrg      -*) dst=./$dst;;
260a966c04fSmrg    esac
261a966c04fSmrg
262a966c04fSmrg    # If destination is a directory, append the input filename; won't work
263a966c04fSmrg    # if double slashes aren't ignored.
264a966c04fSmrg    if test -d "$dst"; then
265a966c04fSmrg      if test -n "$no_target_directory"; then
2662e2dd055Smrg	echo "$0: $dst_arg: Is a directory" >&2
267a966c04fSmrg	exit 1
268a966c04fSmrg      fi
269a966c04fSmrg      dstdir=$dst
270a966c04fSmrg      dst=$dstdir/`basename "$src"`
271a966c04fSmrg      dstdir_status=0
272a966c04fSmrg    else
273a966c04fSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274a966c04fSmrg      dstdir=`
275a966c04fSmrg	(dirname "$dst") 2>/dev/null ||
276a966c04fSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277a966c04fSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
278a966c04fSmrg	     X"$dst" : 'X\(//\)$' \| \
279a966c04fSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280a966c04fSmrg	echo X"$dst" |
281a966c04fSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282a966c04fSmrg		   s//\1/
283a966c04fSmrg		   q
284a966c04fSmrg		 }
285a966c04fSmrg		 /^X\(\/\/\)[^/].*/{
286a966c04fSmrg		   s//\1/
287a966c04fSmrg		   q
288a966c04fSmrg		 }
289a966c04fSmrg		 /^X\(\/\/\)$/{
290a966c04fSmrg		   s//\1/
291a966c04fSmrg		   q
292a966c04fSmrg		 }
293a966c04fSmrg		 /^X\(\/\).*/{
294a966c04fSmrg		   s//\1/
295a966c04fSmrg		   q
296a966c04fSmrg		 }
297a966c04fSmrg		 s/.*/./; q'
298a966c04fSmrg      `
299a966c04fSmrg
300a966c04fSmrg      test -d "$dstdir"
301a966c04fSmrg      dstdir_status=$?
302a966c04fSmrg    fi
303a966c04fSmrg  fi
304a966c04fSmrg
305a966c04fSmrg  obsolete_mkdir_used=false
306a966c04fSmrg
307a966c04fSmrg  if test $dstdir_status != 0; then
308a966c04fSmrg    case $posix_mkdir in
309a966c04fSmrg      '')
310a966c04fSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
311a966c04fSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
312a966c04fSmrg	umask=`umask`
313a966c04fSmrg	case $stripcmd.$umask in
314a966c04fSmrg	  # Optimize common cases.
315a966c04fSmrg	  *[2367][2367]) mkdir_umask=$umask;;
316a966c04fSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317a966c04fSmrg
318a966c04fSmrg	  *[0-7])
319a966c04fSmrg	    mkdir_umask=`expr $umask + 22 \
320a966c04fSmrg	      - $umask % 100 % 40 + $umask % 20 \
321a966c04fSmrg	      - $umask % 10 % 4 + $umask % 2
322a966c04fSmrg	    `;;
323a966c04fSmrg	  *) mkdir_umask=$umask,go-w;;
324a966c04fSmrg	esac
325a966c04fSmrg
326a966c04fSmrg	# With -d, create the new directory with the user-specified mode.
327a966c04fSmrg	# Otherwise, rely on $mkdir_umask.
328a966c04fSmrg	if test -n "$dir_arg"; then
329a966c04fSmrg	  mkdir_mode=-m$mode
330a966c04fSmrg	else
331a966c04fSmrg	  mkdir_mode=
332a966c04fSmrg	fi
333a966c04fSmrg
334a966c04fSmrg	posix_mkdir=false
335a966c04fSmrg	case $umask in
336a966c04fSmrg	  *[123567][0-7][0-7])
337a966c04fSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338a966c04fSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339a966c04fSmrg	    ;;
340a966c04fSmrg	  *)
341a966c04fSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342a966c04fSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343a966c04fSmrg
344a966c04fSmrg	    if (umask $mkdir_umask &&
345a966c04fSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346a966c04fSmrg	    then
347a966c04fSmrg	      if test -z "$dir_arg" || {
348a966c04fSmrg		   # Check for POSIX incompatibilities with -m.
349a966c04fSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350a966c04fSmrg		   # other-writeable bit of parent directory when it shouldn't.
351a966c04fSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352a966c04fSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353a966c04fSmrg		   case $ls_ld_tmpdir in
354a966c04fSmrg		     d????-?r-*) different_mode=700;;
355a966c04fSmrg		     d????-?--*) different_mode=755;;
356a966c04fSmrg		     *) false;;
357a966c04fSmrg		   esac &&
358a966c04fSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359a966c04fSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360a966c04fSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361a966c04fSmrg		   }
362a966c04fSmrg		 }
363a966c04fSmrg	      then posix_mkdir=:
364a966c04fSmrg	      fi
365a966c04fSmrg	      rmdir "$tmpdir/d" "$tmpdir"
366a966c04fSmrg	    else
367a966c04fSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
368a966c04fSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369a966c04fSmrg	    fi
370a966c04fSmrg	    trap '' 0;;
371a966c04fSmrg	esac;;
372a966c04fSmrg    esac
373a966c04fSmrg
374a966c04fSmrg    if
375a966c04fSmrg      $posix_mkdir && (
376a966c04fSmrg	umask $mkdir_umask &&
377a966c04fSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378a966c04fSmrg      )
379a966c04fSmrg    then :
380a966c04fSmrg    else
381a966c04fSmrg
382a966c04fSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383a966c04fSmrg      # or it failed possibly due to a race condition.  Create the
384a966c04fSmrg      # directory the slow way, step by step, checking for races as we go.
385a966c04fSmrg
386a966c04fSmrg      case $dstdir in
3872e2dd055Smrg	/*) prefix='/';;
3882e2dd055Smrg	-*) prefix='./';;
3892e2dd055Smrg	*)  prefix='';;
390a966c04fSmrg      esac
391a966c04fSmrg
3922e2dd055Smrg      eval "$initialize_posix_glob"
393a966c04fSmrg
394a966c04fSmrg      oIFS=$IFS
395a966c04fSmrg      IFS=/
3962e2dd055Smrg      $posix_glob set -f
397a966c04fSmrg      set fnord $dstdir
398a966c04fSmrg      shift
3992e2dd055Smrg      $posix_glob set +f
400a966c04fSmrg      IFS=$oIFS
401a966c04fSmrg
402a966c04fSmrg      prefixes=
403a966c04fSmrg
404a966c04fSmrg      for d
405a966c04fSmrg      do
406a966c04fSmrg	test -z "$d" && continue
407a966c04fSmrg
408a966c04fSmrg	prefix=$prefix$d
409a966c04fSmrg	if test -d "$prefix"; then
410a966c04fSmrg	  prefixes=
411a966c04fSmrg	else
412a966c04fSmrg	  if $posix_mkdir; then
413a966c04fSmrg	    (umask=$mkdir_umask &&
414a966c04fSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415a966c04fSmrg	    # Don't fail if two instances are running concurrently.
416a966c04fSmrg	    test -d "$prefix" || exit 1
417a966c04fSmrg	  else
418a966c04fSmrg	    case $prefix in
419a966c04fSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420a966c04fSmrg	      *) qprefix=$prefix;;
421a966c04fSmrg	    esac
422a966c04fSmrg	    prefixes="$prefixes '$qprefix'"
423a966c04fSmrg	  fi
424a966c04fSmrg	fi
425a966c04fSmrg	prefix=$prefix/
426a966c04fSmrg      done
427a966c04fSmrg
428a966c04fSmrg      if test -n "$prefixes"; then
429a966c04fSmrg	# Don't fail if two instances are running concurrently.
430a966c04fSmrg	(umask $mkdir_umask &&
431a966c04fSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432a966c04fSmrg	  test -d "$dstdir" || exit 1
433a966c04fSmrg	obsolete_mkdir_used=true
434a966c04fSmrg      fi
435a966c04fSmrg    fi
436a966c04fSmrg  fi
437a966c04fSmrg
438a966c04fSmrg  if test -n "$dir_arg"; then
439a966c04fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440a966c04fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441a966c04fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442a966c04fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443a966c04fSmrg  else
444a966c04fSmrg
445a966c04fSmrg    # Make a couple of temp file names in the proper directory.
446a966c04fSmrg    dsttmp=$dstdir/_inst.$$_
447a966c04fSmrg    rmtmp=$dstdir/_rm.$$_
448a966c04fSmrg
449a966c04fSmrg    # Trap to clean up those temp files at exit.
450a966c04fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451a966c04fSmrg
452a966c04fSmrg    # Copy the file name to the temp name.
453a966c04fSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454a966c04fSmrg
455a966c04fSmrg    # and set any options; do chmod last to preserve setuid bits.
456a966c04fSmrg    #
457a966c04fSmrg    # If any of these fail, we abort the whole thing.  If we want to
458a966c04fSmrg    # ignore errors from any of these, just make sure not to ignore
459a966c04fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460a966c04fSmrg    #
4612e2dd055Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4622e2dd055Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4632e2dd055Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4642e2dd055Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4652e2dd055Smrg
4662e2dd055Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4672e2dd055Smrg    if $copy_on_change &&
4682e2dd055Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4692e2dd055Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4702e2dd055Smrg
4712e2dd055Smrg       eval "$initialize_posix_glob" &&
4722e2dd055Smrg       $posix_glob set -f &&
4732e2dd055Smrg       set X $old && old=:$2:$4:$5:$6 &&
4742e2dd055Smrg       set X $new && new=:$2:$4:$5:$6 &&
4752e2dd055Smrg       $posix_glob set +f &&
4762e2dd055Smrg
4772e2dd055Smrg       test "$old" = "$new" &&
4782e2dd055Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4792e2dd055Smrg    then
4802e2dd055Smrg      rm -f "$dsttmp"
4812e2dd055Smrg    else
4822e2dd055Smrg      # Rename the file to the real destination.
4832e2dd055Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4842e2dd055Smrg
4852e2dd055Smrg      # The rename failed, perhaps because mv can't rename something else
4862e2dd055Smrg      # to itself, or perhaps because mv is so ancient that it does not
4872e2dd055Smrg      # support -f.
4882e2dd055Smrg      {
4892e2dd055Smrg	# Now remove or move aside any old file at destination location.
4902e2dd055Smrg	# We try this two ways since rm can't unlink itself on some
4912e2dd055Smrg	# systems and the destination file might be busy for other
4922e2dd055Smrg	# reasons.  In this case, the final cleanup might fail but the new
4932e2dd055Smrg	# file should still install successfully.
4942e2dd055Smrg	{
4952e2dd055Smrg	  test ! -f "$dst" ||
4962e2dd055Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4972e2dd055Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4982e2dd055Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4992e2dd055Smrg	  } ||
5002e2dd055Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5012e2dd055Smrg	    (exit 1); exit 1
5022e2dd055Smrg	  }
5032e2dd055Smrg	} &&
5042e2dd055Smrg
5052e2dd055Smrg	# Now rename the file to the real destination.
5062e2dd055Smrg	$doit $mvcmd "$dsttmp" "$dst"
5072e2dd055Smrg      }
5082e2dd055Smrg    fi || exit 1
509a966c04fSmrg
510a966c04fSmrg    trap '' 0
511a966c04fSmrg  fi
512a966c04fSmrgdone
513a966c04fSmrg
514a966c04fSmrg# Local variables:
515a966c04fSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516a966c04fSmrg# time-stamp-start: "scriptversion="
517a966c04fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5182e2dd055Smrg# time-stamp-time-zone: "UTC"
5192e2dd055Smrg# time-stamp-end: "; # UTC"
520a966c04fSmrg# End:
521