install-sh revision 4a908991
1a0d3b6eaSmrg#!/bin/sh
2a0d3b6eaSmrg# install - install a program, script, or datafile
3a0d3b6eaSmrg
44a908991Smrgscriptversion=2013-12-25.23; # UTC
5a0d3b6eaSmrg
6a0d3b6eaSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a0d3b6eaSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8a0d3b6eaSmrg# following copyright and license.
9a0d3b6eaSmrg#
10a0d3b6eaSmrg# Copyright (C) 1994 X Consortium
11a0d3b6eaSmrg#
12a0d3b6eaSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a0d3b6eaSmrg# of this software and associated documentation files (the "Software"), to
14a0d3b6eaSmrg# deal in the Software without restriction, including without limitation the
15a0d3b6eaSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a0d3b6eaSmrg# sell copies of the Software, and to permit persons to whom the Software is
17a0d3b6eaSmrg# furnished to do so, subject to the following conditions:
18a0d3b6eaSmrg#
19a0d3b6eaSmrg# The above copyright notice and this permission notice shall be included in
20a0d3b6eaSmrg# all copies or substantial portions of the Software.
21a0d3b6eaSmrg#
22a0d3b6eaSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a0d3b6eaSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a0d3b6eaSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a0d3b6eaSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a0d3b6eaSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a0d3b6eaSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a0d3b6eaSmrg#
29a0d3b6eaSmrg# Except as contained in this notice, the name of the X Consortium shall not
30a0d3b6eaSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a0d3b6eaSmrg# ings in this Software without prior written authorization from the X Consor-
32a0d3b6eaSmrg# tium.
33a0d3b6eaSmrg#
34a0d3b6eaSmrg#
35a0d3b6eaSmrg# FSF changes to this file are in the public domain.
36a0d3b6eaSmrg#
37a0d3b6eaSmrg# Calling this script install-sh is preferred over install.sh, to prevent
384a908991Smrg# 'make' implicit rules from creating a file called install from it
39a0d3b6eaSmrg# when there is no Makefile.
40a0d3b6eaSmrg#
41a0d3b6eaSmrg# This script is compatible with the BSD install script, but was written
42a0d3b6eaSmrg# from scratch.
43a0d3b6eaSmrg
444a908991Smrgtab='	'
45a0d3b6eaSmrgnl='
46a0d3b6eaSmrg'
474a908991SmrgIFS=" $tab$nl"
48a0d3b6eaSmrg
494a908991Smrg# Set DOITPROG to "echo" to test this script.
50a0d3b6eaSmrg
51f8a31ee4Smrgdoit=${DOITPROG-}
524a908991Smrgdoit_exec=${doit:-exec}
53a0d3b6eaSmrg
54a0d3b6eaSmrg# Put in absolute file names if you don't have them in your path;
55a0d3b6eaSmrg# or use environment vars.
56a0d3b6eaSmrg
57f8a31ee4Smrgchgrpprog=${CHGRPPROG-chgrp}
58f8a31ee4Smrgchmodprog=${CHMODPROG-chmod}
59f8a31ee4Smrgchownprog=${CHOWNPROG-chown}
60f8a31ee4Smrgcmpprog=${CMPPROG-cmp}
61f8a31ee4Smrgcpprog=${CPPROG-cp}
62f8a31ee4Smrgmkdirprog=${MKDIRPROG-mkdir}
63f8a31ee4Smrgmvprog=${MVPROG-mv}
64f8a31ee4Smrgrmprog=${RMPROG-rm}
65f8a31ee4Smrgstripprog=${STRIPPROG-strip}
66f8a31ee4Smrg
67a0d3b6eaSmrgposix_mkdir=
68a0d3b6eaSmrg
69a0d3b6eaSmrg# Desired mode of installed file.
70a0d3b6eaSmrgmode=0755
71a0d3b6eaSmrg
72f8a31ee4Smrgchgrpcmd=
73a0d3b6eaSmrgchmodcmd=$chmodprog
74a0d3b6eaSmrgchowncmd=
75f8a31ee4Smrgmvcmd=$mvprog
7662770414Smrgrmcmd="$rmprog -f"
77f8a31ee4Smrgstripcmd=
78f8a31ee4Smrg
79a0d3b6eaSmrgsrc=
80a0d3b6eaSmrgdst=
81a0d3b6eaSmrgdir_arg=
82f8a31ee4Smrgdst_arg=
83f8a31ee4Smrg
84f8a31ee4Smrgcopy_on_change=false
854a908991Smrgis_target_a_directory=possibly
86a0d3b6eaSmrg
87f8a31ee4Smrgusage="\
88f8a31ee4SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89a0d3b6eaSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90a0d3b6eaSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91a0d3b6eaSmrg   or: $0 [OPTION]... -d DIRECTORIES...
92a0d3b6eaSmrg
93a0d3b6eaSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94a0d3b6eaSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95a0d3b6eaSmrgIn the 4th, create DIRECTORIES.
96a0d3b6eaSmrg
97a0d3b6eaSmrgOptions:
98f8a31ee4Smrg     --help     display this help and exit.
99f8a31ee4Smrg     --version  display version info and exit.
100f8a31ee4Smrg
101f8a31ee4Smrg  -c            (ignored)
102f8a31ee4Smrg  -C            install only if different (preserve the last data modification time)
103f8a31ee4Smrg  -d            create directories instead of installing files.
104f8a31ee4Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105f8a31ee4Smrg  -m MODE       $chmodprog installed files to MODE.
106f8a31ee4Smrg  -o USER       $chownprog installed files to USER.
107f8a31ee4Smrg  -s            $stripprog installed files.
108f8a31ee4Smrg  -t DIRECTORY  install into DIRECTORY.
109f8a31ee4Smrg  -T            report an error if DSTFILE is a directory.
110a0d3b6eaSmrg
111a0d3b6eaSmrgEnvironment variables override the default commands:
112f8a31ee4Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113f8a31ee4Smrg  RMPROG STRIPPROG
114a0d3b6eaSmrg"
115a0d3b6eaSmrg
116a0d3b6eaSmrgwhile test $# -ne 0; do
117a0d3b6eaSmrg  case $1 in
118f8a31ee4Smrg    -c) ;;
119f8a31ee4Smrg
120f8a31ee4Smrg    -C) copy_on_change=true;;
121a0d3b6eaSmrg
122f8a31ee4Smrg    -d) dir_arg=true;;
123a0d3b6eaSmrg
124a0d3b6eaSmrg    -g) chgrpcmd="$chgrpprog $2"
1254a908991Smrg        shift;;
126a0d3b6eaSmrg
127a0d3b6eaSmrg    --help) echo "$usage"; exit $?;;
128a0d3b6eaSmrg
129a0d3b6eaSmrg    -m) mode=$2
1304a908991Smrg        case $mode in
1314a908991Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1324a908991Smrg            echo "$0: invalid mode: $mode" >&2
1334a908991Smrg            exit 1;;
1344a908991Smrg        esac
1354a908991Smrg        shift;;
136a0d3b6eaSmrg
137a0d3b6eaSmrg    -o) chowncmd="$chownprog $2"
1384a908991Smrg        shift;;
139a0d3b6eaSmrg
140f8a31ee4Smrg    -s) stripcmd=$stripprog;;
141a0d3b6eaSmrg
1424a908991Smrg    -t)
1434a908991Smrg        is_target_a_directory=always
1444a908991Smrg        dst_arg=$2
1454a908991Smrg        # Protect names problematic for 'test' and other utilities.
1464a908991Smrg        case $dst_arg in
1474a908991Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1484a908991Smrg        esac
1494a908991Smrg        shift;;
150a0d3b6eaSmrg
1514a908991Smrg    -T) is_target_a_directory=never;;
152a0d3b6eaSmrg
153a0d3b6eaSmrg    --version) echo "$0 $scriptversion"; exit $?;;
154a0d3b6eaSmrg
1554a908991Smrg    --) shift
1564a908991Smrg        break;;
157a0d3b6eaSmrg
1584a908991Smrg    -*) echo "$0: invalid option: $1" >&2
1594a908991Smrg        exit 1;;
160a0d3b6eaSmrg
161a0d3b6eaSmrg    *)  break;;
162a0d3b6eaSmrg  esac
163f8a31ee4Smrg  shift
164a0d3b6eaSmrgdone
165a0d3b6eaSmrg
1664a908991Smrg# We allow the use of options -d and -T together, by making -d
1674a908991Smrg# take the precedence; this is for compatibility with GNU install.
1684a908991Smrg
1694a908991Smrgif test -n "$dir_arg"; then
1704a908991Smrg  if test -n "$dst_arg"; then
1714a908991Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1724a908991Smrg    exit 1
1734a908991Smrg  fi
1744a908991Smrgfi
1754a908991Smrg
176f8a31ee4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177a0d3b6eaSmrg  # When -d is used, all remaining arguments are directories to create.
178a0d3b6eaSmrg  # When -t is used, the destination is already specified.
179a0d3b6eaSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180a0d3b6eaSmrg  for arg
181a0d3b6eaSmrg  do
182f8a31ee4Smrg    if test -n "$dst_arg"; then
183a0d3b6eaSmrg      # $@ is not empty: it contains at least $arg.
184f8a31ee4Smrg      set fnord "$@" "$dst_arg"
185a0d3b6eaSmrg      shift # fnord
186a0d3b6eaSmrg    fi
187a0d3b6eaSmrg    shift # arg
188f8a31ee4Smrg    dst_arg=$arg
1894a908991Smrg    # Protect names problematic for 'test' and other utilities.
190966b534aSmrg    case $dst_arg in
191966b534aSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192966b534aSmrg    esac
193a0d3b6eaSmrg  done
194a0d3b6eaSmrgfi
195a0d3b6eaSmrg
196a0d3b6eaSmrgif test $# -eq 0; then
197a0d3b6eaSmrg  if test -z "$dir_arg"; then
198a0d3b6eaSmrg    echo "$0: no input file specified." >&2
199a0d3b6eaSmrg    exit 1
200a0d3b6eaSmrg  fi
2014a908991Smrg  # It's OK to call 'install-sh -d' without argument.
202a0d3b6eaSmrg  # This can happen when creating conditional directories.
203a0d3b6eaSmrg  exit 0
204a0d3b6eaSmrgfi
205a0d3b6eaSmrg
2064a908991Smrgif test -z "$dir_arg"; then
2074a908991Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2084a908991Smrg    if test ! -d "$dst_arg"; then
2094a908991Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2104a908991Smrg      exit 1
2114a908991Smrg    fi
2124a908991Smrg  fi
2134a908991Smrgfi
2144a908991Smrg
215a0d3b6eaSmrgif test -z "$dir_arg"; then
216966b534aSmrg  do_exit='(exit $ret); exit $ret'
217966b534aSmrg  trap "ret=129; $do_exit" 1
218966b534aSmrg  trap "ret=130; $do_exit" 2
219966b534aSmrg  trap "ret=141; $do_exit" 13
220966b534aSmrg  trap "ret=143; $do_exit" 15
221a0d3b6eaSmrg
222a0d3b6eaSmrg  # Set umask so as not to create temps with too-generous modes.
223a0d3b6eaSmrg  # However, 'strip' requires both read and write access to temps.
224a0d3b6eaSmrg  case $mode in
225a0d3b6eaSmrg    # Optimize common cases.
226a0d3b6eaSmrg    *644) cp_umask=133;;
227a0d3b6eaSmrg    *755) cp_umask=22;;
228a0d3b6eaSmrg
229a0d3b6eaSmrg    *[0-7])
230a0d3b6eaSmrg      if test -z "$stripcmd"; then
2314a908991Smrg        u_plus_rw=
232a0d3b6eaSmrg      else
2334a908991Smrg        u_plus_rw='% 200'
234a0d3b6eaSmrg      fi
235a0d3b6eaSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236a0d3b6eaSmrg    *)
237a0d3b6eaSmrg      if test -z "$stripcmd"; then
2384a908991Smrg        u_plus_rw=
239a0d3b6eaSmrg      else
2404a908991Smrg        u_plus_rw=,u+rw
241a0d3b6eaSmrg      fi
242a0d3b6eaSmrg      cp_umask=$mode$u_plus_rw;;
243a0d3b6eaSmrg  esac
244a0d3b6eaSmrgfi
245a0d3b6eaSmrg
246a0d3b6eaSmrgfor src
247a0d3b6eaSmrgdo
2484a908991Smrg  # Protect names problematic for 'test' and other utilities.
249a0d3b6eaSmrg  case $src in
250966b534aSmrg    -* | [=\(\)!]) src=./$src;;
251a0d3b6eaSmrg  esac
252a0d3b6eaSmrg
253a0d3b6eaSmrg  if test -n "$dir_arg"; then
254a0d3b6eaSmrg    dst=$src
255a0d3b6eaSmrg    dstdir=$dst
256a0d3b6eaSmrg    test -d "$dstdir"
257a0d3b6eaSmrg    dstdir_status=$?
258a0d3b6eaSmrg  else
259a0d3b6eaSmrg
260a0d3b6eaSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261a0d3b6eaSmrg    # might cause directories to be created, which would be especially bad
262a0d3b6eaSmrg    # if $src (and thus $dsttmp) contains '*'.
263a0d3b6eaSmrg    if test ! -f "$src" && test ! -d "$src"; then
264a0d3b6eaSmrg      echo "$0: $src does not exist." >&2
265a0d3b6eaSmrg      exit 1
266a0d3b6eaSmrg    fi
267a0d3b6eaSmrg
268f8a31ee4Smrg    if test -z "$dst_arg"; then
269a0d3b6eaSmrg      echo "$0: no destination specified." >&2
270a0d3b6eaSmrg      exit 1
271a0d3b6eaSmrg    fi
272f8a31ee4Smrg    dst=$dst_arg
273a0d3b6eaSmrg
274a0d3b6eaSmrg    # If destination is a directory, append the input filename; won't work
275a0d3b6eaSmrg    # if double slashes aren't ignored.
276a0d3b6eaSmrg    if test -d "$dst"; then
2774a908991Smrg      if test "$is_target_a_directory" = never; then
2784a908991Smrg        echo "$0: $dst_arg: Is a directory" >&2
2794a908991Smrg        exit 1
280a0d3b6eaSmrg      fi
281a0d3b6eaSmrg      dstdir=$dst
282a0d3b6eaSmrg      dst=$dstdir/`basename "$src"`
283a0d3b6eaSmrg      dstdir_status=0
284a0d3b6eaSmrg    else
2854a908991Smrg      dstdir=`dirname "$dst"`
286a0d3b6eaSmrg      test -d "$dstdir"
287a0d3b6eaSmrg      dstdir_status=$?
288a0d3b6eaSmrg    fi
289a0d3b6eaSmrg  fi
290a0d3b6eaSmrg
291a0d3b6eaSmrg  obsolete_mkdir_used=false
292a0d3b6eaSmrg
293a0d3b6eaSmrg  if test $dstdir_status != 0; then
294a0d3b6eaSmrg    case $posix_mkdir in
295a0d3b6eaSmrg      '')
2964a908991Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
2974a908991Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
2984a908991Smrg        umask=`umask`
2994a908991Smrg        case $stripcmd.$umask in
3004a908991Smrg          # Optimize common cases.
3014a908991Smrg          *[2367][2367]) mkdir_umask=$umask;;
3024a908991Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3034a908991Smrg
3044a908991Smrg          *[0-7])
3054a908991Smrg            mkdir_umask=`expr $umask + 22 \
3064a908991Smrg              - $umask % 100 % 40 + $umask % 20 \
3074a908991Smrg              - $umask % 10 % 4 + $umask % 2
3084a908991Smrg            `;;
3094a908991Smrg          *) mkdir_umask=$umask,go-w;;
3104a908991Smrg        esac
3114a908991Smrg
3124a908991Smrg        # With -d, create the new directory with the user-specified mode.
3134a908991Smrg        # Otherwise, rely on $mkdir_umask.
3144a908991Smrg        if test -n "$dir_arg"; then
3154a908991Smrg          mkdir_mode=-m$mode
3164a908991Smrg        else
3174a908991Smrg          mkdir_mode=
3184a908991Smrg        fi
3194a908991Smrg
3204a908991Smrg        posix_mkdir=false
3214a908991Smrg        case $umask in
3224a908991Smrg          *[123567][0-7][0-7])
3234a908991Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3244a908991Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3254a908991Smrg            ;;
3264a908991Smrg          *)
3274a908991Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3284a908991Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3294a908991Smrg
3304a908991Smrg            if (umask $mkdir_umask &&
3314a908991Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3324a908991Smrg            then
3334a908991Smrg              if test -z "$dir_arg" || {
3344a908991Smrg                   # Check for POSIX incompatibilities with -m.
3354a908991Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3364a908991Smrg                   # other-writable bit of parent directory when it shouldn't.
3374a908991Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3384a908991Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3394a908991Smrg                   case $ls_ld_tmpdir in
3404a908991Smrg                     d????-?r-*) different_mode=700;;
3414a908991Smrg                     d????-?--*) different_mode=755;;
3424a908991Smrg                     *) false;;
3434a908991Smrg                   esac &&
3444a908991Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3454a908991Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3464a908991Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3474a908991Smrg                   }
3484a908991Smrg                 }
3494a908991Smrg              then posix_mkdir=:
3504a908991Smrg              fi
3514a908991Smrg              rmdir "$tmpdir/d" "$tmpdir"
3524a908991Smrg            else
3534a908991Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3544a908991Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3554a908991Smrg            fi
3564a908991Smrg            trap '' 0;;
3574a908991Smrg        esac;;
358a0d3b6eaSmrg    esac
359a0d3b6eaSmrg
360a0d3b6eaSmrg    if
361a0d3b6eaSmrg      $posix_mkdir && (
3624a908991Smrg        umask $mkdir_umask &&
3634a908991Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364a0d3b6eaSmrg      )
365a0d3b6eaSmrg    then :
366a0d3b6eaSmrg    else
367a0d3b6eaSmrg
368a0d3b6eaSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369a0d3b6eaSmrg      # or it failed possibly due to a race condition.  Create the
370a0d3b6eaSmrg      # directory the slow way, step by step, checking for races as we go.
371a0d3b6eaSmrg
372a0d3b6eaSmrg      case $dstdir in
3734a908991Smrg        /*) prefix='/';;
3744a908991Smrg        [-=\(\)!]*) prefix='./';;
3754a908991Smrg        *)  prefix='';;
376a0d3b6eaSmrg      esac
377a0d3b6eaSmrg
378a0d3b6eaSmrg      oIFS=$IFS
379a0d3b6eaSmrg      IFS=/
3804a908991Smrg      set -f
381a0d3b6eaSmrg      set fnord $dstdir
382a0d3b6eaSmrg      shift
3834a908991Smrg      set +f
384a0d3b6eaSmrg      IFS=$oIFS
385a0d3b6eaSmrg
386a0d3b6eaSmrg      prefixes=
387a0d3b6eaSmrg
388a0d3b6eaSmrg      for d
389a0d3b6eaSmrg      do
3904a908991Smrg        test X"$d" = X && continue
3914a908991Smrg
3924a908991Smrg        prefix=$prefix$d
3934a908991Smrg        if test -d "$prefix"; then
3944a908991Smrg          prefixes=
3954a908991Smrg        else
3964a908991Smrg          if $posix_mkdir; then
3974a908991Smrg            (umask=$mkdir_umask &&
3984a908991Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3994a908991Smrg            # Don't fail if two instances are running concurrently.
4004a908991Smrg            test -d "$prefix" || exit 1
4014a908991Smrg          else
4024a908991Smrg            case $prefix in
4034a908991Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4044a908991Smrg              *) qprefix=$prefix;;
4054a908991Smrg            esac
4064a908991Smrg            prefixes="$prefixes '$qprefix'"
4074a908991Smrg          fi
4084a908991Smrg        fi
4094a908991Smrg        prefix=$prefix/
410a0d3b6eaSmrg      done
411a0d3b6eaSmrg
412a0d3b6eaSmrg      if test -n "$prefixes"; then
4134a908991Smrg        # Don't fail if two instances are running concurrently.
4144a908991Smrg        (umask $mkdir_umask &&
4154a908991Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4164a908991Smrg          test -d "$dstdir" || exit 1
4174a908991Smrg        obsolete_mkdir_used=true
418a0d3b6eaSmrg      fi
419a0d3b6eaSmrg    fi
420a0d3b6eaSmrg  fi
421a0d3b6eaSmrg
422a0d3b6eaSmrg  if test -n "$dir_arg"; then
423a0d3b6eaSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424a0d3b6eaSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425a0d3b6eaSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426a0d3b6eaSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427a0d3b6eaSmrg  else
428a0d3b6eaSmrg
429a0d3b6eaSmrg    # Make a couple of temp file names in the proper directory.
430a0d3b6eaSmrg    dsttmp=$dstdir/_inst.$$_
431a0d3b6eaSmrg    rmtmp=$dstdir/_rm.$$_
432a0d3b6eaSmrg
433a0d3b6eaSmrg    # Trap to clean up those temp files at exit.
434a0d3b6eaSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435a0d3b6eaSmrg
436a0d3b6eaSmrg    # Copy the file name to the temp name.
437a0d3b6eaSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438a0d3b6eaSmrg
439a0d3b6eaSmrg    # and set any options; do chmod last to preserve setuid bits.
440a0d3b6eaSmrg    #
441a0d3b6eaSmrg    # If any of these fail, we abort the whole thing.  If we want to
442a0d3b6eaSmrg    # ignore errors from any of these, just make sure not to ignore
443a0d3b6eaSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444a0d3b6eaSmrg    #
445f8a31ee4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446f8a31ee4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447f8a31ee4Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448f8a31ee4Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449f8a31ee4Smrg
450f8a31ee4Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451f8a31ee4Smrg    if $copy_on_change &&
4524a908991Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4534a908991Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4544a908991Smrg       set -f &&
455f8a31ee4Smrg       set X $old && old=:$2:$4:$5:$6 &&
456f8a31ee4Smrg       set X $new && new=:$2:$4:$5:$6 &&
4574a908991Smrg       set +f &&
458f8a31ee4Smrg       test "$old" = "$new" &&
459f8a31ee4Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460f8a31ee4Smrg    then
461f8a31ee4Smrg      rm -f "$dsttmp"
462f8a31ee4Smrg    else
463f8a31ee4Smrg      # Rename the file to the real destination.
464f8a31ee4Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465f8a31ee4Smrg
466f8a31ee4Smrg      # The rename failed, perhaps because mv can't rename something else
467f8a31ee4Smrg      # to itself, or perhaps because mv is so ancient that it does not
468f8a31ee4Smrg      # support -f.
469f8a31ee4Smrg      {
4704a908991Smrg        # Now remove or move aside any old file at destination location.
4714a908991Smrg        # We try this two ways since rm can't unlink itself on some
4724a908991Smrg        # systems and the destination file might be busy for other
4734a908991Smrg        # reasons.  In this case, the final cleanup might fail but the new
4744a908991Smrg        # file should still install successfully.
4754a908991Smrg        {
4764a908991Smrg          test ! -f "$dst" ||
4774a908991Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4784a908991Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4794a908991Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4804a908991Smrg          } ||
4814a908991Smrg          { echo "$0: cannot unlink or rename $dst" >&2
4824a908991Smrg            (exit 1); exit 1
4834a908991Smrg          }
4844a908991Smrg        } &&
4854a908991Smrg
4864a908991Smrg        # Now rename the file to the real destination.
4874a908991Smrg        $doit $mvcmd "$dsttmp" "$dst"
488f8a31ee4Smrg      }
489f8a31ee4Smrg    fi || exit 1
490a0d3b6eaSmrg
491a0d3b6eaSmrg    trap '' 0
492a0d3b6eaSmrg  fi
493a0d3b6eaSmrgdone
494a0d3b6eaSmrg
495a0d3b6eaSmrg# Local variables:
496a0d3b6eaSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497a0d3b6eaSmrg# time-stamp-start: "scriptversion="
498a0d3b6eaSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499f8a31ee4Smrg# time-stamp-time-zone: "UTC"
500f8a31ee4Smrg# time-stamp-end: "; # UTC"
501a0d3b6eaSmrg# End:
502