install-sh revision 7ed541ca
172b4363aSmrg#!/bin/sh
272b4363aSmrg# install - install a program, script, or datafile
372b4363aSmrg
47ed541caSmrgscriptversion=2020-11-14.01; # UTC
572b4363aSmrg
672b4363aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
772b4363aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
872b4363aSmrg# following copyright and license.
972b4363aSmrg#
1072b4363aSmrg# Copyright (C) 1994 X Consortium
1172b4363aSmrg#
1272b4363aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1372b4363aSmrg# of this software and associated documentation files (the "Software"), to
1472b4363aSmrg# deal in the Software without restriction, including without limitation the
1572b4363aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1672b4363aSmrg# sell copies of the Software, and to permit persons to whom the Software is
1772b4363aSmrg# furnished to do so, subject to the following conditions:
1872b4363aSmrg#
1972b4363aSmrg# The above copyright notice and this permission notice shall be included in
2072b4363aSmrg# all copies or substantial portions of the Software.
2172b4363aSmrg#
2272b4363aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2372b4363aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2472b4363aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2572b4363aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2672b4363aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2772b4363aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2872b4363aSmrg#
2972b4363aSmrg# Except as contained in this notice, the name of the X Consortium shall not
3072b4363aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3172b4363aSmrg# ings in this Software without prior written authorization from the X Consor-
3272b4363aSmrg# tium.
3372b4363aSmrg#
3472b4363aSmrg#
3572b4363aSmrg# FSF changes to this file are in the public domain.
3672b4363aSmrg#
3772b4363aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3833dddc75Smrg# 'make' implicit rules from creating a file called install from it
3972b4363aSmrg# when there is no Makefile.
4072b4363aSmrg#
4172b4363aSmrg# This script is compatible with the BSD install script, but was written
4272b4363aSmrg# from scratch.
4372b4363aSmrg
4433dddc75Smrgtab='	'
4572b4363aSmrgnl='
4672b4363aSmrg'
4733dddc75SmrgIFS=" $tab$nl"
4872b4363aSmrg
4933dddc75Smrg# Set DOITPROG to "echo" to test this script.
5072b4363aSmrg
51dd77ae96Smrgdoit=${DOITPROG-}
5233dddc75Smrgdoit_exec=${doit:-exec}
5372b4363aSmrg
5472b4363aSmrg# Put in absolute file names if you don't have them in your path;
5572b4363aSmrg# or use environment vars.
5672b4363aSmrg
57dd77ae96Smrgchgrpprog=${CHGRPPROG-chgrp}
58dd77ae96Smrgchmodprog=${CHMODPROG-chmod}
59dd77ae96Smrgchownprog=${CHOWNPROG-chown}
60dd77ae96Smrgcmpprog=${CMPPROG-cmp}
61dd77ae96Smrgcpprog=${CPPROG-cp}
62dd77ae96Smrgmkdirprog=${MKDIRPROG-mkdir}
63dd77ae96Smrgmvprog=${MVPROG-mv}
64dd77ae96Smrgrmprog=${RMPROG-rm}
65dd77ae96Smrgstripprog=${STRIPPROG-strip}
66dd77ae96Smrg
6772b4363aSmrgposix_mkdir=
6872b4363aSmrg
6972b4363aSmrg# Desired mode of installed file.
7072b4363aSmrgmode=0755
7172b4363aSmrg
727ed541caSmrg# Create dirs (including intermediate dirs) using mode 755.
737ed541caSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
747ed541caSmrgmkdir_umask=22
757ed541caSmrg
767ed541caSmrgbackupsuffix=
77dd77ae96Smrgchgrpcmd=
7872b4363aSmrgchmodcmd=$chmodprog
7972b4363aSmrgchowncmd=
80dd77ae96Smrgmvcmd=$mvprog
8172b4363aSmrgrmcmd="$rmprog -f"
82dd77ae96Smrgstripcmd=
83dd77ae96Smrg
8472b4363aSmrgsrc=
8572b4363aSmrgdst=
8672b4363aSmrgdir_arg=
87dd77ae96Smrgdst_arg=
88dd77ae96Smrg
89dd77ae96Smrgcopy_on_change=false
9033dddc75Smrgis_target_a_directory=possibly
9172b4363aSmrg
92dd77ae96Smrgusage="\
93dd77ae96SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9472b4363aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9572b4363aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9672b4363aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9772b4363aSmrg
9872b4363aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9972b4363aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10072b4363aSmrgIn the 4th, create DIRECTORIES.
10172b4363aSmrg
10272b4363aSmrgOptions:
103dd77ae96Smrg     --help     display this help and exit.
104dd77ae96Smrg     --version  display version info and exit.
105dd77ae96Smrg
106dd77ae96Smrg  -c            (ignored)
1077ed541caSmrg  -C            install only if different (preserve data modification time)
108dd77ae96Smrg  -d            create directories instead of installing files.
109dd77ae96Smrg  -g GROUP      $chgrpprog installed files to GROUP.
110dd77ae96Smrg  -m MODE       $chmodprog installed files to MODE.
111dd77ae96Smrg  -o USER       $chownprog installed files to USER.
1127ed541caSmrg  -p            pass -p to $cpprog.
113dd77ae96Smrg  -s            $stripprog installed files.
1147ed541caSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115dd77ae96Smrg  -t DIRECTORY  install into DIRECTORY.
116dd77ae96Smrg  -T            report an error if DSTFILE is a directory.
11772b4363aSmrg
11872b4363aSmrgEnvironment variables override the default commands:
119dd77ae96Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120dd77ae96Smrg  RMPROG STRIPPROG
1217ed541caSmrg
1227ed541caSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1237ed541caSmrgit's up to you to specify -f if you want it.
1247ed541caSmrg
1257ed541caSmrgIf -S is not specified, no backups are attempted.
1267ed541caSmrg
1277ed541caSmrgEmail bug reports to bug-automake@gnu.org.
1287ed541caSmrgAutomake home page: https://www.gnu.org/software/automake/
12972b4363aSmrg"
13072b4363aSmrg
13172b4363aSmrgwhile test $# -ne 0; do
13272b4363aSmrg  case $1 in
133dd77ae96Smrg    -c) ;;
134dd77ae96Smrg
135dd77ae96Smrg    -C) copy_on_change=true;;
13672b4363aSmrg
137dd77ae96Smrg    -d) dir_arg=true;;
13872b4363aSmrg
13972b4363aSmrg    -g) chgrpcmd="$chgrpprog $2"
14033dddc75Smrg        shift;;
14172b4363aSmrg
14272b4363aSmrg    --help) echo "$usage"; exit $?;;
14372b4363aSmrg
14472b4363aSmrg    -m) mode=$2
14533dddc75Smrg        case $mode in
14633dddc75Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14733dddc75Smrg            echo "$0: invalid mode: $mode" >&2
14833dddc75Smrg            exit 1;;
14933dddc75Smrg        esac
15033dddc75Smrg        shift;;
15172b4363aSmrg
15272b4363aSmrg    -o) chowncmd="$chownprog $2"
15333dddc75Smrg        shift;;
15472b4363aSmrg
1557ed541caSmrg    -p) cpprog="$cpprog -p";;
1567ed541caSmrg
157dd77ae96Smrg    -s) stripcmd=$stripprog;;
15872b4363aSmrg
1597ed541caSmrg    -S) backupsuffix="$2"
1607ed541caSmrg        shift;;
1617ed541caSmrg
16233dddc75Smrg    -t)
16333dddc75Smrg        is_target_a_directory=always
16433dddc75Smrg        dst_arg=$2
16533dddc75Smrg        # Protect names problematic for 'test' and other utilities.
16633dddc75Smrg        case $dst_arg in
16733dddc75Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16833dddc75Smrg        esac
16933dddc75Smrg        shift;;
17072b4363aSmrg
17133dddc75Smrg    -T) is_target_a_directory=never;;
17272b4363aSmrg
17372b4363aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
17472b4363aSmrg
17533dddc75Smrg    --) shift
17633dddc75Smrg        break;;
17772b4363aSmrg
17833dddc75Smrg    -*) echo "$0: invalid option: $1" >&2
17933dddc75Smrg        exit 1;;
18072b4363aSmrg
18172b4363aSmrg    *)  break;;
18272b4363aSmrg  esac
183dd77ae96Smrg  shift
18472b4363aSmrgdone
18572b4363aSmrg
18633dddc75Smrg# We allow the use of options -d and -T together, by making -d
18733dddc75Smrg# take the precedence; this is for compatibility with GNU install.
18833dddc75Smrg
18933dddc75Smrgif test -n "$dir_arg"; then
19033dddc75Smrg  if test -n "$dst_arg"; then
19133dddc75Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19233dddc75Smrg    exit 1
19333dddc75Smrg  fi
19433dddc75Smrgfi
19533dddc75Smrg
196dd77ae96Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19772b4363aSmrg  # When -d is used, all remaining arguments are directories to create.
19872b4363aSmrg  # When -t is used, the destination is already specified.
19972b4363aSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20072b4363aSmrg  for arg
20172b4363aSmrg  do
202dd77ae96Smrg    if test -n "$dst_arg"; then
20372b4363aSmrg      # $@ is not empty: it contains at least $arg.
204dd77ae96Smrg      set fnord "$@" "$dst_arg"
20572b4363aSmrg      shift # fnord
20672b4363aSmrg    fi
20772b4363aSmrg    shift # arg
208dd77ae96Smrg    dst_arg=$arg
20933dddc75Smrg    # Protect names problematic for 'test' and other utilities.
21033dddc75Smrg    case $dst_arg in
21133dddc75Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21233dddc75Smrg    esac
21372b4363aSmrg  done
21472b4363aSmrgfi
21572b4363aSmrg
21672b4363aSmrgif test $# -eq 0; then
21772b4363aSmrg  if test -z "$dir_arg"; then
21872b4363aSmrg    echo "$0: no input file specified." >&2
21972b4363aSmrg    exit 1
22072b4363aSmrg  fi
22133dddc75Smrg  # It's OK to call 'install-sh -d' without argument.
22272b4363aSmrg  # This can happen when creating conditional directories.
22372b4363aSmrg  exit 0
22472b4363aSmrgfi
22572b4363aSmrg
22672b4363aSmrgif test -z "$dir_arg"; then
22733dddc75Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22833dddc75Smrg    if test ! -d "$dst_arg"; then
22933dddc75Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23033dddc75Smrg      exit 1
23133dddc75Smrg    fi
23233dddc75Smrg  fi
23333dddc75Smrgfi
23433dddc75Smrg
23533dddc75Smrgif test -z "$dir_arg"; then
23633dddc75Smrg  do_exit='(exit $ret); exit $ret'
23733dddc75Smrg  trap "ret=129; $do_exit" 1
23833dddc75Smrg  trap "ret=130; $do_exit" 2
23933dddc75Smrg  trap "ret=141; $do_exit" 13
24033dddc75Smrg  trap "ret=143; $do_exit" 15
24172b4363aSmrg
24272b4363aSmrg  # Set umask so as not to create temps with too-generous modes.
24372b4363aSmrg  # However, 'strip' requires both read and write access to temps.
24472b4363aSmrg  case $mode in
24572b4363aSmrg    # Optimize common cases.
24672b4363aSmrg    *644) cp_umask=133;;
24772b4363aSmrg    *755) cp_umask=22;;
24872b4363aSmrg
24972b4363aSmrg    *[0-7])
25072b4363aSmrg      if test -z "$stripcmd"; then
25133dddc75Smrg        u_plus_rw=
25272b4363aSmrg      else
25333dddc75Smrg        u_plus_rw='% 200'
25472b4363aSmrg      fi
25572b4363aSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25672b4363aSmrg    *)
25772b4363aSmrg      if test -z "$stripcmd"; then
25833dddc75Smrg        u_plus_rw=
25972b4363aSmrg      else
26033dddc75Smrg        u_plus_rw=,u+rw
26172b4363aSmrg      fi
26272b4363aSmrg      cp_umask=$mode$u_plus_rw;;
26372b4363aSmrg  esac
26472b4363aSmrgfi
26572b4363aSmrg
26672b4363aSmrgfor src
26772b4363aSmrgdo
26833dddc75Smrg  # Protect names problematic for 'test' and other utilities.
26972b4363aSmrg  case $src in
27033dddc75Smrg    -* | [=\(\)!]) src=./$src;;
27172b4363aSmrg  esac
27272b4363aSmrg
27372b4363aSmrg  if test -n "$dir_arg"; then
27472b4363aSmrg    dst=$src
27572b4363aSmrg    dstdir=$dst
27672b4363aSmrg    test -d "$dstdir"
27772b4363aSmrg    dstdir_status=$?
2787ed541caSmrg    # Don't chown directories that already exist.
2797ed541caSmrg    if test $dstdir_status = 0; then
2807ed541caSmrg      chowncmd=""
2817ed541caSmrg    fi
28272b4363aSmrg  else
28372b4363aSmrg
28472b4363aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28572b4363aSmrg    # might cause directories to be created, which would be especially bad
28672b4363aSmrg    # if $src (and thus $dsttmp) contains '*'.
28772b4363aSmrg    if test ! -f "$src" && test ! -d "$src"; then
28872b4363aSmrg      echo "$0: $src does not exist." >&2
28972b4363aSmrg      exit 1
29072b4363aSmrg    fi
29172b4363aSmrg
292dd77ae96Smrg    if test -z "$dst_arg"; then
29372b4363aSmrg      echo "$0: no destination specified." >&2
29472b4363aSmrg      exit 1
29572b4363aSmrg    fi
296dd77ae96Smrg    dst=$dst_arg
29772b4363aSmrg
2987ed541caSmrg    # If destination is a directory, append the input filename.
29972b4363aSmrg    if test -d "$dst"; then
30033dddc75Smrg      if test "$is_target_a_directory" = never; then
30133dddc75Smrg        echo "$0: $dst_arg: Is a directory" >&2
30233dddc75Smrg        exit 1
30372b4363aSmrg      fi
30472b4363aSmrg      dstdir=$dst
3057ed541caSmrg      dstbase=`basename "$src"`
3067ed541caSmrg      case $dst in
3077ed541caSmrg	*/) dst=$dst$dstbase;;
3087ed541caSmrg	*)  dst=$dst/$dstbase;;
3097ed541caSmrg      esac
31072b4363aSmrg      dstdir_status=0
31172b4363aSmrg    else
31233dddc75Smrg      dstdir=`dirname "$dst"`
31372b4363aSmrg      test -d "$dstdir"
31472b4363aSmrg      dstdir_status=$?
31572b4363aSmrg    fi
31672b4363aSmrg  fi
31772b4363aSmrg
3187ed541caSmrg  case $dstdir in
3197ed541caSmrg    */) dstdirslash=$dstdir;;
3207ed541caSmrg    *)  dstdirslash=$dstdir/;;
3217ed541caSmrg  esac
3227ed541caSmrg
32372b4363aSmrg  obsolete_mkdir_used=false
32472b4363aSmrg
32572b4363aSmrg  if test $dstdir_status != 0; then
32672b4363aSmrg    case $posix_mkdir in
32772b4363aSmrg      '')
32833dddc75Smrg        # With -d, create the new directory with the user-specified mode.
32933dddc75Smrg        # Otherwise, rely on $mkdir_umask.
33033dddc75Smrg        if test -n "$dir_arg"; then
33133dddc75Smrg          mkdir_mode=-m$mode
33233dddc75Smrg        else
33333dddc75Smrg          mkdir_mode=
33433dddc75Smrg        fi
33533dddc75Smrg
33633dddc75Smrg        posix_mkdir=false
3377ed541caSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3387ed541caSmrg	# here however when possible just to lower collision chance.
3397ed541caSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3407ed541caSmrg
3417ed541caSmrg	trap '
3427ed541caSmrg	  ret=$?
3437ed541caSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3447ed541caSmrg	  exit $ret
3457ed541caSmrg	' 0
3467ed541caSmrg
3477ed541caSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3487ed541caSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3497ed541caSmrg	# directory is successfully created first before we actually test
3507ed541caSmrg	# 'mkdir -p'.
3517ed541caSmrg	if (umask $mkdir_umask &&
3527ed541caSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3537ed541caSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3547ed541caSmrg	then
3557ed541caSmrg	  if test -z "$dir_arg" || {
3567ed541caSmrg	       # Check for POSIX incompatibilities with -m.
3577ed541caSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3587ed541caSmrg	       # other-writable bit of parent directory when it shouldn't.
3597ed541caSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3607ed541caSmrg	       test_tmpdir="$tmpdir/a"
3617ed541caSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3627ed541caSmrg	       case $ls_ld_tmpdir in
3637ed541caSmrg		 d????-?r-*) different_mode=700;;
3647ed541caSmrg		 d????-?--*) different_mode=755;;
3657ed541caSmrg		 *) false;;
3667ed541caSmrg	       esac &&
3677ed541caSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3687ed541caSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3697ed541caSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3707ed541caSmrg	       }
3717ed541caSmrg	     }
3727ed541caSmrg	  then posix_mkdir=:
3737ed541caSmrg	  fi
3747ed541caSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3757ed541caSmrg	else
3767ed541caSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3777ed541caSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3787ed541caSmrg	fi
3797ed541caSmrg	trap '' 0;;
38072b4363aSmrg    esac
38172b4363aSmrg
38272b4363aSmrg    if
38372b4363aSmrg      $posix_mkdir && (
38433dddc75Smrg        umask $mkdir_umask &&
38533dddc75Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38672b4363aSmrg      )
38772b4363aSmrg    then :
38872b4363aSmrg    else
38972b4363aSmrg
3907ed541caSmrg      # mkdir does not conform to POSIX,
39172b4363aSmrg      # or it failed possibly due to a race condition.  Create the
39272b4363aSmrg      # directory the slow way, step by step, checking for races as we go.
39372b4363aSmrg
39472b4363aSmrg      case $dstdir in
39533dddc75Smrg        /*) prefix='/';;
39633dddc75Smrg        [-=\(\)!]*) prefix='./';;
39733dddc75Smrg        *)  prefix='';;
39872b4363aSmrg      esac
39972b4363aSmrg
40072b4363aSmrg      oIFS=$IFS
40172b4363aSmrg      IFS=/
40233dddc75Smrg      set -f
40372b4363aSmrg      set fnord $dstdir
40472b4363aSmrg      shift
40533dddc75Smrg      set +f
40672b4363aSmrg      IFS=$oIFS
40772b4363aSmrg
40872b4363aSmrg      prefixes=
40972b4363aSmrg
41072b4363aSmrg      for d
41172b4363aSmrg      do
41233dddc75Smrg        test X"$d" = X && continue
41333dddc75Smrg
41433dddc75Smrg        prefix=$prefix$d
41533dddc75Smrg        if test -d "$prefix"; then
41633dddc75Smrg          prefixes=
41733dddc75Smrg        else
41833dddc75Smrg          if $posix_mkdir; then
4197ed541caSmrg            (umask $mkdir_umask &&
42033dddc75Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42133dddc75Smrg            # Don't fail if two instances are running concurrently.
42233dddc75Smrg            test -d "$prefix" || exit 1
42333dddc75Smrg          else
42433dddc75Smrg            case $prefix in
42533dddc75Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42633dddc75Smrg              *) qprefix=$prefix;;
42733dddc75Smrg            esac
42833dddc75Smrg            prefixes="$prefixes '$qprefix'"
42933dddc75Smrg          fi
43033dddc75Smrg        fi
43133dddc75Smrg        prefix=$prefix/
43272b4363aSmrg      done
43372b4363aSmrg
43472b4363aSmrg      if test -n "$prefixes"; then
43533dddc75Smrg        # Don't fail if two instances are running concurrently.
43633dddc75Smrg        (umask $mkdir_umask &&
43733dddc75Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43833dddc75Smrg          test -d "$dstdir" || exit 1
43933dddc75Smrg        obsolete_mkdir_used=true
44072b4363aSmrg      fi
44172b4363aSmrg    fi
44272b4363aSmrg  fi
44372b4363aSmrg
44472b4363aSmrg  if test -n "$dir_arg"; then
44572b4363aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44672b4363aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44772b4363aSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44872b4363aSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44972b4363aSmrg  else
45072b4363aSmrg
45172b4363aSmrg    # Make a couple of temp file names in the proper directory.
4527ed541caSmrg    dsttmp=${dstdirslash}_inst.$$_
4537ed541caSmrg    rmtmp=${dstdirslash}_rm.$$_
45472b4363aSmrg
45572b4363aSmrg    # Trap to clean up those temp files at exit.
45672b4363aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45772b4363aSmrg
45872b4363aSmrg    # Copy the file name to the temp name.
4597ed541caSmrg    (umask $cp_umask &&
4607ed541caSmrg     { test -z "$stripcmd" || {
4617ed541caSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4627ed541caSmrg	 # which would cause strip to fail.
4637ed541caSmrg	 if test -z "$doit"; then
4647ed541caSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4657ed541caSmrg	 else
4667ed541caSmrg	   $doit touch "$dsttmp"
4677ed541caSmrg	 fi
4687ed541caSmrg       }
4697ed541caSmrg     } &&
4707ed541caSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47172b4363aSmrg
47272b4363aSmrg    # and set any options; do chmod last to preserve setuid bits.
47372b4363aSmrg    #
47472b4363aSmrg    # If any of these fail, we abort the whole thing.  If we want to
47572b4363aSmrg    # ignore errors from any of these, just make sure not to ignore
47672b4363aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47772b4363aSmrg    #
478dd77ae96Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479dd77ae96Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480dd77ae96Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481dd77ae96Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482dd77ae96Smrg
483dd77ae96Smrg    # If -C, don't bother to copy if it wouldn't change the file.
484dd77ae96Smrg    if $copy_on_change &&
48533dddc75Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48633dddc75Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48733dddc75Smrg       set -f &&
488dd77ae96Smrg       set X $old && old=:$2:$4:$5:$6 &&
489dd77ae96Smrg       set X $new && new=:$2:$4:$5:$6 &&
49033dddc75Smrg       set +f &&
491dd77ae96Smrg       test "$old" = "$new" &&
492dd77ae96Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493dd77ae96Smrg    then
494dd77ae96Smrg      rm -f "$dsttmp"
495dd77ae96Smrg    else
4967ed541caSmrg      # If $backupsuffix is set, and the file being installed
4977ed541caSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4987ed541caSmrg      # e.g., if mv doesn't support -f.
4997ed541caSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5007ed541caSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5017ed541caSmrg      fi
5027ed541caSmrg
503dd77ae96Smrg      # Rename the file to the real destination.
504dd77ae96Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505dd77ae96Smrg
506dd77ae96Smrg      # The rename failed, perhaps because mv can't rename something else
507dd77ae96Smrg      # to itself, or perhaps because mv is so ancient that it does not
508dd77ae96Smrg      # support -f.
509dd77ae96Smrg      {
51033dddc75Smrg        # Now remove or move aside any old file at destination location.
51133dddc75Smrg        # We try this two ways since rm can't unlink itself on some
51233dddc75Smrg        # systems and the destination file might be busy for other
51333dddc75Smrg        # reasons.  In this case, the final cleanup might fail but the new
51433dddc75Smrg        # file should still install successfully.
51533dddc75Smrg        {
51633dddc75Smrg          test ! -f "$dst" ||
5177ed541caSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51833dddc75Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5197ed541caSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52033dddc75Smrg          } ||
52133dddc75Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52233dddc75Smrg            (exit 1); exit 1
52333dddc75Smrg          }
52433dddc75Smrg        } &&
52533dddc75Smrg
52633dddc75Smrg        # Now rename the file to the real destination.
52733dddc75Smrg        $doit $mvcmd "$dsttmp" "$dst"
528dd77ae96Smrg      }
529dd77ae96Smrg    fi || exit 1
53072b4363aSmrg
53172b4363aSmrg    trap '' 0
53272b4363aSmrg  fi
53372b4363aSmrgdone
53472b4363aSmrg
53572b4363aSmrg# Local variables:
5367ed541caSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
53772b4363aSmrg# time-stamp-start: "scriptversion="
53872b4363aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5397ed541caSmrg# time-stamp-time-zone: "UTC0"
540dd77ae96Smrg# time-stamp-end: "; # UTC"
54172b4363aSmrg# End:
542