install-sh revision 1d778f8e
1145b7b3cSmrg#!/bin/sh
2145b7b3cSmrg# install - install a program, script, or datafile
3145b7b3cSmrg
41d778f8eSmrgscriptversion=2024-06-19.01; # UTC
5145b7b3cSmrg
6145b7b3cSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7145b7b3cSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8145b7b3cSmrg# following copyright and license.
9145b7b3cSmrg#
10145b7b3cSmrg# Copyright (C) 1994 X Consortium
11145b7b3cSmrg#
12145b7b3cSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13145b7b3cSmrg# of this software and associated documentation files (the "Software"), to
14145b7b3cSmrg# deal in the Software without restriction, including without limitation the
15145b7b3cSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16145b7b3cSmrg# sell copies of the Software, and to permit persons to whom the Software is
17145b7b3cSmrg# furnished to do so, subject to the following conditions:
18145b7b3cSmrg#
19145b7b3cSmrg# The above copyright notice and this permission notice shall be included in
20145b7b3cSmrg# all copies or substantial portions of the Software.
21145b7b3cSmrg#
22145b7b3cSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23145b7b3cSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24145b7b3cSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25145b7b3cSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26145b7b3cSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27145b7b3cSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28145b7b3cSmrg#
29145b7b3cSmrg# Except as contained in this notice, the name of the X Consortium shall not
30145b7b3cSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31145b7b3cSmrg# ings in this Software without prior written authorization from the X Consor-
32145b7b3cSmrg# tium.
33145b7b3cSmrg#
34145b7b3cSmrg#
35145b7b3cSmrg# FSF changes to this file are in the public domain.
36145b7b3cSmrg#
37145b7b3cSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38b7d26471Smrg# 'make' implicit rules from creating a file called install from it
39145b7b3cSmrg# when there is no Makefile.
40145b7b3cSmrg#
41145b7b3cSmrg# This script is compatible with the BSD install script, but was written
42145b7b3cSmrg# from scratch.
43145b7b3cSmrg
44b7d26471Smrgtab='	'
45145b7b3cSmrgnl='
46145b7b3cSmrg'
47b7d26471SmrgIFS=" $tab$nl"
48145b7b3cSmrg
49b7d26471Smrg# Set DOITPROG to "echo" to test this script.
50145b7b3cSmrg
51578741aaSmrgdoit=${DOITPROG-}
52b7d26471Smrgdoit_exec=${doit:-exec}
53145b7b3cSmrg
54145b7b3cSmrg# Put in absolute file names if you don't have them in your path;
55145b7b3cSmrg# or use environment vars.
56145b7b3cSmrg
57578741aaSmrgchgrpprog=${CHGRPPROG-chgrp}
58578741aaSmrgchmodprog=${CHMODPROG-chmod}
59578741aaSmrgchownprog=${CHOWNPROG-chown}
60578741aaSmrgcmpprog=${CMPPROG-cmp}
61578741aaSmrgcpprog=${CPPROG-cp}
62578741aaSmrgmkdirprog=${MKDIRPROG-mkdir}
63578741aaSmrgmvprog=${MVPROG-mv}
64578741aaSmrgrmprog=${RMPROG-rm}
65578741aaSmrgstripprog=${STRIPPROG-strip}
66578741aaSmrg
67145b7b3cSmrgposix_mkdir=
68145b7b3cSmrg
69145b7b3cSmrg# Desired mode of installed file.
70145b7b3cSmrgmode=0755
71145b7b3cSmrg
72da2e2ef6Smrg# Create dirs (including intermediate dirs) using mode 755.
73da2e2ef6Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74da2e2ef6Smrgmkdir_umask=22
75da2e2ef6Smrg
76da2e2ef6Smrgbackupsuffix=
77578741aaSmrgchgrpcmd=
78145b7b3cSmrgchmodcmd=$chmodprog
79145b7b3cSmrgchowncmd=
80578741aaSmrgmvcmd=$mvprog
81145b7b3cSmrgrmcmd="$rmprog -f"
82578741aaSmrgstripcmd=
83578741aaSmrg
84145b7b3cSmrgsrc=
85145b7b3cSmrgdst=
86145b7b3cSmrgdir_arg=
87578741aaSmrgdst_arg=
88578741aaSmrg
89578741aaSmrgcopy_on_change=false
90b7d26471Smrgis_target_a_directory=possibly
91145b7b3cSmrg
92578741aaSmrgusage="\
93578741aaSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94145b7b3cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95145b7b3cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96145b7b3cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97145b7b3cSmrg
98145b7b3cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99145b7b3cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100145b7b3cSmrgIn the 4th, create DIRECTORIES.
101145b7b3cSmrg
102145b7b3cSmrgOptions:
103578741aaSmrg     --help     display this help and exit.
104578741aaSmrg     --version  display version info and exit.
105578741aaSmrg
106578741aaSmrg  -c            (ignored)
107da2e2ef6Smrg  -C            install only if different (preserve data modification time)
108578741aaSmrg  -d            create directories instead of installing files.
109578741aaSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110578741aaSmrg  -m MODE       $chmodprog installed files to MODE.
111578741aaSmrg  -o USER       $chownprog installed files to USER.
112da2e2ef6Smrg  -p            pass -p to $cpprog.
113578741aaSmrg  -s            $stripprog installed files.
114da2e2ef6Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115578741aaSmrg  -t DIRECTORY  install into DIRECTORY.
116578741aaSmrg  -T            report an error if DSTFILE is a directory.
117145b7b3cSmrg
118145b7b3cSmrgEnvironment variables override the default commands:
119578741aaSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120578741aaSmrg  RMPROG STRIPPROG
121da2e2ef6Smrg
122da2e2ef6SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123da2e2ef6Smrgit's up to you to specify -f if you want it.
124da2e2ef6Smrg
125da2e2ef6SmrgIf -S is not specified, no backups are attempted.
126da2e2ef6Smrg
1271d778f8eSmrgReport bugs to <bug-automake@gnu.org>.
1281d778f8eSmrgGNU Automake home page: <https://www.gnu.org/software/automake/>.
1291d778f8eSmrgGeneral help using GNU software: <https://www.gnu.org/gethelp/>."
130145b7b3cSmrg
131145b7b3cSmrgwhile test $# -ne 0; do
132145b7b3cSmrg  case $1 in
133578741aaSmrg    -c) ;;
134578741aaSmrg
135578741aaSmrg    -C) copy_on_change=true;;
136145b7b3cSmrg
137578741aaSmrg    -d) dir_arg=true;;
138145b7b3cSmrg
139145b7b3cSmrg    -g) chgrpcmd="$chgrpprog $2"
140b7d26471Smrg        shift;;
141145b7b3cSmrg
142145b7b3cSmrg    --help) echo "$usage"; exit $?;;
143145b7b3cSmrg
144145b7b3cSmrg    -m) mode=$2
145b7d26471Smrg        case $mode in
146b7d26471Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147b7d26471Smrg            echo "$0: invalid mode: $mode" >&2
148b7d26471Smrg            exit 1;;
149b7d26471Smrg        esac
150b7d26471Smrg        shift;;
151145b7b3cSmrg
152145b7b3cSmrg    -o) chowncmd="$chownprog $2"
153b7d26471Smrg        shift;;
154145b7b3cSmrg
155da2e2ef6Smrg    -p) cpprog="$cpprog -p";;
156da2e2ef6Smrg
157578741aaSmrg    -s) stripcmd=$stripprog;;
158145b7b3cSmrg
159da2e2ef6Smrg    -S) backupsuffix="$2"
160da2e2ef6Smrg        shift;;
161da2e2ef6Smrg
162b7d26471Smrg    -t)
163b7d26471Smrg        is_target_a_directory=always
164b7d26471Smrg        dst_arg=$2
165b7d26471Smrg        # Protect names problematic for 'test' and other utilities.
166b7d26471Smrg        case $dst_arg in
167b7d26471Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168b7d26471Smrg        esac
169b7d26471Smrg        shift;;
170145b7b3cSmrg
171b7d26471Smrg    -T) is_target_a_directory=never;;
172145b7b3cSmrg
1731d778f8eSmrg    --version) echo "$0 (GNU Automake) $scriptversion"; exit $?;;
174145b7b3cSmrg
175b7d26471Smrg    --) shift
176b7d26471Smrg        break;;
177145b7b3cSmrg
178b7d26471Smrg    -*) echo "$0: invalid option: $1" >&2
179b7d26471Smrg        exit 1;;
180145b7b3cSmrg
181145b7b3cSmrg    *)  break;;
182145b7b3cSmrg  esac
183578741aaSmrg  shift
184145b7b3cSmrgdone
185145b7b3cSmrg
186b7d26471Smrg# We allow the use of options -d and -T together, by making -d
187b7d26471Smrg# take the precedence; this is for compatibility with GNU install.
188b7d26471Smrg
189b7d26471Smrgif test -n "$dir_arg"; then
190b7d26471Smrg  if test -n "$dst_arg"; then
191b7d26471Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192b7d26471Smrg    exit 1
193b7d26471Smrg  fi
194b7d26471Smrgfi
195b7d26471Smrg
196578741aaSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197145b7b3cSmrg  # When -d is used, all remaining arguments are directories to create.
198145b7b3cSmrg  # When -t is used, the destination is already specified.
199145b7b3cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200145b7b3cSmrg  for arg
201145b7b3cSmrg  do
202578741aaSmrg    if test -n "$dst_arg"; then
203145b7b3cSmrg      # $@ is not empty: it contains at least $arg.
204578741aaSmrg      set fnord "$@" "$dst_arg"
205145b7b3cSmrg      shift # fnord
206145b7b3cSmrg    fi
207145b7b3cSmrg    shift # arg
208578741aaSmrg    dst_arg=$arg
209b7d26471Smrg    # Protect names problematic for 'test' and other utilities.
210b7d26471Smrg    case $dst_arg in
211b7d26471Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212b7d26471Smrg    esac
213145b7b3cSmrg  done
214145b7b3cSmrgfi
215145b7b3cSmrg
216145b7b3cSmrgif test $# -eq 0; then
217145b7b3cSmrg  if test -z "$dir_arg"; then
218145b7b3cSmrg    echo "$0: no input file specified." >&2
219145b7b3cSmrg    exit 1
220145b7b3cSmrg  fi
221b7d26471Smrg  # It's OK to call 'install-sh -d' without argument.
222145b7b3cSmrg  # This can happen when creating conditional directories.
223145b7b3cSmrg  exit 0
224145b7b3cSmrgfi
225145b7b3cSmrg
226145b7b3cSmrgif test -z "$dir_arg"; then
227b7d26471Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228b7d26471Smrg    if test ! -d "$dst_arg"; then
229b7d26471Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230b7d26471Smrg      exit 1
231b7d26471Smrg    fi
232b7d26471Smrg  fi
233b7d26471Smrgfi
234b7d26471Smrg
235b7d26471Smrgif test -z "$dir_arg"; then
236b7d26471Smrg  do_exit='(exit $ret); exit $ret'
237b7d26471Smrg  trap "ret=129; $do_exit" 1
238b7d26471Smrg  trap "ret=130; $do_exit" 2
239b7d26471Smrg  trap "ret=141; $do_exit" 13
240b7d26471Smrg  trap "ret=143; $do_exit" 15
241145b7b3cSmrg
242145b7b3cSmrg  # Set umask so as not to create temps with too-generous modes.
243145b7b3cSmrg  # However, 'strip' requires both read and write access to temps.
244145b7b3cSmrg  case $mode in
245145b7b3cSmrg    # Optimize common cases.
246145b7b3cSmrg    *644) cp_umask=133;;
247145b7b3cSmrg    *755) cp_umask=22;;
248145b7b3cSmrg
249145b7b3cSmrg    *[0-7])
250145b7b3cSmrg      if test -z "$stripcmd"; then
251b7d26471Smrg        u_plus_rw=
252145b7b3cSmrg      else
253b7d26471Smrg        u_plus_rw='% 200'
254145b7b3cSmrg      fi
255145b7b3cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256145b7b3cSmrg    *)
257145b7b3cSmrg      if test -z "$stripcmd"; then
258b7d26471Smrg        u_plus_rw=
259145b7b3cSmrg      else
260b7d26471Smrg        u_plus_rw=,u+rw
261145b7b3cSmrg      fi
262145b7b3cSmrg      cp_umask=$mode$u_plus_rw;;
263145b7b3cSmrg  esac
264145b7b3cSmrgfi
265145b7b3cSmrg
266145b7b3cSmrgfor src
267145b7b3cSmrgdo
268b7d26471Smrg  # Protect names problematic for 'test' and other utilities.
269145b7b3cSmrg  case $src in
270b7d26471Smrg    -* | [=\(\)!]) src=./$src;;
271145b7b3cSmrg  esac
272145b7b3cSmrg
273145b7b3cSmrg  if test -n "$dir_arg"; then
274145b7b3cSmrg    dst=$src
275145b7b3cSmrg    dstdir=$dst
276145b7b3cSmrg    test -d "$dstdir"
277145b7b3cSmrg    dstdir_status=$?
278da2e2ef6Smrg    # Don't chown directories that already exist.
279da2e2ef6Smrg    if test $dstdir_status = 0; then
280da2e2ef6Smrg      chowncmd=""
281da2e2ef6Smrg    fi
282145b7b3cSmrg  else
283145b7b3cSmrg
284145b7b3cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285145b7b3cSmrg    # might cause directories to be created, which would be especially bad
286145b7b3cSmrg    # if $src (and thus $dsttmp) contains '*'.
287145b7b3cSmrg    if test ! -f "$src" && test ! -d "$src"; then
288145b7b3cSmrg      echo "$0: $src does not exist." >&2
289145b7b3cSmrg      exit 1
290145b7b3cSmrg    fi
291145b7b3cSmrg
292578741aaSmrg    if test -z "$dst_arg"; then
293145b7b3cSmrg      echo "$0: no destination specified." >&2
294145b7b3cSmrg      exit 1
295145b7b3cSmrg    fi
296578741aaSmrg    dst=$dst_arg
297145b7b3cSmrg
298da2e2ef6Smrg    # If destination is a directory, append the input filename.
299145b7b3cSmrg    if test -d "$dst"; then
300b7d26471Smrg      if test "$is_target_a_directory" = never; then
301b7d26471Smrg        echo "$0: $dst_arg: Is a directory" >&2
302b7d26471Smrg        exit 1
303145b7b3cSmrg      fi
304145b7b3cSmrg      dstdir=$dst
305da2e2ef6Smrg      dstbase=`basename "$src"`
306da2e2ef6Smrg      case $dst in
307da2e2ef6Smrg	*/) dst=$dst$dstbase;;
308da2e2ef6Smrg	*)  dst=$dst/$dstbase;;
309da2e2ef6Smrg      esac
310145b7b3cSmrg      dstdir_status=0
311145b7b3cSmrg    else
312b7d26471Smrg      dstdir=`dirname "$dst"`
313145b7b3cSmrg      test -d "$dstdir"
314145b7b3cSmrg      dstdir_status=$?
315145b7b3cSmrg    fi
316145b7b3cSmrg  fi
317145b7b3cSmrg
318da2e2ef6Smrg  case $dstdir in
319da2e2ef6Smrg    */) dstdirslash=$dstdir;;
320da2e2ef6Smrg    *)  dstdirslash=$dstdir/;;
321da2e2ef6Smrg  esac
322da2e2ef6Smrg
323145b7b3cSmrg  obsolete_mkdir_used=false
324145b7b3cSmrg
325145b7b3cSmrg  if test $dstdir_status != 0; then
326145b7b3cSmrg    case $posix_mkdir in
327145b7b3cSmrg      '')
328b7d26471Smrg        # With -d, create the new directory with the user-specified mode.
329b7d26471Smrg        # Otherwise, rely on $mkdir_umask.
330b7d26471Smrg        if test -n "$dir_arg"; then
331b7d26471Smrg          mkdir_mode=-m$mode
332b7d26471Smrg        else
333b7d26471Smrg          mkdir_mode=
334b7d26471Smrg        fi
335b7d26471Smrg
336b7d26471Smrg        posix_mkdir=false
337da2e2ef6Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338da2e2ef6Smrg	# here however when possible just to lower collision chance.
339da2e2ef6Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340da2e2ef6Smrg
341da2e2ef6Smrg	trap '
342da2e2ef6Smrg	  ret=$?
343da2e2ef6Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344da2e2ef6Smrg	  exit $ret
345da2e2ef6Smrg	' 0
346da2e2ef6Smrg
347da2e2ef6Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3481d778f8eSmrg	# directly in world-writable /tmp, make sure that the '$tmpdir'
349da2e2ef6Smrg	# directory is successfully created first before we actually test
350da2e2ef6Smrg	# 'mkdir -p'.
351da2e2ef6Smrg	if (umask $mkdir_umask &&
352da2e2ef6Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353da2e2ef6Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354da2e2ef6Smrg	then
355da2e2ef6Smrg	  if test -z "$dir_arg" || {
3561d778f8eSmrg	       # Check for POSIX incompatibility with -m.
357da2e2ef6Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358da2e2ef6Smrg	       # other-writable bit of parent directory when it shouldn't.
359da2e2ef6Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360da2e2ef6Smrg	       test_tmpdir="$tmpdir/a"
361da2e2ef6Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362da2e2ef6Smrg	       case $ls_ld_tmpdir in
363da2e2ef6Smrg		 d????-?r-*) different_mode=700;;
364da2e2ef6Smrg		 d????-?--*) different_mode=755;;
365da2e2ef6Smrg		 *) false;;
366da2e2ef6Smrg	       esac &&
367da2e2ef6Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368da2e2ef6Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369da2e2ef6Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370da2e2ef6Smrg	       }
371da2e2ef6Smrg	     }
372da2e2ef6Smrg	  then posix_mkdir=:
373da2e2ef6Smrg	  fi
374da2e2ef6Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375da2e2ef6Smrg	else
376da2e2ef6Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377da2e2ef6Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378da2e2ef6Smrg	fi
379da2e2ef6Smrg	trap '' 0;;
380145b7b3cSmrg    esac
381145b7b3cSmrg
382145b7b3cSmrg    if
383145b7b3cSmrg      $posix_mkdir && (
384b7d26471Smrg        umask $mkdir_umask &&
385b7d26471Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386145b7b3cSmrg      )
387145b7b3cSmrg    then :
388145b7b3cSmrg    else
389145b7b3cSmrg
390da2e2ef6Smrg      # mkdir does not conform to POSIX,
391145b7b3cSmrg      # or it failed possibly due to a race condition.  Create the
392145b7b3cSmrg      # directory the slow way, step by step, checking for races as we go.
393145b7b3cSmrg
394145b7b3cSmrg      case $dstdir in
395b7d26471Smrg        /*) prefix='/';;
396b7d26471Smrg        [-=\(\)!]*) prefix='./';;
397b7d26471Smrg        *)  prefix='';;
398145b7b3cSmrg      esac
399145b7b3cSmrg
400145b7b3cSmrg      oIFS=$IFS
401145b7b3cSmrg      IFS=/
402b7d26471Smrg      set -f
403145b7b3cSmrg      set fnord $dstdir
404145b7b3cSmrg      shift
405b7d26471Smrg      set +f
406145b7b3cSmrg      IFS=$oIFS
407145b7b3cSmrg
408145b7b3cSmrg      prefixes=
409145b7b3cSmrg
410145b7b3cSmrg      for d
411145b7b3cSmrg      do
412b7d26471Smrg        test X"$d" = X && continue
413b7d26471Smrg
414b7d26471Smrg        prefix=$prefix$d
415b7d26471Smrg        if test -d "$prefix"; then
416b7d26471Smrg          prefixes=
417b7d26471Smrg        else
418b7d26471Smrg          if $posix_mkdir; then
419da2e2ef6Smrg            (umask $mkdir_umask &&
420b7d26471Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421b7d26471Smrg            # Don't fail if two instances are running concurrently.
422b7d26471Smrg            test -d "$prefix" || exit 1
423b7d26471Smrg          else
424b7d26471Smrg            case $prefix in
425b7d26471Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426b7d26471Smrg              *) qprefix=$prefix;;
427b7d26471Smrg            esac
428b7d26471Smrg            prefixes="$prefixes '$qprefix'"
429b7d26471Smrg          fi
430b7d26471Smrg        fi
431b7d26471Smrg        prefix=$prefix/
432145b7b3cSmrg      done
433145b7b3cSmrg
434145b7b3cSmrg      if test -n "$prefixes"; then
435b7d26471Smrg        # Don't fail if two instances are running concurrently.
436b7d26471Smrg        (umask $mkdir_umask &&
437b7d26471Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438b7d26471Smrg          test -d "$dstdir" || exit 1
439b7d26471Smrg        obsolete_mkdir_used=true
440145b7b3cSmrg      fi
441145b7b3cSmrg    fi
442145b7b3cSmrg  fi
443145b7b3cSmrg
444145b7b3cSmrg  if test -n "$dir_arg"; then
445145b7b3cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446145b7b3cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447145b7b3cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448145b7b3cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449145b7b3cSmrg  else
450145b7b3cSmrg
451145b7b3cSmrg    # Make a couple of temp file names in the proper directory.
452da2e2ef6Smrg    dsttmp=${dstdirslash}_inst.$$_
453da2e2ef6Smrg    rmtmp=${dstdirslash}_rm.$$_
454145b7b3cSmrg
455145b7b3cSmrg    # Trap to clean up those temp files at exit.
456145b7b3cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457145b7b3cSmrg
458145b7b3cSmrg    # Copy the file name to the temp name.
459da2e2ef6Smrg    (umask $cp_umask &&
460da2e2ef6Smrg     { test -z "$stripcmd" || {
461da2e2ef6Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462da2e2ef6Smrg	 # which would cause strip to fail.
463da2e2ef6Smrg	 if test -z "$doit"; then
464da2e2ef6Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465da2e2ef6Smrg	 else
466da2e2ef6Smrg	   $doit touch "$dsttmp"
467da2e2ef6Smrg	 fi
468da2e2ef6Smrg       }
469da2e2ef6Smrg     } &&
470da2e2ef6Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471145b7b3cSmrg
472145b7b3cSmrg    # and set any options; do chmod last to preserve setuid bits.
473145b7b3cSmrg    #
474145b7b3cSmrg    # If any of these fail, we abort the whole thing.  If we want to
475145b7b3cSmrg    # ignore errors from any of these, just make sure not to ignore
476145b7b3cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477145b7b3cSmrg    #
478578741aaSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479578741aaSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480578741aaSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481578741aaSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482578741aaSmrg
483578741aaSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484578741aaSmrg    if $copy_on_change &&
485b7d26471Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486b7d26471Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487b7d26471Smrg       set -f &&
488578741aaSmrg       set X $old && old=:$2:$4:$5:$6 &&
489578741aaSmrg       set X $new && new=:$2:$4:$5:$6 &&
490b7d26471Smrg       set +f &&
491578741aaSmrg       test "$old" = "$new" &&
492578741aaSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493578741aaSmrg    then
494578741aaSmrg      rm -f "$dsttmp"
495578741aaSmrg    else
496da2e2ef6Smrg      # If $backupsuffix is set, and the file being installed
497da2e2ef6Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498da2e2ef6Smrg      # e.g., if mv doesn't support -f.
499da2e2ef6Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500da2e2ef6Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501da2e2ef6Smrg      fi
502da2e2ef6Smrg
503578741aaSmrg      # Rename the file to the real destination.
504578741aaSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505578741aaSmrg
506578741aaSmrg      # The rename failed, perhaps because mv can't rename something else
507578741aaSmrg      # to itself, or perhaps because mv is so ancient that it does not
508578741aaSmrg      # support -f.
509578741aaSmrg      {
510b7d26471Smrg        # Now remove or move aside any old file at destination location.
511b7d26471Smrg        # We try this two ways since rm can't unlink itself on some
512b7d26471Smrg        # systems and the destination file might be busy for other
513b7d26471Smrg        # reasons.  In this case, the final cleanup might fail but the new
514b7d26471Smrg        # file should still install successfully.
515b7d26471Smrg        {
516b7d26471Smrg          test ! -f "$dst" ||
517da2e2ef6Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518b7d26471Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519da2e2ef6Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520b7d26471Smrg          } ||
521b7d26471Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522b7d26471Smrg            (exit 1); exit 1
523b7d26471Smrg          }
524b7d26471Smrg        } &&
525b7d26471Smrg
526b7d26471Smrg        # Now rename the file to the real destination.
527b7d26471Smrg        $doit $mvcmd "$dsttmp" "$dst"
528578741aaSmrg      }
529578741aaSmrg    fi || exit 1
530145b7b3cSmrg
531145b7b3cSmrg    trap '' 0
532145b7b3cSmrg  fi
533145b7b3cSmrgdone
534145b7b3cSmrg
535145b7b3cSmrg# Local variables:
536da2e2ef6Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537145b7b3cSmrg# time-stamp-start: "scriptversion="
538145b7b3cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539da2e2ef6Smrg# time-stamp-time-zone: "UTC0"
540578741aaSmrg# time-stamp-end: "; # UTC"
541145b7b3cSmrg# End:
542