install-sh revision 33adc6ac
1fda9279dSmrg#!/bin/sh
2fda9279dSmrg# install - install a program, script, or datafile
3fda9279dSmrg
433adc6acSmrgscriptversion=2020-11-14.01; # UTC
5fda9279dSmrg
6fda9279dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7fda9279dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8fda9279dSmrg# following copyright and license.
9fda9279dSmrg#
10fda9279dSmrg# Copyright (C) 1994 X Consortium
11fda9279dSmrg#
12fda9279dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13fda9279dSmrg# of this software and associated documentation files (the "Software"), to
14fda9279dSmrg# deal in the Software without restriction, including without limitation the
15fda9279dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16fda9279dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17fda9279dSmrg# furnished to do so, subject to the following conditions:
18fda9279dSmrg#
19fda9279dSmrg# The above copyright notice and this permission notice shall be included in
20fda9279dSmrg# all copies or substantial portions of the Software.
21fda9279dSmrg#
22fda9279dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23fda9279dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24fda9279dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25fda9279dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26fda9279dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27fda9279dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28fda9279dSmrg#
29fda9279dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30fda9279dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31fda9279dSmrg# ings in this Software without prior written authorization from the X Consor-
32fda9279dSmrg# tium.
33fda9279dSmrg#
34fda9279dSmrg#
35fda9279dSmrg# FSF changes to this file are in the public domain.
36fda9279dSmrg#
37fda9279dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38fda9279dSmrg# 'make' implicit rules from creating a file called install from it
39fda9279dSmrg# when there is no Makefile.
40fda9279dSmrg#
41fda9279dSmrg# This script is compatible with the BSD install script, but was written
42fda9279dSmrg# from scratch.
43fda9279dSmrg
4416ee1e9aSmrgtab='	'
45fda9279dSmrgnl='
46fda9279dSmrg'
4716ee1e9aSmrgIFS=" $tab$nl"
48fda9279dSmrg
4916ee1e9aSmrg# Set DOITPROG to "echo" to test this script.
50fda9279dSmrg
51fda9279dSmrgdoit=${DOITPROG-}
5216ee1e9aSmrgdoit_exec=${doit:-exec}
53fda9279dSmrg
54fda9279dSmrg# Put in absolute file names if you don't have them in your path;
55fda9279dSmrg# or use environment vars.
56fda9279dSmrg
57fda9279dSmrgchgrpprog=${CHGRPPROG-chgrp}
58fda9279dSmrgchmodprog=${CHMODPROG-chmod}
59fda9279dSmrgchownprog=${CHOWNPROG-chown}
60fda9279dSmrgcmpprog=${CMPPROG-cmp}
61fda9279dSmrgcpprog=${CPPROG-cp}
62fda9279dSmrgmkdirprog=${MKDIRPROG-mkdir}
63fda9279dSmrgmvprog=${MVPROG-mv}
64fda9279dSmrgrmprog=${RMPROG-rm}
65fda9279dSmrgstripprog=${STRIPPROG-strip}
66fda9279dSmrg
67fda9279dSmrgposix_mkdir=
68fda9279dSmrg
69fda9279dSmrg# Desired mode of installed file.
70fda9279dSmrgmode=0755
71fda9279dSmrg
7233adc6acSmrg# Create dirs (including intermediate dirs) using mode 755.
7333adc6acSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7433adc6acSmrgmkdir_umask=22
7533adc6acSmrg
7633adc6acSmrgbackupsuffix=
77fda9279dSmrgchgrpcmd=
78fda9279dSmrgchmodcmd=$chmodprog
79fda9279dSmrgchowncmd=
80fda9279dSmrgmvcmd=$mvprog
81fda9279dSmrgrmcmd="$rmprog -f"
82fda9279dSmrgstripcmd=
83fda9279dSmrg
84fda9279dSmrgsrc=
85fda9279dSmrgdst=
86fda9279dSmrgdir_arg=
87fda9279dSmrgdst_arg=
88fda9279dSmrg
89fda9279dSmrgcopy_on_change=false
9016ee1e9aSmrgis_target_a_directory=possibly
91fda9279dSmrg
92fda9279dSmrgusage="\
93fda9279dSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94fda9279dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95fda9279dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96fda9279dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97fda9279dSmrg
98fda9279dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99fda9279dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100fda9279dSmrgIn the 4th, create DIRECTORIES.
101fda9279dSmrg
102fda9279dSmrgOptions:
103fda9279dSmrg     --help     display this help and exit.
104fda9279dSmrg     --version  display version info and exit.
105fda9279dSmrg
106fda9279dSmrg  -c            (ignored)
10733adc6acSmrg  -C            install only if different (preserve data modification time)
108fda9279dSmrg  -d            create directories instead of installing files.
109fda9279dSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110fda9279dSmrg  -m MODE       $chmodprog installed files to MODE.
111fda9279dSmrg  -o USER       $chownprog installed files to USER.
11233adc6acSmrg  -p            pass -p to $cpprog.
113fda9279dSmrg  -s            $stripprog installed files.
11433adc6acSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115fda9279dSmrg  -t DIRECTORY  install into DIRECTORY.
116fda9279dSmrg  -T            report an error if DSTFILE is a directory.
117fda9279dSmrg
118fda9279dSmrgEnvironment variables override the default commands:
119fda9279dSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120fda9279dSmrg  RMPROG STRIPPROG
12133adc6acSmrg
12233adc6acSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12333adc6acSmrgit's up to you to specify -f if you want it.
12433adc6acSmrg
12533adc6acSmrgIf -S is not specified, no backups are attempted.
12633adc6acSmrg
12733adc6acSmrgEmail bug reports to bug-automake@gnu.org.
12833adc6acSmrgAutomake home page: https://www.gnu.org/software/automake/
129fda9279dSmrg"
130fda9279dSmrg
131fda9279dSmrgwhile test $# -ne 0; do
132fda9279dSmrg  case $1 in
133fda9279dSmrg    -c) ;;
134fda9279dSmrg
135fda9279dSmrg    -C) copy_on_change=true;;
136fda9279dSmrg
137fda9279dSmrg    -d) dir_arg=true;;
138fda9279dSmrg
139fda9279dSmrg    -g) chgrpcmd="$chgrpprog $2"
14016ee1e9aSmrg        shift;;
141fda9279dSmrg
142fda9279dSmrg    --help) echo "$usage"; exit $?;;
143fda9279dSmrg
144fda9279dSmrg    -m) mode=$2
14516ee1e9aSmrg        case $mode in
14616ee1e9aSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14716ee1e9aSmrg            echo "$0: invalid mode: $mode" >&2
14816ee1e9aSmrg            exit 1;;
14916ee1e9aSmrg        esac
15016ee1e9aSmrg        shift;;
151fda9279dSmrg
152fda9279dSmrg    -o) chowncmd="$chownprog $2"
15316ee1e9aSmrg        shift;;
154fda9279dSmrg
15533adc6acSmrg    -p) cpprog="$cpprog -p";;
15633adc6acSmrg
157fda9279dSmrg    -s) stripcmd=$stripprog;;
158fda9279dSmrg
15933adc6acSmrg    -S) backupsuffix="$2"
16033adc6acSmrg        shift;;
16133adc6acSmrg
16216ee1e9aSmrg    -t)
16316ee1e9aSmrg        is_target_a_directory=always
16416ee1e9aSmrg        dst_arg=$2
16516ee1e9aSmrg        # Protect names problematic for 'test' and other utilities.
16616ee1e9aSmrg        case $dst_arg in
16716ee1e9aSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16816ee1e9aSmrg        esac
16916ee1e9aSmrg        shift;;
170fda9279dSmrg
17116ee1e9aSmrg    -T) is_target_a_directory=never;;
172fda9279dSmrg
173fda9279dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174fda9279dSmrg
17516ee1e9aSmrg    --) shift
17616ee1e9aSmrg        break;;
177fda9279dSmrg
17816ee1e9aSmrg    -*) echo "$0: invalid option: $1" >&2
17916ee1e9aSmrg        exit 1;;
180fda9279dSmrg
181fda9279dSmrg    *)  break;;
182fda9279dSmrg  esac
183fda9279dSmrg  shift
184fda9279dSmrgdone
185fda9279dSmrg
18616ee1e9aSmrg# We allow the use of options -d and -T together, by making -d
18716ee1e9aSmrg# take the precedence; this is for compatibility with GNU install.
18816ee1e9aSmrg
18916ee1e9aSmrgif test -n "$dir_arg"; then
19016ee1e9aSmrg  if test -n "$dst_arg"; then
19116ee1e9aSmrg    echo "$0: target directory not allowed when installing a directory." >&2
19216ee1e9aSmrg    exit 1
19316ee1e9aSmrg  fi
19416ee1e9aSmrgfi
19516ee1e9aSmrg
196fda9279dSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197fda9279dSmrg  # When -d is used, all remaining arguments are directories to create.
198fda9279dSmrg  # When -t is used, the destination is already specified.
199fda9279dSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200fda9279dSmrg  for arg
201fda9279dSmrg  do
202fda9279dSmrg    if test -n "$dst_arg"; then
203fda9279dSmrg      # $@ is not empty: it contains at least $arg.
204fda9279dSmrg      set fnord "$@" "$dst_arg"
205fda9279dSmrg      shift # fnord
206fda9279dSmrg    fi
207fda9279dSmrg    shift # arg
208fda9279dSmrg    dst_arg=$arg
209fda9279dSmrg    # Protect names problematic for 'test' and other utilities.
210fda9279dSmrg    case $dst_arg in
211fda9279dSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212fda9279dSmrg    esac
213fda9279dSmrg  done
214fda9279dSmrgfi
215fda9279dSmrg
216fda9279dSmrgif test $# -eq 0; then
217fda9279dSmrg  if test -z "$dir_arg"; then
218fda9279dSmrg    echo "$0: no input file specified." >&2
219fda9279dSmrg    exit 1
220fda9279dSmrg  fi
221fda9279dSmrg  # It's OK to call 'install-sh -d' without argument.
222fda9279dSmrg  # This can happen when creating conditional directories.
223fda9279dSmrg  exit 0
224fda9279dSmrgfi
225fda9279dSmrg
22616ee1e9aSmrgif test -z "$dir_arg"; then
22716ee1e9aSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22816ee1e9aSmrg    if test ! -d "$dst_arg"; then
22916ee1e9aSmrg      echo "$0: $dst_arg: Is not a directory." >&2
23016ee1e9aSmrg      exit 1
23116ee1e9aSmrg    fi
23216ee1e9aSmrg  fi
23316ee1e9aSmrgfi
23416ee1e9aSmrg
235fda9279dSmrgif test -z "$dir_arg"; then
236fda9279dSmrg  do_exit='(exit $ret); exit $ret'
237fda9279dSmrg  trap "ret=129; $do_exit" 1
238fda9279dSmrg  trap "ret=130; $do_exit" 2
239fda9279dSmrg  trap "ret=141; $do_exit" 13
240fda9279dSmrg  trap "ret=143; $do_exit" 15
241fda9279dSmrg
242fda9279dSmrg  # Set umask so as not to create temps with too-generous modes.
243fda9279dSmrg  # However, 'strip' requires both read and write access to temps.
244fda9279dSmrg  case $mode in
245fda9279dSmrg    # Optimize common cases.
246fda9279dSmrg    *644) cp_umask=133;;
247fda9279dSmrg    *755) cp_umask=22;;
248fda9279dSmrg
249fda9279dSmrg    *[0-7])
250fda9279dSmrg      if test -z "$stripcmd"; then
25116ee1e9aSmrg        u_plus_rw=
252fda9279dSmrg      else
25316ee1e9aSmrg        u_plus_rw='% 200'
254fda9279dSmrg      fi
255fda9279dSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256fda9279dSmrg    *)
257fda9279dSmrg      if test -z "$stripcmd"; then
25816ee1e9aSmrg        u_plus_rw=
259fda9279dSmrg      else
26016ee1e9aSmrg        u_plus_rw=,u+rw
261fda9279dSmrg      fi
262fda9279dSmrg      cp_umask=$mode$u_plus_rw;;
263fda9279dSmrg  esac
264fda9279dSmrgfi
265fda9279dSmrg
266fda9279dSmrgfor src
267fda9279dSmrgdo
268fda9279dSmrg  # Protect names problematic for 'test' and other utilities.
269fda9279dSmrg  case $src in
270fda9279dSmrg    -* | [=\(\)!]) src=./$src;;
271fda9279dSmrg  esac
272fda9279dSmrg
273fda9279dSmrg  if test -n "$dir_arg"; then
274fda9279dSmrg    dst=$src
275fda9279dSmrg    dstdir=$dst
276fda9279dSmrg    test -d "$dstdir"
277fda9279dSmrg    dstdir_status=$?
27833adc6acSmrg    # Don't chown directories that already exist.
27933adc6acSmrg    if test $dstdir_status = 0; then
28033adc6acSmrg      chowncmd=""
28133adc6acSmrg    fi
282fda9279dSmrg  else
283fda9279dSmrg
284fda9279dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285fda9279dSmrg    # might cause directories to be created, which would be especially bad
286fda9279dSmrg    # if $src (and thus $dsttmp) contains '*'.
287fda9279dSmrg    if test ! -f "$src" && test ! -d "$src"; then
288fda9279dSmrg      echo "$0: $src does not exist." >&2
289fda9279dSmrg      exit 1
290fda9279dSmrg    fi
291fda9279dSmrg
292fda9279dSmrg    if test -z "$dst_arg"; then
293fda9279dSmrg      echo "$0: no destination specified." >&2
294fda9279dSmrg      exit 1
295fda9279dSmrg    fi
296fda9279dSmrg    dst=$dst_arg
297fda9279dSmrg
298dd52494dSmrg    # If destination is a directory, append the input filename.
299fda9279dSmrg    if test -d "$dst"; then
30016ee1e9aSmrg      if test "$is_target_a_directory" = never; then
30116ee1e9aSmrg        echo "$0: $dst_arg: Is a directory" >&2
30216ee1e9aSmrg        exit 1
303fda9279dSmrg      fi
304fda9279dSmrg      dstdir=$dst
305dd52494dSmrg      dstbase=`basename "$src"`
306dd52494dSmrg      case $dst in
307dd52494dSmrg	*/) dst=$dst$dstbase;;
308dd52494dSmrg	*)  dst=$dst/$dstbase;;
309dd52494dSmrg      esac
310fda9279dSmrg      dstdir_status=0
311fda9279dSmrg    else
31216ee1e9aSmrg      dstdir=`dirname "$dst"`
313fda9279dSmrg      test -d "$dstdir"
314fda9279dSmrg      dstdir_status=$?
315fda9279dSmrg    fi
316fda9279dSmrg  fi
317fda9279dSmrg
318dd52494dSmrg  case $dstdir in
319dd52494dSmrg    */) dstdirslash=$dstdir;;
320dd52494dSmrg    *)  dstdirslash=$dstdir/;;
321dd52494dSmrg  esac
322dd52494dSmrg
323fda9279dSmrg  obsolete_mkdir_used=false
324fda9279dSmrg
325fda9279dSmrg  if test $dstdir_status != 0; then
326fda9279dSmrg    case $posix_mkdir in
327fda9279dSmrg      '')
32816ee1e9aSmrg        # With -d, create the new directory with the user-specified mode.
32916ee1e9aSmrg        # Otherwise, rely on $mkdir_umask.
33016ee1e9aSmrg        if test -n "$dir_arg"; then
33116ee1e9aSmrg          mkdir_mode=-m$mode
33216ee1e9aSmrg        else
33316ee1e9aSmrg          mkdir_mode=
33416ee1e9aSmrg        fi
33516ee1e9aSmrg
33616ee1e9aSmrg        posix_mkdir=false
33733adc6acSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33833adc6acSmrg	# here however when possible just to lower collision chance.
33933adc6acSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34033adc6acSmrg
34133adc6acSmrg	trap '
34233adc6acSmrg	  ret=$?
34333adc6acSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34433adc6acSmrg	  exit $ret
34533adc6acSmrg	' 0
34633adc6acSmrg
34733adc6acSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
34833adc6acSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34933adc6acSmrg	# directory is successfully created first before we actually test
35033adc6acSmrg	# 'mkdir -p'.
35133adc6acSmrg	if (umask $mkdir_umask &&
35233adc6acSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35333adc6acSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35433adc6acSmrg	then
35533adc6acSmrg	  if test -z "$dir_arg" || {
35633adc6acSmrg	       # Check for POSIX incompatibilities with -m.
35733adc6acSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35833adc6acSmrg	       # other-writable bit of parent directory when it shouldn't.
35933adc6acSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36033adc6acSmrg	       test_tmpdir="$tmpdir/a"
36133adc6acSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36233adc6acSmrg	       case $ls_ld_tmpdir in
36333adc6acSmrg		 d????-?r-*) different_mode=700;;
36433adc6acSmrg		 d????-?--*) different_mode=755;;
36533adc6acSmrg		 *) false;;
36633adc6acSmrg	       esac &&
36733adc6acSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36833adc6acSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36933adc6acSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37033adc6acSmrg	       }
37133adc6acSmrg	     }
37233adc6acSmrg	  then posix_mkdir=:
37333adc6acSmrg	  fi
37433adc6acSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37533adc6acSmrg	else
37633adc6acSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
37733adc6acSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37833adc6acSmrg	fi
37933adc6acSmrg	trap '' 0;;
380fda9279dSmrg    esac
381fda9279dSmrg
382fda9279dSmrg    if
383fda9279dSmrg      $posix_mkdir && (
38416ee1e9aSmrg        umask $mkdir_umask &&
38516ee1e9aSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386fda9279dSmrg      )
387fda9279dSmrg    then :
388fda9279dSmrg    else
389fda9279dSmrg
39033adc6acSmrg      # mkdir does not conform to POSIX,
391fda9279dSmrg      # or it failed possibly due to a race condition.  Create the
392fda9279dSmrg      # directory the slow way, step by step, checking for races as we go.
393fda9279dSmrg
394fda9279dSmrg      case $dstdir in
39516ee1e9aSmrg        /*) prefix='/';;
39616ee1e9aSmrg        [-=\(\)!]*) prefix='./';;
39716ee1e9aSmrg        *)  prefix='';;
398fda9279dSmrg      esac
399fda9279dSmrg
400fda9279dSmrg      oIFS=$IFS
401fda9279dSmrg      IFS=/
40216ee1e9aSmrg      set -f
403fda9279dSmrg      set fnord $dstdir
404fda9279dSmrg      shift
40516ee1e9aSmrg      set +f
406fda9279dSmrg      IFS=$oIFS
407fda9279dSmrg
408fda9279dSmrg      prefixes=
409fda9279dSmrg
410fda9279dSmrg      for d
411fda9279dSmrg      do
41216ee1e9aSmrg        test X"$d" = X && continue
41316ee1e9aSmrg
41416ee1e9aSmrg        prefix=$prefix$d
41516ee1e9aSmrg        if test -d "$prefix"; then
41616ee1e9aSmrg          prefixes=
41716ee1e9aSmrg        else
41816ee1e9aSmrg          if $posix_mkdir; then
41933adc6acSmrg            (umask $mkdir_umask &&
42016ee1e9aSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42116ee1e9aSmrg            # Don't fail if two instances are running concurrently.
42216ee1e9aSmrg            test -d "$prefix" || exit 1
42316ee1e9aSmrg          else
42416ee1e9aSmrg            case $prefix in
42516ee1e9aSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42616ee1e9aSmrg              *) qprefix=$prefix;;
42716ee1e9aSmrg            esac
42816ee1e9aSmrg            prefixes="$prefixes '$qprefix'"
42916ee1e9aSmrg          fi
43016ee1e9aSmrg        fi
43116ee1e9aSmrg        prefix=$prefix/
432fda9279dSmrg      done
433fda9279dSmrg
434fda9279dSmrg      if test -n "$prefixes"; then
43516ee1e9aSmrg        # Don't fail if two instances are running concurrently.
43616ee1e9aSmrg        (umask $mkdir_umask &&
43716ee1e9aSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43816ee1e9aSmrg          test -d "$dstdir" || exit 1
43916ee1e9aSmrg        obsolete_mkdir_used=true
440fda9279dSmrg      fi
441fda9279dSmrg    fi
442fda9279dSmrg  fi
443fda9279dSmrg
444fda9279dSmrg  if test -n "$dir_arg"; then
445fda9279dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446fda9279dSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447fda9279dSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448fda9279dSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449fda9279dSmrg  else
450fda9279dSmrg
451fda9279dSmrg    # Make a couple of temp file names in the proper directory.
452dd52494dSmrg    dsttmp=${dstdirslash}_inst.$$_
453dd52494dSmrg    rmtmp=${dstdirslash}_rm.$$_
454fda9279dSmrg
455fda9279dSmrg    # Trap to clean up those temp files at exit.
456fda9279dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457fda9279dSmrg
458fda9279dSmrg    # Copy the file name to the temp name.
45992405695Smrg    (umask $cp_umask &&
46092405695Smrg     { test -z "$stripcmd" || {
46192405695Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46292405695Smrg	 # which would cause strip to fail.
46392405695Smrg	 if test -z "$doit"; then
46492405695Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46592405695Smrg	 else
46692405695Smrg	   $doit touch "$dsttmp"
46792405695Smrg	 fi
46892405695Smrg       }
46992405695Smrg     } &&
47092405695Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471fda9279dSmrg
472fda9279dSmrg    # and set any options; do chmod last to preserve setuid bits.
473fda9279dSmrg    #
474fda9279dSmrg    # If any of these fail, we abort the whole thing.  If we want to
475fda9279dSmrg    # ignore errors from any of these, just make sure not to ignore
476fda9279dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477fda9279dSmrg    #
478fda9279dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479fda9279dSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480fda9279dSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481fda9279dSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482fda9279dSmrg
483fda9279dSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484fda9279dSmrg    if $copy_on_change &&
48516ee1e9aSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48616ee1e9aSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48716ee1e9aSmrg       set -f &&
488fda9279dSmrg       set X $old && old=:$2:$4:$5:$6 &&
489fda9279dSmrg       set X $new && new=:$2:$4:$5:$6 &&
49016ee1e9aSmrg       set +f &&
491fda9279dSmrg       test "$old" = "$new" &&
492fda9279dSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493fda9279dSmrg    then
494fda9279dSmrg      rm -f "$dsttmp"
495fda9279dSmrg    else
49633adc6acSmrg      # If $backupsuffix is set, and the file being installed
49733adc6acSmrg      # already exists, attempt a backup.  Don't worry if it fails,
49833adc6acSmrg      # e.g., if mv doesn't support -f.
49933adc6acSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
50033adc6acSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50133adc6acSmrg      fi
50233adc6acSmrg
503fda9279dSmrg      # Rename the file to the real destination.
504fda9279dSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505fda9279dSmrg
506fda9279dSmrg      # The rename failed, perhaps because mv can't rename something else
507fda9279dSmrg      # to itself, or perhaps because mv is so ancient that it does not
508fda9279dSmrg      # support -f.
509fda9279dSmrg      {
51016ee1e9aSmrg        # Now remove or move aside any old file at destination location.
51116ee1e9aSmrg        # We try this two ways since rm can't unlink itself on some
51216ee1e9aSmrg        # systems and the destination file might be busy for other
51316ee1e9aSmrg        # reasons.  In this case, the final cleanup might fail but the new
51416ee1e9aSmrg        # file should still install successfully.
51516ee1e9aSmrg        {
51616ee1e9aSmrg          test ! -f "$dst" ||
51733adc6acSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
51816ee1e9aSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51933adc6acSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52016ee1e9aSmrg          } ||
52116ee1e9aSmrg          { echo "$0: cannot unlink or rename $dst" >&2
52216ee1e9aSmrg            (exit 1); exit 1
52316ee1e9aSmrg          }
52416ee1e9aSmrg        } &&
52516ee1e9aSmrg
52616ee1e9aSmrg        # Now rename the file to the real destination.
52716ee1e9aSmrg        $doit $mvcmd "$dsttmp" "$dst"
528fda9279dSmrg      }
529fda9279dSmrg    fi || exit 1
530fda9279dSmrg
531fda9279dSmrg    trap '' 0
532fda9279dSmrg  fi
533fda9279dSmrgdone
534fda9279dSmrg
535fda9279dSmrg# Local variables:
536dd52494dSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537fda9279dSmrg# time-stamp-start: "scriptversion="
538fda9279dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539dd52494dSmrg# time-stamp-time-zone: "UTC0"
540fda9279dSmrg# time-stamp-end: "; # UTC"
541fda9279dSmrg# End:
542