install-sh revision a3bd7f05
1444c061aSmrg#!/bin/sh
2444c061aSmrg# install - install a program, script, or datafile
3444c061aSmrg
40568f49bSmrgscriptversion=2018-03-11.20; # UTC
5444c061aSmrg
6444c061aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7444c061aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8444c061aSmrg# following copyright and license.
9444c061aSmrg#
10444c061aSmrg# Copyright (C) 1994 X Consortium
11444c061aSmrg#
12444c061aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13444c061aSmrg# of this software and associated documentation files (the "Software"), to
14444c061aSmrg# deal in the Software without restriction, including without limitation the
15444c061aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16444c061aSmrg# sell copies of the Software, and to permit persons to whom the Software is
17444c061aSmrg# furnished to do so, subject to the following conditions:
18444c061aSmrg#
19444c061aSmrg# The above copyright notice and this permission notice shall be included in
20444c061aSmrg# all copies or substantial portions of the Software.
21444c061aSmrg#
22444c061aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23444c061aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24444c061aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25444c061aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26444c061aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27444c061aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28444c061aSmrg#
29444c061aSmrg# Except as contained in this notice, the name of the X Consortium shall not
30444c061aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31444c061aSmrg# ings in this Software without prior written authorization from the X Consor-
32444c061aSmrg# tium.
33444c061aSmrg#
34444c061aSmrg#
35444c061aSmrg# FSF changes to this file are in the public domain.
36444c061aSmrg#
37444c061aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
389e7bcd65Smrg# 'make' implicit rules from creating a file called install from it
39444c061aSmrg# when there is no Makefile.
40444c061aSmrg#
41444c061aSmrg# This script is compatible with the BSD install script, but was written
422265a131Smrg# from scratch.
432265a131Smrg
44339a7c43Smrgtab='	'
452265a131Smrgnl='
462265a131Smrg'
47339a7c43SmrgIFS=" $tab$nl"
48444c061aSmrg
49339a7c43Smrg# Set DOITPROG to "echo" to test this script.
50444c061aSmrg
512265a131Smrgdoit=${DOITPROG-}
52339a7c43Smrgdoit_exec=${doit:-exec}
53444c061aSmrg
542265a131Smrg# Put in absolute file names if you don't have them in your path;
552265a131Smrg# or use environment vars.
562265a131Smrg
572265a131Smrgchgrpprog=${CHGRPPROG-chgrp}
582265a131Smrgchmodprog=${CHMODPROG-chmod}
592265a131Smrgchownprog=${CHOWNPROG-chown}
602265a131Smrgcmpprog=${CMPPROG-cmp}
612265a131Smrgcpprog=${CPPROG-cp}
622265a131Smrgmkdirprog=${MKDIRPROG-mkdir}
632265a131Smrgmvprog=${MVPROG-mv}
642265a131Smrgrmprog=${RMPROG-rm}
652265a131Smrgstripprog=${STRIPPROG-strip}
662265a131Smrg
672265a131Smrgposix_mkdir=
682265a131Smrg
692265a131Smrg# Desired mode of installed file.
702265a131Smrgmode=0755
71444c061aSmrg
72444c061aSmrgchgrpcmd=
732265a131Smrgchmodcmd=$chmodprog
742265a131Smrgchowncmd=
752265a131Smrgmvcmd=$mvprog
76444c061aSmrgrmcmd="$rmprog -f"
772265a131Smrgstripcmd=
782265a131Smrg
79444c061aSmrgsrc=
80444c061aSmrgdst=
81444c061aSmrgdir_arg=
822265a131Smrgdst_arg=
832265a131Smrg
842265a131Smrgcopy_on_change=false
85339a7c43Smrgis_target_a_directory=possibly
86444c061aSmrg
872265a131Smrgusage="\
882265a131SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89444c061aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90444c061aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91444c061aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
92444c061aSmrg
93444c061aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94444c061aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95444c061aSmrgIn the 4th, create DIRECTORIES.
96444c061aSmrg
97444c061aSmrgOptions:
982265a131Smrg     --help     display this help and exit.
992265a131Smrg     --version  display version info and exit.
1002265a131Smrg
1012265a131Smrg  -c            (ignored)
1022265a131Smrg  -C            install only if different (preserve the last data modification time)
1032265a131Smrg  -d            create directories instead of installing files.
1042265a131Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1052265a131Smrg  -m MODE       $chmodprog installed files to MODE.
1062265a131Smrg  -o USER       $chownprog installed files to USER.
1072265a131Smrg  -s            $stripprog installed files.
1082265a131Smrg  -t DIRECTORY  install into DIRECTORY.
1092265a131Smrg  -T            report an error if DSTFILE is a directory.
110444c061aSmrg
111444c061aSmrgEnvironment variables override the default commands:
1122265a131Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1132265a131Smrg  RMPROG STRIPPROG
114444c061aSmrg"
115444c061aSmrg
1162265a131Smrgwhile test $# -ne 0; do
117444c061aSmrg  case $1 in
1182265a131Smrg    -c) ;;
1192265a131Smrg
1202265a131Smrg    -C) copy_on_change=true;;
121444c061aSmrg
1222265a131Smrg    -d) dir_arg=true;;
123444c061aSmrg
124444c061aSmrg    -g) chgrpcmd="$chgrpprog $2"
125339a7c43Smrg        shift;;
126444c061aSmrg
127444c061aSmrg    --help) echo "$usage"; exit $?;;
128444c061aSmrg
1292265a131Smrg    -m) mode=$2
130339a7c43Smrg        case $mode in
131339a7c43Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132339a7c43Smrg            echo "$0: invalid mode: $mode" >&2
133339a7c43Smrg            exit 1;;
134339a7c43Smrg        esac
135339a7c43Smrg        shift;;
136444c061aSmrg
137444c061aSmrg    -o) chowncmd="$chownprog $2"
138339a7c43Smrg        shift;;
139444c061aSmrg
1402265a131Smrg    -s) stripcmd=$stripprog;;
141444c061aSmrg
142339a7c43Smrg    -t)
143339a7c43Smrg        is_target_a_directory=always
144339a7c43Smrg        dst_arg=$2
145339a7c43Smrg        # Protect names problematic for 'test' and other utilities.
146339a7c43Smrg        case $dst_arg in
147339a7c43Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148339a7c43Smrg        esac
149339a7c43Smrg        shift;;
150444c061aSmrg
151339a7c43Smrg    -T) is_target_a_directory=never;;
152444c061aSmrg
153444c061aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
154444c061aSmrg
155339a7c43Smrg    --) shift
156339a7c43Smrg        break;;
1572265a131Smrg
158339a7c43Smrg    -*) echo "$0: invalid option: $1" >&2
159339a7c43Smrg        exit 1;;
1602265a131Smrg
1612265a131Smrg    *)  break;;
162444c061aSmrg  esac
1632265a131Smrg  shift
164444c061aSmrgdone
165444c061aSmrg
166339a7c43Smrg# We allow the use of options -d and -T together, by making -d
167339a7c43Smrg# take the precedence; this is for compatibility with GNU install.
168339a7c43Smrg
169339a7c43Smrgif test -n "$dir_arg"; then
170339a7c43Smrg  if test -n "$dst_arg"; then
171339a7c43Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172339a7c43Smrg    exit 1
173339a7c43Smrg  fi
174339a7c43Smrgfi
175339a7c43Smrg
1762265a131Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1772265a131Smrg  # When -d is used, all remaining arguments are directories to create.
1782265a131Smrg  # When -t is used, the destination is already specified.
1792265a131Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1802265a131Smrg  for arg
1812265a131Smrg  do
1822265a131Smrg    if test -n "$dst_arg"; then
1832265a131Smrg      # $@ is not empty: it contains at least $arg.
1842265a131Smrg      set fnord "$@" "$dst_arg"
1852265a131Smrg      shift # fnord
1862265a131Smrg    fi
1872265a131Smrg    shift # arg
1882265a131Smrg    dst_arg=$arg
1899e7bcd65Smrg    # Protect names problematic for 'test' and other utilities.
1909e7bcd65Smrg    case $dst_arg in
1919e7bcd65Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1929e7bcd65Smrg    esac
1932265a131Smrg  done
1942265a131Smrgfi
1952265a131Smrg
1962265a131Smrgif test $# -eq 0; then
197444c061aSmrg  if test -z "$dir_arg"; then
198444c061aSmrg    echo "$0: no input file specified." >&2
199444c061aSmrg    exit 1
200444c061aSmrg  fi
2019e7bcd65Smrg  # It's OK to call 'install-sh -d' without argument.
202444c061aSmrg  # This can happen when creating conditional directories.
203444c061aSmrg  exit 0
204444c061aSmrgfi
205444c061aSmrg
206339a7c43Smrgif test -z "$dir_arg"; then
207339a7c43Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208339a7c43Smrg    if test ! -d "$dst_arg"; then
209339a7c43Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210339a7c43Smrg      exit 1
211339a7c43Smrg    fi
212339a7c43Smrg  fi
213339a7c43Smrgfi
214339a7c43Smrg
2152265a131Smrgif test -z "$dir_arg"; then
2169e7bcd65Smrg  do_exit='(exit $ret); exit $ret'
2179e7bcd65Smrg  trap "ret=129; $do_exit" 1
2189e7bcd65Smrg  trap "ret=130; $do_exit" 2
2199e7bcd65Smrg  trap "ret=141; $do_exit" 13
2209e7bcd65Smrg  trap "ret=143; $do_exit" 15
2212265a131Smrg
2222265a131Smrg  # Set umask so as not to create temps with too-generous modes.
2232265a131Smrg  # However, 'strip' requires both read and write access to temps.
2242265a131Smrg  case $mode in
2252265a131Smrg    # Optimize common cases.
2262265a131Smrg    *644) cp_umask=133;;
2272265a131Smrg    *755) cp_umask=22;;
2282265a131Smrg
2292265a131Smrg    *[0-7])
2302265a131Smrg      if test -z "$stripcmd"; then
231339a7c43Smrg        u_plus_rw=
2322265a131Smrg      else
233339a7c43Smrg        u_plus_rw='% 200'
2342265a131Smrg      fi
2352265a131Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2362265a131Smrg    *)
2372265a131Smrg      if test -z "$stripcmd"; then
238339a7c43Smrg        u_plus_rw=
2392265a131Smrg      else
240339a7c43Smrg        u_plus_rw=,u+rw
2412265a131Smrg      fi
2422265a131Smrg      cp_umask=$mode$u_plus_rw;;
2432265a131Smrg  esac
2442265a131Smrgfi
2452265a131Smrg
246444c061aSmrgfor src
247444c061aSmrgdo
2489e7bcd65Smrg  # Protect names problematic for 'test' and other utilities.
249444c061aSmrg  case $src in
2509e7bcd65Smrg    -* | [=\(\)!]) src=./$src;;
251444c061aSmrg  esac
252444c061aSmrg
253444c061aSmrg  if test -n "$dir_arg"; then
254444c061aSmrg    dst=$src
2552265a131Smrg    dstdir=$dst
2562265a131Smrg    test -d "$dstdir"
2572265a131Smrg    dstdir_status=$?
258444c061aSmrg  else
2592265a131Smrg
260444c061aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261444c061aSmrg    # might cause directories to be created, which would be especially bad
262444c061aSmrg    # if $src (and thus $dsttmp) contains '*'.
263444c061aSmrg    if test ! -f "$src" && test ! -d "$src"; then
264444c061aSmrg      echo "$0: $src does not exist." >&2
265444c061aSmrg      exit 1
266444c061aSmrg    fi
267444c061aSmrg
2682265a131Smrg    if test -z "$dst_arg"; then
269444c061aSmrg      echo "$0: no destination specified." >&2
270444c061aSmrg      exit 1
271444c061aSmrg    fi
2722265a131Smrg    dst=$dst_arg
273444c061aSmrg
2740568f49bSmrg    # If destination is a directory, append the input filename.
275444c061aSmrg    if test -d "$dst"; then
276339a7c43Smrg      if test "$is_target_a_directory" = never; then
277339a7c43Smrg        echo "$0: $dst_arg: Is a directory" >&2
278339a7c43Smrg        exit 1
279444c061aSmrg      fi
2802265a131Smrg      dstdir=$dst
2810568f49bSmrg      dstbase=`basename "$src"`
2820568f49bSmrg      case $dst in
2830568f49bSmrg	*/) dst=$dst$dstbase;;
2840568f49bSmrg	*)  dst=$dst/$dstbase;;
2850568f49bSmrg      esac
2862265a131Smrg      dstdir_status=0
2872265a131Smrg    else
288339a7c43Smrg      dstdir=`dirname "$dst"`
2892265a131Smrg      test -d "$dstdir"
2902265a131Smrg      dstdir_status=$?
291444c061aSmrg    fi
292444c061aSmrg  fi
293444c061aSmrg
2940568f49bSmrg  case $dstdir in
2950568f49bSmrg    */) dstdirslash=$dstdir;;
2960568f49bSmrg    *)  dstdirslash=$dstdir/;;
2970568f49bSmrg  esac
2980568f49bSmrg
2992265a131Smrg  obsolete_mkdir_used=false
3002265a131Smrg
3012265a131Smrg  if test $dstdir_status != 0; then
3022265a131Smrg    case $posix_mkdir in
3032265a131Smrg      '')
304339a7c43Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
305339a7c43Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
306339a7c43Smrg        umask=`umask`
307339a7c43Smrg        case $stripcmd.$umask in
308339a7c43Smrg          # Optimize common cases.
309339a7c43Smrg          *[2367][2367]) mkdir_umask=$umask;;
310339a7c43Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311339a7c43Smrg
312339a7c43Smrg          *[0-7])
313339a7c43Smrg            mkdir_umask=`expr $umask + 22 \
314339a7c43Smrg              - $umask % 100 % 40 + $umask % 20 \
315339a7c43Smrg              - $umask % 10 % 4 + $umask % 2
316339a7c43Smrg            `;;
317339a7c43Smrg          *) mkdir_umask=$umask,go-w;;
318339a7c43Smrg        esac
319339a7c43Smrg
320339a7c43Smrg        # With -d, create the new directory with the user-specified mode.
321339a7c43Smrg        # Otherwise, rely on $mkdir_umask.
322339a7c43Smrg        if test -n "$dir_arg"; then
323339a7c43Smrg          mkdir_mode=-m$mode
324339a7c43Smrg        else
325339a7c43Smrg          mkdir_mode=
326339a7c43Smrg        fi
327339a7c43Smrg
328339a7c43Smrg        posix_mkdir=false
329339a7c43Smrg        case $umask in
330339a7c43Smrg          *[123567][0-7][0-7])
331339a7c43Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
332339a7c43Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333339a7c43Smrg            ;;
334339a7c43Smrg          *)
3350568f49bSmrg            # Note that $RANDOM variable is not portable (e.g. dash);  Use it
3360568f49bSmrg            # here however when possible just to lower collision chance.
337339a7c43Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
338339a7c43Smrg
3390568f49bSmrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
3400568f49bSmrg
3410568f49bSmrg            # Because "mkdir -p" follows existing symlinks and we likely work
3420568f49bSmrg            # directly in world-writeable /tmp, make sure that the '$tmpdir'
3430568f49bSmrg            # directory is successfully created first before we actually test
3440568f49bSmrg            # 'mkdir -p' feature.
345339a7c43Smrg            if (umask $mkdir_umask &&
3460568f49bSmrg                $mkdirprog $mkdir_mode "$tmpdir" &&
3470568f49bSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
348339a7c43Smrg            then
349339a7c43Smrg              if test -z "$dir_arg" || {
350339a7c43Smrg                   # Check for POSIX incompatibilities with -m.
351339a7c43Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
352339a7c43Smrg                   # other-writable bit of parent directory when it shouldn't.
353339a7c43Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3540568f49bSmrg                   test_tmpdir="$tmpdir/a"
3550568f49bSmrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
356339a7c43Smrg                   case $ls_ld_tmpdir in
357339a7c43Smrg                     d????-?r-*) different_mode=700;;
358339a7c43Smrg                     d????-?--*) different_mode=755;;
359339a7c43Smrg                     *) false;;
360339a7c43Smrg                   esac &&
3610568f49bSmrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3620568f49bSmrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
363339a7c43Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
364339a7c43Smrg                   }
365339a7c43Smrg                 }
366339a7c43Smrg              then posix_mkdir=:
367339a7c43Smrg              fi
3680568f49bSmrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
369339a7c43Smrg            else
370339a7c43Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3710568f49bSmrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
372339a7c43Smrg            fi
373339a7c43Smrg            trap '' 0;;
374339a7c43Smrg        esac;;
3752265a131Smrg    esac
376444c061aSmrg
3772265a131Smrg    if
3782265a131Smrg      $posix_mkdir && (
379339a7c43Smrg        umask $mkdir_umask &&
380339a7c43Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3812265a131Smrg      )
3822265a131Smrg    then :
3832265a131Smrg    else
384444c061aSmrg
3852265a131Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3862265a131Smrg      # or it failed possibly due to a race condition.  Create the
3872265a131Smrg      # directory the slow way, step by step, checking for races as we go.
388444c061aSmrg
3892265a131Smrg      case $dstdir in
390339a7c43Smrg        /*) prefix='/';;
391339a7c43Smrg        [-=\(\)!]*) prefix='./';;
392339a7c43Smrg        *)  prefix='';;
3932265a131Smrg      esac
394444c061aSmrg
3952265a131Smrg      oIFS=$IFS
3962265a131Smrg      IFS=/
397339a7c43Smrg      set -f
3982265a131Smrg      set fnord $dstdir
399444c061aSmrg      shift
400339a7c43Smrg      set +f
4012265a131Smrg      IFS=$oIFS
4022265a131Smrg
4032265a131Smrg      prefixes=
4042265a131Smrg
4052265a131Smrg      for d
4062265a131Smrg      do
407339a7c43Smrg        test X"$d" = X && continue
408339a7c43Smrg
409339a7c43Smrg        prefix=$prefix$d
410339a7c43Smrg        if test -d "$prefix"; then
411339a7c43Smrg          prefixes=
412339a7c43Smrg        else
413339a7c43Smrg          if $posix_mkdir; then
414339a7c43Smrg            (umask=$mkdir_umask &&
415339a7c43Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416339a7c43Smrg            # Don't fail if two instances are running concurrently.
417339a7c43Smrg            test -d "$prefix" || exit 1
418339a7c43Smrg          else
419339a7c43Smrg            case $prefix in
420339a7c43Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421339a7c43Smrg              *) qprefix=$prefix;;
422339a7c43Smrg            esac
423339a7c43Smrg            prefixes="$prefixes '$qprefix'"
424339a7c43Smrg          fi
425339a7c43Smrg        fi
426339a7c43Smrg        prefix=$prefix/
4272265a131Smrg      done
4282265a131Smrg
4292265a131Smrg      if test -n "$prefixes"; then
430339a7c43Smrg        # Don't fail if two instances are running concurrently.
431339a7c43Smrg        (umask $mkdir_umask &&
432339a7c43Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
433339a7c43Smrg          test -d "$dstdir" || exit 1
434339a7c43Smrg        obsolete_mkdir_used=true
435444c061aSmrg      fi
4362265a131Smrg    fi
437444c061aSmrg  fi
438444c061aSmrg
439444c061aSmrg  if test -n "$dir_arg"; then
4402265a131Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4412265a131Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4422265a131Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4432265a131Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
444444c061aSmrg  else
445444c061aSmrg
446444c061aSmrg    # Make a couple of temp file names in the proper directory.
4470568f49bSmrg    dsttmp=${dstdirslash}_inst.$$_
4480568f49bSmrg    rmtmp=${dstdirslash}_rm.$$_
449444c061aSmrg
450444c061aSmrg    # Trap to clean up those temp files at exit.
451444c061aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
452444c061aSmrg
453444c061aSmrg    # Copy the file name to the temp name.
454a3bd7f05Smrg    (umask $cp_umask &&
455a3bd7f05Smrg     { test -z "$stripcmd" || {
456a3bd7f05Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
457a3bd7f05Smrg	 # which would cause strip to fail.
458a3bd7f05Smrg	 if test -z "$doit"; then
459a3bd7f05Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
460a3bd7f05Smrg	 else
461a3bd7f05Smrg	   $doit touch "$dsttmp"
462a3bd7f05Smrg	 fi
463a3bd7f05Smrg       }
464a3bd7f05Smrg     } &&
465a3bd7f05Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
466444c061aSmrg
467444c061aSmrg    # and set any options; do chmod last to preserve setuid bits.
468444c061aSmrg    #
469444c061aSmrg    # If any of these fail, we abort the whole thing.  If we want to
470444c061aSmrg    # ignore errors from any of these, just make sure not to ignore
471444c061aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
472444c061aSmrg    #
4732265a131Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4742265a131Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4752265a131Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4762265a131Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4772265a131Smrg
4782265a131Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4792265a131Smrg    if $copy_on_change &&
480339a7c43Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
481339a7c43Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
482339a7c43Smrg       set -f &&
4832265a131Smrg       set X $old && old=:$2:$4:$5:$6 &&
4842265a131Smrg       set X $new && new=:$2:$4:$5:$6 &&
485339a7c43Smrg       set +f &&
4862265a131Smrg       test "$old" = "$new" &&
4872265a131Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4882265a131Smrg    then
4892265a131Smrg      rm -f "$dsttmp"
4902265a131Smrg    else
4912265a131Smrg      # Rename the file to the real destination.
4922265a131Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4932265a131Smrg
4942265a131Smrg      # The rename failed, perhaps because mv can't rename something else
4952265a131Smrg      # to itself, or perhaps because mv is so ancient that it does not
4962265a131Smrg      # support -f.
4972265a131Smrg      {
498339a7c43Smrg        # Now remove or move aside any old file at destination location.
499339a7c43Smrg        # We try this two ways since rm can't unlink itself on some
500339a7c43Smrg        # systems and the destination file might be busy for other
501339a7c43Smrg        # reasons.  In this case, the final cleanup might fail but the new
502339a7c43Smrg        # file should still install successfully.
503339a7c43Smrg        {
504339a7c43Smrg          test ! -f "$dst" ||
505339a7c43Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
506339a7c43Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
507339a7c43Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
508339a7c43Smrg          } ||
509339a7c43Smrg          { echo "$0: cannot unlink or rename $dst" >&2
510339a7c43Smrg            (exit 1); exit 1
511339a7c43Smrg          }
512339a7c43Smrg        } &&
513339a7c43Smrg
514339a7c43Smrg        # Now rename the file to the real destination.
515339a7c43Smrg        $doit $mvcmd "$dsttmp" "$dst"
5162265a131Smrg      }
5172265a131Smrg    fi || exit 1
5182265a131Smrg
5192265a131Smrg    trap '' 0
5202265a131Smrg  fi
521444c061aSmrgdone
522444c061aSmrg
523444c061aSmrg# Local variables:
5240568f49bSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
525444c061aSmrg# time-stamp-start: "scriptversion="
526444c061aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5270568f49bSmrg# time-stamp-time-zone: "UTC0"
5282ae83157Smrg# time-stamp-end: "; # UTC"
529444c061aSmrg# End:
530