install-sh revision d769e936
11d54945dSmrg#!/bin/sh
21d54945dSmrg# install - install a program, script, or datafile
31d54945dSmrg
4d769e936Smrgscriptversion=2011-01-19.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
159d769e936Smrg	# Protect names problematic for `test' and other utilities.
160d769e936Smrg	case $dst_arg in
161d769e936Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162d769e936Smrg	esac
163786a6f21Smrg	shift;;
1641d54945dSmrg
165786a6f21Smrg    -T) no_target_directory=true;;
1661d54945dSmrg
1671d54945dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1681d54945dSmrg
169ba85709eSmrg    --)	shift
1701d54945dSmrg	break;;
171ba85709eSmrg
172ba85709eSmrg    -*)	echo "$0: invalid option: $1" >&2
173ba85709eSmrg	exit 1;;
174ba85709eSmrg
175ba85709eSmrg    *)  break;;
1761d54945dSmrg  esac
177786a6f21Smrg  shift
1781d54945dSmrgdone
1791d54945dSmrg
180786a6f21Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181ba85709eSmrg  # When -d is used, all remaining arguments are directories to create.
182ba85709eSmrg  # When -t is used, the destination is already specified.
183ba85709eSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184ba85709eSmrg  for arg
185ba85709eSmrg  do
186786a6f21Smrg    if test -n "$dst_arg"; then
187ba85709eSmrg      # $@ is not empty: it contains at least $arg.
188786a6f21Smrg      set fnord "$@" "$dst_arg"
189ba85709eSmrg      shift # fnord
190ba85709eSmrg    fi
191ba85709eSmrg    shift # arg
192786a6f21Smrg    dst_arg=$arg
193d769e936Smrg    # Protect names problematic for `test' and other utilities.
194d769e936Smrg    case $dst_arg in
195d769e936Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196d769e936Smrg    esac
197ba85709eSmrg  done
198ba85709eSmrgfi
199ba85709eSmrg
200ba85709eSmrgif test $# -eq 0; then
2011d54945dSmrg  if test -z "$dir_arg"; then
2021d54945dSmrg    echo "$0: no input file specified." >&2
2031d54945dSmrg    exit 1
2041d54945dSmrg  fi
2051d54945dSmrg  # It's OK to call `install-sh -d' without argument.
2061d54945dSmrg  # This can happen when creating conditional directories.
2071d54945dSmrg  exit 0
2081d54945dSmrgfi
2091d54945dSmrg
210ba85709eSmrgif test -z "$dir_arg"; then
211d769e936Smrg  do_exit='(exit $ret); exit $ret'
212d769e936Smrg  trap "ret=129; $do_exit" 1
213d769e936Smrg  trap "ret=130; $do_exit" 2
214d769e936Smrg  trap "ret=141; $do_exit" 13
215d769e936Smrg  trap "ret=143; $do_exit" 15
216ba85709eSmrg
217ba85709eSmrg  # Set umask so as not to create temps with too-generous modes.
218ba85709eSmrg  # However, 'strip' requires both read and write access to temps.
219ba85709eSmrg  case $mode in
220ba85709eSmrg    # Optimize common cases.
221ba85709eSmrg    *644) cp_umask=133;;
222ba85709eSmrg    *755) cp_umask=22;;
223ba85709eSmrg
224ba85709eSmrg    *[0-7])
225ba85709eSmrg      if test -z "$stripcmd"; then
226ba85709eSmrg	u_plus_rw=
227ba85709eSmrg      else
228ba85709eSmrg	u_plus_rw='% 200'
229ba85709eSmrg      fi
230ba85709eSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231ba85709eSmrg    *)
232ba85709eSmrg      if test -z "$stripcmd"; then
233ba85709eSmrg	u_plus_rw=
234ba85709eSmrg      else
235ba85709eSmrg	u_plus_rw=,u+rw
236ba85709eSmrg      fi
237ba85709eSmrg      cp_umask=$mode$u_plus_rw;;
238ba85709eSmrg  esac
239ba85709eSmrgfi
240ba85709eSmrg
2411d54945dSmrgfor src
2421d54945dSmrgdo
243d769e936Smrg  # Protect names problematic for `test' and other utilities.
2441d54945dSmrg  case $src in
245d769e936Smrg    -* | [=\(\)!]) src=./$src;;
2461d54945dSmrg  esac
2471d54945dSmrg
2481d54945dSmrg  if test -n "$dir_arg"; then
2491d54945dSmrg    dst=$src
250ba85709eSmrg    dstdir=$dst
251ba85709eSmrg    test -d "$dstdir"
252ba85709eSmrg    dstdir_status=$?
2531d54945dSmrg  else
254ba85709eSmrg
2551d54945dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2561d54945dSmrg    # might cause directories to be created, which would be especially bad
2571d54945dSmrg    # if $src (and thus $dsttmp) contains '*'.
2581d54945dSmrg    if test ! -f "$src" && test ! -d "$src"; then
2591d54945dSmrg      echo "$0: $src does not exist." >&2
2601d54945dSmrg      exit 1
2611d54945dSmrg    fi
2621d54945dSmrg
263786a6f21Smrg    if test -z "$dst_arg"; then
2641d54945dSmrg      echo "$0: no destination specified." >&2
2651d54945dSmrg      exit 1
2661d54945dSmrg    fi
267786a6f21Smrg    dst=$dst_arg
2681d54945dSmrg
2691d54945dSmrg    # If destination is a directory, append the input filename; won't work
2701d54945dSmrg    # if double slashes aren't ignored.
2711d54945dSmrg    if test -d "$dst"; then
2721d54945dSmrg      if test -n "$no_target_directory"; then
273786a6f21Smrg	echo "$0: $dst_arg: Is a directory" >&2
2741d54945dSmrg	exit 1
2751d54945dSmrg      fi
276ba85709eSmrg      dstdir=$dst
277ba85709eSmrg      dst=$dstdir/`basename "$src"`
278ba85709eSmrg      dstdir_status=0
279ba85709eSmrg    else
280ba85709eSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281ba85709eSmrg      dstdir=`
282ba85709eSmrg	(dirname "$dst") 2>/dev/null ||
283ba85709eSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284ba85709eSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
285ba85709eSmrg	     X"$dst" : 'X\(//\)$' \| \
286ba85709eSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287ba85709eSmrg	echo X"$dst" |
288ba85709eSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289ba85709eSmrg		   s//\1/
290ba85709eSmrg		   q
291ba85709eSmrg		 }
292ba85709eSmrg		 /^X\(\/\/\)[^/].*/{
293ba85709eSmrg		   s//\1/
294ba85709eSmrg		   q
295ba85709eSmrg		 }
296ba85709eSmrg		 /^X\(\/\/\)$/{
297ba85709eSmrg		   s//\1/
298ba85709eSmrg		   q
299ba85709eSmrg		 }
300ba85709eSmrg		 /^X\(\/\).*/{
301ba85709eSmrg		   s//\1/
302ba85709eSmrg		   q
303ba85709eSmrg		 }
304ba85709eSmrg		 s/.*/./; q'
305ba85709eSmrg      `
306ba85709eSmrg
307ba85709eSmrg      test -d "$dstdir"
308ba85709eSmrg      dstdir_status=$?
3091d54945dSmrg    fi
3101d54945dSmrg  fi
3111d54945dSmrg
312ba85709eSmrg  obsolete_mkdir_used=false
313ba85709eSmrg
314ba85709eSmrg  if test $dstdir_status != 0; then
315ba85709eSmrg    case $posix_mkdir in
316ba85709eSmrg      '')
317ba85709eSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
318ba85709eSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
319ba85709eSmrg	umask=`umask`
320ba85709eSmrg	case $stripcmd.$umask in
321ba85709eSmrg	  # Optimize common cases.
322ba85709eSmrg	  *[2367][2367]) mkdir_umask=$umask;;
323ba85709eSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324ba85709eSmrg
325ba85709eSmrg	  *[0-7])
326ba85709eSmrg	    mkdir_umask=`expr $umask + 22 \
327ba85709eSmrg	      - $umask % 100 % 40 + $umask % 20 \
328ba85709eSmrg	      - $umask % 10 % 4 + $umask % 2
329ba85709eSmrg	    `;;
330ba85709eSmrg	  *) mkdir_umask=$umask,go-w;;
331ba85709eSmrg	esac
332ba85709eSmrg
333ba85709eSmrg	# With -d, create the new directory with the user-specified mode.
334ba85709eSmrg	# Otherwise, rely on $mkdir_umask.
335ba85709eSmrg	if test -n "$dir_arg"; then
336ba85709eSmrg	  mkdir_mode=-m$mode
337ba85709eSmrg	else
338ba85709eSmrg	  mkdir_mode=
339ba85709eSmrg	fi
340ba85709eSmrg
341ba85709eSmrg	posix_mkdir=false
342ba85709eSmrg	case $umask in
343ba85709eSmrg	  *[123567][0-7][0-7])
344ba85709eSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345ba85709eSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346ba85709eSmrg	    ;;
347ba85709eSmrg	  *)
348ba85709eSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349ba85709eSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350ba85709eSmrg
351ba85709eSmrg	    if (umask $mkdir_umask &&
352ba85709eSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353ba85709eSmrg	    then
354ba85709eSmrg	      if test -z "$dir_arg" || {
355ba85709eSmrg		   # Check for POSIX incompatibilities with -m.
356ba85709eSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357ba85709eSmrg		   # other-writeable bit of parent directory when it shouldn't.
358ba85709eSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359ba85709eSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360ba85709eSmrg		   case $ls_ld_tmpdir in
361ba85709eSmrg		     d????-?r-*) different_mode=700;;
362ba85709eSmrg		     d????-?--*) different_mode=755;;
363ba85709eSmrg		     *) false;;
364ba85709eSmrg		   esac &&
365ba85709eSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366ba85709eSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367ba85709eSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368ba85709eSmrg		   }
369ba85709eSmrg		 }
370ba85709eSmrg	      then posix_mkdir=:
371ba85709eSmrg	      fi
372ba85709eSmrg	      rmdir "$tmpdir/d" "$tmpdir"
373ba85709eSmrg	    else
374ba85709eSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
375ba85709eSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376ba85709eSmrg	    fi
377ba85709eSmrg	    trap '' 0;;
378ba85709eSmrg	esac;;
379ba85709eSmrg    esac
3801d54945dSmrg
381ba85709eSmrg    if
382ba85709eSmrg      $posix_mkdir && (
383ba85709eSmrg	umask $mkdir_umask &&
384ba85709eSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385ba85709eSmrg      )
386ba85709eSmrg    then :
387ba85709eSmrg    else
3881d54945dSmrg
389ba85709eSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390ba85709eSmrg      # or it failed possibly due to a race condition.  Create the
391ba85709eSmrg      # directory the slow way, step by step, checking for races as we go.
392ba85709eSmrg
393ba85709eSmrg      case $dstdir in
394786a6f21Smrg	/*) prefix='/';;
395d769e936Smrg	[-=\(\)!]*) prefix='./';;
396786a6f21Smrg	*)  prefix='';;
397ba85709eSmrg      esac
398ba85709eSmrg
399786a6f21Smrg      eval "$initialize_posix_glob"
400ba85709eSmrg
401ba85709eSmrg      oIFS=$IFS
402ba85709eSmrg      IFS=/
403786a6f21Smrg      $posix_glob set -f
404ba85709eSmrg      set fnord $dstdir
4051d54945dSmrg      shift
406786a6f21Smrg      $posix_glob set +f
407ba85709eSmrg      IFS=$oIFS
408ba85709eSmrg
409ba85709eSmrg      prefixes=
410ba85709eSmrg
411ba85709eSmrg      for d
412ba85709eSmrg      do
413d769e936Smrg	test X"$d" = X && continue
414ba85709eSmrg
415ba85709eSmrg	prefix=$prefix$d
416ba85709eSmrg	if test -d "$prefix"; then
417ba85709eSmrg	  prefixes=
418ba85709eSmrg	else
419ba85709eSmrg	  if $posix_mkdir; then
420ba85709eSmrg	    (umask=$mkdir_umask &&
421ba85709eSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422ba85709eSmrg	    # Don't fail if two instances are running concurrently.
423ba85709eSmrg	    test -d "$prefix" || exit 1
424ba85709eSmrg	  else
425ba85709eSmrg	    case $prefix in
426ba85709eSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427ba85709eSmrg	      *) qprefix=$prefix;;
428ba85709eSmrg	    esac
429ba85709eSmrg	    prefixes="$prefixes '$qprefix'"
430ba85709eSmrg	  fi
431ba85709eSmrg	fi
432ba85709eSmrg	prefix=$prefix/
433ba85709eSmrg      done
434ba85709eSmrg
435ba85709eSmrg      if test -n "$prefixes"; then
436ba85709eSmrg	# Don't fail if two instances are running concurrently.
437ba85709eSmrg	(umask $mkdir_umask &&
438ba85709eSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439ba85709eSmrg	  test -d "$dstdir" || exit 1
440ba85709eSmrg	obsolete_mkdir_used=true
4411d54945dSmrg      fi
442ba85709eSmrg    fi
4431d54945dSmrg  fi
4441d54945dSmrg
4451d54945dSmrg  if test -n "$dir_arg"; then
446ba85709eSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447ba85709eSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448ba85709eSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449ba85709eSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4501d54945dSmrg  else
4511d54945dSmrg
4521d54945dSmrg    # Make a couple of temp file names in the proper directory.
4531d54945dSmrg    dsttmp=$dstdir/_inst.$$_
4541d54945dSmrg    rmtmp=$dstdir/_rm.$$_
4551d54945dSmrg
4561d54945dSmrg    # Trap to clean up those temp files at exit.
4571d54945dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4581d54945dSmrg
4591d54945dSmrg    # Copy the file name to the temp name.
460ba85709eSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4611d54945dSmrg
4621d54945dSmrg    # and set any options; do chmod last to preserve setuid bits.
4631d54945dSmrg    #
4641d54945dSmrg    # If any of these fail, we abort the whole thing.  If we want to
4651d54945dSmrg    # ignore errors from any of these, just make sure not to ignore
4661d54945dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4671d54945dSmrg    #
468786a6f21Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469786a6f21Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470786a6f21Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471786a6f21Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472786a6f21Smrg
473786a6f21Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474786a6f21Smrg    if $copy_on_change &&
475786a6f21Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476786a6f21Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477786a6f21Smrg
478786a6f21Smrg       eval "$initialize_posix_glob" &&
479786a6f21Smrg       $posix_glob set -f &&
480786a6f21Smrg       set X $old && old=:$2:$4:$5:$6 &&
481786a6f21Smrg       set X $new && new=:$2:$4:$5:$6 &&
482786a6f21Smrg       $posix_glob set +f &&
483786a6f21Smrg
484786a6f21Smrg       test "$old" = "$new" &&
485786a6f21Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486786a6f21Smrg    then
487786a6f21Smrg      rm -f "$dsttmp"
488786a6f21Smrg    else
489786a6f21Smrg      # Rename the file to the real destination.
490786a6f21Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491786a6f21Smrg
492786a6f21Smrg      # The rename failed, perhaps because mv can't rename something else
493786a6f21Smrg      # to itself, or perhaps because mv is so ancient that it does not
494786a6f21Smrg      # support -f.
495786a6f21Smrg      {
496786a6f21Smrg	# Now remove or move aside any old file at destination location.
497786a6f21Smrg	# We try this two ways since rm can't unlink itself on some
498786a6f21Smrg	# systems and the destination file might be busy for other
499786a6f21Smrg	# reasons.  In this case, the final cleanup might fail but the new
500786a6f21Smrg	# file should still install successfully.
501786a6f21Smrg	{
502786a6f21Smrg	  test ! -f "$dst" ||
503786a6f21Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504786a6f21Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505786a6f21Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506786a6f21Smrg	  } ||
507786a6f21Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
508786a6f21Smrg	    (exit 1); exit 1
509786a6f21Smrg	  }
510786a6f21Smrg	} &&
511786a6f21Smrg
512786a6f21Smrg	# Now rename the file to the real destination.
513786a6f21Smrg	$doit $mvcmd "$dsttmp" "$dst"
514786a6f21Smrg      }
515786a6f21Smrg    fi || exit 1
5161d54945dSmrg
517ba85709eSmrg    trap '' 0
518ba85709eSmrg  fi
519ba85709eSmrgdone
5201d54945dSmrg
5211d54945dSmrg# Local variables:
5221d54945dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5231d54945dSmrg# time-stamp-start: "scriptversion="
5241d54945dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525ec713c28Smrg# time-stamp-time-zone: "UTC"
526ec713c28Smrg# time-stamp-end: "; # UTC"
5271d54945dSmrg# End:
528