install-sh revision d422ce2e
1fc5a983dSmrg#!/bin/sh
2fc5a983dSmrg# install - install a program, script, or datafile
3fc5a983dSmrg
4d422ce2eSmrgscriptversion=2020-11-14.01; # UTC
5fc5a983dSmrg
6fc5a983dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7fc5a983dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8fc5a983dSmrg# following copyright and license.
9fc5a983dSmrg#
10fc5a983dSmrg# Copyright (C) 1994 X Consortium
11fc5a983dSmrg#
12fc5a983dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13fc5a983dSmrg# of this software and associated documentation files (the "Software"), to
14fc5a983dSmrg# deal in the Software without restriction, including without limitation the
15fc5a983dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16fc5a983dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17fc5a983dSmrg# furnished to do so, subject to the following conditions:
18fc5a983dSmrg#
19fc5a983dSmrg# The above copyright notice and this permission notice shall be included in
20fc5a983dSmrg# all copies or substantial portions of the Software.
21fc5a983dSmrg#
22fc5a983dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23fc5a983dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24fc5a983dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25fc5a983dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26fc5a983dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27fc5a983dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28fc5a983dSmrg#
29fc5a983dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30fc5a983dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31fc5a983dSmrg# ings in this Software without prior written authorization from the X Consor-
32fc5a983dSmrg# tium.
33fc5a983dSmrg#
34fc5a983dSmrg#
35fc5a983dSmrg# FSF changes to this file are in the public domain.
36fc5a983dSmrg#
37fc5a983dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
386257f37dSmrg# 'make' implicit rules from creating a file called install from it
39fc5a983dSmrg# when there is no Makefile.
40fc5a983dSmrg#
41fc5a983dSmrg# This script is compatible with the BSD install script, but was written
42fc5a983dSmrg# from scratch.
43fc5a983dSmrg
446257f37dSmrgtab='	'
45fc5a983dSmrgnl='
46fc5a983dSmrg'
476257f37dSmrgIFS=" $tab$nl"
48fc5a983dSmrg
496257f37dSmrg# Set DOITPROG to "echo" to test this script.
50fc5a983dSmrg
51fc5a983dSmrgdoit=${DOITPROG-}
526257f37dSmrgdoit_exec=${doit:-exec}
53fc5a983dSmrg
54fc5a983dSmrg# Put in absolute file names if you don't have them in your path;
55fc5a983dSmrg# or use environment vars.
56fc5a983dSmrg
57fc5a983dSmrgchgrpprog=${CHGRPPROG-chgrp}
58fc5a983dSmrgchmodprog=${CHMODPROG-chmod}
59fc5a983dSmrgchownprog=${CHOWNPROG-chown}
60fc5a983dSmrgcmpprog=${CMPPROG-cmp}
61fc5a983dSmrgcpprog=${CPPROG-cp}
62fc5a983dSmrgmkdirprog=${MKDIRPROG-mkdir}
63fc5a983dSmrgmvprog=${MVPROG-mv}
64fc5a983dSmrgrmprog=${RMPROG-rm}
65fc5a983dSmrgstripprog=${STRIPPROG-strip}
66fc5a983dSmrg
67fc5a983dSmrgposix_mkdir=
68fc5a983dSmrg
69fc5a983dSmrg# Desired mode of installed file.
70fc5a983dSmrgmode=0755
71fc5a983dSmrg
72d422ce2eSmrg# Create dirs (including intermediate dirs) using mode 755.
73d422ce2eSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74d422ce2eSmrgmkdir_umask=22
75d422ce2eSmrg
76d422ce2eSmrgbackupsuffix=
77fc5a983dSmrgchgrpcmd=
78fc5a983dSmrgchmodcmd=$chmodprog
79fc5a983dSmrgchowncmd=
80fc5a983dSmrgmvcmd=$mvprog
81fc5a983dSmrgrmcmd="$rmprog -f"
82fc5a983dSmrgstripcmd=
83fc5a983dSmrg
84fc5a983dSmrgsrc=
85fc5a983dSmrgdst=
86fc5a983dSmrgdir_arg=
87fc5a983dSmrgdst_arg=
88fc5a983dSmrg
89fc5a983dSmrgcopy_on_change=false
906257f37dSmrgis_target_a_directory=possibly
91fc5a983dSmrg
92fc5a983dSmrgusage="\
93fc5a983dSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94fc5a983dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95fc5a983dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96fc5a983dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
97fc5a983dSmrg
98fc5a983dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99fc5a983dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100fc5a983dSmrgIn the 4th, create DIRECTORIES.
101fc5a983dSmrg
102fc5a983dSmrgOptions:
103fc5a983dSmrg     --help     display this help and exit.
104fc5a983dSmrg     --version  display version info and exit.
105fc5a983dSmrg
106fc5a983dSmrg  -c            (ignored)
107d422ce2eSmrg  -C            install only if different (preserve data modification time)
108fc5a983dSmrg  -d            create directories instead of installing files.
109fc5a983dSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110fc5a983dSmrg  -m MODE       $chmodprog installed files to MODE.
111fc5a983dSmrg  -o USER       $chownprog installed files to USER.
112d422ce2eSmrg  -p            pass -p to $cpprog.
113fc5a983dSmrg  -s            $stripprog installed files.
114d422ce2eSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115fc5a983dSmrg  -t DIRECTORY  install into DIRECTORY.
116fc5a983dSmrg  -T            report an error if DSTFILE is a directory.
117fc5a983dSmrg
118fc5a983dSmrgEnvironment variables override the default commands:
119fc5a983dSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120fc5a983dSmrg  RMPROG STRIPPROG
121d422ce2eSmrg
122d422ce2eSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123d422ce2eSmrgit's up to you to specify -f if you want it.
124d422ce2eSmrg
125d422ce2eSmrgIf -S is not specified, no backups are attempted.
126d422ce2eSmrg
127d422ce2eSmrgEmail bug reports to bug-automake@gnu.org.
128d422ce2eSmrgAutomake home page: https://www.gnu.org/software/automake/
129fc5a983dSmrg"
130fc5a983dSmrg
131fc5a983dSmrgwhile test $# -ne 0; do
132fc5a983dSmrg  case $1 in
133fc5a983dSmrg    -c) ;;
134fc5a983dSmrg
135fc5a983dSmrg    -C) copy_on_change=true;;
136fc5a983dSmrg
137fc5a983dSmrg    -d) dir_arg=true;;
138fc5a983dSmrg
139fc5a983dSmrg    -g) chgrpcmd="$chgrpprog $2"
1406257f37dSmrg        shift;;
141fc5a983dSmrg
142fc5a983dSmrg    --help) echo "$usage"; exit $?;;
143fc5a983dSmrg
144fc5a983dSmrg    -m) mode=$2
1456257f37dSmrg        case $mode in
1466257f37dSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1476257f37dSmrg            echo "$0: invalid mode: $mode" >&2
1486257f37dSmrg            exit 1;;
1496257f37dSmrg        esac
1506257f37dSmrg        shift;;
151fc5a983dSmrg
152fc5a983dSmrg    -o) chowncmd="$chownprog $2"
1536257f37dSmrg        shift;;
154fc5a983dSmrg
155d422ce2eSmrg    -p) cpprog="$cpprog -p";;
156d422ce2eSmrg
157fc5a983dSmrg    -s) stripcmd=$stripprog;;
158fc5a983dSmrg
159d422ce2eSmrg    -S) backupsuffix="$2"
160d422ce2eSmrg        shift;;
161d422ce2eSmrg
1626257f37dSmrg    -t)
1636257f37dSmrg        is_target_a_directory=always
1646257f37dSmrg        dst_arg=$2
1656257f37dSmrg        # Protect names problematic for 'test' and other utilities.
1666257f37dSmrg        case $dst_arg in
1676257f37dSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1686257f37dSmrg        esac
1696257f37dSmrg        shift;;
170fc5a983dSmrg
1716257f37dSmrg    -T) is_target_a_directory=never;;
172fc5a983dSmrg
173fc5a983dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
174fc5a983dSmrg
1756257f37dSmrg    --) shift
1766257f37dSmrg        break;;
177fc5a983dSmrg
1786257f37dSmrg    -*) echo "$0: invalid option: $1" >&2
1796257f37dSmrg        exit 1;;
180fc5a983dSmrg
181fc5a983dSmrg    *)  break;;
182fc5a983dSmrg  esac
183fc5a983dSmrg  shift
184fc5a983dSmrgdone
185fc5a983dSmrg
1866257f37dSmrg# We allow the use of options -d and -T together, by making -d
1876257f37dSmrg# take the precedence; this is for compatibility with GNU install.
1886257f37dSmrg
1896257f37dSmrgif test -n "$dir_arg"; then
1906257f37dSmrg  if test -n "$dst_arg"; then
1916257f37dSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1926257f37dSmrg    exit 1
1936257f37dSmrg  fi
1946257f37dSmrgfi
1956257f37dSmrg
196fc5a983dSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197fc5a983dSmrg  # When -d is used, all remaining arguments are directories to create.
198fc5a983dSmrg  # When -t is used, the destination is already specified.
199fc5a983dSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200fc5a983dSmrg  for arg
201fc5a983dSmrg  do
202fc5a983dSmrg    if test -n "$dst_arg"; then
203fc5a983dSmrg      # $@ is not empty: it contains at least $arg.
204fc5a983dSmrg      set fnord "$@" "$dst_arg"
205fc5a983dSmrg      shift # fnord
206fc5a983dSmrg    fi
207fc5a983dSmrg    shift # arg
208fc5a983dSmrg    dst_arg=$arg
2096257f37dSmrg    # Protect names problematic for 'test' and other utilities.
210bd304fc0Smrg    case $dst_arg in
211bd304fc0Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212bd304fc0Smrg    esac
213fc5a983dSmrg  done
214fc5a983dSmrgfi
215fc5a983dSmrg
216fc5a983dSmrgif test $# -eq 0; then
217fc5a983dSmrg  if test -z "$dir_arg"; then
218fc5a983dSmrg    echo "$0: no input file specified." >&2
219fc5a983dSmrg    exit 1
220fc5a983dSmrg  fi
2216257f37dSmrg  # It's OK to call 'install-sh -d' without argument.
222fc5a983dSmrg  # This can happen when creating conditional directories.
223fc5a983dSmrg  exit 0
224fc5a983dSmrgfi
225fc5a983dSmrg
2266257f37dSmrgif test -z "$dir_arg"; then
2276257f37dSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2286257f37dSmrg    if test ! -d "$dst_arg"; then
2296257f37dSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2306257f37dSmrg      exit 1
2316257f37dSmrg    fi
2326257f37dSmrg  fi
2336257f37dSmrgfi
2346257f37dSmrg
235fc5a983dSmrgif test -z "$dir_arg"; then
236bd304fc0Smrg  do_exit='(exit $ret); exit $ret'
237bd304fc0Smrg  trap "ret=129; $do_exit" 1
238bd304fc0Smrg  trap "ret=130; $do_exit" 2
239bd304fc0Smrg  trap "ret=141; $do_exit" 13
240bd304fc0Smrg  trap "ret=143; $do_exit" 15
241fc5a983dSmrg
242fc5a983dSmrg  # Set umask so as not to create temps with too-generous modes.
243fc5a983dSmrg  # However, 'strip' requires both read and write access to temps.
244fc5a983dSmrg  case $mode in
245fc5a983dSmrg    # Optimize common cases.
246fc5a983dSmrg    *644) cp_umask=133;;
247fc5a983dSmrg    *755) cp_umask=22;;
248fc5a983dSmrg
249fc5a983dSmrg    *[0-7])
250fc5a983dSmrg      if test -z "$stripcmd"; then
2516257f37dSmrg        u_plus_rw=
252fc5a983dSmrg      else
2536257f37dSmrg        u_plus_rw='% 200'
254fc5a983dSmrg      fi
255fc5a983dSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256fc5a983dSmrg    *)
257fc5a983dSmrg      if test -z "$stripcmd"; then
2586257f37dSmrg        u_plus_rw=
259fc5a983dSmrg      else
2606257f37dSmrg        u_plus_rw=,u+rw
261fc5a983dSmrg      fi
262fc5a983dSmrg      cp_umask=$mode$u_plus_rw;;
263fc5a983dSmrg  esac
264fc5a983dSmrgfi
265fc5a983dSmrg
266fc5a983dSmrgfor src
267fc5a983dSmrgdo
2686257f37dSmrg  # Protect names problematic for 'test' and other utilities.
269fc5a983dSmrg  case $src in
270bd304fc0Smrg    -* | [=\(\)!]) src=./$src;;
271fc5a983dSmrg  esac
272fc5a983dSmrg
273fc5a983dSmrg  if test -n "$dir_arg"; then
274fc5a983dSmrg    dst=$src
275fc5a983dSmrg    dstdir=$dst
276fc5a983dSmrg    test -d "$dstdir"
277fc5a983dSmrg    dstdir_status=$?
278d422ce2eSmrg    # Don't chown directories that already exist.
279d422ce2eSmrg    if test $dstdir_status = 0; then
280d422ce2eSmrg      chowncmd=""
281d422ce2eSmrg    fi
282fc5a983dSmrg  else
283fc5a983dSmrg
284fc5a983dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285fc5a983dSmrg    # might cause directories to be created, which would be especially bad
286fc5a983dSmrg    # if $src (and thus $dsttmp) contains '*'.
287fc5a983dSmrg    if test ! -f "$src" && test ! -d "$src"; then
288fc5a983dSmrg      echo "$0: $src does not exist." >&2
289fc5a983dSmrg      exit 1
290fc5a983dSmrg    fi
291fc5a983dSmrg
292fc5a983dSmrg    if test -z "$dst_arg"; then
293fc5a983dSmrg      echo "$0: no destination specified." >&2
294fc5a983dSmrg      exit 1
295fc5a983dSmrg    fi
296fc5a983dSmrg    dst=$dst_arg
297fc5a983dSmrg
298d422ce2eSmrg    # If destination is a directory, append the input filename.
299fc5a983dSmrg    if test -d "$dst"; then
3006257f37dSmrg      if test "$is_target_a_directory" = never; then
3016257f37dSmrg        echo "$0: $dst_arg: Is a directory" >&2
3026257f37dSmrg        exit 1
303fc5a983dSmrg      fi
304fc5a983dSmrg      dstdir=$dst
305d422ce2eSmrg      dstbase=`basename "$src"`
306d422ce2eSmrg      case $dst in
307d422ce2eSmrg	*/) dst=$dst$dstbase;;
308d422ce2eSmrg	*)  dst=$dst/$dstbase;;
309d422ce2eSmrg      esac
310fc5a983dSmrg      dstdir_status=0
311fc5a983dSmrg    else
3126257f37dSmrg      dstdir=`dirname "$dst"`
313fc5a983dSmrg      test -d "$dstdir"
314fc5a983dSmrg      dstdir_status=$?
315fc5a983dSmrg    fi
316fc5a983dSmrg  fi
317fc5a983dSmrg
318d422ce2eSmrg  case $dstdir in
319d422ce2eSmrg    */) dstdirslash=$dstdir;;
320d422ce2eSmrg    *)  dstdirslash=$dstdir/;;
321d422ce2eSmrg  esac
322d422ce2eSmrg
323fc5a983dSmrg  obsolete_mkdir_used=false
324fc5a983dSmrg
325fc5a983dSmrg  if test $dstdir_status != 0; then
326fc5a983dSmrg    case $posix_mkdir in
327fc5a983dSmrg      '')
3286257f37dSmrg        # With -d, create the new directory with the user-specified mode.
3296257f37dSmrg        # Otherwise, rely on $mkdir_umask.
3306257f37dSmrg        if test -n "$dir_arg"; then
3316257f37dSmrg          mkdir_mode=-m$mode
3326257f37dSmrg        else
3336257f37dSmrg          mkdir_mode=
3346257f37dSmrg        fi
3356257f37dSmrg
3366257f37dSmrg        posix_mkdir=false
337d422ce2eSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338d422ce2eSmrg	# here however when possible just to lower collision chance.
339d422ce2eSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340d422ce2eSmrg
341d422ce2eSmrg	trap '
342d422ce2eSmrg	  ret=$?
343d422ce2eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344d422ce2eSmrg	  exit $ret
345d422ce2eSmrg	' 0
346d422ce2eSmrg
347d422ce2eSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
348d422ce2eSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349d422ce2eSmrg	# directory is successfully created first before we actually test
350d422ce2eSmrg	# 'mkdir -p'.
351d422ce2eSmrg	if (umask $mkdir_umask &&
352d422ce2eSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353d422ce2eSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354d422ce2eSmrg	then
355d422ce2eSmrg	  if test -z "$dir_arg" || {
356d422ce2eSmrg	       # Check for POSIX incompatibilities with -m.
357d422ce2eSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358d422ce2eSmrg	       # other-writable bit of parent directory when it shouldn't.
359d422ce2eSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360d422ce2eSmrg	       test_tmpdir="$tmpdir/a"
361d422ce2eSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362d422ce2eSmrg	       case $ls_ld_tmpdir in
363d422ce2eSmrg		 d????-?r-*) different_mode=700;;
364d422ce2eSmrg		 d????-?--*) different_mode=755;;
365d422ce2eSmrg		 *) false;;
366d422ce2eSmrg	       esac &&
367d422ce2eSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368d422ce2eSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369d422ce2eSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370d422ce2eSmrg	       }
371d422ce2eSmrg	     }
372d422ce2eSmrg	  then posix_mkdir=:
373d422ce2eSmrg	  fi
374d422ce2eSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375d422ce2eSmrg	else
376d422ce2eSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
377d422ce2eSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378d422ce2eSmrg	fi
379d422ce2eSmrg	trap '' 0;;
380fc5a983dSmrg    esac
381fc5a983dSmrg
382fc5a983dSmrg    if
383fc5a983dSmrg      $posix_mkdir && (
3846257f37dSmrg        umask $mkdir_umask &&
3856257f37dSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386fc5a983dSmrg      )
387fc5a983dSmrg    then :
388fc5a983dSmrg    else
389fc5a983dSmrg
390d422ce2eSmrg      # mkdir does not conform to POSIX,
391fc5a983dSmrg      # or it failed possibly due to a race condition.  Create the
392fc5a983dSmrg      # directory the slow way, step by step, checking for races as we go.
393fc5a983dSmrg
394fc5a983dSmrg      case $dstdir in
3956257f37dSmrg        /*) prefix='/';;
3966257f37dSmrg        [-=\(\)!]*) prefix='./';;
3976257f37dSmrg        *)  prefix='';;
398fc5a983dSmrg      esac
399fc5a983dSmrg
400fc5a983dSmrg      oIFS=$IFS
401fc5a983dSmrg      IFS=/
4026257f37dSmrg      set -f
403fc5a983dSmrg      set fnord $dstdir
404fc5a983dSmrg      shift
4056257f37dSmrg      set +f
406fc5a983dSmrg      IFS=$oIFS
407fc5a983dSmrg
408fc5a983dSmrg      prefixes=
409fc5a983dSmrg
410fc5a983dSmrg      for d
411fc5a983dSmrg      do
4126257f37dSmrg        test X"$d" = X && continue
4136257f37dSmrg
4146257f37dSmrg        prefix=$prefix$d
4156257f37dSmrg        if test -d "$prefix"; then
4166257f37dSmrg          prefixes=
4176257f37dSmrg        else
4186257f37dSmrg          if $posix_mkdir; then
419d422ce2eSmrg            (umask $mkdir_umask &&
4206257f37dSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4216257f37dSmrg            # Don't fail if two instances are running concurrently.
4226257f37dSmrg            test -d "$prefix" || exit 1
4236257f37dSmrg          else
4246257f37dSmrg            case $prefix in
4256257f37dSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4266257f37dSmrg              *) qprefix=$prefix;;
4276257f37dSmrg            esac
4286257f37dSmrg            prefixes="$prefixes '$qprefix'"
4296257f37dSmrg          fi
4306257f37dSmrg        fi
4316257f37dSmrg        prefix=$prefix/
432fc5a983dSmrg      done
433fc5a983dSmrg
434fc5a983dSmrg      if test -n "$prefixes"; then
4356257f37dSmrg        # Don't fail if two instances are running concurrently.
4366257f37dSmrg        (umask $mkdir_umask &&
4376257f37dSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4386257f37dSmrg          test -d "$dstdir" || exit 1
4396257f37dSmrg        obsolete_mkdir_used=true
440fc5a983dSmrg      fi
441fc5a983dSmrg    fi
442fc5a983dSmrg  fi
443fc5a983dSmrg
444fc5a983dSmrg  if test -n "$dir_arg"; then
445fc5a983dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446fc5a983dSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447fc5a983dSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448fc5a983dSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449fc5a983dSmrg  else
450fc5a983dSmrg
451fc5a983dSmrg    # Make a couple of temp file names in the proper directory.
452d422ce2eSmrg    dsttmp=${dstdirslash}_inst.$$_
453d422ce2eSmrg    rmtmp=${dstdirslash}_rm.$$_
454fc5a983dSmrg
455fc5a983dSmrg    # Trap to clean up those temp files at exit.
456fc5a983dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457fc5a983dSmrg
458fc5a983dSmrg    # Copy the file name to the temp name.
459d422ce2eSmrg    (umask $cp_umask &&
460d422ce2eSmrg     { test -z "$stripcmd" || {
461d422ce2eSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462d422ce2eSmrg	 # which would cause strip to fail.
463d422ce2eSmrg	 if test -z "$doit"; then
464d422ce2eSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465d422ce2eSmrg	 else
466d422ce2eSmrg	   $doit touch "$dsttmp"
467d422ce2eSmrg	 fi
468d422ce2eSmrg       }
469d422ce2eSmrg     } &&
470d422ce2eSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471fc5a983dSmrg
472fc5a983dSmrg    # and set any options; do chmod last to preserve setuid bits.
473fc5a983dSmrg    #
474fc5a983dSmrg    # If any of these fail, we abort the whole thing.  If we want to
475fc5a983dSmrg    # ignore errors from any of these, just make sure not to ignore
476fc5a983dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477fc5a983dSmrg    #
478fc5a983dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479fc5a983dSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480fc5a983dSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481fc5a983dSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482fc5a983dSmrg
483fc5a983dSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484fc5a983dSmrg    if $copy_on_change &&
4856257f37dSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4866257f37dSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4876257f37dSmrg       set -f &&
488fc5a983dSmrg       set X $old && old=:$2:$4:$5:$6 &&
489fc5a983dSmrg       set X $new && new=:$2:$4:$5:$6 &&
4906257f37dSmrg       set +f &&
491fc5a983dSmrg       test "$old" = "$new" &&
492fc5a983dSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493fc5a983dSmrg    then
494fc5a983dSmrg      rm -f "$dsttmp"
495fc5a983dSmrg    else
496d422ce2eSmrg      # If $backupsuffix is set, and the file being installed
497d422ce2eSmrg      # already exists, attempt a backup.  Don't worry if it fails,
498d422ce2eSmrg      # e.g., if mv doesn't support -f.
499d422ce2eSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
500d422ce2eSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501d422ce2eSmrg      fi
502d422ce2eSmrg
503fc5a983dSmrg      # Rename the file to the real destination.
504fc5a983dSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505fc5a983dSmrg
506fc5a983dSmrg      # The rename failed, perhaps because mv can't rename something else
507fc5a983dSmrg      # to itself, or perhaps because mv is so ancient that it does not
508fc5a983dSmrg      # support -f.
509fc5a983dSmrg      {
5106257f37dSmrg        # Now remove or move aside any old file at destination location.
5116257f37dSmrg        # We try this two ways since rm can't unlink itself on some
5126257f37dSmrg        # systems and the destination file might be busy for other
5136257f37dSmrg        # reasons.  In this case, the final cleanup might fail but the new
5146257f37dSmrg        # file should still install successfully.
5156257f37dSmrg        {
5166257f37dSmrg          test ! -f "$dst" ||
517d422ce2eSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
5186257f37dSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519d422ce2eSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5206257f37dSmrg          } ||
5216257f37dSmrg          { echo "$0: cannot unlink or rename $dst" >&2
5226257f37dSmrg            (exit 1); exit 1
5236257f37dSmrg          }
5246257f37dSmrg        } &&
5256257f37dSmrg
5266257f37dSmrg        # Now rename the file to the real destination.
5276257f37dSmrg        $doit $mvcmd "$dsttmp" "$dst"
528fc5a983dSmrg      }
529fc5a983dSmrg    fi || exit 1
530fc5a983dSmrg
531fc5a983dSmrg    trap '' 0
532fc5a983dSmrg  fi
533fc5a983dSmrgdone
534fc5a983dSmrg
535fc5a983dSmrg# Local variables:
536d422ce2eSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537fc5a983dSmrg# time-stamp-start: "scriptversion="
538fc5a983dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539d422ce2eSmrg# time-stamp-time-zone: "UTC0"
5406086d97eSmrg# time-stamp-end: "; # UTC"
541fc5a983dSmrg# End:
542