install-sh revision 24047306
114c0a534Smrg#!/bin/sh
214c0a534Smrg# install - install a program, script, or datafile
314c0a534Smrg
424047306Smrgscriptversion=2013-12-25.23; # UTC
514c0a534Smrg
614c0a534Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
714c0a534Smrg# later released in X11R6 (xc/config/util/install.sh) with the
814c0a534Smrg# following copyright and license.
914c0a534Smrg#
1014c0a534Smrg# Copyright (C) 1994 X Consortium
1114c0a534Smrg#
1214c0a534Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1314c0a534Smrg# of this software and associated documentation files (the "Software"), to
1414c0a534Smrg# deal in the Software without restriction, including without limitation the
1514c0a534Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1614c0a534Smrg# sell copies of the Software, and to permit persons to whom the Software is
1714c0a534Smrg# furnished to do so, subject to the following conditions:
1814c0a534Smrg#
1914c0a534Smrg# The above copyright notice and this permission notice shall be included in
2014c0a534Smrg# all copies or substantial portions of the Software.
2114c0a534Smrg#
2214c0a534Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2314c0a534Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2414c0a534Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2514c0a534Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2614c0a534Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2714c0a534Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2814c0a534Smrg#
2914c0a534Smrg# Except as contained in this notice, the name of the X Consortium shall not
3014c0a534Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3114c0a534Smrg# ings in this Software without prior written authorization from the X Consor-
3214c0a534Smrg# tium.
3314c0a534Smrg#
3414c0a534Smrg#
3514c0a534Smrg# FSF changes to this file are in the public domain.
3614c0a534Smrg#
3714c0a534Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3824047306Smrg# 'make' implicit rules from creating a file called install from it
3914c0a534Smrg# when there is no Makefile.
4014c0a534Smrg#
4114c0a534Smrg# This script is compatible with the BSD install script, but was written
42bf2eeab3Smrg# from scratch.
43bf2eeab3Smrg
4424047306Smrgtab='	'
45bf2eeab3Smrgnl='
46bf2eeab3Smrg'
4724047306SmrgIFS=" $tab$nl"
4814c0a534Smrg
4924047306Smrg# Set DOITPROG to "echo" to test this script.
5014c0a534Smrg
51bf2eeab3Smrgdoit=${DOITPROG-}
5224047306Smrgdoit_exec=${doit:-exec}
5314c0a534Smrg
54bf2eeab3Smrg# Put in absolute file names if you don't have them in your path;
55bf2eeab3Smrg# or use environment vars.
56bf2eeab3Smrg
57bf2eeab3Smrgchgrpprog=${CHGRPPROG-chgrp}
58bf2eeab3Smrgchmodprog=${CHMODPROG-chmod}
59bf2eeab3Smrgchownprog=${CHOWNPROG-chown}
60bf2eeab3Smrgcmpprog=${CMPPROG-cmp}
61bf2eeab3Smrgcpprog=${CPPROG-cp}
62bf2eeab3Smrgmkdirprog=${MKDIRPROG-mkdir}
63bf2eeab3Smrgmvprog=${MVPROG-mv}
64bf2eeab3Smrgrmprog=${RMPROG-rm}
65bf2eeab3Smrgstripprog=${STRIPPROG-strip}
66bf2eeab3Smrg
67bf2eeab3Smrgposix_mkdir=
68bf2eeab3Smrg
69bf2eeab3Smrg# Desired mode of installed file.
70bf2eeab3Smrgmode=0755
7114c0a534Smrg
7214c0a534Smrgchgrpcmd=
73bf2eeab3Smrgchmodcmd=$chmodprog
74bf2eeab3Smrgchowncmd=
75bf2eeab3Smrgmvcmd=$mvprog
7614c0a534Smrgrmcmd="$rmprog -f"
77bf2eeab3Smrgstripcmd=
78bf2eeab3Smrg
7914c0a534Smrgsrc=
8014c0a534Smrgdst=
8114c0a534Smrgdir_arg=
82bf2eeab3Smrgdst_arg=
83bf2eeab3Smrg
84bf2eeab3Smrgcopy_on_change=false
8524047306Smrgis_target_a_directory=possibly
8614c0a534Smrg
87bf2eeab3Smrgusage="\
88bf2eeab3SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8914c0a534Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9014c0a534Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9114c0a534Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9214c0a534Smrg
9314c0a534SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9414c0a534SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9514c0a534SmrgIn the 4th, create DIRECTORIES.
9614c0a534Smrg
9714c0a534SmrgOptions:
98bf2eeab3Smrg     --help     display this help and exit.
99bf2eeab3Smrg     --version  display version info and exit.
100bf2eeab3Smrg
101bf2eeab3Smrg  -c            (ignored)
102bf2eeab3Smrg  -C            install only if different (preserve the last data modification time)
103bf2eeab3Smrg  -d            create directories instead of installing files.
104bf2eeab3Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105bf2eeab3Smrg  -m MODE       $chmodprog installed files to MODE.
106bf2eeab3Smrg  -o USER       $chownprog installed files to USER.
107bf2eeab3Smrg  -s            $stripprog installed files.
108bf2eeab3Smrg  -t DIRECTORY  install into DIRECTORY.
109bf2eeab3Smrg  -T            report an error if DSTFILE is a directory.
11014c0a534Smrg
11114c0a534SmrgEnvironment variables override the default commands:
112bf2eeab3Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113bf2eeab3Smrg  RMPROG STRIPPROG
11414c0a534Smrg"
11514c0a534Smrg
116bf2eeab3Smrgwhile test $# -ne 0; do
11714c0a534Smrg  case $1 in
118bf2eeab3Smrg    -c) ;;
119bf2eeab3Smrg
120bf2eeab3Smrg    -C) copy_on_change=true;;
12114c0a534Smrg
122bf2eeab3Smrg    -d) dir_arg=true;;
12314c0a534Smrg
12414c0a534Smrg    -g) chgrpcmd="$chgrpprog $2"
12524047306Smrg        shift;;
12614c0a534Smrg
12714c0a534Smrg    --help) echo "$usage"; exit $?;;
12814c0a534Smrg
129bf2eeab3Smrg    -m) mode=$2
13024047306Smrg        case $mode in
13124047306Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13224047306Smrg            echo "$0: invalid mode: $mode" >&2
13324047306Smrg            exit 1;;
13424047306Smrg        esac
13524047306Smrg        shift;;
13614c0a534Smrg
13714c0a534Smrg    -o) chowncmd="$chownprog $2"
13824047306Smrg        shift;;
13914c0a534Smrg
140bf2eeab3Smrg    -s) stripcmd=$stripprog;;
14114c0a534Smrg
14224047306Smrg    -t)
14324047306Smrg        is_target_a_directory=always
14424047306Smrg        dst_arg=$2
14524047306Smrg        # Protect names problematic for 'test' and other utilities.
14624047306Smrg        case $dst_arg in
14724047306Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14824047306Smrg        esac
14924047306Smrg        shift;;
15014c0a534Smrg
15124047306Smrg    -T) is_target_a_directory=never;;
15214c0a534Smrg
15314c0a534Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15414c0a534Smrg
15524047306Smrg    --) shift
15624047306Smrg        break;;
157bf2eeab3Smrg
15824047306Smrg    -*) echo "$0: invalid option: $1" >&2
15924047306Smrg        exit 1;;
160bf2eeab3Smrg
161bf2eeab3Smrg    *)  break;;
16214c0a534Smrg  esac
163bf2eeab3Smrg  shift
16414c0a534Smrgdone
16514c0a534Smrg
16624047306Smrg# We allow the use of options -d and -T together, by making -d
16724047306Smrg# take the precedence; this is for compatibility with GNU install.
16824047306Smrg
16924047306Smrgif test -n "$dir_arg"; then
17024047306Smrg  if test -n "$dst_arg"; then
17124047306Smrg    echo "$0: target directory not allowed when installing a directory." >&2
17224047306Smrg    exit 1
17324047306Smrg  fi
17424047306Smrgfi
17524047306Smrg
176bf2eeab3Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177bf2eeab3Smrg  # When -d is used, all remaining arguments are directories to create.
178bf2eeab3Smrg  # When -t is used, the destination is already specified.
179bf2eeab3Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180bf2eeab3Smrg  for arg
181bf2eeab3Smrg  do
182bf2eeab3Smrg    if test -n "$dst_arg"; then
183bf2eeab3Smrg      # $@ is not empty: it contains at least $arg.
184bf2eeab3Smrg      set fnord "$@" "$dst_arg"
185bf2eeab3Smrg      shift # fnord
186bf2eeab3Smrg    fi
187bf2eeab3Smrg    shift # arg
188bf2eeab3Smrg    dst_arg=$arg
18924047306Smrg    # Protect names problematic for 'test' and other utilities.
19024047306Smrg    case $dst_arg in
19124047306Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19224047306Smrg    esac
193bf2eeab3Smrg  done
194bf2eeab3Smrgfi
195bf2eeab3Smrg
196bf2eeab3Smrgif test $# -eq 0; then
19714c0a534Smrg  if test -z "$dir_arg"; then
19814c0a534Smrg    echo "$0: no input file specified." >&2
19914c0a534Smrg    exit 1
20014c0a534Smrg  fi
20124047306Smrg  # It's OK to call 'install-sh -d' without argument.
20214c0a534Smrg  # This can happen when creating conditional directories.
20314c0a534Smrg  exit 0
20414c0a534Smrgfi
20514c0a534Smrg
206bf2eeab3Smrgif test -z "$dir_arg"; then
20724047306Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20824047306Smrg    if test ! -d "$dst_arg"; then
20924047306Smrg      echo "$0: $dst_arg: Is not a directory." >&2
21024047306Smrg      exit 1
21124047306Smrg    fi
21224047306Smrg  fi
21324047306Smrgfi
21424047306Smrg
21524047306Smrgif test -z "$dir_arg"; then
21624047306Smrg  do_exit='(exit $ret); exit $ret'
21724047306Smrg  trap "ret=129; $do_exit" 1
21824047306Smrg  trap "ret=130; $do_exit" 2
21924047306Smrg  trap "ret=141; $do_exit" 13
22024047306Smrg  trap "ret=143; $do_exit" 15
221bf2eeab3Smrg
222bf2eeab3Smrg  # Set umask so as not to create temps with too-generous modes.
223bf2eeab3Smrg  # However, 'strip' requires both read and write access to temps.
224bf2eeab3Smrg  case $mode in
225bf2eeab3Smrg    # Optimize common cases.
226bf2eeab3Smrg    *644) cp_umask=133;;
227bf2eeab3Smrg    *755) cp_umask=22;;
228bf2eeab3Smrg
229bf2eeab3Smrg    *[0-7])
230bf2eeab3Smrg      if test -z "$stripcmd"; then
23124047306Smrg        u_plus_rw=
232bf2eeab3Smrg      else
23324047306Smrg        u_plus_rw='% 200'
234bf2eeab3Smrg      fi
235bf2eeab3Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236bf2eeab3Smrg    *)
237bf2eeab3Smrg      if test -z "$stripcmd"; then
23824047306Smrg        u_plus_rw=
239bf2eeab3Smrg      else
24024047306Smrg        u_plus_rw=,u+rw
241bf2eeab3Smrg      fi
242bf2eeab3Smrg      cp_umask=$mode$u_plus_rw;;
243bf2eeab3Smrg  esac
244bf2eeab3Smrgfi
245bf2eeab3Smrg
24614c0a534Smrgfor src
24714c0a534Smrgdo
24824047306Smrg  # Protect names problematic for 'test' and other utilities.
24914c0a534Smrg  case $src in
25024047306Smrg    -* | [=\(\)!]) src=./$src;;
25114c0a534Smrg  esac
25214c0a534Smrg
25314c0a534Smrg  if test -n "$dir_arg"; then
25414c0a534Smrg    dst=$src
255bf2eeab3Smrg    dstdir=$dst
256bf2eeab3Smrg    test -d "$dstdir"
257bf2eeab3Smrg    dstdir_status=$?
25814c0a534Smrg  else
259bf2eeab3Smrg
26014c0a534Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26114c0a534Smrg    # might cause directories to be created, which would be especially bad
26214c0a534Smrg    # if $src (and thus $dsttmp) contains '*'.
26314c0a534Smrg    if test ! -f "$src" && test ! -d "$src"; then
26414c0a534Smrg      echo "$0: $src does not exist." >&2
26514c0a534Smrg      exit 1
26614c0a534Smrg    fi
26714c0a534Smrg
268bf2eeab3Smrg    if test -z "$dst_arg"; then
26914c0a534Smrg      echo "$0: no destination specified." >&2
27014c0a534Smrg      exit 1
27114c0a534Smrg    fi
272bf2eeab3Smrg    dst=$dst_arg
27314c0a534Smrg
27414c0a534Smrg    # If destination is a directory, append the input filename; won't work
27514c0a534Smrg    # if double slashes aren't ignored.
27614c0a534Smrg    if test -d "$dst"; then
27724047306Smrg      if test "$is_target_a_directory" = never; then
27824047306Smrg        echo "$0: $dst_arg: Is a directory" >&2
27924047306Smrg        exit 1
28014c0a534Smrg      fi
281bf2eeab3Smrg      dstdir=$dst
282bf2eeab3Smrg      dst=$dstdir/`basename "$src"`
283bf2eeab3Smrg      dstdir_status=0
284bf2eeab3Smrg    else
28524047306Smrg      dstdir=`dirname "$dst"`
286bf2eeab3Smrg      test -d "$dstdir"
287bf2eeab3Smrg      dstdir_status=$?
28814c0a534Smrg    fi
28914c0a534Smrg  fi
29014c0a534Smrg
291bf2eeab3Smrg  obsolete_mkdir_used=false
292bf2eeab3Smrg
293bf2eeab3Smrg  if test $dstdir_status != 0; then
294bf2eeab3Smrg    case $posix_mkdir in
295bf2eeab3Smrg      '')
29624047306Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
29724047306Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
29824047306Smrg        umask=`umask`
29924047306Smrg        case $stripcmd.$umask in
30024047306Smrg          # Optimize common cases.
30124047306Smrg          *[2367][2367]) mkdir_umask=$umask;;
30224047306Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30324047306Smrg
30424047306Smrg          *[0-7])
30524047306Smrg            mkdir_umask=`expr $umask + 22 \
30624047306Smrg              - $umask % 100 % 40 + $umask % 20 \
30724047306Smrg              - $umask % 10 % 4 + $umask % 2
30824047306Smrg            `;;
30924047306Smrg          *) mkdir_umask=$umask,go-w;;
31024047306Smrg        esac
31124047306Smrg
31224047306Smrg        # With -d, create the new directory with the user-specified mode.
31324047306Smrg        # Otherwise, rely on $mkdir_umask.
31424047306Smrg        if test -n "$dir_arg"; then
31524047306Smrg          mkdir_mode=-m$mode
31624047306Smrg        else
31724047306Smrg          mkdir_mode=
31824047306Smrg        fi
31924047306Smrg
32024047306Smrg        posix_mkdir=false
32124047306Smrg        case $umask in
32224047306Smrg          *[123567][0-7][0-7])
32324047306Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32424047306Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32524047306Smrg            ;;
32624047306Smrg          *)
32724047306Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
32824047306Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
32924047306Smrg
33024047306Smrg            if (umask $mkdir_umask &&
33124047306Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
33224047306Smrg            then
33324047306Smrg              if test -z "$dir_arg" || {
33424047306Smrg                   # Check for POSIX incompatibilities with -m.
33524047306Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
33624047306Smrg                   # other-writable bit of parent directory when it shouldn't.
33724047306Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
33824047306Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
33924047306Smrg                   case $ls_ld_tmpdir in
34024047306Smrg                     d????-?r-*) different_mode=700;;
34124047306Smrg                     d????-?--*) different_mode=755;;
34224047306Smrg                     *) false;;
34324047306Smrg                   esac &&
34424047306Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
34524047306Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
34624047306Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
34724047306Smrg                   }
34824047306Smrg                 }
34924047306Smrg              then posix_mkdir=:
35024047306Smrg              fi
35124047306Smrg              rmdir "$tmpdir/d" "$tmpdir"
35224047306Smrg            else
35324047306Smrg              # Remove any dirs left behind by ancient mkdir implementations.
35424047306Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
35524047306Smrg            fi
35624047306Smrg            trap '' 0;;
35724047306Smrg        esac;;
358bf2eeab3Smrg    esac
35914c0a534Smrg
360bf2eeab3Smrg    if
361bf2eeab3Smrg      $posix_mkdir && (
36224047306Smrg        umask $mkdir_umask &&
36324047306Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364bf2eeab3Smrg      )
365bf2eeab3Smrg    then :
366bf2eeab3Smrg    else
36714c0a534Smrg
368bf2eeab3Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369bf2eeab3Smrg      # or it failed possibly due to a race condition.  Create the
370bf2eeab3Smrg      # directory the slow way, step by step, checking for races as we go.
37114c0a534Smrg
372bf2eeab3Smrg      case $dstdir in
37324047306Smrg        /*) prefix='/';;
37424047306Smrg        [-=\(\)!]*) prefix='./';;
37524047306Smrg        *)  prefix='';;
376bf2eeab3Smrg      esac
37714c0a534Smrg
378bf2eeab3Smrg      oIFS=$IFS
379bf2eeab3Smrg      IFS=/
38024047306Smrg      set -f
381bf2eeab3Smrg      set fnord $dstdir
38214c0a534Smrg      shift
38324047306Smrg      set +f
384bf2eeab3Smrg      IFS=$oIFS
385bf2eeab3Smrg
386bf2eeab3Smrg      prefixes=
387bf2eeab3Smrg
388bf2eeab3Smrg      for d
389bf2eeab3Smrg      do
39024047306Smrg        test X"$d" = X && continue
39124047306Smrg
39224047306Smrg        prefix=$prefix$d
39324047306Smrg        if test -d "$prefix"; then
39424047306Smrg          prefixes=
39524047306Smrg        else
39624047306Smrg          if $posix_mkdir; then
39724047306Smrg            (umask=$mkdir_umask &&
39824047306Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
39924047306Smrg            # Don't fail if two instances are running concurrently.
40024047306Smrg            test -d "$prefix" || exit 1
40124047306Smrg          else
40224047306Smrg            case $prefix in
40324047306Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
40424047306Smrg              *) qprefix=$prefix;;
40524047306Smrg            esac
40624047306Smrg            prefixes="$prefixes '$qprefix'"
40724047306Smrg          fi
40824047306Smrg        fi
40924047306Smrg        prefix=$prefix/
410bf2eeab3Smrg      done
411bf2eeab3Smrg
412bf2eeab3Smrg      if test -n "$prefixes"; then
41324047306Smrg        # Don't fail if two instances are running concurrently.
41424047306Smrg        (umask $mkdir_umask &&
41524047306Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
41624047306Smrg          test -d "$dstdir" || exit 1
41724047306Smrg        obsolete_mkdir_used=true
41814c0a534Smrg      fi
419bf2eeab3Smrg    fi
42014c0a534Smrg  fi
42114c0a534Smrg
42214c0a534Smrg  if test -n "$dir_arg"; then
423bf2eeab3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424bf2eeab3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425bf2eeab3Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426bf2eeab3Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
42714c0a534Smrg  else
42814c0a534Smrg
42914c0a534Smrg    # Make a couple of temp file names in the proper directory.
43014c0a534Smrg    dsttmp=$dstdir/_inst.$$_
43114c0a534Smrg    rmtmp=$dstdir/_rm.$$_
43214c0a534Smrg
43314c0a534Smrg    # Trap to clean up those temp files at exit.
43414c0a534Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
43514c0a534Smrg
43614c0a534Smrg    # Copy the file name to the temp name.
437bf2eeab3Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
43814c0a534Smrg
43914c0a534Smrg    # and set any options; do chmod last to preserve setuid bits.
44014c0a534Smrg    #
44114c0a534Smrg    # If any of these fail, we abort the whole thing.  If we want to
44214c0a534Smrg    # ignore errors from any of these, just make sure not to ignore
44314c0a534Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
44414c0a534Smrg    #
445bf2eeab3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446bf2eeab3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447bf2eeab3Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448bf2eeab3Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449bf2eeab3Smrg
450bf2eeab3Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451bf2eeab3Smrg    if $copy_on_change &&
45224047306Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
45324047306Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
45424047306Smrg       set -f &&
455bf2eeab3Smrg       set X $old && old=:$2:$4:$5:$6 &&
456bf2eeab3Smrg       set X $new && new=:$2:$4:$5:$6 &&
45724047306Smrg       set +f &&
458bf2eeab3Smrg       test "$old" = "$new" &&
459bf2eeab3Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460bf2eeab3Smrg    then
461bf2eeab3Smrg      rm -f "$dsttmp"
462bf2eeab3Smrg    else
463bf2eeab3Smrg      # Rename the file to the real destination.
464bf2eeab3Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465bf2eeab3Smrg
466bf2eeab3Smrg      # The rename failed, perhaps because mv can't rename something else
467bf2eeab3Smrg      # to itself, or perhaps because mv is so ancient that it does not
468bf2eeab3Smrg      # support -f.
469bf2eeab3Smrg      {
47024047306Smrg        # Now remove or move aside any old file at destination location.
47124047306Smrg        # We try this two ways since rm can't unlink itself on some
47224047306Smrg        # systems and the destination file might be busy for other
47324047306Smrg        # reasons.  In this case, the final cleanup might fail but the new
47424047306Smrg        # file should still install successfully.
47524047306Smrg        {
47624047306Smrg          test ! -f "$dst" ||
47724047306Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
47824047306Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
47924047306Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48024047306Smrg          } ||
48124047306Smrg          { echo "$0: cannot unlink or rename $dst" >&2
48224047306Smrg            (exit 1); exit 1
48324047306Smrg          }
48424047306Smrg        } &&
48524047306Smrg
48624047306Smrg        # Now rename the file to the real destination.
48724047306Smrg        $doit $mvcmd "$dsttmp" "$dst"
488bf2eeab3Smrg      }
489bf2eeab3Smrg    fi || exit 1
490bf2eeab3Smrg
491bf2eeab3Smrg    trap '' 0
492bf2eeab3Smrg  fi
49314c0a534Smrgdone
49414c0a534Smrg
49514c0a534Smrg# Local variables:
49614c0a534Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
49714c0a534Smrg# time-stamp-start: "scriptversion="
49814c0a534Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499bf2eeab3Smrg# time-stamp-time-zone: "UTC"
500bf2eeab3Smrg# time-stamp-end: "; # UTC"
50114c0a534Smrg# End:
502