install-sh revision 6c9573ad
1af7c02bdSmrg#!/bin/sh
2af7c02bdSmrg# install - install a program, script, or datafile
3af7c02bdSmrg
46c9573adSmrgscriptversion=2020-11-14.01; # UTC
5af7c02bdSmrg
6af7c02bdSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7af7c02bdSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8af7c02bdSmrg# following copyright and license.
9af7c02bdSmrg#
10af7c02bdSmrg# Copyright (C) 1994 X Consortium
11af7c02bdSmrg#
12af7c02bdSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13af7c02bdSmrg# of this software and associated documentation files (the "Software"), to
14af7c02bdSmrg# deal in the Software without restriction, including without limitation the
15af7c02bdSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16af7c02bdSmrg# sell copies of the Software, and to permit persons to whom the Software is
17af7c02bdSmrg# furnished to do so, subject to the following conditions:
18af7c02bdSmrg#
19af7c02bdSmrg# The above copyright notice and this permission notice shall be included in
20af7c02bdSmrg# all copies or substantial portions of the Software.
21af7c02bdSmrg#
22af7c02bdSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23af7c02bdSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24af7c02bdSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25af7c02bdSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26af7c02bdSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27af7c02bdSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28af7c02bdSmrg#
29af7c02bdSmrg# Except as contained in this notice, the name of the X Consortium shall not
30af7c02bdSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31af7c02bdSmrg# ings in this Software without prior written authorization from the X Consor-
32af7c02bdSmrg# tium.
33af7c02bdSmrg#
34af7c02bdSmrg#
35af7c02bdSmrg# FSF changes to this file are in the public domain.
36af7c02bdSmrg#
37af7c02bdSmrg# Calling this script install-sh is preferred over install.sh, to prevent
386c9573adSmrg# 'make' implicit rules from creating a file called install from it
39af7c02bdSmrg# when there is no Makefile.
40af7c02bdSmrg#
41af7c02bdSmrg# This script is compatible with the BSD install script, but was written
4281c81b28Smrg# from scratch.
4381c81b28Smrg
446c9573adSmrgtab='	'
4581c81b28Smrgnl='
4681c81b28Smrg'
476c9573adSmrgIFS=" $tab$nl"
48af7c02bdSmrg
496c9573adSmrg# Set DOITPROG to "echo" to test this script.
50af7c02bdSmrg
5181c81b28Smrgdoit=${DOITPROG-}
526c9573adSmrgdoit_exec=${doit:-exec}
53af7c02bdSmrg
5481c81b28Smrg# Put in absolute file names if you don't have them in your path;
5581c81b28Smrg# or use environment vars.
5681c81b28Smrg
5781c81b28Smrgchgrpprog=${CHGRPPROG-chgrp}
5881c81b28Smrgchmodprog=${CHMODPROG-chmod}
5981c81b28Smrgchownprog=${CHOWNPROG-chown}
6081c81b28Smrgcmpprog=${CMPPROG-cmp}
6181c81b28Smrgcpprog=${CPPROG-cp}
6281c81b28Smrgmkdirprog=${MKDIRPROG-mkdir}
6381c81b28Smrgmvprog=${MVPROG-mv}
6481c81b28Smrgrmprog=${RMPROG-rm}
6581c81b28Smrgstripprog=${STRIPPROG-strip}
6681c81b28Smrg
6781c81b28Smrgposix_mkdir=
6881c81b28Smrg
6981c81b28Smrg# Desired mode of installed file.
7081c81b28Smrgmode=0755
71af7c02bdSmrg
726c9573adSmrg# Create dirs (including intermediate dirs) using mode 755.
736c9573adSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
746c9573adSmrgmkdir_umask=22
756c9573adSmrg
766c9573adSmrgbackupsuffix=
77af7c02bdSmrgchgrpcmd=
7881c81b28Smrgchmodcmd=$chmodprog
7981c81b28Smrgchowncmd=
8081c81b28Smrgmvcmd=$mvprog
81af7c02bdSmrgrmcmd="$rmprog -f"
8281c81b28Smrgstripcmd=
8381c81b28Smrg
84af7c02bdSmrgsrc=
85af7c02bdSmrgdst=
86af7c02bdSmrgdir_arg=
8781c81b28Smrgdst_arg=
8881c81b28Smrg
8981c81b28Smrgcopy_on_change=false
906c9573adSmrgis_target_a_directory=possibly
91af7c02bdSmrg
9281c81b28Smrgusage="\
9381c81b28SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94af7c02bdSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95af7c02bdSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96af7c02bdSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97af7c02bdSmrg
98af7c02bdSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99af7c02bdSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100af7c02bdSmrgIn the 4th, create DIRECTORIES.
101af7c02bdSmrg
102af7c02bdSmrgOptions:
10381c81b28Smrg     --help     display this help and exit.
10481c81b28Smrg     --version  display version info and exit.
10581c81b28Smrg
10681c81b28Smrg  -c            (ignored)
1076c9573adSmrg  -C            install only if different (preserve data modification time)
10881c81b28Smrg  -d            create directories instead of installing files.
10981c81b28Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11081c81b28Smrg  -m MODE       $chmodprog installed files to MODE.
11181c81b28Smrg  -o USER       $chownprog installed files to USER.
1126c9573adSmrg  -p            pass -p to $cpprog.
11381c81b28Smrg  -s            $stripprog installed files.
1146c9573adSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11581c81b28Smrg  -t DIRECTORY  install into DIRECTORY.
11681c81b28Smrg  -T            report an error if DSTFILE is a directory.
117af7c02bdSmrg
118af7c02bdSmrgEnvironment variables override the default commands:
11981c81b28Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12081c81b28Smrg  RMPROG STRIPPROG
1216c9573adSmrg
1226c9573adSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1236c9573adSmrgit's up to you to specify -f if you want it.
1246c9573adSmrg
1256c9573adSmrgIf -S is not specified, no backups are attempted.
1266c9573adSmrg
1276c9573adSmrgEmail bug reports to bug-automake@gnu.org.
1286c9573adSmrgAutomake home page: https://www.gnu.org/software/automake/
129af7c02bdSmrg"
130af7c02bdSmrg
13181c81b28Smrgwhile test $# -ne 0; do
132af7c02bdSmrg  case $1 in
13381c81b28Smrg    -c) ;;
13481c81b28Smrg
13581c81b28Smrg    -C) copy_on_change=true;;
136af7c02bdSmrg
13781c81b28Smrg    -d) dir_arg=true;;
138af7c02bdSmrg
139af7c02bdSmrg    -g) chgrpcmd="$chgrpprog $2"
1406c9573adSmrg        shift;;
141af7c02bdSmrg
142af7c02bdSmrg    --help) echo "$usage"; exit $?;;
143af7c02bdSmrg
14481c81b28Smrg    -m) mode=$2
1456c9573adSmrg        case $mode in
1466c9573adSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1476c9573adSmrg            echo "$0: invalid mode: $mode" >&2
1486c9573adSmrg            exit 1;;
1496c9573adSmrg        esac
1506c9573adSmrg        shift;;
151af7c02bdSmrg
152af7c02bdSmrg    -o) chowncmd="$chownprog $2"
1536c9573adSmrg        shift;;
1546c9573adSmrg
1556c9573adSmrg    -p) cpprog="$cpprog -p";;
156af7c02bdSmrg
15781c81b28Smrg    -s) stripcmd=$stripprog;;
158af7c02bdSmrg
1596c9573adSmrg    -S) backupsuffix="$2"
1606c9573adSmrg        shift;;
161af7c02bdSmrg
1626c9573adSmrg    -t)
1636c9573adSmrg        is_target_a_directory=always
1646c9573adSmrg        dst_arg=$2
1656c9573adSmrg        # Protect names problematic for 'test' and other utilities.
1666c9573adSmrg        case $dst_arg in
1676c9573adSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1686c9573adSmrg        esac
1696c9573adSmrg        shift;;
1706c9573adSmrg
1716c9573adSmrg    -T) is_target_a_directory=never;;
172af7c02bdSmrg
173af7c02bdSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174af7c02bdSmrg
1756c9573adSmrg    --) shift
1766c9573adSmrg        break;;
17781c81b28Smrg
1786c9573adSmrg    -*) echo "$0: invalid option: $1" >&2
1796c9573adSmrg        exit 1;;
18081c81b28Smrg
18181c81b28Smrg    *)  break;;
182af7c02bdSmrg  esac
18381c81b28Smrg  shift
184af7c02bdSmrgdone
185af7c02bdSmrg
1866c9573adSmrg# We allow the use of options -d and -T together, by making -d
1876c9573adSmrg# take the precedence; this is for compatibility with GNU install.
1886c9573adSmrg
1896c9573adSmrgif test -n "$dir_arg"; then
1906c9573adSmrg  if test -n "$dst_arg"; then
1916c9573adSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1926c9573adSmrg    exit 1
1936c9573adSmrg  fi
1946c9573adSmrgfi
1956c9573adSmrg
19681c81b28Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19781c81b28Smrg  # When -d is used, all remaining arguments are directories to create.
19881c81b28Smrg  # When -t is used, the destination is already specified.
19981c81b28Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20081c81b28Smrg  for arg
20181c81b28Smrg  do
20281c81b28Smrg    if test -n "$dst_arg"; then
20381c81b28Smrg      # $@ is not empty: it contains at least $arg.
20481c81b28Smrg      set fnord "$@" "$dst_arg"
20581c81b28Smrg      shift # fnord
20681c81b28Smrg    fi
20781c81b28Smrg    shift # arg
20881c81b28Smrg    dst_arg=$arg
2096c9573adSmrg    # Protect names problematic for 'test' and other utilities.
2106dda92f9Smrg    case $dst_arg in
2116dda92f9Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2126dda92f9Smrg    esac
21381c81b28Smrg  done
21481c81b28Smrgfi
21581c81b28Smrg
21681c81b28Smrgif test $# -eq 0; then
217af7c02bdSmrg  if test -z "$dir_arg"; then
218af7c02bdSmrg    echo "$0: no input file specified." >&2
219af7c02bdSmrg    exit 1
220af7c02bdSmrg  fi
2216c9573adSmrg  # It's OK to call 'install-sh -d' without argument.
222af7c02bdSmrg  # This can happen when creating conditional directories.
223af7c02bdSmrg  exit 0
224af7c02bdSmrgfi
225af7c02bdSmrg
2266c9573adSmrgif test -z "$dir_arg"; then
2276c9573adSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2286c9573adSmrg    if test ! -d "$dst_arg"; then
2296c9573adSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2306c9573adSmrg      exit 1
2316c9573adSmrg    fi
2326c9573adSmrg  fi
2336c9573adSmrgfi
2346c9573adSmrg
23581c81b28Smrgif test -z "$dir_arg"; then
2366dda92f9Smrg  do_exit='(exit $ret); exit $ret'
2376dda92f9Smrg  trap "ret=129; $do_exit" 1
2386dda92f9Smrg  trap "ret=130; $do_exit" 2
2396dda92f9Smrg  trap "ret=141; $do_exit" 13
2406dda92f9Smrg  trap "ret=143; $do_exit" 15
24181c81b28Smrg
24281c81b28Smrg  # Set umask so as not to create temps with too-generous modes.
24381c81b28Smrg  # However, 'strip' requires both read and write access to temps.
24481c81b28Smrg  case $mode in
24581c81b28Smrg    # Optimize common cases.
24681c81b28Smrg    *644) cp_umask=133;;
24781c81b28Smrg    *755) cp_umask=22;;
24881c81b28Smrg
24981c81b28Smrg    *[0-7])
25081c81b28Smrg      if test -z "$stripcmd"; then
2516c9573adSmrg        u_plus_rw=
25281c81b28Smrg      else
2536c9573adSmrg        u_plus_rw='% 200'
25481c81b28Smrg      fi
25581c81b28Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25681c81b28Smrg    *)
25781c81b28Smrg      if test -z "$stripcmd"; then
2586c9573adSmrg        u_plus_rw=
25981c81b28Smrg      else
2606c9573adSmrg        u_plus_rw=,u+rw
26181c81b28Smrg      fi
26281c81b28Smrg      cp_umask=$mode$u_plus_rw;;
26381c81b28Smrg  esac
26481c81b28Smrgfi
26581c81b28Smrg
266af7c02bdSmrgfor src
267af7c02bdSmrgdo
2686c9573adSmrg  # Protect names problematic for 'test' and other utilities.
269af7c02bdSmrg  case $src in
2706dda92f9Smrg    -* | [=\(\)!]) src=./$src;;
271af7c02bdSmrg  esac
272af7c02bdSmrg
273af7c02bdSmrg  if test -n "$dir_arg"; then
274af7c02bdSmrg    dst=$src
27581c81b28Smrg    dstdir=$dst
27681c81b28Smrg    test -d "$dstdir"
27781c81b28Smrg    dstdir_status=$?
2786c9573adSmrg    # Don't chown directories that already exist.
2796c9573adSmrg    if test $dstdir_status = 0; then
2806c9573adSmrg      chowncmd=""
2816c9573adSmrg    fi
282af7c02bdSmrg  else
28381c81b28Smrg
284af7c02bdSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285af7c02bdSmrg    # might cause directories to be created, which would be especially bad
286af7c02bdSmrg    # if $src (and thus $dsttmp) contains '*'.
287af7c02bdSmrg    if test ! -f "$src" && test ! -d "$src"; then
288af7c02bdSmrg      echo "$0: $src does not exist." >&2
289af7c02bdSmrg      exit 1
290af7c02bdSmrg    fi
291af7c02bdSmrg
29281c81b28Smrg    if test -z "$dst_arg"; then
293af7c02bdSmrg      echo "$0: no destination specified." >&2
294af7c02bdSmrg      exit 1
295af7c02bdSmrg    fi
29681c81b28Smrg    dst=$dst_arg
297af7c02bdSmrg
2986c9573adSmrg    # If destination is a directory, append the input filename.
299af7c02bdSmrg    if test -d "$dst"; then
3006c9573adSmrg      if test "$is_target_a_directory" = never; then
3016c9573adSmrg        echo "$0: $dst_arg: Is a directory" >&2
3026c9573adSmrg        exit 1
303af7c02bdSmrg      fi
30481c81b28Smrg      dstdir=$dst
3056c9573adSmrg      dstbase=`basename "$src"`
3066c9573adSmrg      case $dst in
3076c9573adSmrg	*/) dst=$dst$dstbase;;
3086c9573adSmrg	*)  dst=$dst/$dstbase;;
3096c9573adSmrg      esac
31081c81b28Smrg      dstdir_status=0
31181c81b28Smrg    else
3126c9573adSmrg      dstdir=`dirname "$dst"`
31381c81b28Smrg      test -d "$dstdir"
31481c81b28Smrg      dstdir_status=$?
315af7c02bdSmrg    fi
316af7c02bdSmrg  fi
317af7c02bdSmrg
3186c9573adSmrg  case $dstdir in
3196c9573adSmrg    */) dstdirslash=$dstdir;;
3206c9573adSmrg    *)  dstdirslash=$dstdir/;;
3216c9573adSmrg  esac
3226c9573adSmrg
32381c81b28Smrg  obsolete_mkdir_used=false
32481c81b28Smrg
32581c81b28Smrg  if test $dstdir_status != 0; then
32681c81b28Smrg    case $posix_mkdir in
32781c81b28Smrg      '')
3286c9573adSmrg        # With -d, create the new directory with the user-specified mode.
3296c9573adSmrg        # Otherwise, rely on $mkdir_umask.
3306c9573adSmrg        if test -n "$dir_arg"; then
3316c9573adSmrg          mkdir_mode=-m$mode
3326c9573adSmrg        else
3336c9573adSmrg          mkdir_mode=
3346c9573adSmrg        fi
3356c9573adSmrg
3366c9573adSmrg        posix_mkdir=false
3376c9573adSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3386c9573adSmrg	# here however when possible just to lower collision chance.
3396c9573adSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3406c9573adSmrg
3416c9573adSmrg	trap '
3426c9573adSmrg	  ret=$?
3436c9573adSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3446c9573adSmrg	  exit $ret
3456c9573adSmrg	' 0
3466c9573adSmrg
3476c9573adSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3486c9573adSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3496c9573adSmrg	# directory is successfully created first before we actually test
3506c9573adSmrg	# 'mkdir -p'.
3516c9573adSmrg	if (umask $mkdir_umask &&
3526c9573adSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3536c9573adSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3546c9573adSmrg	then
3556c9573adSmrg	  if test -z "$dir_arg" || {
3566c9573adSmrg	       # Check for POSIX incompatibilities with -m.
3576c9573adSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3586c9573adSmrg	       # other-writable bit of parent directory when it shouldn't.
3596c9573adSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3606c9573adSmrg	       test_tmpdir="$tmpdir/a"
3616c9573adSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3626c9573adSmrg	       case $ls_ld_tmpdir in
3636c9573adSmrg		 d????-?r-*) different_mode=700;;
3646c9573adSmrg		 d????-?--*) different_mode=755;;
3656c9573adSmrg		 *) false;;
3666c9573adSmrg	       esac &&
3676c9573adSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3686c9573adSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3696c9573adSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3706c9573adSmrg	       }
3716c9573adSmrg	     }
3726c9573adSmrg	  then posix_mkdir=:
3736c9573adSmrg	  fi
3746c9573adSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37581c81b28Smrg	else
3766c9573adSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3776c9573adSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37881c81b28Smrg	fi
3796c9573adSmrg	trap '' 0;;
38081c81b28Smrg    esac
381af7c02bdSmrg
38281c81b28Smrg    if
38381c81b28Smrg      $posix_mkdir && (
3846c9573adSmrg        umask $mkdir_umask &&
3856c9573adSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38681c81b28Smrg      )
38781c81b28Smrg    then :
38881c81b28Smrg    else
389af7c02bdSmrg
3906c9573adSmrg      # mkdir does not conform to POSIX,
39181c81b28Smrg      # or it failed possibly due to a race condition.  Create the
39281c81b28Smrg      # directory the slow way, step by step, checking for races as we go.
393af7c02bdSmrg
39481c81b28Smrg      case $dstdir in
3956c9573adSmrg        /*) prefix='/';;
3966c9573adSmrg        [-=\(\)!]*) prefix='./';;
3976c9573adSmrg        *)  prefix='';;
39881c81b28Smrg      esac
399af7c02bdSmrg
40081c81b28Smrg      oIFS=$IFS
40181c81b28Smrg      IFS=/
4026c9573adSmrg      set -f
40381c81b28Smrg      set fnord $dstdir
404af7c02bdSmrg      shift
4056c9573adSmrg      set +f
40681c81b28Smrg      IFS=$oIFS
40781c81b28Smrg
40881c81b28Smrg      prefixes=
40981c81b28Smrg
41081c81b28Smrg      for d
41181c81b28Smrg      do
4126c9573adSmrg        test X"$d" = X && continue
4136c9573adSmrg
4146c9573adSmrg        prefix=$prefix$d
4156c9573adSmrg        if test -d "$prefix"; then
4166c9573adSmrg          prefixes=
4176c9573adSmrg        else
4186c9573adSmrg          if $posix_mkdir; then
4196c9573adSmrg            (umask $mkdir_umask &&
4206c9573adSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4216c9573adSmrg            # Don't fail if two instances are running concurrently.
4226c9573adSmrg            test -d "$prefix" || exit 1
4236c9573adSmrg          else
4246c9573adSmrg            case $prefix in
4256c9573adSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4266c9573adSmrg              *) qprefix=$prefix;;
4276c9573adSmrg            esac
4286c9573adSmrg            prefixes="$prefixes '$qprefix'"
4296c9573adSmrg          fi
4306c9573adSmrg        fi
4316c9573adSmrg        prefix=$prefix/
43281c81b28Smrg      done
43381c81b28Smrg
43481c81b28Smrg      if test -n "$prefixes"; then
4356c9573adSmrg        # Don't fail if two instances are running concurrently.
4366c9573adSmrg        (umask $mkdir_umask &&
4376c9573adSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4386c9573adSmrg          test -d "$dstdir" || exit 1
4396c9573adSmrg        obsolete_mkdir_used=true
440af7c02bdSmrg      fi
44181c81b28Smrg    fi
442af7c02bdSmrg  fi
443af7c02bdSmrg
444af7c02bdSmrg  if test -n "$dir_arg"; then
44581c81b28Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44681c81b28Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44781c81b28Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44881c81b28Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449af7c02bdSmrg  else
450af7c02bdSmrg
451af7c02bdSmrg    # Make a couple of temp file names in the proper directory.
4526c9573adSmrg    dsttmp=${dstdirslash}_inst.$$_
4536c9573adSmrg    rmtmp=${dstdirslash}_rm.$$_
454af7c02bdSmrg
455af7c02bdSmrg    # Trap to clean up those temp files at exit.
456af7c02bdSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457af7c02bdSmrg
458af7c02bdSmrg    # Copy the file name to the temp name.
4596c9573adSmrg    (umask $cp_umask &&
4606c9573adSmrg     { test -z "$stripcmd" || {
4616c9573adSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4626c9573adSmrg	 # which would cause strip to fail.
4636c9573adSmrg	 if test -z "$doit"; then
4646c9573adSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4656c9573adSmrg	 else
4666c9573adSmrg	   $doit touch "$dsttmp"
4676c9573adSmrg	 fi
4686c9573adSmrg       }
4696c9573adSmrg     } &&
4706c9573adSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471af7c02bdSmrg
472af7c02bdSmrg    # and set any options; do chmod last to preserve setuid bits.
473af7c02bdSmrg    #
474af7c02bdSmrg    # If any of these fail, we abort the whole thing.  If we want to
475af7c02bdSmrg    # ignore errors from any of these, just make sure not to ignore
476af7c02bdSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477af7c02bdSmrg    #
47881c81b28Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47981c81b28Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48081c81b28Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48181c81b28Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48281c81b28Smrg
48381c81b28Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48481c81b28Smrg    if $copy_on_change &&
4856c9573adSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4866c9573adSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4876c9573adSmrg       set -f &&
48881c81b28Smrg       set X $old && old=:$2:$4:$5:$6 &&
48981c81b28Smrg       set X $new && new=:$2:$4:$5:$6 &&
4906c9573adSmrg       set +f &&
49181c81b28Smrg       test "$old" = "$new" &&
49281c81b28Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49381c81b28Smrg    then
49481c81b28Smrg      rm -f "$dsttmp"
49581c81b28Smrg    else
4966c9573adSmrg      # If $backupsuffix is set, and the file being installed
4976c9573adSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4986c9573adSmrg      # e.g., if mv doesn't support -f.
4996c9573adSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5006c9573adSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5016c9573adSmrg      fi
5026c9573adSmrg
50381c81b28Smrg      # Rename the file to the real destination.
50481c81b28Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50581c81b28Smrg
50681c81b28Smrg      # The rename failed, perhaps because mv can't rename something else
50781c81b28Smrg      # to itself, or perhaps because mv is so ancient that it does not
50881c81b28Smrg      # support -f.
50981c81b28Smrg      {
5106c9573adSmrg        # Now remove or move aside any old file at destination location.
5116c9573adSmrg        # We try this two ways since rm can't unlink itself on some
5126c9573adSmrg        # systems and the destination file might be busy for other
5136c9573adSmrg        # reasons.  In this case, the final cleanup might fail but the new
5146c9573adSmrg        # file should still install successfully.
5156c9573adSmrg        {
5166c9573adSmrg          test ! -f "$dst" ||
5176c9573adSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
5186c9573adSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5196c9573adSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5206c9573adSmrg          } ||
5216c9573adSmrg          { echo "$0: cannot unlink or rename $dst" >&2
5226c9573adSmrg            (exit 1); exit 1
5236c9573adSmrg          }
5246c9573adSmrg        } &&
5256c9573adSmrg
5266c9573adSmrg        # Now rename the file to the real destination.
5276c9573adSmrg        $doit $mvcmd "$dsttmp" "$dst"
52881c81b28Smrg      }
52981c81b28Smrg    fi || exit 1
53081c81b28Smrg
53181c81b28Smrg    trap '' 0
53281c81b28Smrg  fi
533af7c02bdSmrgdone
534af7c02bdSmrg
535af7c02bdSmrg# Local variables:
5366c9573adSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537af7c02bdSmrg# time-stamp-start: "scriptversion="
538af7c02bdSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5396c9573adSmrg# time-stamp-time-zone: "UTC0"
54081c81b28Smrg# time-stamp-end: "; # UTC"
541af7c02bdSmrg# End:
542