install-sh revision da4b5163
1a850946eSmrg#!/bin/sh
2a850946eSmrg# install - install a program, script, or datafile
370f7c90cSmrg
4da4b5163Smrgscriptversion=2020-11-14.01; # UTC
570f7c90cSmrg
6a850946eSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a850946eSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8a850946eSmrg# following copyright and license.
9a850946eSmrg#
10a850946eSmrg# Copyright (C) 1994 X Consortium
11a850946eSmrg#
12a850946eSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a850946eSmrg# of this software and associated documentation files (the "Software"), to
14a850946eSmrg# deal in the Software without restriction, including without limitation the
15a850946eSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a850946eSmrg# sell copies of the Software, and to permit persons to whom the Software is
17a850946eSmrg# furnished to do so, subject to the following conditions:
18a850946eSmrg#
19a850946eSmrg# The above copyright notice and this permission notice shall be included in
20a850946eSmrg# all copies or substantial portions of the Software.
21a850946eSmrg#
22a850946eSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a850946eSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a850946eSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a850946eSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a850946eSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a850946eSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a850946eSmrg#
29a850946eSmrg# Except as contained in this notice, the name of the X Consortium shall not
30a850946eSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a850946eSmrg# ings in this Software without prior written authorization from the X Consor-
32a850946eSmrg# tium.
33a850946eSmrg#
34a850946eSmrg#
35a850946eSmrg# FSF changes to this file are in the public domain.
36a850946eSmrg#
37a850946eSmrg# Calling this script install-sh is preferred over install.sh, to prevent
389511053fSmrg# 'make' implicit rules from creating a file called install from it
39a850946eSmrg# when there is no Makefile.
40a850946eSmrg#
41a850946eSmrg# This script is compatible with the BSD install script, but was written
4270f7c90cSmrg# from scratch.
43a850946eSmrg
44c44a0236Smrgtab='	'
4570f7c90cSmrgnl='
4670f7c90cSmrg'
47c44a0236SmrgIFS=" $tab$nl"
48a850946eSmrg
49c44a0236Smrg# Set DOITPROG to "echo" to test this script.
50a850946eSmrg
5170f7c90cSmrgdoit=${DOITPROG-}
52c44a0236Smrgdoit_exec=${doit:-exec}
53a850946eSmrg
5470f7c90cSmrg# Put in absolute file names if you don't have them in your path;
5570f7c90cSmrg# or use environment vars.
5670f7c90cSmrg
5770f7c90cSmrgchgrpprog=${CHGRPPROG-chgrp}
5870f7c90cSmrgchmodprog=${CHMODPROG-chmod}
5970f7c90cSmrgchownprog=${CHOWNPROG-chown}
6070f7c90cSmrgcmpprog=${CMPPROG-cmp}
6170f7c90cSmrgcpprog=${CPPROG-cp}
6270f7c90cSmrgmkdirprog=${MKDIRPROG-mkdir}
6370f7c90cSmrgmvprog=${MVPROG-mv}
6470f7c90cSmrgrmprog=${RMPROG-rm}
6570f7c90cSmrgstripprog=${STRIPPROG-strip}
6670f7c90cSmrg
6770f7c90cSmrgposix_mkdir=
6870f7c90cSmrg
6970f7c90cSmrg# Desired mode of installed file.
7070f7c90cSmrgmode=0755
7170f7c90cSmrg
72da4b5163Smrg# Create dirs (including intermediate dirs) using mode 755.
73da4b5163Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74da4b5163Smrgmkdir_umask=22
75da4b5163Smrg
76da4b5163Smrgbackupsuffix=
7770f7c90cSmrgchgrpcmd=
7870f7c90cSmrgchmodcmd=$chmodprog
7970f7c90cSmrgchowncmd=
8070f7c90cSmrgmvcmd=$mvprog
8170f7c90cSmrgrmcmd="$rmprog -f"
8270f7c90cSmrgstripcmd=
83a850946eSmrg
8470f7c90cSmrgsrc=
8570f7c90cSmrgdst=
8670f7c90cSmrgdir_arg=
8770f7c90cSmrgdst_arg=
88a850946eSmrg
8970f7c90cSmrgcopy_on_change=false
90c44a0236Smrgis_target_a_directory=possibly
91a850946eSmrg
9270f7c90cSmrgusage="\
9370f7c90cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9470f7c90cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9570f7c90cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9670f7c90cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97a850946eSmrg
9870f7c90cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9970f7c90cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10070f7c90cSmrgIn the 4th, create DIRECTORIES.
101a850946eSmrg
10270f7c90cSmrgOptions:
10370f7c90cSmrg     --help     display this help and exit.
10470f7c90cSmrg     --version  display version info and exit.
105a850946eSmrg
10670f7c90cSmrg  -c            (ignored)
107da4b5163Smrg  -C            install only if different (preserve data modification time)
10870f7c90cSmrg  -d            create directories instead of installing files.
10970f7c90cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
11070f7c90cSmrg  -m MODE       $chmodprog installed files to MODE.
11170f7c90cSmrg  -o USER       $chownprog installed files to USER.
112da4b5163Smrg  -p            pass -p to $cpprog.
11370f7c90cSmrg  -s            $stripprog installed files.
114da4b5163Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11570f7c90cSmrg  -t DIRECTORY  install into DIRECTORY.
11670f7c90cSmrg  -T            report an error if DSTFILE is a directory.
117a850946eSmrg
11870f7c90cSmrgEnvironment variables override the default commands:
11970f7c90cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12070f7c90cSmrg  RMPROG STRIPPROG
121da4b5163Smrg
122da4b5163SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123da4b5163Smrgit's up to you to specify -f if you want it.
124da4b5163Smrg
125da4b5163SmrgIf -S is not specified, no backups are attempted.
126da4b5163Smrg
127da4b5163SmrgEmail bug reports to bug-automake@gnu.org.
128da4b5163SmrgAutomake home page: https://www.gnu.org/software/automake/
12970f7c90cSmrg"
130a850946eSmrg
13170f7c90cSmrgwhile test $# -ne 0; do
13270f7c90cSmrg  case $1 in
13370f7c90cSmrg    -c) ;;
134a850946eSmrg
13570f7c90cSmrg    -C) copy_on_change=true;;
136a850946eSmrg
13770f7c90cSmrg    -d) dir_arg=true;;
138a850946eSmrg
13970f7c90cSmrg    -g) chgrpcmd="$chgrpprog $2"
140c44a0236Smrg        shift;;
141a850946eSmrg
14270f7c90cSmrg    --help) echo "$usage"; exit $?;;
143a850946eSmrg
14470f7c90cSmrg    -m) mode=$2
145c44a0236Smrg        case $mode in
146c44a0236Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147c44a0236Smrg            echo "$0: invalid mode: $mode" >&2
148c44a0236Smrg            exit 1;;
149c44a0236Smrg        esac
150c44a0236Smrg        shift;;
151a850946eSmrg
15270f7c90cSmrg    -o) chowncmd="$chownprog $2"
153c44a0236Smrg        shift;;
154a850946eSmrg
155da4b5163Smrg    -p) cpprog="$cpprog -p";;
156da4b5163Smrg
15770f7c90cSmrg    -s) stripcmd=$stripprog;;
158a850946eSmrg
159da4b5163Smrg    -S) backupsuffix="$2"
160da4b5163Smrg        shift;;
161da4b5163Smrg
162c44a0236Smrg    -t)
163c44a0236Smrg        is_target_a_directory=always
164c44a0236Smrg        dst_arg=$2
165c44a0236Smrg        # Protect names problematic for 'test' and other utilities.
166c44a0236Smrg        case $dst_arg in
167c44a0236Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168c44a0236Smrg        esac
169c44a0236Smrg        shift;;
170a850946eSmrg
171c44a0236Smrg    -T) is_target_a_directory=never;;
172a850946eSmrg
17370f7c90cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174a850946eSmrg
175c44a0236Smrg    --) shift
176c44a0236Smrg        break;;
177a850946eSmrg
178c44a0236Smrg    -*) echo "$0: invalid option: $1" >&2
179c44a0236Smrg        exit 1;;
180a850946eSmrg
18170f7c90cSmrg    *)  break;;
18270f7c90cSmrg  esac
18370f7c90cSmrg  shift
18470f7c90cSmrgdone
185a850946eSmrg
186c44a0236Smrg# We allow the use of options -d and -T together, by making -d
187c44a0236Smrg# take the precedence; this is for compatibility with GNU install.
188c44a0236Smrg
189c44a0236Smrgif test -n "$dir_arg"; then
190c44a0236Smrg  if test -n "$dst_arg"; then
191c44a0236Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192c44a0236Smrg    exit 1
193c44a0236Smrg  fi
194c44a0236Smrgfi
195c44a0236Smrg
19670f7c90cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19770f7c90cSmrg  # When -d is used, all remaining arguments are directories to create.
19870f7c90cSmrg  # When -t is used, the destination is already specified.
19970f7c90cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20070f7c90cSmrg  for arg
20170f7c90cSmrg  do
20270f7c90cSmrg    if test -n "$dst_arg"; then
20370f7c90cSmrg      # $@ is not empty: it contains at least $arg.
20470f7c90cSmrg      set fnord "$@" "$dst_arg"
20570f7c90cSmrg      shift # fnord
20670f7c90cSmrg    fi
20770f7c90cSmrg    shift # arg
20870f7c90cSmrg    dst_arg=$arg
2099511053fSmrg    # Protect names problematic for 'test' and other utilities.
2109511053fSmrg    case $dst_arg in
2119511053fSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2129511053fSmrg    esac
21370f7c90cSmrg  done
21470f7c90cSmrgfi
215a850946eSmrg
21670f7c90cSmrgif test $# -eq 0; then
21770f7c90cSmrg  if test -z "$dir_arg"; then
21870f7c90cSmrg    echo "$0: no input file specified." >&2
21970f7c90cSmrg    exit 1
22070f7c90cSmrg  fi
2219511053fSmrg  # It's OK to call 'install-sh -d' without argument.
22270f7c90cSmrg  # This can happen when creating conditional directories.
22370f7c90cSmrg  exit 0
22470f7c90cSmrgfi
225a850946eSmrg
226c44a0236Smrgif test -z "$dir_arg"; then
227c44a0236Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228c44a0236Smrg    if test ! -d "$dst_arg"; then
229c44a0236Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230c44a0236Smrg      exit 1
231c44a0236Smrg    fi
232c44a0236Smrg  fi
233c44a0236Smrgfi
234c44a0236Smrg
23570f7c90cSmrgif test -z "$dir_arg"; then
2369511053fSmrg  do_exit='(exit $ret); exit $ret'
2379511053fSmrg  trap "ret=129; $do_exit" 1
2389511053fSmrg  trap "ret=130; $do_exit" 2
2399511053fSmrg  trap "ret=141; $do_exit" 13
2409511053fSmrg  trap "ret=143; $do_exit" 15
24170f7c90cSmrg
24270f7c90cSmrg  # Set umask so as not to create temps with too-generous modes.
24370f7c90cSmrg  # However, 'strip' requires both read and write access to temps.
24470f7c90cSmrg  case $mode in
24570f7c90cSmrg    # Optimize common cases.
24670f7c90cSmrg    *644) cp_umask=133;;
24770f7c90cSmrg    *755) cp_umask=22;;
24870f7c90cSmrg
24970f7c90cSmrg    *[0-7])
25070f7c90cSmrg      if test -z "$stripcmd"; then
251c44a0236Smrg        u_plus_rw=
25270f7c90cSmrg      else
253c44a0236Smrg        u_plus_rw='% 200'
25470f7c90cSmrg      fi
25570f7c90cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25670f7c90cSmrg    *)
25770f7c90cSmrg      if test -z "$stripcmd"; then
258c44a0236Smrg        u_plus_rw=
25970f7c90cSmrg      else
260c44a0236Smrg        u_plus_rw=,u+rw
26170f7c90cSmrg      fi
26270f7c90cSmrg      cp_umask=$mode$u_plus_rw;;
26370f7c90cSmrg  esac
26470f7c90cSmrgfi
265a850946eSmrg
26670f7c90cSmrgfor src
26770f7c90cSmrgdo
2689511053fSmrg  # Protect names problematic for 'test' and other utilities.
26970f7c90cSmrg  case $src in
2709511053fSmrg    -* | [=\(\)!]) src=./$src;;
27170f7c90cSmrg  esac
27270f7c90cSmrg
27370f7c90cSmrg  if test -n "$dir_arg"; then
27470f7c90cSmrg    dst=$src
27570f7c90cSmrg    dstdir=$dst
27670f7c90cSmrg    test -d "$dstdir"
27770f7c90cSmrg    dstdir_status=$?
278da4b5163Smrg    # Don't chown directories that already exist.
279da4b5163Smrg    if test $dstdir_status = 0; then
280da4b5163Smrg      chowncmd=""
281da4b5163Smrg    fi
28270f7c90cSmrg  else
28370f7c90cSmrg
28470f7c90cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28570f7c90cSmrg    # might cause directories to be created, which would be especially bad
28670f7c90cSmrg    # if $src (and thus $dsttmp) contains '*'.
28770f7c90cSmrg    if test ! -f "$src" && test ! -d "$src"; then
28870f7c90cSmrg      echo "$0: $src does not exist." >&2
28970f7c90cSmrg      exit 1
29070f7c90cSmrg    fi
29170f7c90cSmrg
29270f7c90cSmrg    if test -z "$dst_arg"; then
29370f7c90cSmrg      echo "$0: no destination specified." >&2
29470f7c90cSmrg      exit 1
29570f7c90cSmrg    fi
29670f7c90cSmrg    dst=$dst_arg
297a850946eSmrg
298da4b5163Smrg    # If destination is a directory, append the input filename.
29970f7c90cSmrg    if test -d "$dst"; then
300c44a0236Smrg      if test "$is_target_a_directory" = never; then
301c44a0236Smrg        echo "$0: $dst_arg: Is a directory" >&2
302c44a0236Smrg        exit 1
30370f7c90cSmrg      fi
30470f7c90cSmrg      dstdir=$dst
305da4b5163Smrg      dstbase=`basename "$src"`
306da4b5163Smrg      case $dst in
307da4b5163Smrg	*/) dst=$dst$dstbase;;
308da4b5163Smrg	*)  dst=$dst/$dstbase;;
309da4b5163Smrg      esac
31070f7c90cSmrg      dstdir_status=0
31170f7c90cSmrg    else
312c44a0236Smrg      dstdir=`dirname "$dst"`
31370f7c90cSmrg      test -d "$dstdir"
31470f7c90cSmrg      dstdir_status=$?
31570f7c90cSmrg    fi
31670f7c90cSmrg  fi
31770f7c90cSmrg
318da4b5163Smrg  case $dstdir in
319da4b5163Smrg    */) dstdirslash=$dstdir;;
320da4b5163Smrg    *)  dstdirslash=$dstdir/;;
321da4b5163Smrg  esac
322da4b5163Smrg
32370f7c90cSmrg  obsolete_mkdir_used=false
32470f7c90cSmrg
32570f7c90cSmrg  if test $dstdir_status != 0; then
32670f7c90cSmrg    case $posix_mkdir in
32770f7c90cSmrg      '')
328c44a0236Smrg        # With -d, create the new directory with the user-specified mode.
329c44a0236Smrg        # Otherwise, rely on $mkdir_umask.
330c44a0236Smrg        if test -n "$dir_arg"; then
331c44a0236Smrg          mkdir_mode=-m$mode
332c44a0236Smrg        else
333c44a0236Smrg          mkdir_mode=
334c44a0236Smrg        fi
335c44a0236Smrg
336c44a0236Smrg        posix_mkdir=false
337da4b5163Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338da4b5163Smrg	# here however when possible just to lower collision chance.
339da4b5163Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340da4b5163Smrg
341da4b5163Smrg	trap '
342da4b5163Smrg	  ret=$?
343da4b5163Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344da4b5163Smrg	  exit $ret
345da4b5163Smrg	' 0
346da4b5163Smrg
347da4b5163Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348da4b5163Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349da4b5163Smrg	# directory is successfully created first before we actually test
350da4b5163Smrg	# 'mkdir -p'.
351da4b5163Smrg	if (umask $mkdir_umask &&
352da4b5163Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353da4b5163Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354da4b5163Smrg	then
355da4b5163Smrg	  if test -z "$dir_arg" || {
356da4b5163Smrg	       # Check for POSIX incompatibilities with -m.
357da4b5163Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358da4b5163Smrg	       # other-writable bit of parent directory when it shouldn't.
359da4b5163Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360da4b5163Smrg	       test_tmpdir="$tmpdir/a"
361da4b5163Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362da4b5163Smrg	       case $ls_ld_tmpdir in
363da4b5163Smrg		 d????-?r-*) different_mode=700;;
364da4b5163Smrg		 d????-?--*) different_mode=755;;
365da4b5163Smrg		 *) false;;
366da4b5163Smrg	       esac &&
367da4b5163Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368da4b5163Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369da4b5163Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370da4b5163Smrg	       }
371da4b5163Smrg	     }
372da4b5163Smrg	  then posix_mkdir=:
373da4b5163Smrg	  fi
374da4b5163Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375da4b5163Smrg	else
376da4b5163Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377da4b5163Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378da4b5163Smrg	fi
379da4b5163Smrg	trap '' 0;;
38070f7c90cSmrg    esac
381a850946eSmrg
38270f7c90cSmrg    if
38370f7c90cSmrg      $posix_mkdir && (
384c44a0236Smrg        umask $mkdir_umask &&
385c44a0236Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38670f7c90cSmrg      )
38770f7c90cSmrg    then :
38870f7c90cSmrg    else
38970f7c90cSmrg
390da4b5163Smrg      # mkdir does not conform to POSIX,
39170f7c90cSmrg      # or it failed possibly due to a race condition.  Create the
39270f7c90cSmrg      # directory the slow way, step by step, checking for races as we go.
39370f7c90cSmrg
39470f7c90cSmrg      case $dstdir in
395c44a0236Smrg        /*) prefix='/';;
396c44a0236Smrg        [-=\(\)!]*) prefix='./';;
397c44a0236Smrg        *)  prefix='';;
39870f7c90cSmrg      esac
39970f7c90cSmrg
40070f7c90cSmrg      oIFS=$IFS
40170f7c90cSmrg      IFS=/
402c44a0236Smrg      set -f
40370f7c90cSmrg      set fnord $dstdir
40470f7c90cSmrg      shift
405c44a0236Smrg      set +f
40670f7c90cSmrg      IFS=$oIFS
40770f7c90cSmrg
40870f7c90cSmrg      prefixes=
40970f7c90cSmrg
41070f7c90cSmrg      for d
41170f7c90cSmrg      do
412c44a0236Smrg        test X"$d" = X && continue
413c44a0236Smrg
414c44a0236Smrg        prefix=$prefix$d
415c44a0236Smrg        if test -d "$prefix"; then
416c44a0236Smrg          prefixes=
417c44a0236Smrg        else
418c44a0236Smrg          if $posix_mkdir; then
419da4b5163Smrg            (umask $mkdir_umask &&
420c44a0236Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421c44a0236Smrg            # Don't fail if two instances are running concurrently.
422c44a0236Smrg            test -d "$prefix" || exit 1
423c44a0236Smrg          else
424c44a0236Smrg            case $prefix in
425c44a0236Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426c44a0236Smrg              *) qprefix=$prefix;;
427c44a0236Smrg            esac
428c44a0236Smrg            prefixes="$prefixes '$qprefix'"
429c44a0236Smrg          fi
430c44a0236Smrg        fi
431c44a0236Smrg        prefix=$prefix/
43270f7c90cSmrg      done
43370f7c90cSmrg
43470f7c90cSmrg      if test -n "$prefixes"; then
435c44a0236Smrg        # Don't fail if two instances are running concurrently.
436c44a0236Smrg        (umask $mkdir_umask &&
437c44a0236Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438c44a0236Smrg          test -d "$dstdir" || exit 1
439c44a0236Smrg        obsolete_mkdir_used=true
44070f7c90cSmrg      fi
44170f7c90cSmrg    fi
44270f7c90cSmrg  fi
44370f7c90cSmrg
44470f7c90cSmrg  if test -n "$dir_arg"; then
44570f7c90cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44670f7c90cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44770f7c90cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44870f7c90cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44970f7c90cSmrg  else
45070f7c90cSmrg
45170f7c90cSmrg    # Make a couple of temp file names in the proper directory.
452da4b5163Smrg    dsttmp=${dstdirslash}_inst.$$_
453da4b5163Smrg    rmtmp=${dstdirslash}_rm.$$_
45470f7c90cSmrg
45570f7c90cSmrg    # Trap to clean up those temp files at exit.
45670f7c90cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45770f7c90cSmrg
45870f7c90cSmrg    # Copy the file name to the temp name.
459da4b5163Smrg    (umask $cp_umask &&
460da4b5163Smrg     { test -z "$stripcmd" || {
461da4b5163Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462da4b5163Smrg	 # which would cause strip to fail.
463da4b5163Smrg	 if test -z "$doit"; then
464da4b5163Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465da4b5163Smrg	 else
466da4b5163Smrg	   $doit touch "$dsttmp"
467da4b5163Smrg	 fi
468da4b5163Smrg       }
469da4b5163Smrg     } &&
470da4b5163Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47170f7c90cSmrg
47270f7c90cSmrg    # and set any options; do chmod last to preserve setuid bits.
47370f7c90cSmrg    #
47470f7c90cSmrg    # If any of these fail, we abort the whole thing.  If we want to
47570f7c90cSmrg    # ignore errors from any of these, just make sure not to ignore
47670f7c90cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47770f7c90cSmrg    #
47870f7c90cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47970f7c90cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48070f7c90cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48170f7c90cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48270f7c90cSmrg
48370f7c90cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
48470f7c90cSmrg    if $copy_on_change &&
485c44a0236Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486c44a0236Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487c44a0236Smrg       set -f &&
48870f7c90cSmrg       set X $old && old=:$2:$4:$5:$6 &&
48970f7c90cSmrg       set X $new && new=:$2:$4:$5:$6 &&
490c44a0236Smrg       set +f &&
49170f7c90cSmrg       test "$old" = "$new" &&
49270f7c90cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49370f7c90cSmrg    then
49470f7c90cSmrg      rm -f "$dsttmp"
49570f7c90cSmrg    else
496da4b5163Smrg      # If $backupsuffix is set, and the file being installed
497da4b5163Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498da4b5163Smrg      # e.g., if mv doesn't support -f.
499da4b5163Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500da4b5163Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501da4b5163Smrg      fi
502da4b5163Smrg
50370f7c90cSmrg      # Rename the file to the real destination.
50470f7c90cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50570f7c90cSmrg
50670f7c90cSmrg      # The rename failed, perhaps because mv can't rename something else
50770f7c90cSmrg      # to itself, or perhaps because mv is so ancient that it does not
50870f7c90cSmrg      # support -f.
50970f7c90cSmrg      {
510c44a0236Smrg        # Now remove or move aside any old file at destination location.
511c44a0236Smrg        # We try this two ways since rm can't unlink itself on some
512c44a0236Smrg        # systems and the destination file might be busy for other
513c44a0236Smrg        # reasons.  In this case, the final cleanup might fail but the new
514c44a0236Smrg        # file should still install successfully.
515c44a0236Smrg        {
516c44a0236Smrg          test ! -f "$dst" ||
517da4b5163Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518c44a0236Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519da4b5163Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520c44a0236Smrg          } ||
521c44a0236Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522c44a0236Smrg            (exit 1); exit 1
523c44a0236Smrg          }
524c44a0236Smrg        } &&
525c44a0236Smrg
526c44a0236Smrg        # Now rename the file to the real destination.
527c44a0236Smrg        $doit $mvcmd "$dsttmp" "$dst"
52870f7c90cSmrg      }
52970f7c90cSmrg    fi || exit 1
53070f7c90cSmrg
53170f7c90cSmrg    trap '' 0
53270f7c90cSmrg  fi
53370f7c90cSmrgdone
534a850946eSmrg
53570f7c90cSmrg# Local variables:
536da4b5163Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
53770f7c90cSmrg# time-stamp-start: "scriptversion="
53870f7c90cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539da4b5163Smrg# time-stamp-time-zone: "UTC0"
54070f7c90cSmrg# time-stamp-end: "; # UTC"
54170f7c90cSmrg# End:
542