138b41dabSmrg#!/bin/sh
238b41dabSmrg# install - install a program, script, or datafile
338b41dabSmrg
438b41dabSmrgscriptversion=2020-11-14.01; # UTC
538b41dabSmrg
638b41dabSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
738b41dabSmrg# later released in X11R6 (xc/config/util/install.sh) with the
838b41dabSmrg# following copyright and license.
938b41dabSmrg#
1038b41dabSmrg# Copyright (C) 1994 X Consortium
1138b41dabSmrg#
1238b41dabSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1338b41dabSmrg# of this software and associated documentation files (the "Software"), to
1438b41dabSmrg# deal in the Software without restriction, including without limitation the
1538b41dabSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1638b41dabSmrg# sell copies of the Software, and to permit persons to whom the Software is
1738b41dabSmrg# furnished to do so, subject to the following conditions:
1838b41dabSmrg#
1938b41dabSmrg# The above copyright notice and this permission notice shall be included in
2038b41dabSmrg# all copies or substantial portions of the Software.
2138b41dabSmrg#
2238b41dabSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2338b41dabSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2438b41dabSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2538b41dabSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2638b41dabSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2738b41dabSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2838b41dabSmrg#
2938b41dabSmrg# Except as contained in this notice, the name of the X Consortium shall not
3038b41dabSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3138b41dabSmrg# ings in this Software without prior written authorization from the X Consor-
3238b41dabSmrg# tium.
3338b41dabSmrg#
3438b41dabSmrg#
3538b41dabSmrg# FSF changes to this file are in the public domain.
3638b41dabSmrg#
3738b41dabSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3838b41dabSmrg# 'make' implicit rules from creating a file called install from it
3938b41dabSmrg# when there is no Makefile.
4038b41dabSmrg#
4138b41dabSmrg# This script is compatible with the BSD install script, but was written
4238b41dabSmrg# from scratch.
4338b41dabSmrg
4438b41dabSmrgtab='	'
4538b41dabSmrgnl='
4638b41dabSmrg'
4738b41dabSmrgIFS=" $tab$nl"
4838b41dabSmrg
4938b41dabSmrg# Set DOITPROG to "echo" to test this script.
5038b41dabSmrg
5138b41dabSmrgdoit=${DOITPROG-}
5238b41dabSmrgdoit_exec=${doit:-exec}
5338b41dabSmrg
5438b41dabSmrg# Put in absolute file names if you don't have them in your path;
5538b41dabSmrg# or use environment vars.
5638b41dabSmrg
5738b41dabSmrgchgrpprog=${CHGRPPROG-chgrp}
5838b41dabSmrgchmodprog=${CHMODPROG-chmod}
5938b41dabSmrgchownprog=${CHOWNPROG-chown}
6038b41dabSmrgcmpprog=${CMPPROG-cmp}
6138b41dabSmrgcpprog=${CPPROG-cp}
6238b41dabSmrgmkdirprog=${MKDIRPROG-mkdir}
6338b41dabSmrgmvprog=${MVPROG-mv}
6438b41dabSmrgrmprog=${RMPROG-rm}
6538b41dabSmrgstripprog=${STRIPPROG-strip}
6638b41dabSmrg
6738b41dabSmrgposix_mkdir=
6838b41dabSmrg
6938b41dabSmrg# Desired mode of installed file.
7038b41dabSmrgmode=0755
7138b41dabSmrg
7238b41dabSmrg# Create dirs (including intermediate dirs) using mode 755.
7338b41dabSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7438b41dabSmrgmkdir_umask=22
7538b41dabSmrg
7638b41dabSmrgbackupsuffix=
7738b41dabSmrgchgrpcmd=
7838b41dabSmrgchmodcmd=$chmodprog
7938b41dabSmrgchowncmd=
8038b41dabSmrgmvcmd=$mvprog
8138b41dabSmrgrmcmd="$rmprog -f"
8238b41dabSmrgstripcmd=
8338b41dabSmrg
8438b41dabSmrgsrc=
8538b41dabSmrgdst=
8638b41dabSmrgdir_arg=
8738b41dabSmrgdst_arg=
8838b41dabSmrg
8938b41dabSmrgcopy_on_change=false
9038b41dabSmrgis_target_a_directory=possibly
9138b41dabSmrg
9238b41dabSmrgusage="\
9338b41dabSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9438b41dabSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9538b41dabSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9638b41dabSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9738b41dabSmrg
9838b41dabSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9938b41dabSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10038b41dabSmrgIn the 4th, create DIRECTORIES.
10138b41dabSmrg
10238b41dabSmrgOptions:
10338b41dabSmrg     --help     display this help and exit.
10438b41dabSmrg     --version  display version info and exit.
10538b41dabSmrg
10638b41dabSmrg  -c            (ignored)
10738b41dabSmrg  -C            install only if different (preserve data modification time)
10838b41dabSmrg  -d            create directories instead of installing files.
10938b41dabSmrg  -g GROUP      $chgrpprog installed files to GROUP.
11038b41dabSmrg  -m MODE       $chmodprog installed files to MODE.
11138b41dabSmrg  -o USER       $chownprog installed files to USER.
11238b41dabSmrg  -p            pass -p to $cpprog.
11338b41dabSmrg  -s            $stripprog installed files.
11438b41dabSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11538b41dabSmrg  -t DIRECTORY  install into DIRECTORY.
11638b41dabSmrg  -T            report an error if DSTFILE is a directory.
11738b41dabSmrg
11838b41dabSmrgEnvironment variables override the default commands:
11938b41dabSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12038b41dabSmrg  RMPROG STRIPPROG
12138b41dabSmrg
12238b41dabSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12338b41dabSmrgit's up to you to specify -f if you want it.
12438b41dabSmrg
12538b41dabSmrgIf -S is not specified, no backups are attempted.
12638b41dabSmrg
12738b41dabSmrgEmail bug reports to bug-automake@gnu.org.
12838b41dabSmrgAutomake home page: https://www.gnu.org/software/automake/
12938b41dabSmrg"
13038b41dabSmrg
13138b41dabSmrgwhile test $# -ne 0; do
13238b41dabSmrg  case $1 in
13338b41dabSmrg    -c) ;;
13438b41dabSmrg
13538b41dabSmrg    -C) copy_on_change=true;;
13638b41dabSmrg
13738b41dabSmrg    -d) dir_arg=true;;
13838b41dabSmrg
13938b41dabSmrg    -g) chgrpcmd="$chgrpprog $2"
14038b41dabSmrg        shift;;
14138b41dabSmrg
14238b41dabSmrg    --help) echo "$usage"; exit $?;;
14338b41dabSmrg
14438b41dabSmrg    -m) mode=$2
14538b41dabSmrg        case $mode in
14638b41dabSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14738b41dabSmrg            echo "$0: invalid mode: $mode" >&2
14838b41dabSmrg            exit 1;;
14938b41dabSmrg        esac
15038b41dabSmrg        shift;;
15138b41dabSmrg
15238b41dabSmrg    -o) chowncmd="$chownprog $2"
15338b41dabSmrg        shift;;
15438b41dabSmrg
15538b41dabSmrg    -p) cpprog="$cpprog -p";;
15638b41dabSmrg
15738b41dabSmrg    -s) stripcmd=$stripprog;;
15838b41dabSmrg
15938b41dabSmrg    -S) backupsuffix="$2"
16038b41dabSmrg        shift;;
16138b41dabSmrg
16238b41dabSmrg    -t)
16338b41dabSmrg        is_target_a_directory=always
16438b41dabSmrg        dst_arg=$2
16538b41dabSmrg        # Protect names problematic for 'test' and other utilities.
16638b41dabSmrg        case $dst_arg in
16738b41dabSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16838b41dabSmrg        esac
16938b41dabSmrg        shift;;
17038b41dabSmrg
17138b41dabSmrg    -T) is_target_a_directory=never;;
17238b41dabSmrg
17338b41dabSmrg    --version) echo "$0 $scriptversion"; exit $?;;
17438b41dabSmrg
17538b41dabSmrg    --) shift
17638b41dabSmrg        break;;
17738b41dabSmrg
17838b41dabSmrg    -*) echo "$0: invalid option: $1" >&2
17938b41dabSmrg        exit 1;;
18038b41dabSmrg
18138b41dabSmrg    *)  break;;
18238b41dabSmrg  esac
18338b41dabSmrg  shift
18438b41dabSmrgdone
18538b41dabSmrg
18638b41dabSmrg# We allow the use of options -d and -T together, by making -d
18738b41dabSmrg# take the precedence; this is for compatibility with GNU install.
18838b41dabSmrg
18938b41dabSmrgif test -n "$dir_arg"; then
19038b41dabSmrg  if test -n "$dst_arg"; then
19138b41dabSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19238b41dabSmrg    exit 1
19338b41dabSmrg  fi
19438b41dabSmrgfi
19538b41dabSmrg
19638b41dabSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19738b41dabSmrg  # When -d is used, all remaining arguments are directories to create.
19838b41dabSmrg  # When -t is used, the destination is already specified.
19938b41dabSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20038b41dabSmrg  for arg
20138b41dabSmrg  do
20238b41dabSmrg    if test -n "$dst_arg"; then
20338b41dabSmrg      # $@ is not empty: it contains at least $arg.
20438b41dabSmrg      set fnord "$@" "$dst_arg"
20538b41dabSmrg      shift # fnord
20638b41dabSmrg    fi
20738b41dabSmrg    shift # arg
20838b41dabSmrg    dst_arg=$arg
20938b41dabSmrg    # Protect names problematic for 'test' and other utilities.
21038b41dabSmrg    case $dst_arg in
21138b41dabSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21238b41dabSmrg    esac
21338b41dabSmrg  done
21438b41dabSmrgfi
21538b41dabSmrg
21638b41dabSmrgif test $# -eq 0; then
21738b41dabSmrg  if test -z "$dir_arg"; then
21838b41dabSmrg    echo "$0: no input file specified." >&2
21938b41dabSmrg    exit 1
22038b41dabSmrg  fi
22138b41dabSmrg  # It's OK to call 'install-sh -d' without argument.
22238b41dabSmrg  # This can happen when creating conditional directories.
22338b41dabSmrg  exit 0
22438b41dabSmrgfi
22538b41dabSmrg
22638b41dabSmrgif test -z "$dir_arg"; then
22738b41dabSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22838b41dabSmrg    if test ! -d "$dst_arg"; then
22938b41dabSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23038b41dabSmrg      exit 1
23138b41dabSmrg    fi
23238b41dabSmrg  fi
23338b41dabSmrgfi
23438b41dabSmrg
23538b41dabSmrgif test -z "$dir_arg"; then
23638b41dabSmrg  do_exit='(exit $ret); exit $ret'
23738b41dabSmrg  trap "ret=129; $do_exit" 1
23838b41dabSmrg  trap "ret=130; $do_exit" 2
23938b41dabSmrg  trap "ret=141; $do_exit" 13
24038b41dabSmrg  trap "ret=143; $do_exit" 15
24138b41dabSmrg
24238b41dabSmrg  # Set umask so as not to create temps with too-generous modes.
24338b41dabSmrg  # However, 'strip' requires both read and write access to temps.
24438b41dabSmrg  case $mode in
24538b41dabSmrg    # Optimize common cases.
24638b41dabSmrg    *644) cp_umask=133;;
24738b41dabSmrg    *755) cp_umask=22;;
24838b41dabSmrg
24938b41dabSmrg    *[0-7])
25038b41dabSmrg      if test -z "$stripcmd"; then
25138b41dabSmrg        u_plus_rw=
25238b41dabSmrg      else
25338b41dabSmrg        u_plus_rw='% 200'
25438b41dabSmrg      fi
25538b41dabSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25638b41dabSmrg    *)
25738b41dabSmrg      if test -z "$stripcmd"; then
25838b41dabSmrg        u_plus_rw=
25938b41dabSmrg      else
26038b41dabSmrg        u_plus_rw=,u+rw
26138b41dabSmrg      fi
26238b41dabSmrg      cp_umask=$mode$u_plus_rw;;
26338b41dabSmrg  esac
26438b41dabSmrgfi
26538b41dabSmrg
26638b41dabSmrgfor src
26738b41dabSmrgdo
26838b41dabSmrg  # Protect names problematic for 'test' and other utilities.
26938b41dabSmrg  case $src in
27038b41dabSmrg    -* | [=\(\)!]) src=./$src;;
27138b41dabSmrg  esac
27238b41dabSmrg
27338b41dabSmrg  if test -n "$dir_arg"; then
27438b41dabSmrg    dst=$src
27538b41dabSmrg    dstdir=$dst
27638b41dabSmrg    test -d "$dstdir"
27738b41dabSmrg    dstdir_status=$?
27838b41dabSmrg    # Don't chown directories that already exist.
27938b41dabSmrg    if test $dstdir_status = 0; then
28038b41dabSmrg      chowncmd=""
28138b41dabSmrg    fi
28238b41dabSmrg  else
28338b41dabSmrg
28438b41dabSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28538b41dabSmrg    # might cause directories to be created, which would be especially bad
28638b41dabSmrg    # if $src (and thus $dsttmp) contains '*'.
28738b41dabSmrg    if test ! -f "$src" && test ! -d "$src"; then
28838b41dabSmrg      echo "$0: $src does not exist." >&2
28938b41dabSmrg      exit 1
29038b41dabSmrg    fi
29138b41dabSmrg
29238b41dabSmrg    if test -z "$dst_arg"; then
29338b41dabSmrg      echo "$0: no destination specified." >&2
29438b41dabSmrg      exit 1
29538b41dabSmrg    fi
29638b41dabSmrg    dst=$dst_arg
29738b41dabSmrg
29838b41dabSmrg    # If destination is a directory, append the input filename.
29938b41dabSmrg    if test -d "$dst"; then
30038b41dabSmrg      if test "$is_target_a_directory" = never; then
30138b41dabSmrg        echo "$0: $dst_arg: Is a directory" >&2
30238b41dabSmrg        exit 1
30338b41dabSmrg      fi
30438b41dabSmrg      dstdir=$dst
30538b41dabSmrg      dstbase=`basename "$src"`
30638b41dabSmrg      case $dst in
30738b41dabSmrg	*/) dst=$dst$dstbase;;
30838b41dabSmrg	*)  dst=$dst/$dstbase;;
30938b41dabSmrg      esac
31038b41dabSmrg      dstdir_status=0
31138b41dabSmrg    else
31238b41dabSmrg      dstdir=`dirname "$dst"`
31338b41dabSmrg      test -d "$dstdir"
31438b41dabSmrg      dstdir_status=$?
31538b41dabSmrg    fi
31638b41dabSmrg  fi
31738b41dabSmrg
31838b41dabSmrg  case $dstdir in
31938b41dabSmrg    */) dstdirslash=$dstdir;;
32038b41dabSmrg    *)  dstdirslash=$dstdir/;;
32138b41dabSmrg  esac
32238b41dabSmrg
32338b41dabSmrg  obsolete_mkdir_used=false
32438b41dabSmrg
32538b41dabSmrg  if test $dstdir_status != 0; then
32638b41dabSmrg    case $posix_mkdir in
32738b41dabSmrg      '')
32838b41dabSmrg        # With -d, create the new directory with the user-specified mode.
32938b41dabSmrg        # Otherwise, rely on $mkdir_umask.
33038b41dabSmrg        if test -n "$dir_arg"; then
33138b41dabSmrg          mkdir_mode=-m$mode
33238b41dabSmrg        else
33338b41dabSmrg          mkdir_mode=
33438b41dabSmrg        fi
33538b41dabSmrg
33638b41dabSmrg        posix_mkdir=false
33738b41dabSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33838b41dabSmrg	# here however when possible just to lower collision chance.
33938b41dabSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34038b41dabSmrg
34138b41dabSmrg	trap '
34238b41dabSmrg	  ret=$?
34338b41dabSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34438b41dabSmrg	  exit $ret
34538b41dabSmrg	' 0
34638b41dabSmrg
34738b41dabSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
34838b41dabSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34938b41dabSmrg	# directory is successfully created first before we actually test
35038b41dabSmrg	# 'mkdir -p'.
35138b41dabSmrg	if (umask $mkdir_umask &&
35238b41dabSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35338b41dabSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35438b41dabSmrg	then
35538b41dabSmrg	  if test -z "$dir_arg" || {
35638b41dabSmrg	       # Check for POSIX incompatibilities with -m.
35738b41dabSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35838b41dabSmrg	       # other-writable bit of parent directory when it shouldn't.
35938b41dabSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36038b41dabSmrg	       test_tmpdir="$tmpdir/a"
36138b41dabSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36238b41dabSmrg	       case $ls_ld_tmpdir in
36338b41dabSmrg		 d????-?r-*) different_mode=700;;
36438b41dabSmrg		 d????-?--*) different_mode=755;;
36538b41dabSmrg		 *) false;;
36638b41dabSmrg	       esac &&
36738b41dabSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36838b41dabSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36938b41dabSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37038b41dabSmrg	       }
37138b41dabSmrg	     }
37238b41dabSmrg	  then posix_mkdir=:
37338b41dabSmrg	  fi
37438b41dabSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37538b41dabSmrg	else
37638b41dabSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
37738b41dabSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37838b41dabSmrg	fi
37938b41dabSmrg	trap '' 0;;
38038b41dabSmrg    esac
38138b41dabSmrg
38238b41dabSmrg    if
38338b41dabSmrg      $posix_mkdir && (
38438b41dabSmrg        umask $mkdir_umask &&
38538b41dabSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38638b41dabSmrg      )
38738b41dabSmrg    then :
38838b41dabSmrg    else
38938b41dabSmrg
39038b41dabSmrg      # mkdir does not conform to POSIX,
39138b41dabSmrg      # or it failed possibly due to a race condition.  Create the
39238b41dabSmrg      # directory the slow way, step by step, checking for races as we go.
39338b41dabSmrg
39438b41dabSmrg      case $dstdir in
39538b41dabSmrg        /*) prefix='/';;
39638b41dabSmrg        [-=\(\)!]*) prefix='./';;
39738b41dabSmrg        *)  prefix='';;
39838b41dabSmrg      esac
39938b41dabSmrg
40038b41dabSmrg      oIFS=$IFS
40138b41dabSmrg      IFS=/
40238b41dabSmrg      set -f
40338b41dabSmrg      set fnord $dstdir
40438b41dabSmrg      shift
40538b41dabSmrg      set +f
40638b41dabSmrg      IFS=$oIFS
40738b41dabSmrg
40838b41dabSmrg      prefixes=
40938b41dabSmrg
41038b41dabSmrg      for d
41138b41dabSmrg      do
41238b41dabSmrg        test X"$d" = X && continue
41338b41dabSmrg
41438b41dabSmrg        prefix=$prefix$d
41538b41dabSmrg        if test -d "$prefix"; then
41638b41dabSmrg          prefixes=
41738b41dabSmrg        else
41838b41dabSmrg          if $posix_mkdir; then
41938b41dabSmrg            (umask $mkdir_umask &&
42038b41dabSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42138b41dabSmrg            # Don't fail if two instances are running concurrently.
42238b41dabSmrg            test -d "$prefix" || exit 1
42338b41dabSmrg          else
42438b41dabSmrg            case $prefix in
42538b41dabSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42638b41dabSmrg              *) qprefix=$prefix;;
42738b41dabSmrg            esac
42838b41dabSmrg            prefixes="$prefixes '$qprefix'"
42938b41dabSmrg          fi
43038b41dabSmrg        fi
43138b41dabSmrg        prefix=$prefix/
43238b41dabSmrg      done
43338b41dabSmrg
43438b41dabSmrg      if test -n "$prefixes"; then
43538b41dabSmrg        # Don't fail if two instances are running concurrently.
43638b41dabSmrg        (umask $mkdir_umask &&
43738b41dabSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43838b41dabSmrg          test -d "$dstdir" || exit 1
43938b41dabSmrg        obsolete_mkdir_used=true
44038b41dabSmrg      fi
44138b41dabSmrg    fi
44238b41dabSmrg  fi
44338b41dabSmrg
44438b41dabSmrg  if test -n "$dir_arg"; then
44538b41dabSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44638b41dabSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44738b41dabSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44838b41dabSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44938b41dabSmrg  else
45038b41dabSmrg
45138b41dabSmrg    # Make a couple of temp file names in the proper directory.
45238b41dabSmrg    dsttmp=${dstdirslash}_inst.$$_
45338b41dabSmrg    rmtmp=${dstdirslash}_rm.$$_
45438b41dabSmrg
45538b41dabSmrg    # Trap to clean up those temp files at exit.
45638b41dabSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45738b41dabSmrg
45838b41dabSmrg    # Copy the file name to the temp name.
45938b41dabSmrg    (umask $cp_umask &&
46038b41dabSmrg     { test -z "$stripcmd" || {
46138b41dabSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46238b41dabSmrg	 # which would cause strip to fail.
46338b41dabSmrg	 if test -z "$doit"; then
46438b41dabSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46538b41dabSmrg	 else
46638b41dabSmrg	   $doit touch "$dsttmp"
46738b41dabSmrg	 fi
46838b41dabSmrg       }
46938b41dabSmrg     } &&
47038b41dabSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47138b41dabSmrg
47238b41dabSmrg    # and set any options; do chmod last to preserve setuid bits.
47338b41dabSmrg    #
47438b41dabSmrg    # If any of these fail, we abort the whole thing.  If we want to
47538b41dabSmrg    # ignore errors from any of these, just make sure not to ignore
47638b41dabSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47738b41dabSmrg    #
47838b41dabSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47938b41dabSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48038b41dabSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48138b41dabSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48238b41dabSmrg
48338b41dabSmrg    # If -C, don't bother to copy if it wouldn't change the file.
48438b41dabSmrg    if $copy_on_change &&
48538b41dabSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48638b41dabSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48738b41dabSmrg       set -f &&
48838b41dabSmrg       set X $old && old=:$2:$4:$5:$6 &&
48938b41dabSmrg       set X $new && new=:$2:$4:$5:$6 &&
49038b41dabSmrg       set +f &&
49138b41dabSmrg       test "$old" = "$new" &&
49238b41dabSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49338b41dabSmrg    then
49438b41dabSmrg      rm -f "$dsttmp"
49538b41dabSmrg    else
49638b41dabSmrg      # If $backupsuffix is set, and the file being installed
49738b41dabSmrg      # already exists, attempt a backup.  Don't worry if it fails,
49838b41dabSmrg      # e.g., if mv doesn't support -f.
49938b41dabSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
50038b41dabSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50138b41dabSmrg      fi
50238b41dabSmrg
50338b41dabSmrg      # Rename the file to the real destination.
50438b41dabSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50538b41dabSmrg
50638b41dabSmrg      # The rename failed, perhaps because mv can't rename something else
50738b41dabSmrg      # to itself, or perhaps because mv is so ancient that it does not
50838b41dabSmrg      # support -f.
50938b41dabSmrg      {
51038b41dabSmrg        # Now remove or move aside any old file at destination location.
51138b41dabSmrg        # We try this two ways since rm can't unlink itself on some
51238b41dabSmrg        # systems and the destination file might be busy for other
51338b41dabSmrg        # reasons.  In this case, the final cleanup might fail but the new
51438b41dabSmrg        # file should still install successfully.
51538b41dabSmrg        {
51638b41dabSmrg          test ! -f "$dst" ||
51738b41dabSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51838b41dabSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51938b41dabSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52038b41dabSmrg          } ||
52138b41dabSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52238b41dabSmrg            (exit 1); exit 1
52338b41dabSmrg          }
52438b41dabSmrg        } &&
52538b41dabSmrg
52638b41dabSmrg        # Now rename the file to the real destination.
52738b41dabSmrg        $doit $mvcmd "$dsttmp" "$dst"
52838b41dabSmrg      }
52938b41dabSmrg    fi || exit 1
53038b41dabSmrg
53138b41dabSmrg    trap '' 0
53238b41dabSmrg  fi
53338b41dabSmrgdone
53438b41dabSmrg
53538b41dabSmrg# Local variables:
53638b41dabSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
53738b41dabSmrg# time-stamp-start: "scriptversion="
53838b41dabSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53938b41dabSmrg# time-stamp-time-zone: "UTC0"
54038b41dabSmrg# time-stamp-end: "; # UTC"
54138b41dabSmrg# End:
542