129459361Smrg#!/bin/sh
229459361Smrg# install - install a program, script, or datafile
329459361Smrg
4eb323118Smrgscriptversion=2024-06-19.01; # UTC
529459361Smrg
629459361Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
729459361Smrg# later released in X11R6 (xc/config/util/install.sh) with the
829459361Smrg# following copyright and license.
929459361Smrg#
1029459361Smrg# Copyright (C) 1994 X Consortium
1129459361Smrg#
1229459361Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1329459361Smrg# of this software and associated documentation files (the "Software"), to
1429459361Smrg# deal in the Software without restriction, including without limitation the
1529459361Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1629459361Smrg# sell copies of the Software, and to permit persons to whom the Software is
1729459361Smrg# furnished to do so, subject to the following conditions:
1829459361Smrg#
1929459361Smrg# The above copyright notice and this permission notice shall be included in
2029459361Smrg# all copies or substantial portions of the Software.
2129459361Smrg#
2229459361Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2329459361Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2429459361Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2529459361Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2629459361Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2729459361Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2829459361Smrg#
2929459361Smrg# Except as contained in this notice, the name of the X Consortium shall not
3029459361Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3129459361Smrg# ings in this Software without prior written authorization from the X Consor-
3229459361Smrg# tium.
3329459361Smrg#
3429459361Smrg#
3529459361Smrg# FSF changes to this file are in the public domain.
3629459361Smrg#
3729459361Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38fb4ebca8Smrg# 'make' implicit rules from creating a file called install from it
3929459361Smrg# when there is no Makefile.
4029459361Smrg#
4129459361Smrg# This script is compatible with the BSD install script, but was written
4247202d7bSmrg# from scratch.
4347202d7bSmrg
44fa2b3b63Smrgtab='	'
4547202d7bSmrgnl='
4647202d7bSmrg'
47fa2b3b63SmrgIFS=" $tab$nl"
4829459361Smrg
49fa2b3b63Smrg# Set DOITPROG to "echo" to test this script.
5029459361Smrg
5147202d7bSmrgdoit=${DOITPROG-}
52fa2b3b63Smrgdoit_exec=${doit:-exec}
5329459361Smrg
5447202d7bSmrg# Put in absolute file names if you don't have them in your path;
5547202d7bSmrg# or use environment vars.
5647202d7bSmrg
5747202d7bSmrgchgrpprog=${CHGRPPROG-chgrp}
5847202d7bSmrgchmodprog=${CHMODPROG-chmod}
5947202d7bSmrgchownprog=${CHOWNPROG-chown}
6047202d7bSmrgcmpprog=${CMPPROG-cmp}
6147202d7bSmrgcpprog=${CPPROG-cp}
6247202d7bSmrgmkdirprog=${MKDIRPROG-mkdir}
6347202d7bSmrgmvprog=${MVPROG-mv}
6447202d7bSmrgrmprog=${RMPROG-rm}
6547202d7bSmrgstripprog=${STRIPPROG-strip}
6647202d7bSmrg
6747202d7bSmrgposix_mkdir=
6847202d7bSmrg
6947202d7bSmrg# Desired mode of installed file.
7047202d7bSmrgmode=0755
7129459361Smrg
72e24f450bSmrg# Create dirs (including intermediate dirs) using mode 755.
73e24f450bSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74e24f450bSmrgmkdir_umask=22
75e24f450bSmrg
76e24f450bSmrgbackupsuffix=
7729459361Smrgchgrpcmd=
7847202d7bSmrgchmodcmd=$chmodprog
7947202d7bSmrgchowncmd=
8047202d7bSmrgmvcmd=$mvprog
8129459361Smrgrmcmd="$rmprog -f"
8247202d7bSmrgstripcmd=
8347202d7bSmrg
8429459361Smrgsrc=
8529459361Smrgdst=
8629459361Smrgdir_arg=
8747202d7bSmrgdst_arg=
8847202d7bSmrg
8947202d7bSmrgcopy_on_change=false
90fa2b3b63Smrgis_target_a_directory=possibly
9129459361Smrg
9247202d7bSmrgusage="\
9347202d7bSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9429459361Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9529459361Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9629459361Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9729459361Smrg
9829459361SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9929459361SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10029459361SmrgIn the 4th, create DIRECTORIES.
10129459361Smrg
10229459361SmrgOptions:
10347202d7bSmrg     --help     display this help and exit.
10447202d7bSmrg     --version  display version info and exit.
10547202d7bSmrg
10647202d7bSmrg  -c            (ignored)
107e24f450bSmrg  -C            install only if different (preserve data modification time)
10847202d7bSmrg  -d            create directories instead of installing files.
10947202d7bSmrg  -g GROUP      $chgrpprog installed files to GROUP.
11047202d7bSmrg  -m MODE       $chmodprog installed files to MODE.
11147202d7bSmrg  -o USER       $chownprog installed files to USER.
112e24f450bSmrg  -p            pass -p to $cpprog.
11347202d7bSmrg  -s            $stripprog installed files.
114e24f450bSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11547202d7bSmrg  -t DIRECTORY  install into DIRECTORY.
11647202d7bSmrg  -T            report an error if DSTFILE is a directory.
11729459361Smrg
11829459361SmrgEnvironment variables override the default commands:
11947202d7bSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12047202d7bSmrg  RMPROG STRIPPROG
121e24f450bSmrg
122e24f450bSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123e24f450bSmrgit's up to you to specify -f if you want it.
124e24f450bSmrg
125e24f450bSmrgIf -S is not specified, no backups are attempted.
126e24f450bSmrg
127eb323118SmrgReport bugs to <bug-automake@gnu.org>.
128eb323118SmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
129eb323118SmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
13029459361Smrg
13147202d7bSmrgwhile test $# -ne 0; do
13229459361Smrg  case $1 in
13347202d7bSmrg    -c) ;;
13447202d7bSmrg
13547202d7bSmrg    -C) copy_on_change=true;;
13629459361Smrg
13747202d7bSmrg    -d) dir_arg=true;;
13829459361Smrg
13929459361Smrg    -g) chgrpcmd="$chgrpprog $2"
140fa2b3b63Smrg        shift;;
14129459361Smrg
14229459361Smrg    --help) echo "$usage"; exit $?;;
14329459361Smrg
14447202d7bSmrg    -m) mode=$2
145fa2b3b63Smrg        case $mode in
146fa2b3b63Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147fa2b3b63Smrg            echo "$0: invalid mode: $mode" >&2
148fa2b3b63Smrg            exit 1;;
149fa2b3b63Smrg        esac
150fa2b3b63Smrg        shift;;
15129459361Smrg
15229459361Smrg    -o) chowncmd="$chownprog $2"
153fa2b3b63Smrg        shift;;
15429459361Smrg
155e24f450bSmrg    -p) cpprog="$cpprog -p";;
156e24f450bSmrg
15747202d7bSmrg    -s) stripcmd=$stripprog;;
15829459361Smrg
159e24f450bSmrg    -S) backupsuffix="$2"
160e24f450bSmrg        shift;;
161e24f450bSmrg
162fa2b3b63Smrg    -t)
163fa2b3b63Smrg        is_target_a_directory=always
164fa2b3b63Smrg        dst_arg=$2
165fa2b3b63Smrg        # Protect names problematic for 'test' and other utilities.
166fa2b3b63Smrg        case $dst_arg in
167fa2b3b63Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168fa2b3b63Smrg        esac
169fa2b3b63Smrg        shift;;
17029459361Smrg
171fa2b3b63Smrg    -T) is_target_a_directory=never;;
17229459361Smrg
173eb323118Smrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
17429459361Smrg
175fa2b3b63Smrg    --) shift
176fa2b3b63Smrg        break;;
17747202d7bSmrg
178fa2b3b63Smrg    -*) echo "$0: invalid option: $1" >&2
179fa2b3b63Smrg        exit 1;;
18047202d7bSmrg
18147202d7bSmrg    *)  break;;
18229459361Smrg  esac
18347202d7bSmrg  shift
18429459361Smrgdone
18529459361Smrg
186fa2b3b63Smrg# We allow the use of options -d and -T together, by making -d
187fa2b3b63Smrg# take the precedence; this is for compatibility with GNU install.
188fa2b3b63Smrg
189fa2b3b63Smrgif test -n "$dir_arg"; then
190fa2b3b63Smrg  if test -n "$dst_arg"; then
191fa2b3b63Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192fa2b3b63Smrg    exit 1
193fa2b3b63Smrg  fi
194fa2b3b63Smrgfi
195fa2b3b63Smrg
19647202d7bSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19747202d7bSmrg  # When -d is used, all remaining arguments are directories to create.
19847202d7bSmrg  # When -t is used, the destination is already specified.
19947202d7bSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20047202d7bSmrg  for arg
20147202d7bSmrg  do
20247202d7bSmrg    if test -n "$dst_arg"; then
20347202d7bSmrg      # $@ is not empty: it contains at least $arg.
20447202d7bSmrg      set fnord "$@" "$dst_arg"
20547202d7bSmrg      shift # fnord
20647202d7bSmrg    fi
20747202d7bSmrg    shift # arg
20847202d7bSmrg    dst_arg=$arg
209fb4ebca8Smrg    # Protect names problematic for 'test' and other utilities.
2105dd2154eSmrg    case $dst_arg in
2115dd2154eSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2125dd2154eSmrg    esac
21347202d7bSmrg  done
21447202d7bSmrgfi
21547202d7bSmrg
21647202d7bSmrgif test $# -eq 0; then
21729459361Smrg  if test -z "$dir_arg"; then
21829459361Smrg    echo "$0: no input file specified." >&2
21929459361Smrg    exit 1
22029459361Smrg  fi
221fb4ebca8Smrg  # It's OK to call 'install-sh -d' without argument.
22229459361Smrg  # This can happen when creating conditional directories.
22329459361Smrg  exit 0
22429459361Smrgfi
22529459361Smrg
226fa2b3b63Smrgif test -z "$dir_arg"; then
227fa2b3b63Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228fa2b3b63Smrg    if test ! -d "$dst_arg"; then
229fa2b3b63Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230fa2b3b63Smrg      exit 1
231fa2b3b63Smrg    fi
232fa2b3b63Smrg  fi
233fa2b3b63Smrgfi
234fa2b3b63Smrg
23547202d7bSmrgif test -z "$dir_arg"; then
2365dd2154eSmrg  do_exit='(exit $ret); exit $ret'
2375dd2154eSmrg  trap "ret=129; $do_exit" 1
2385dd2154eSmrg  trap "ret=130; $do_exit" 2
2395dd2154eSmrg  trap "ret=141; $do_exit" 13
2405dd2154eSmrg  trap "ret=143; $do_exit" 15
24147202d7bSmrg
24247202d7bSmrg  # Set umask so as not to create temps with too-generous modes.
24347202d7bSmrg  # However, 'strip' requires both read and write access to temps.
24447202d7bSmrg  case $mode in
24547202d7bSmrg    # Optimize common cases.
24647202d7bSmrg    *644) cp_umask=133;;
24747202d7bSmrg    *755) cp_umask=22;;
24847202d7bSmrg
24947202d7bSmrg    *[0-7])
25047202d7bSmrg      if test -z "$stripcmd"; then
251fa2b3b63Smrg        u_plus_rw=
25247202d7bSmrg      else
253fa2b3b63Smrg        u_plus_rw='% 200'
25447202d7bSmrg      fi
25547202d7bSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25647202d7bSmrg    *)
25747202d7bSmrg      if test -z "$stripcmd"; then
258fa2b3b63Smrg        u_plus_rw=
25947202d7bSmrg      else
260fa2b3b63Smrg        u_plus_rw=,u+rw
26147202d7bSmrg      fi
26247202d7bSmrg      cp_umask=$mode$u_plus_rw;;
26347202d7bSmrg  esac
26447202d7bSmrgfi
26547202d7bSmrg
26629459361Smrgfor src
26729459361Smrgdo
268fb4ebca8Smrg  # Protect names problematic for 'test' and other utilities.
26929459361Smrg  case $src in
2705dd2154eSmrg    -* | [=\(\)!]) src=./$src;;
27129459361Smrg  esac
27229459361Smrg
27329459361Smrg  if test -n "$dir_arg"; then
27429459361Smrg    dst=$src
27547202d7bSmrg    dstdir=$dst
27647202d7bSmrg    test -d "$dstdir"
27747202d7bSmrg    dstdir_status=$?
278e24f450bSmrg    # Don't chown directories that already exist.
279e24f450bSmrg    if test $dstdir_status = 0; then
280e24f450bSmrg      chowncmd=""
281e24f450bSmrg    fi
28229459361Smrg  else
28347202d7bSmrg
28429459361Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28529459361Smrg    # might cause directories to be created, which would be especially bad
28629459361Smrg    # if $src (and thus $dsttmp) contains '*'.
28729459361Smrg    if test ! -f "$src" && test ! -d "$src"; then
28829459361Smrg      echo "$0: $src does not exist." >&2
28929459361Smrg      exit 1
29029459361Smrg    fi
29129459361Smrg
29247202d7bSmrg    if test -z "$dst_arg"; then
29329459361Smrg      echo "$0: no destination specified." >&2
29429459361Smrg      exit 1
29529459361Smrg    fi
29647202d7bSmrg    dst=$dst_arg
29729459361Smrg
298e24f450bSmrg    # If destination is a directory, append the input filename.
29929459361Smrg    if test -d "$dst"; then
300fa2b3b63Smrg      if test "$is_target_a_directory" = never; then
301fa2b3b63Smrg        echo "$0: $dst_arg: Is a directory" >&2
302fa2b3b63Smrg        exit 1
30329459361Smrg      fi
30447202d7bSmrg      dstdir=$dst
305e24f450bSmrg      dstbase=`basename "$src"`
306e24f450bSmrg      case $dst in
307e24f450bSmrg	*/) dst=$dst$dstbase;;
308e24f450bSmrg	*)  dst=$dst/$dstbase;;
309e24f450bSmrg      esac
31047202d7bSmrg      dstdir_status=0
31147202d7bSmrg    else
312fa2b3b63Smrg      dstdir=`dirname "$dst"`
31347202d7bSmrg      test -d "$dstdir"
31447202d7bSmrg      dstdir_status=$?
31529459361Smrg    fi
31629459361Smrg  fi
31729459361Smrg
318e24f450bSmrg  case $dstdir in
319e24f450bSmrg    */) dstdirslash=$dstdir;;
320e24f450bSmrg    *)  dstdirslash=$dstdir/;;
321e24f450bSmrg  esac
322e24f450bSmrg
32347202d7bSmrg  obsolete_mkdir_used=false
32447202d7bSmrg
32547202d7bSmrg  if test $dstdir_status != 0; then
32647202d7bSmrg    case $posix_mkdir in
32747202d7bSmrg      '')
328fa2b3b63Smrg        # With -d, create the new directory with the user-specified mode.
329fa2b3b63Smrg        # Otherwise, rely on $mkdir_umask.
330fa2b3b63Smrg        if test -n "$dir_arg"; then
331fa2b3b63Smrg          mkdir_mode=-m$mode
332fa2b3b63Smrg        else
333fa2b3b63Smrg          mkdir_mode=
334fa2b3b63Smrg        fi
335fa2b3b63Smrg
336fa2b3b63Smrg        posix_mkdir=false
337e24f450bSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338e24f450bSmrg	# here however when possible just to lower collision chance.
339e24f450bSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340e24f450bSmrg
341e24f450bSmrg	trap '
342e24f450bSmrg	  ret=$?
343e24f450bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344e24f450bSmrg	  exit $ret
345e24f450bSmrg	' 0
346e24f450bSmrg
347e24f450bSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348eb323118Smrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
349e24f450bSmrg	# directory is successfully created first before we actually test
350e24f450bSmrg	# 'mkdir -p'.
351e24f450bSmrg	if (umask $mkdir_umask &&
352e24f450bSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353e24f450bSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354e24f450bSmrg	then
355e24f450bSmrg	  if test -z "$dir_arg" || {
356eb323118Smrg	       # Check for POSIX incompatibility with -m.
357e24f450bSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358e24f450bSmrg	       # other-writable bit of parent directory when it shouldn't.
359e24f450bSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360e24f450bSmrg	       test_tmpdir="$tmpdir/a"
361e24f450bSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362e24f450bSmrg	       case $ls_ld_tmpdir in
363e24f450bSmrg		 d????-?r-*) different_mode=700;;
364e24f450bSmrg		 d????-?--*) different_mode=755;;
365e24f450bSmrg		 *) false;;
366e24f450bSmrg	       esac &&
367e24f450bSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368e24f450bSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369e24f450bSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370e24f450bSmrg	       }
371e24f450bSmrg	     }
372e24f450bSmrg	  then posix_mkdir=:
373e24f450bSmrg	  fi
374e24f450bSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375e24f450bSmrg	else
376e24f450bSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377e24f450bSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378e24f450bSmrg	fi
379e24f450bSmrg	trap '' 0;;
38047202d7bSmrg    esac
38129459361Smrg
38247202d7bSmrg    if
38347202d7bSmrg      $posix_mkdir && (
384fa2b3b63Smrg        umask $mkdir_umask &&
385fa2b3b63Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38647202d7bSmrg      )
38747202d7bSmrg    then :
38847202d7bSmrg    else
38929459361Smrg
390e24f450bSmrg      # mkdir does not conform to POSIX,
39147202d7bSmrg      # or it failed possibly due to a race condition.  Create the
39247202d7bSmrg      # directory the slow way, step by step, checking for races as we go.
39329459361Smrg
39447202d7bSmrg      case $dstdir in
395fa2b3b63Smrg        /*) prefix='/';;
396fa2b3b63Smrg        [-=\(\)!]*) prefix='./';;
397fa2b3b63Smrg        *)  prefix='';;
39847202d7bSmrg      esac
39929459361Smrg
40047202d7bSmrg      oIFS=$IFS
40147202d7bSmrg      IFS=/
402fa2b3b63Smrg      set -f
40347202d7bSmrg      set fnord $dstdir
40429459361Smrg      shift
405fa2b3b63Smrg      set +f
40647202d7bSmrg      IFS=$oIFS
40747202d7bSmrg
40847202d7bSmrg      prefixes=
40947202d7bSmrg
41047202d7bSmrg      for d
41147202d7bSmrg      do
412fa2b3b63Smrg        test X"$d" = X && continue
413fa2b3b63Smrg
414fa2b3b63Smrg        prefix=$prefix$d
415fa2b3b63Smrg        if test -d "$prefix"; then
416fa2b3b63Smrg          prefixes=
417fa2b3b63Smrg        else
418fa2b3b63Smrg          if $posix_mkdir; then
419e24f450bSmrg            (umask $mkdir_umask &&
420fa2b3b63Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421fa2b3b63Smrg            # Don't fail if two instances are running concurrently.
422fa2b3b63Smrg            test -d "$prefix" || exit 1
423fa2b3b63Smrg          else
424fa2b3b63Smrg            case $prefix in
425fa2b3b63Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426fa2b3b63Smrg              *) qprefix=$prefix;;
427fa2b3b63Smrg            esac
428fa2b3b63Smrg            prefixes="$prefixes '$qprefix'"
429fa2b3b63Smrg          fi
430fa2b3b63Smrg        fi
431fa2b3b63Smrg        prefix=$prefix/
43247202d7bSmrg      done
43347202d7bSmrg
43447202d7bSmrg      if test -n "$prefixes"; then
435fa2b3b63Smrg        # Don't fail if two instances are running concurrently.
436fa2b3b63Smrg        (umask $mkdir_umask &&
437fa2b3b63Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438fa2b3b63Smrg          test -d "$dstdir" || exit 1
439fa2b3b63Smrg        obsolete_mkdir_used=true
44029459361Smrg      fi
44147202d7bSmrg    fi
44229459361Smrg  fi
44329459361Smrg
44429459361Smrg  if test -n "$dir_arg"; then
44547202d7bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44647202d7bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44747202d7bSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44847202d7bSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44929459361Smrg  else
45029459361Smrg
45129459361Smrg    # Make a couple of temp file names in the proper directory.
452e24f450bSmrg    dsttmp=${dstdirslash}_inst.$$_
453e24f450bSmrg    rmtmp=${dstdirslash}_rm.$$_
45429459361Smrg
45529459361Smrg    # Trap to clean up those temp files at exit.
45629459361Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45729459361Smrg
45829459361Smrg    # Copy the file name to the temp name.
459e24f450bSmrg    (umask $cp_umask &&
460e24f450bSmrg     { test -z "$stripcmd" || {
461e24f450bSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462e24f450bSmrg	 # which would cause strip to fail.
463e24f450bSmrg	 if test -z "$doit"; then
464e24f450bSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465e24f450bSmrg	 else
466e24f450bSmrg	   $doit touch "$dsttmp"
467e24f450bSmrg	 fi
468e24f450bSmrg       }
469e24f450bSmrg     } &&
470e24f450bSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47129459361Smrg
47229459361Smrg    # and set any options; do chmod last to preserve setuid bits.
47329459361Smrg    #
47429459361Smrg    # If any of these fail, we abort the whole thing.  If we want to
47529459361Smrg    # ignore errors from any of these, just make sure not to ignore
47629459361Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47729459361Smrg    #
47847202d7bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47947202d7bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48047202d7bSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48147202d7bSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48247202d7bSmrg
48347202d7bSmrg    # If -C, don't bother to copy if it wouldn't change the file.
48447202d7bSmrg    if $copy_on_change &&
485fa2b3b63Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486fa2b3b63Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487fa2b3b63Smrg       set -f &&
48847202d7bSmrg       set X $old && old=:$2:$4:$5:$6 &&
48947202d7bSmrg       set X $new && new=:$2:$4:$5:$6 &&
490fa2b3b63Smrg       set +f &&
49147202d7bSmrg       test "$old" = "$new" &&
49247202d7bSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49347202d7bSmrg    then
49447202d7bSmrg      rm -f "$dsttmp"
49547202d7bSmrg    else
496e24f450bSmrg      # If $backupsuffix is set, and the file being installed
497e24f450bSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498e24f450bSmrg      # e.g., if mv doesn't support -f.
499e24f450bSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500e24f450bSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501e24f450bSmrg      fi
502e24f450bSmrg
50347202d7bSmrg      # Rename the file to the real destination.
50447202d7bSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50547202d7bSmrg
50647202d7bSmrg      # The rename failed, perhaps because mv can't rename something else
50747202d7bSmrg      # to itself, or perhaps because mv is so ancient that it does not
50847202d7bSmrg      # support -f.
50947202d7bSmrg      {
510fa2b3b63Smrg        # Now remove or move aside any old file at destination location.
511fa2b3b63Smrg        # We try this two ways since rm can't unlink itself on some
512fa2b3b63Smrg        # systems and the destination file might be busy for other
513fa2b3b63Smrg        # reasons.  In this case, the final cleanup might fail but the new
514fa2b3b63Smrg        # file should still install successfully.
515fa2b3b63Smrg        {
516fa2b3b63Smrg          test ! -f "$dst" ||
517e24f450bSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
518fa2b3b63Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519e24f450bSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520fa2b3b63Smrg          } ||
521fa2b3b63Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522fa2b3b63Smrg            (exit 1); exit 1
523fa2b3b63Smrg          }
524fa2b3b63Smrg        } &&
525fa2b3b63Smrg
526fa2b3b63Smrg        # Now rename the file to the real destination.
527fa2b3b63Smrg        $doit $mvcmd "$dsttmp" "$dst"
52847202d7bSmrg      }
52947202d7bSmrg    fi || exit 1
53047202d7bSmrg
53147202d7bSmrg    trap '' 0
53247202d7bSmrg  fi
53329459361Smrgdone
53429459361Smrg
53529459361Smrg# Local variables:
536e24f450bSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
53729459361Smrg# time-stamp-start: "scriptversion="
53829459361Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539fa2b3b63Smrg# time-stamp-time-zone: "UTC0"
54047202d7bSmrg# time-stamp-end: "; # UTC"
54129459361Smrg# End:
542