install-sh revision 48e5ce07
1c43cc173Smrg#!/bin/sh
2c43cc173Smrg# install - install a program, script, or datafile
3c43cc173Smrg
448e5ce07Smrgscriptversion=2013-12-25.23; # UTC
5c43cc173Smrg
6c43cc173Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c43cc173Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8c43cc173Smrg# following copyright and license.
9c43cc173Smrg#
10c43cc173Smrg# Copyright (C) 1994 X Consortium
11c43cc173Smrg#
12c43cc173Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c43cc173Smrg# of this software and associated documentation files (the "Software"), to
14c43cc173Smrg# deal in the Software without restriction, including without limitation the
15c43cc173Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c43cc173Smrg# sell copies of the Software, and to permit persons to whom the Software is
17c43cc173Smrg# furnished to do so, subject to the following conditions:
18c43cc173Smrg#
19c43cc173Smrg# The above copyright notice and this permission notice shall be included in
20c43cc173Smrg# all copies or substantial portions of the Software.
21c43cc173Smrg#
22c43cc173Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c43cc173Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c43cc173Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c43cc173Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c43cc173Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c43cc173Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c43cc173Smrg#
29c43cc173Smrg# Except as contained in this notice, the name of the X Consortium shall not
30c43cc173Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c43cc173Smrg# ings in this Software without prior written authorization from the X Consor-
32c43cc173Smrg# tium.
33c43cc173Smrg#
34c43cc173Smrg#
35c43cc173Smrg# FSF changes to this file are in the public domain.
36c43cc173Smrg#
37c43cc173Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38f1ee322dSmrg# 'make' implicit rules from creating a file called install from it
39c43cc173Smrg# when there is no Makefile.
40c43cc173Smrg#
41c43cc173Smrg# This script is compatible with the BSD install script, but was written
42c43cc173Smrg# from scratch.
43c43cc173Smrg
4448e5ce07Smrgtab='	'
45c43cc173Smrgnl='
46c43cc173Smrg'
4748e5ce07SmrgIFS=" $tab$nl"
48c43cc173Smrg
4948e5ce07Smrg# Set DOITPROG to "echo" to test this script.
50c43cc173Smrg
51c27c18e8Smrgdoit=${DOITPROG-}
5248e5ce07Smrgdoit_exec=${doit:-exec}
53c43cc173Smrg
54c43cc173Smrg# Put in absolute file names if you don't have them in your path;
55c43cc173Smrg# or use environment vars.
56c43cc173Smrg
57c27c18e8Smrgchgrpprog=${CHGRPPROG-chgrp}
58c27c18e8Smrgchmodprog=${CHMODPROG-chmod}
59c27c18e8Smrgchownprog=${CHOWNPROG-chown}
60c27c18e8Smrgcmpprog=${CMPPROG-cmp}
61c27c18e8Smrgcpprog=${CPPROG-cp}
62c27c18e8Smrgmkdirprog=${MKDIRPROG-mkdir}
63c27c18e8Smrgmvprog=${MVPROG-mv}
64c27c18e8Smrgrmprog=${RMPROG-rm}
65c27c18e8Smrgstripprog=${STRIPPROG-strip}
66c27c18e8Smrg
67c43cc173Smrgposix_mkdir=
68c43cc173Smrg
69c43cc173Smrg# Desired mode of installed file.
70c43cc173Smrgmode=0755
71c43cc173Smrg
72c27c18e8Smrgchgrpcmd=
73c43cc173Smrgchmodcmd=$chmodprog
74c43cc173Smrgchowncmd=
75c27c18e8Smrgmvcmd=$mvprog
76c43cc173Smrgrmcmd="$rmprog -f"
77c27c18e8Smrgstripcmd=
78c27c18e8Smrg
79c43cc173Smrgsrc=
80c43cc173Smrgdst=
81c43cc173Smrgdir_arg=
82c27c18e8Smrgdst_arg=
83c27c18e8Smrg
84c27c18e8Smrgcopy_on_change=false
8548e5ce07Smrgis_target_a_directory=possibly
86c43cc173Smrg
87c27c18e8Smrgusage="\
88c27c18e8SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89c43cc173Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90c43cc173Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91c43cc173Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92c43cc173Smrg
93c43cc173SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94c43cc173SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95c43cc173SmrgIn the 4th, create DIRECTORIES.
96c43cc173Smrg
97c43cc173SmrgOptions:
98c27c18e8Smrg     --help     display this help and exit.
99c27c18e8Smrg     --version  display version info and exit.
100c27c18e8Smrg
101c27c18e8Smrg  -c            (ignored)
102c27c18e8Smrg  -C            install only if different (preserve the last data modification time)
103c27c18e8Smrg  -d            create directories instead of installing files.
104c27c18e8Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105c27c18e8Smrg  -m MODE       $chmodprog installed files to MODE.
106c27c18e8Smrg  -o USER       $chownprog installed files to USER.
107c27c18e8Smrg  -s            $stripprog installed files.
108c27c18e8Smrg  -t DIRECTORY  install into DIRECTORY.
109c27c18e8Smrg  -T            report an error if DSTFILE is a directory.
110c43cc173Smrg
111c43cc173SmrgEnvironment variables override the default commands:
112c27c18e8Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113c27c18e8Smrg  RMPROG STRIPPROG
114c43cc173Smrg"
115c43cc173Smrg
116c43cc173Smrgwhile test $# -ne 0; do
117c43cc173Smrg  case $1 in
118c27c18e8Smrg    -c) ;;
119c27c18e8Smrg
120c27c18e8Smrg    -C) copy_on_change=true;;
121c43cc173Smrg
122c27c18e8Smrg    -d) dir_arg=true;;
123c43cc173Smrg
124c43cc173Smrg    -g) chgrpcmd="$chgrpprog $2"
12548e5ce07Smrg        shift;;
126c43cc173Smrg
127c43cc173Smrg    --help) echo "$usage"; exit $?;;
128c43cc173Smrg
129c43cc173Smrg    -m) mode=$2
13048e5ce07Smrg        case $mode in
13148e5ce07Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13248e5ce07Smrg            echo "$0: invalid mode: $mode" >&2
13348e5ce07Smrg            exit 1;;
13448e5ce07Smrg        esac
13548e5ce07Smrg        shift;;
136c43cc173Smrg
137c43cc173Smrg    -o) chowncmd="$chownprog $2"
13848e5ce07Smrg        shift;;
139c43cc173Smrg
140c27c18e8Smrg    -s) stripcmd=$stripprog;;
141c43cc173Smrg
14248e5ce07Smrg    -t)
14348e5ce07Smrg        is_target_a_directory=always
14448e5ce07Smrg        dst_arg=$2
14548e5ce07Smrg        # Protect names problematic for 'test' and other utilities.
14648e5ce07Smrg        case $dst_arg in
14748e5ce07Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14848e5ce07Smrg        esac
14948e5ce07Smrg        shift;;
150c43cc173Smrg
15148e5ce07Smrg    -T) is_target_a_directory=never;;
152c43cc173Smrg
153c43cc173Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154c43cc173Smrg
15548e5ce07Smrg    --) shift
15648e5ce07Smrg        break;;
157c43cc173Smrg
15848e5ce07Smrg    -*) echo "$0: invalid option: $1" >&2
15948e5ce07Smrg        exit 1;;
160c43cc173Smrg
161c43cc173Smrg    *)  break;;
162c43cc173Smrg  esac
163c27c18e8Smrg  shift
164c43cc173Smrgdone
165c43cc173Smrg
16648e5ce07Smrg# We allow the use of options -d and -T together, by making -d
16748e5ce07Smrg# take the precedence; this is for compatibility with GNU install.
16848e5ce07Smrg
16948e5ce07Smrgif test -n "$dir_arg"; then
17048e5ce07Smrg  if test -n "$dst_arg"; then
17148e5ce07Smrg    echo "$0: target directory not allowed when installing a directory." >&2
17248e5ce07Smrg    exit 1
17348e5ce07Smrg  fi
17448e5ce07Smrgfi
17548e5ce07Smrg
176c27c18e8Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177c43cc173Smrg  # When -d is used, all remaining arguments are directories to create.
178c43cc173Smrg  # When -t is used, the destination is already specified.
179c43cc173Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180c43cc173Smrg  for arg
181c43cc173Smrg  do
182c27c18e8Smrg    if test -n "$dst_arg"; then
183c43cc173Smrg      # $@ is not empty: it contains at least $arg.
184c27c18e8Smrg      set fnord "$@" "$dst_arg"
185c43cc173Smrg      shift # fnord
186c43cc173Smrg    fi
187c43cc173Smrg    shift # arg
188c27c18e8Smrg    dst_arg=$arg
189f1ee322dSmrg    # Protect names problematic for 'test' and other utilities.
190f1ee322dSmrg    case $dst_arg in
191f1ee322dSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192f1ee322dSmrg    esac
193c43cc173Smrg  done
194c43cc173Smrgfi
195c43cc173Smrg
196c43cc173Smrgif test $# -eq 0; then
197c43cc173Smrg  if test -z "$dir_arg"; then
198c43cc173Smrg    echo "$0: no input file specified." >&2
199c43cc173Smrg    exit 1
200c43cc173Smrg  fi
201f1ee322dSmrg  # It's OK to call 'install-sh -d' without argument.
202c43cc173Smrg  # This can happen when creating conditional directories.
203c43cc173Smrg  exit 0
204c43cc173Smrgfi
205c43cc173Smrg
20648e5ce07Smrgif test -z "$dir_arg"; then
20748e5ce07Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20848e5ce07Smrg    if test ! -d "$dst_arg"; then
20948e5ce07Smrg      echo "$0: $dst_arg: Is not a directory." >&2
21048e5ce07Smrg      exit 1
21148e5ce07Smrg    fi
21248e5ce07Smrg  fi
21348e5ce07Smrgfi
21448e5ce07Smrg
215c43cc173Smrgif test -z "$dir_arg"; then
216f1ee322dSmrg  do_exit='(exit $ret); exit $ret'
217f1ee322dSmrg  trap "ret=129; $do_exit" 1
218f1ee322dSmrg  trap "ret=130; $do_exit" 2
219f1ee322dSmrg  trap "ret=141; $do_exit" 13
220f1ee322dSmrg  trap "ret=143; $do_exit" 15
221c43cc173Smrg
222c43cc173Smrg  # Set umask so as not to create temps with too-generous modes.
223c43cc173Smrg  # However, 'strip' requires both read and write access to temps.
224c43cc173Smrg  case $mode in
225c43cc173Smrg    # Optimize common cases.
226c43cc173Smrg    *644) cp_umask=133;;
227c43cc173Smrg    *755) cp_umask=22;;
228c43cc173Smrg
229c43cc173Smrg    *[0-7])
230c43cc173Smrg      if test -z "$stripcmd"; then
23148e5ce07Smrg        u_plus_rw=
232c43cc173Smrg      else
23348e5ce07Smrg        u_plus_rw='% 200'
234c43cc173Smrg      fi
235c43cc173Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236c43cc173Smrg    *)
237c43cc173Smrg      if test -z "$stripcmd"; then
23848e5ce07Smrg        u_plus_rw=
239c43cc173Smrg      else
24048e5ce07Smrg        u_plus_rw=,u+rw
241c43cc173Smrg      fi
242c43cc173Smrg      cp_umask=$mode$u_plus_rw;;
243c43cc173Smrg  esac
244c43cc173Smrgfi
245c43cc173Smrg
246c43cc173Smrgfor src
247c43cc173Smrgdo
248f1ee322dSmrg  # Protect names problematic for 'test' and other utilities.
249c43cc173Smrg  case $src in
250f1ee322dSmrg    -* | [=\(\)!]) src=./$src;;
251c43cc173Smrg  esac
252c43cc173Smrg
253c43cc173Smrg  if test -n "$dir_arg"; then
254c43cc173Smrg    dst=$src
255c43cc173Smrg    dstdir=$dst
256c43cc173Smrg    test -d "$dstdir"
257c43cc173Smrg    dstdir_status=$?
258c43cc173Smrg  else
259c43cc173Smrg
260c43cc173Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261c43cc173Smrg    # might cause directories to be created, which would be especially bad
262c43cc173Smrg    # if $src (and thus $dsttmp) contains '*'.
263c43cc173Smrg    if test ! -f "$src" && test ! -d "$src"; then
264c43cc173Smrg      echo "$0: $src does not exist." >&2
265c43cc173Smrg      exit 1
266c43cc173Smrg    fi
267c43cc173Smrg
268c27c18e8Smrg    if test -z "$dst_arg"; then
269c43cc173Smrg      echo "$0: no destination specified." >&2
270c43cc173Smrg      exit 1
271c43cc173Smrg    fi
272c27c18e8Smrg    dst=$dst_arg
273c43cc173Smrg
274c43cc173Smrg    # If destination is a directory, append the input filename; won't work
275c43cc173Smrg    # if double slashes aren't ignored.
276c43cc173Smrg    if test -d "$dst"; then
27748e5ce07Smrg      if test "$is_target_a_directory" = never; then
27848e5ce07Smrg        echo "$0: $dst_arg: Is a directory" >&2
27948e5ce07Smrg        exit 1
280c43cc173Smrg      fi
281c43cc173Smrg      dstdir=$dst
282c43cc173Smrg      dst=$dstdir/`basename "$src"`
283c43cc173Smrg      dstdir_status=0
284c43cc173Smrg    else
28548e5ce07Smrg      dstdir=`dirname "$dst"`
286c43cc173Smrg      test -d "$dstdir"
287c43cc173Smrg      dstdir_status=$?
288c43cc173Smrg    fi
289c43cc173Smrg  fi
290c43cc173Smrg
291c43cc173Smrg  obsolete_mkdir_used=false
292c43cc173Smrg
293c43cc173Smrg  if test $dstdir_status != 0; then
294c43cc173Smrg    case $posix_mkdir in
295c43cc173Smrg      '')
29648e5ce07Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
29748e5ce07Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
29848e5ce07Smrg        umask=`umask`
29948e5ce07Smrg        case $stripcmd.$umask in
30048e5ce07Smrg          # Optimize common cases.
30148e5ce07Smrg          *[2367][2367]) mkdir_umask=$umask;;
30248e5ce07Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30348e5ce07Smrg
30448e5ce07Smrg          *[0-7])
30548e5ce07Smrg            mkdir_umask=`expr $umask + 22 \
30648e5ce07Smrg              - $umask % 100 % 40 + $umask % 20 \
30748e5ce07Smrg              - $umask % 10 % 4 + $umask % 2
30848e5ce07Smrg            `;;
30948e5ce07Smrg          *) mkdir_umask=$umask,go-w;;
31048e5ce07Smrg        esac
31148e5ce07Smrg
31248e5ce07Smrg        # With -d, create the new directory with the user-specified mode.
31348e5ce07Smrg        # Otherwise, rely on $mkdir_umask.
31448e5ce07Smrg        if test -n "$dir_arg"; then
31548e5ce07Smrg          mkdir_mode=-m$mode
31648e5ce07Smrg        else
31748e5ce07Smrg          mkdir_mode=
31848e5ce07Smrg        fi
31948e5ce07Smrg
32048e5ce07Smrg        posix_mkdir=false
32148e5ce07Smrg        case $umask in
32248e5ce07Smrg          *[123567][0-7][0-7])
32348e5ce07Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32448e5ce07Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32548e5ce07Smrg            ;;
32648e5ce07Smrg          *)
32748e5ce07Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
32848e5ce07Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
32948e5ce07Smrg
33048e5ce07Smrg            if (umask $mkdir_umask &&
33148e5ce07Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
33248e5ce07Smrg            then
33348e5ce07Smrg              if test -z "$dir_arg" || {
33448e5ce07Smrg                   # Check for POSIX incompatibilities with -m.
33548e5ce07Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
33648e5ce07Smrg                   # other-writable bit of parent directory when it shouldn't.
33748e5ce07Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
33848e5ce07Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
33948e5ce07Smrg                   case $ls_ld_tmpdir in
34048e5ce07Smrg                     d????-?r-*) different_mode=700;;
34148e5ce07Smrg                     d????-?--*) different_mode=755;;
34248e5ce07Smrg                     *) false;;
34348e5ce07Smrg                   esac &&
34448e5ce07Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
34548e5ce07Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
34648e5ce07Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
34748e5ce07Smrg                   }
34848e5ce07Smrg                 }
34948e5ce07Smrg              then posix_mkdir=:
35048e5ce07Smrg              fi
35148e5ce07Smrg              rmdir "$tmpdir/d" "$tmpdir"
35248e5ce07Smrg            else
35348e5ce07Smrg              # Remove any dirs left behind by ancient mkdir implementations.
35448e5ce07Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
35548e5ce07Smrg            fi
35648e5ce07Smrg            trap '' 0;;
35748e5ce07Smrg        esac;;
358c43cc173Smrg    esac
359c43cc173Smrg
360c43cc173Smrg    if
361c43cc173Smrg      $posix_mkdir && (
36248e5ce07Smrg        umask $mkdir_umask &&
36348e5ce07Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364c43cc173Smrg      )
365c43cc173Smrg    then :
366c43cc173Smrg    else
367c43cc173Smrg
368c43cc173Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369c43cc173Smrg      # or it failed possibly due to a race condition.  Create the
370c43cc173Smrg      # directory the slow way, step by step, checking for races as we go.
371c43cc173Smrg
372c43cc173Smrg      case $dstdir in
37348e5ce07Smrg        /*) prefix='/';;
37448e5ce07Smrg        [-=\(\)!]*) prefix='./';;
37548e5ce07Smrg        *)  prefix='';;
376c43cc173Smrg      esac
377c43cc173Smrg
378c43cc173Smrg      oIFS=$IFS
379c43cc173Smrg      IFS=/
38048e5ce07Smrg      set -f
381c43cc173Smrg      set fnord $dstdir
382c43cc173Smrg      shift
38348e5ce07Smrg      set +f
384c43cc173Smrg      IFS=$oIFS
385c43cc173Smrg
386c43cc173Smrg      prefixes=
387c43cc173Smrg
388c43cc173Smrg      for d
389c43cc173Smrg      do
39048e5ce07Smrg        test X"$d" = X && continue
39148e5ce07Smrg
39248e5ce07Smrg        prefix=$prefix$d
39348e5ce07Smrg        if test -d "$prefix"; then
39448e5ce07Smrg          prefixes=
39548e5ce07Smrg        else
39648e5ce07Smrg          if $posix_mkdir; then
39748e5ce07Smrg            (umask=$mkdir_umask &&
39848e5ce07Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
39948e5ce07Smrg            # Don't fail if two instances are running concurrently.
40048e5ce07Smrg            test -d "$prefix" || exit 1
40148e5ce07Smrg          else
40248e5ce07Smrg            case $prefix in
40348e5ce07Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
40448e5ce07Smrg              *) qprefix=$prefix;;
40548e5ce07Smrg            esac
40648e5ce07Smrg            prefixes="$prefixes '$qprefix'"
40748e5ce07Smrg          fi
40848e5ce07Smrg        fi
40948e5ce07Smrg        prefix=$prefix/
410c43cc173Smrg      done
411c43cc173Smrg
412c43cc173Smrg      if test -n "$prefixes"; then
41348e5ce07Smrg        # Don't fail if two instances are running concurrently.
41448e5ce07Smrg        (umask $mkdir_umask &&
41548e5ce07Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
41648e5ce07Smrg          test -d "$dstdir" || exit 1
41748e5ce07Smrg        obsolete_mkdir_used=true
418c43cc173Smrg      fi
419c43cc173Smrg    fi
420c43cc173Smrg  fi
421c43cc173Smrg
422c43cc173Smrg  if test -n "$dir_arg"; then
423c43cc173Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424c43cc173Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425c43cc173Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426c43cc173Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427c43cc173Smrg  else
428c43cc173Smrg
429c43cc173Smrg    # Make a couple of temp file names in the proper directory.
430c43cc173Smrg    dsttmp=$dstdir/_inst.$$_
431c43cc173Smrg    rmtmp=$dstdir/_rm.$$_
432c43cc173Smrg
433c43cc173Smrg    # Trap to clean up those temp files at exit.
434c43cc173Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435c43cc173Smrg
436c43cc173Smrg    # Copy the file name to the temp name.
437c43cc173Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438c43cc173Smrg
439c43cc173Smrg    # and set any options; do chmod last to preserve setuid bits.
440c43cc173Smrg    #
441c43cc173Smrg    # If any of these fail, we abort the whole thing.  If we want to
442c43cc173Smrg    # ignore errors from any of these, just make sure not to ignore
443c43cc173Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444c43cc173Smrg    #
445c27c18e8Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446c27c18e8Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447c27c18e8Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448c27c18e8Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449c27c18e8Smrg
450c27c18e8Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451c27c18e8Smrg    if $copy_on_change &&
45248e5ce07Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
45348e5ce07Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
45448e5ce07Smrg       set -f &&
455c27c18e8Smrg       set X $old && old=:$2:$4:$5:$6 &&
456c27c18e8Smrg       set X $new && new=:$2:$4:$5:$6 &&
45748e5ce07Smrg       set +f &&
458c27c18e8Smrg       test "$old" = "$new" &&
459c27c18e8Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460c27c18e8Smrg    then
461c27c18e8Smrg      rm -f "$dsttmp"
462c27c18e8Smrg    else
463c27c18e8Smrg      # Rename the file to the real destination.
464c27c18e8Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465c27c18e8Smrg
466c27c18e8Smrg      # The rename failed, perhaps because mv can't rename something else
467c27c18e8Smrg      # to itself, or perhaps because mv is so ancient that it does not
468c27c18e8Smrg      # support -f.
469c27c18e8Smrg      {
47048e5ce07Smrg        # Now remove or move aside any old file at destination location.
47148e5ce07Smrg        # We try this two ways since rm can't unlink itself on some
47248e5ce07Smrg        # systems and the destination file might be busy for other
47348e5ce07Smrg        # reasons.  In this case, the final cleanup might fail but the new
47448e5ce07Smrg        # file should still install successfully.
47548e5ce07Smrg        {
47648e5ce07Smrg          test ! -f "$dst" ||
47748e5ce07Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
47848e5ce07Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
47948e5ce07Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48048e5ce07Smrg          } ||
48148e5ce07Smrg          { echo "$0: cannot unlink or rename $dst" >&2
48248e5ce07Smrg            (exit 1); exit 1
48348e5ce07Smrg          }
48448e5ce07Smrg        } &&
48548e5ce07Smrg
48648e5ce07Smrg        # Now rename the file to the real destination.
48748e5ce07Smrg        $doit $mvcmd "$dsttmp" "$dst"
488c27c18e8Smrg      }
489c27c18e8Smrg    fi || exit 1
490c43cc173Smrg
491c43cc173Smrg    trap '' 0
492c43cc173Smrg  fi
493c43cc173Smrgdone
494c43cc173Smrg
495c43cc173Smrg# Local variables:
496c43cc173Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497c43cc173Smrg# time-stamp-start: "scriptversion="
498c43cc173Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499c27c18e8Smrg# time-stamp-time-zone: "UTC"
500c27c18e8Smrg# time-stamp-end: "; # UTC"
501c43cc173Smrg# End:
502