install-sh revision ec713c28
11d54945dSmrg#!/bin/sh
21d54945dSmrg# install - install a program, script, or datafile
31d54945dSmrg
4ec713c28Smrgscriptversion=2009-04-28.21; # UTC
51d54945dSmrg
61d54945dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71d54945dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
81d54945dSmrg# following copyright and license.
91d54945dSmrg#
101d54945dSmrg# Copyright (C) 1994 X Consortium
111d54945dSmrg#
121d54945dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131d54945dSmrg# of this software and associated documentation files (the "Software"), to
141d54945dSmrg# deal in the Software without restriction, including without limitation the
151d54945dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161d54945dSmrg# sell copies of the Software, and to permit persons to whom the Software is
171d54945dSmrg# furnished to do so, subject to the following conditions:
181d54945dSmrg#
191d54945dSmrg# The above copyright notice and this permission notice shall be included in
201d54945dSmrg# all copies or substantial portions of the Software.
211d54945dSmrg#
221d54945dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231d54945dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241d54945dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251d54945dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261d54945dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271d54945dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281d54945dSmrg#
291d54945dSmrg# Except as contained in this notice, the name of the X Consortium shall not
301d54945dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
311d54945dSmrg# ings in this Software without prior written authorization from the X Consor-
321d54945dSmrg# tium.
331d54945dSmrg#
341d54945dSmrg#
351d54945dSmrg# FSF changes to this file are in the public domain.
361d54945dSmrg#
371d54945dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
381d54945dSmrg# `make' implicit rules from creating a file called install from it
391d54945dSmrg# when there is no Makefile.
401d54945dSmrg#
411d54945dSmrg# This script is compatible with the BSD install script, but was written
42ba85709eSmrg# from scratch.
43ba85709eSmrg
44ba85709eSmrgnl='
45ba85709eSmrg'
46ba85709eSmrgIFS=" ""	$nl"
471d54945dSmrg
481d54945dSmrg# set DOITPROG to echo to test this script
491d54945dSmrg
501d54945dSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51786a6f21Smrgdoit=${DOITPROG-}
52ba85709eSmrgif test -z "$doit"; then
53ba85709eSmrg  doit_exec=exec
54ba85709eSmrgelse
55ba85709eSmrg  doit_exec=$doit
56ba85709eSmrgfi
571d54945dSmrg
58ba85709eSmrg# Put in absolute file names if you don't have them in your path;
59ba85709eSmrg# or use environment vars.
601d54945dSmrg
61786a6f21Smrgchgrpprog=${CHGRPPROG-chgrp}
62786a6f21Smrgchmodprog=${CHMODPROG-chmod}
63786a6f21Smrgchownprog=${CHOWNPROG-chown}
64786a6f21Smrgcmpprog=${CMPPROG-cmp}
65786a6f21Smrgcpprog=${CPPROG-cp}
66786a6f21Smrgmkdirprog=${MKDIRPROG-mkdir}
67786a6f21Smrgmvprog=${MVPROG-mv}
68786a6f21Smrgrmprog=${RMPROG-rm}
69786a6f21Smrgstripprog=${STRIPPROG-strip}
70786a6f21Smrg
71786a6f21Smrgposix_glob='?'
72786a6f21Smrginitialize_posix_glob='
73786a6f21Smrg  test "$posix_glob" != "?" || {
74786a6f21Smrg    if (set -f) 2>/dev/null; then
75786a6f21Smrg      posix_glob=
76786a6f21Smrg    else
77786a6f21Smrg      posix_glob=:
78786a6f21Smrg    fi
79786a6f21Smrg  }
80786a6f21Smrg'
811d54945dSmrg
82ba85709eSmrgposix_mkdir=
83ba85709eSmrg
84ba85709eSmrg# Desired mode of installed file.
85ba85709eSmrgmode=0755
86ba85709eSmrg
87786a6f21Smrgchgrpcmd=
88ba85709eSmrgchmodcmd=$chmodprog
891d54945dSmrgchowncmd=
90786a6f21Smrgmvcmd=$mvprog
911d54945dSmrgrmcmd="$rmprog -f"
92786a6f21Smrgstripcmd=
93786a6f21Smrg
941d54945dSmrgsrc=
951d54945dSmrgdst=
961d54945dSmrgdir_arg=
97786a6f21Smrgdst_arg=
98786a6f21Smrg
99786a6f21Smrgcopy_on_change=false
1001d54945dSmrgno_target_directory=
1011d54945dSmrg
102786a6f21Smrgusage="\
103786a6f21SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1041d54945dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1051d54945dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1061d54945dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
1071d54945dSmrg
1081d54945dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
1091d54945dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1101d54945dSmrgIn the 4th, create DIRECTORIES.
1111d54945dSmrg
1121d54945dSmrgOptions:
113786a6f21Smrg     --help     display this help and exit.
114786a6f21Smrg     --version  display version info and exit.
115786a6f21Smrg
116786a6f21Smrg  -c            (ignored)
117786a6f21Smrg  -C            install only if different (preserve the last data modification time)
118786a6f21Smrg  -d            create directories instead of installing files.
119786a6f21Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120786a6f21Smrg  -m MODE       $chmodprog installed files to MODE.
121786a6f21Smrg  -o USER       $chownprog installed files to USER.
122786a6f21Smrg  -s            $stripprog installed files.
123786a6f21Smrg  -t DIRECTORY  install into DIRECTORY.
124786a6f21Smrg  -T            report an error if DSTFILE is a directory.
1251d54945dSmrg
1261d54945dSmrgEnvironment variables override the default commands:
127786a6f21Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128786a6f21Smrg  RMPROG STRIPPROG
1291d54945dSmrg"
1301d54945dSmrg
131ba85709eSmrgwhile test $# -ne 0; do
1321d54945dSmrg  case $1 in
133786a6f21Smrg    -c) ;;
134786a6f21Smrg
135786a6f21Smrg    -C) copy_on_change=true;;
1361d54945dSmrg
137786a6f21Smrg    -d) dir_arg=true;;
1381d54945dSmrg
1391d54945dSmrg    -g) chgrpcmd="$chgrpprog $2"
140786a6f21Smrg	shift;;
1411d54945dSmrg
1421d54945dSmrg    --help) echo "$usage"; exit $?;;
1431d54945dSmrg
144ba85709eSmrg    -m) mode=$2
145ba85709eSmrg	case $mode in
146ba85709eSmrg	  *' '* | *'	'* | *'
147ba85709eSmrg'*	  | *'*'* | *'?'* | *'['*)
148ba85709eSmrg	    echo "$0: invalid mode: $mode" >&2
149ba85709eSmrg	    exit 1;;
150ba85709eSmrg	esac
151786a6f21Smrg	shift;;
1521d54945dSmrg
1531d54945dSmrg    -o) chowncmd="$chownprog $2"
154786a6f21Smrg	shift;;
1551d54945dSmrg
156786a6f21Smrg    -s) stripcmd=$stripprog;;
1571d54945dSmrg
158786a6f21Smrg    -t) dst_arg=$2
159786a6f21Smrg	shift;;
1601d54945dSmrg
161786a6f21Smrg    -T) no_target_directory=true;;
1621d54945dSmrg
1631d54945dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1641d54945dSmrg
165ba85709eSmrg    --)	shift
1661d54945dSmrg	break;;
167ba85709eSmrg
168ba85709eSmrg    -*)	echo "$0: invalid option: $1" >&2
169ba85709eSmrg	exit 1;;
170ba85709eSmrg
171ba85709eSmrg    *)  break;;
1721d54945dSmrg  esac
173786a6f21Smrg  shift
1741d54945dSmrgdone
1751d54945dSmrg
176786a6f21Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177ba85709eSmrg  # When -d is used, all remaining arguments are directories to create.
178ba85709eSmrg  # When -t is used, the destination is already specified.
179ba85709eSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180ba85709eSmrg  for arg
181ba85709eSmrg  do
182786a6f21Smrg    if test -n "$dst_arg"; then
183ba85709eSmrg      # $@ is not empty: it contains at least $arg.
184786a6f21Smrg      set fnord "$@" "$dst_arg"
185ba85709eSmrg      shift # fnord
186ba85709eSmrg    fi
187ba85709eSmrg    shift # arg
188786a6f21Smrg    dst_arg=$arg
189ba85709eSmrg  done
190ba85709eSmrgfi
191ba85709eSmrg
192ba85709eSmrgif test $# -eq 0; then
1931d54945dSmrg  if test -z "$dir_arg"; then
1941d54945dSmrg    echo "$0: no input file specified." >&2
1951d54945dSmrg    exit 1
1961d54945dSmrg  fi
1971d54945dSmrg  # It's OK to call `install-sh -d' without argument.
1981d54945dSmrg  # This can happen when creating conditional directories.
1991d54945dSmrg  exit 0
2001d54945dSmrgfi
2011d54945dSmrg
202ba85709eSmrgif test -z "$dir_arg"; then
203ba85709eSmrg  trap '(exit $?); exit' 1 2 13 15
204ba85709eSmrg
205ba85709eSmrg  # Set umask so as not to create temps with too-generous modes.
206ba85709eSmrg  # However, 'strip' requires both read and write access to temps.
207ba85709eSmrg  case $mode in
208ba85709eSmrg    # Optimize common cases.
209ba85709eSmrg    *644) cp_umask=133;;
210ba85709eSmrg    *755) cp_umask=22;;
211ba85709eSmrg
212ba85709eSmrg    *[0-7])
213ba85709eSmrg      if test -z "$stripcmd"; then
214ba85709eSmrg	u_plus_rw=
215ba85709eSmrg      else
216ba85709eSmrg	u_plus_rw='% 200'
217ba85709eSmrg      fi
218ba85709eSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219ba85709eSmrg    *)
220ba85709eSmrg      if test -z "$stripcmd"; then
221ba85709eSmrg	u_plus_rw=
222ba85709eSmrg      else
223ba85709eSmrg	u_plus_rw=,u+rw
224ba85709eSmrg      fi
225ba85709eSmrg      cp_umask=$mode$u_plus_rw;;
226ba85709eSmrg  esac
227ba85709eSmrgfi
228ba85709eSmrg
2291d54945dSmrgfor src
2301d54945dSmrgdo
2311d54945dSmrg  # Protect names starting with `-'.
2321d54945dSmrg  case $src in
233786a6f21Smrg    -*) src=./$src;;
2341d54945dSmrg  esac
2351d54945dSmrg
2361d54945dSmrg  if test -n "$dir_arg"; then
2371d54945dSmrg    dst=$src
238ba85709eSmrg    dstdir=$dst
239ba85709eSmrg    test -d "$dstdir"
240ba85709eSmrg    dstdir_status=$?
2411d54945dSmrg  else
242ba85709eSmrg
2431d54945dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2441d54945dSmrg    # might cause directories to be created, which would be especially bad
2451d54945dSmrg    # if $src (and thus $dsttmp) contains '*'.
2461d54945dSmrg    if test ! -f "$src" && test ! -d "$src"; then
2471d54945dSmrg      echo "$0: $src does not exist." >&2
2481d54945dSmrg      exit 1
2491d54945dSmrg    fi
2501d54945dSmrg
251786a6f21Smrg    if test -z "$dst_arg"; then
2521d54945dSmrg      echo "$0: no destination specified." >&2
2531d54945dSmrg      exit 1
2541d54945dSmrg    fi
2551d54945dSmrg
256786a6f21Smrg    dst=$dst_arg
2571d54945dSmrg    # Protect names starting with `-'.
2581d54945dSmrg    case $dst in
259786a6f21Smrg      -*) dst=./$dst;;
2601d54945dSmrg    esac
2611d54945dSmrg
2621d54945dSmrg    # If destination is a directory, append the input filename; won't work
2631d54945dSmrg    # if double slashes aren't ignored.
2641d54945dSmrg    if test -d "$dst"; then
2651d54945dSmrg      if test -n "$no_target_directory"; then
266786a6f21Smrg	echo "$0: $dst_arg: Is a directory" >&2
2671d54945dSmrg	exit 1
2681d54945dSmrg      fi
269ba85709eSmrg      dstdir=$dst
270ba85709eSmrg      dst=$dstdir/`basename "$src"`
271ba85709eSmrg      dstdir_status=0
272ba85709eSmrg    else
273ba85709eSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274ba85709eSmrg      dstdir=`
275ba85709eSmrg	(dirname "$dst") 2>/dev/null ||
276ba85709eSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277ba85709eSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
278ba85709eSmrg	     X"$dst" : 'X\(//\)$' \| \
279ba85709eSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280ba85709eSmrg	echo X"$dst" |
281ba85709eSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282ba85709eSmrg		   s//\1/
283ba85709eSmrg		   q
284ba85709eSmrg		 }
285ba85709eSmrg		 /^X\(\/\/\)[^/].*/{
286ba85709eSmrg		   s//\1/
287ba85709eSmrg		   q
288ba85709eSmrg		 }
289ba85709eSmrg		 /^X\(\/\/\)$/{
290ba85709eSmrg		   s//\1/
291ba85709eSmrg		   q
292ba85709eSmrg		 }
293ba85709eSmrg		 /^X\(\/\).*/{
294ba85709eSmrg		   s//\1/
295ba85709eSmrg		   q
296ba85709eSmrg		 }
297ba85709eSmrg		 s/.*/./; q'
298ba85709eSmrg      `
299ba85709eSmrg
300ba85709eSmrg      test -d "$dstdir"
301ba85709eSmrg      dstdir_status=$?
3021d54945dSmrg    fi
3031d54945dSmrg  fi
3041d54945dSmrg
305ba85709eSmrg  obsolete_mkdir_used=false
306ba85709eSmrg
307ba85709eSmrg  if test $dstdir_status != 0; then
308ba85709eSmrg    case $posix_mkdir in
309ba85709eSmrg      '')
310ba85709eSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
311ba85709eSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
312ba85709eSmrg	umask=`umask`
313ba85709eSmrg	case $stripcmd.$umask in
314ba85709eSmrg	  # Optimize common cases.
315ba85709eSmrg	  *[2367][2367]) mkdir_umask=$umask;;
316ba85709eSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317ba85709eSmrg
318ba85709eSmrg	  *[0-7])
319ba85709eSmrg	    mkdir_umask=`expr $umask + 22 \
320ba85709eSmrg	      - $umask % 100 % 40 + $umask % 20 \
321ba85709eSmrg	      - $umask % 10 % 4 + $umask % 2
322ba85709eSmrg	    `;;
323ba85709eSmrg	  *) mkdir_umask=$umask,go-w;;
324ba85709eSmrg	esac
325ba85709eSmrg
326ba85709eSmrg	# With -d, create the new directory with the user-specified mode.
327ba85709eSmrg	# Otherwise, rely on $mkdir_umask.
328ba85709eSmrg	if test -n "$dir_arg"; then
329ba85709eSmrg	  mkdir_mode=-m$mode
330ba85709eSmrg	else
331ba85709eSmrg	  mkdir_mode=
332ba85709eSmrg	fi
333ba85709eSmrg
334ba85709eSmrg	posix_mkdir=false
335ba85709eSmrg	case $umask in
336ba85709eSmrg	  *[123567][0-7][0-7])
337ba85709eSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338ba85709eSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339ba85709eSmrg	    ;;
340ba85709eSmrg	  *)
341ba85709eSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342ba85709eSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343ba85709eSmrg
344ba85709eSmrg	    if (umask $mkdir_umask &&
345ba85709eSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346ba85709eSmrg	    then
347ba85709eSmrg	      if test -z "$dir_arg" || {
348ba85709eSmrg		   # Check for POSIX incompatibilities with -m.
349ba85709eSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350ba85709eSmrg		   # other-writeable bit of parent directory when it shouldn't.
351ba85709eSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352ba85709eSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353ba85709eSmrg		   case $ls_ld_tmpdir in
354ba85709eSmrg		     d????-?r-*) different_mode=700;;
355ba85709eSmrg		     d????-?--*) different_mode=755;;
356ba85709eSmrg		     *) false;;
357ba85709eSmrg		   esac &&
358ba85709eSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359ba85709eSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360ba85709eSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361ba85709eSmrg		   }
362ba85709eSmrg		 }
363ba85709eSmrg	      then posix_mkdir=:
364ba85709eSmrg	      fi
365ba85709eSmrg	      rmdir "$tmpdir/d" "$tmpdir"
366ba85709eSmrg	    else
367ba85709eSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
368ba85709eSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369ba85709eSmrg	    fi
370ba85709eSmrg	    trap '' 0;;
371ba85709eSmrg	esac;;
372ba85709eSmrg    esac
3731d54945dSmrg
374ba85709eSmrg    if
375ba85709eSmrg      $posix_mkdir && (
376ba85709eSmrg	umask $mkdir_umask &&
377ba85709eSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378ba85709eSmrg      )
379ba85709eSmrg    then :
380ba85709eSmrg    else
3811d54945dSmrg
382ba85709eSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383ba85709eSmrg      # or it failed possibly due to a race condition.  Create the
384ba85709eSmrg      # directory the slow way, step by step, checking for races as we go.
385ba85709eSmrg
386ba85709eSmrg      case $dstdir in
387786a6f21Smrg	/*) prefix='/';;
388786a6f21Smrg	-*) prefix='./';;
389786a6f21Smrg	*)  prefix='';;
390ba85709eSmrg      esac
391ba85709eSmrg
392786a6f21Smrg      eval "$initialize_posix_glob"
393ba85709eSmrg
394ba85709eSmrg      oIFS=$IFS
395ba85709eSmrg      IFS=/
396786a6f21Smrg      $posix_glob set -f
397ba85709eSmrg      set fnord $dstdir
3981d54945dSmrg      shift
399786a6f21Smrg      $posix_glob set +f
400ba85709eSmrg      IFS=$oIFS
401ba85709eSmrg
402ba85709eSmrg      prefixes=
403ba85709eSmrg
404ba85709eSmrg      for d
405ba85709eSmrg      do
406ba85709eSmrg	test -z "$d" && continue
407ba85709eSmrg
408ba85709eSmrg	prefix=$prefix$d
409ba85709eSmrg	if test -d "$prefix"; then
410ba85709eSmrg	  prefixes=
411ba85709eSmrg	else
412ba85709eSmrg	  if $posix_mkdir; then
413ba85709eSmrg	    (umask=$mkdir_umask &&
414ba85709eSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415ba85709eSmrg	    # Don't fail if two instances are running concurrently.
416ba85709eSmrg	    test -d "$prefix" || exit 1
417ba85709eSmrg	  else
418ba85709eSmrg	    case $prefix in
419ba85709eSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420ba85709eSmrg	      *) qprefix=$prefix;;
421ba85709eSmrg	    esac
422ba85709eSmrg	    prefixes="$prefixes '$qprefix'"
423ba85709eSmrg	  fi
424ba85709eSmrg	fi
425ba85709eSmrg	prefix=$prefix/
426ba85709eSmrg      done
427ba85709eSmrg
428ba85709eSmrg      if test -n "$prefixes"; then
429ba85709eSmrg	# Don't fail if two instances are running concurrently.
430ba85709eSmrg	(umask $mkdir_umask &&
431ba85709eSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432ba85709eSmrg	  test -d "$dstdir" || exit 1
433ba85709eSmrg	obsolete_mkdir_used=true
4341d54945dSmrg      fi
435ba85709eSmrg    fi
4361d54945dSmrg  fi
4371d54945dSmrg
4381d54945dSmrg  if test -n "$dir_arg"; then
439ba85709eSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440ba85709eSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441ba85709eSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442ba85709eSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4431d54945dSmrg  else
4441d54945dSmrg
4451d54945dSmrg    # Make a couple of temp file names in the proper directory.
4461d54945dSmrg    dsttmp=$dstdir/_inst.$$_
4471d54945dSmrg    rmtmp=$dstdir/_rm.$$_
4481d54945dSmrg
4491d54945dSmrg    # Trap to clean up those temp files at exit.
4501d54945dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4511d54945dSmrg
4521d54945dSmrg    # Copy the file name to the temp name.
453ba85709eSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4541d54945dSmrg
4551d54945dSmrg    # and set any options; do chmod last to preserve setuid bits.
4561d54945dSmrg    #
4571d54945dSmrg    # If any of these fail, we abort the whole thing.  If we want to
4581d54945dSmrg    # ignore errors from any of these, just make sure not to ignore
4591d54945dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4601d54945dSmrg    #
461786a6f21Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462786a6f21Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463786a6f21Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464786a6f21Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465786a6f21Smrg
466786a6f21Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467786a6f21Smrg    if $copy_on_change &&
468786a6f21Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469786a6f21Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470786a6f21Smrg
471786a6f21Smrg       eval "$initialize_posix_glob" &&
472786a6f21Smrg       $posix_glob set -f &&
473786a6f21Smrg       set X $old && old=:$2:$4:$5:$6 &&
474786a6f21Smrg       set X $new && new=:$2:$4:$5:$6 &&
475786a6f21Smrg       $posix_glob set +f &&
476786a6f21Smrg
477786a6f21Smrg       test "$old" = "$new" &&
478786a6f21Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479786a6f21Smrg    then
480786a6f21Smrg      rm -f "$dsttmp"
481786a6f21Smrg    else
482786a6f21Smrg      # Rename the file to the real destination.
483786a6f21Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484786a6f21Smrg
485786a6f21Smrg      # The rename failed, perhaps because mv can't rename something else
486786a6f21Smrg      # to itself, or perhaps because mv is so ancient that it does not
487786a6f21Smrg      # support -f.
488786a6f21Smrg      {
489786a6f21Smrg	# Now remove or move aside any old file at destination location.
490786a6f21Smrg	# We try this two ways since rm can't unlink itself on some
491786a6f21Smrg	# systems and the destination file might be busy for other
492786a6f21Smrg	# reasons.  In this case, the final cleanup might fail but the new
493786a6f21Smrg	# file should still install successfully.
494786a6f21Smrg	{
495786a6f21Smrg	  test ! -f "$dst" ||
496786a6f21Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497786a6f21Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498786a6f21Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499786a6f21Smrg	  } ||
500786a6f21Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
501786a6f21Smrg	    (exit 1); exit 1
502786a6f21Smrg	  }
503786a6f21Smrg	} &&
504786a6f21Smrg
505786a6f21Smrg	# Now rename the file to the real destination.
506786a6f21Smrg	$doit $mvcmd "$dsttmp" "$dst"
507786a6f21Smrg      }
508786a6f21Smrg    fi || exit 1
5091d54945dSmrg
510ba85709eSmrg    trap '' 0
511ba85709eSmrg  fi
512ba85709eSmrgdone
5131d54945dSmrg
5141d54945dSmrg# Local variables:
5151d54945dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5161d54945dSmrg# time-stamp-start: "scriptversion="
5171d54945dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518ec713c28Smrg# time-stamp-time-zone: "UTC"
519ec713c28Smrg# time-stamp-end: "; # UTC"
5201d54945dSmrg# End:
521