install-sh revision 1c145c9f
14fb28925Smrg#!/bin/sh
24fb28925Smrg# install - install a program, script, or datafile
3b66d1acbSmrg
41c145c9fSmrgscriptversion=2020-11-14.01; # UTC
5b66d1acbSmrg
64fb28925Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
74fb28925Smrg# later released in X11R6 (xc/config/util/install.sh) with the
84fb28925Smrg# following copyright and license.
94fb28925Smrg#
104fb28925Smrg# Copyright (C) 1994 X Consortium
114fb28925Smrg#
124fb28925Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
134fb28925Smrg# of this software and associated documentation files (the "Software"), to
144fb28925Smrg# deal in the Software without restriction, including without limitation the
154fb28925Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164fb28925Smrg# sell copies of the Software, and to permit persons to whom the Software is
174fb28925Smrg# furnished to do so, subject to the following conditions:
184fb28925Smrg#
194fb28925Smrg# The above copyright notice and this permission notice shall be included in
204fb28925Smrg# all copies or substantial portions of the Software.
214fb28925Smrg#
224fb28925Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234fb28925Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244fb28925Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254fb28925Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264fb28925Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274fb28925Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284fb28925Smrg#
294fb28925Smrg# Except as contained in this notice, the name of the X Consortium shall not
304fb28925Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
314fb28925Smrg# ings in this Software without prior written authorization from the X Consor-
324fb28925Smrg# tium.
334fb28925Smrg#
344fb28925Smrg#
354fb28925Smrg# FSF changes to this file are in the public domain.
364fb28925Smrg#
374fb28925Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38df58931aSmrg# 'make' implicit rules from creating a file called install from it
394fb28925Smrg# when there is no Makefile.
404fb28925Smrg#
414fb28925Smrg# This script is compatible with the BSD install script, but was written
42b66d1acbSmrg# from scratch.
434fb28925Smrg
444929c8f8Smrgtab='	'
45b66d1acbSmrgnl='
46b66d1acbSmrg'
474929c8f8SmrgIFS=" $tab$nl"
484fb28925Smrg
494929c8f8Smrg# Set DOITPROG to "echo" to test this script.
504fb28925Smrg
51b66d1acbSmrgdoit=${DOITPROG-}
524929c8f8Smrgdoit_exec=${doit:-exec}
534fb28925Smrg
54b66d1acbSmrg# Put in absolute file names if you don't have them in your path;
55b66d1acbSmrg# or use environment vars.
56b66d1acbSmrg
57b66d1acbSmrgchgrpprog=${CHGRPPROG-chgrp}
58b66d1acbSmrgchmodprog=${CHMODPROG-chmod}
59b66d1acbSmrgchownprog=${CHOWNPROG-chown}
60b66d1acbSmrgcmpprog=${CMPPROG-cmp}
61b66d1acbSmrgcpprog=${CPPROG-cp}
62b66d1acbSmrgmkdirprog=${MKDIRPROG-mkdir}
63b66d1acbSmrgmvprog=${MVPROG-mv}
64b66d1acbSmrgrmprog=${RMPROG-rm}
65b66d1acbSmrgstripprog=${STRIPPROG-strip}
66b66d1acbSmrg
67b66d1acbSmrgposix_mkdir=
68b66d1acbSmrg
69b66d1acbSmrg# Desired mode of installed file.
70b66d1acbSmrgmode=0755
71b66d1acbSmrg
721c145c9fSmrg# Create dirs (including intermediate dirs) using mode 755.
731c145c9fSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
741c145c9fSmrgmkdir_umask=22
751c145c9fSmrg
761c145c9fSmrgbackupsuffix=
77b66d1acbSmrgchgrpcmd=
78b66d1acbSmrgchmodcmd=$chmodprog
79b66d1acbSmrgchowncmd=
80b66d1acbSmrgmvcmd=$mvprog
81b66d1acbSmrgrmcmd="$rmprog -f"
82b66d1acbSmrgstripcmd=
834fb28925Smrg
84b66d1acbSmrgsrc=
85b66d1acbSmrgdst=
86b66d1acbSmrgdir_arg=
87b66d1acbSmrgdst_arg=
884fb28925Smrg
89b66d1acbSmrgcopy_on_change=false
904929c8f8Smrgis_target_a_directory=possibly
914fb28925Smrg
92b66d1acbSmrgusage="\
93b66d1acbSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94b66d1acbSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95b66d1acbSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96b66d1acbSmrg   or: $0 [OPTION]... -d DIRECTORIES...
974fb28925Smrg
98b66d1acbSmrgIn the 1st form, copy SRCFILE to DSTFILE.
99b66d1acbSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100b66d1acbSmrgIn the 4th, create DIRECTORIES.
1014fb28925Smrg
102b66d1acbSmrgOptions:
103b66d1acbSmrg     --help     display this help and exit.
104b66d1acbSmrg     --version  display version info and exit.
1054fb28925Smrg
106b66d1acbSmrg  -c            (ignored)
1071c145c9fSmrg  -C            install only if different (preserve data modification time)
108b66d1acbSmrg  -d            create directories instead of installing files.
109b66d1acbSmrg  -g GROUP      $chgrpprog installed files to GROUP.
110b66d1acbSmrg  -m MODE       $chmodprog installed files to MODE.
111b66d1acbSmrg  -o USER       $chownprog installed files to USER.
1121c145c9fSmrg  -p            pass -p to $cpprog.
113b66d1acbSmrg  -s            $stripprog installed files.
1141c145c9fSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
115b66d1acbSmrg  -t DIRECTORY  install into DIRECTORY.
116b66d1acbSmrg  -T            report an error if DSTFILE is a directory.
1174fb28925Smrg
118b66d1acbSmrgEnvironment variables override the default commands:
119b66d1acbSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
120b66d1acbSmrg  RMPROG STRIPPROG
1211c145c9fSmrg
1221c145c9fSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1231c145c9fSmrgit's up to you to specify -f if you want it.
1241c145c9fSmrg
1251c145c9fSmrgIf -S is not specified, no backups are attempted.
1261c145c9fSmrg
1271c145c9fSmrgEmail bug reports to bug-automake@gnu.org.
1281c145c9fSmrgAutomake home page: https://www.gnu.org/software/automake/
129b66d1acbSmrg"
1304fb28925Smrg
131b66d1acbSmrgwhile test $# -ne 0; do
132b66d1acbSmrg  case $1 in
133b66d1acbSmrg    -c) ;;
1344fb28925Smrg
135b66d1acbSmrg    -C) copy_on_change=true;;
1364fb28925Smrg
137b66d1acbSmrg    -d) dir_arg=true;;
1384fb28925Smrg
139b66d1acbSmrg    -g) chgrpcmd="$chgrpprog $2"
1404929c8f8Smrg        shift;;
1414fb28925Smrg
142b66d1acbSmrg    --help) echo "$usage"; exit $?;;
1434fb28925Smrg
144b66d1acbSmrg    -m) mode=$2
1454929c8f8Smrg        case $mode in
1464929c8f8Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1474929c8f8Smrg            echo "$0: invalid mode: $mode" >&2
1484929c8f8Smrg            exit 1;;
1494929c8f8Smrg        esac
1504929c8f8Smrg        shift;;
1514fb28925Smrg
152b66d1acbSmrg    -o) chowncmd="$chownprog $2"
1534929c8f8Smrg        shift;;
1544fb28925Smrg
1551c145c9fSmrg    -p) cpprog="$cpprog -p";;
1561c145c9fSmrg
157b66d1acbSmrg    -s) stripcmd=$stripprog;;
1584fb28925Smrg
1591c145c9fSmrg    -S) backupsuffix="$2"
1601c145c9fSmrg        shift;;
1611c145c9fSmrg
1624929c8f8Smrg    -t)
1634929c8f8Smrg        is_target_a_directory=always
1644929c8f8Smrg        dst_arg=$2
1654929c8f8Smrg        # Protect names problematic for 'test' and other utilities.
1664929c8f8Smrg        case $dst_arg in
1674929c8f8Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1684929c8f8Smrg        esac
1694929c8f8Smrg        shift;;
1704fb28925Smrg
1714929c8f8Smrg    -T) is_target_a_directory=never;;
1724fb28925Smrg
173b66d1acbSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1744fb28925Smrg
1754929c8f8Smrg    --) shift
1764929c8f8Smrg        break;;
1774fb28925Smrg
1784929c8f8Smrg    -*) echo "$0: invalid option: $1" >&2
1794929c8f8Smrg        exit 1;;
1804fb28925Smrg
181b66d1acbSmrg    *)  break;;
182b66d1acbSmrg  esac
183b66d1acbSmrg  shift
184b66d1acbSmrgdone
1854fb28925Smrg
1864929c8f8Smrg# We allow the use of options -d and -T together, by making -d
1874929c8f8Smrg# take the precedence; this is for compatibility with GNU install.
1884929c8f8Smrg
1894929c8f8Smrgif test -n "$dir_arg"; then
1904929c8f8Smrg  if test -n "$dst_arg"; then
1914929c8f8Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1924929c8f8Smrg    exit 1
1934929c8f8Smrg  fi
1944929c8f8Smrgfi
1954929c8f8Smrg
196b66d1acbSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
197b66d1acbSmrg  # When -d is used, all remaining arguments are directories to create.
198b66d1acbSmrg  # When -t is used, the destination is already specified.
199b66d1acbSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
200b66d1acbSmrg  for arg
201b66d1acbSmrg  do
202b66d1acbSmrg    if test -n "$dst_arg"; then
203b66d1acbSmrg      # $@ is not empty: it contains at least $arg.
204b66d1acbSmrg      set fnord "$@" "$dst_arg"
205b66d1acbSmrg      shift # fnord
206b66d1acbSmrg    fi
207b66d1acbSmrg    shift # arg
208b66d1acbSmrg    dst_arg=$arg
209df58931aSmrg    # Protect names problematic for 'test' and other utilities.
210df58931aSmrg    case $dst_arg in
211df58931aSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212df58931aSmrg    esac
213b66d1acbSmrg  done
214b66d1acbSmrgfi
2154fb28925Smrg
216b66d1acbSmrgif test $# -eq 0; then
217b66d1acbSmrg  if test -z "$dir_arg"; then
218b66d1acbSmrg    echo "$0: no input file specified." >&2
219b66d1acbSmrg    exit 1
220b66d1acbSmrg  fi
221df58931aSmrg  # It's OK to call 'install-sh -d' without argument.
222b66d1acbSmrg  # This can happen when creating conditional directories.
223b66d1acbSmrg  exit 0
224b66d1acbSmrgfi
2254fb28925Smrg
2264929c8f8Smrgif test -z "$dir_arg"; then
2274929c8f8Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2284929c8f8Smrg    if test ! -d "$dst_arg"; then
2294929c8f8Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2304929c8f8Smrg      exit 1
2314929c8f8Smrg    fi
2324929c8f8Smrg  fi
2334929c8f8Smrgfi
2344929c8f8Smrg
235b66d1acbSmrgif test -z "$dir_arg"; then
236df58931aSmrg  do_exit='(exit $ret); exit $ret'
237df58931aSmrg  trap "ret=129; $do_exit" 1
238df58931aSmrg  trap "ret=130; $do_exit" 2
239df58931aSmrg  trap "ret=141; $do_exit" 13
240df58931aSmrg  trap "ret=143; $do_exit" 15
241b66d1acbSmrg
242b66d1acbSmrg  # Set umask so as not to create temps with too-generous modes.
243b66d1acbSmrg  # However, 'strip' requires both read and write access to temps.
244b66d1acbSmrg  case $mode in
245b66d1acbSmrg    # Optimize common cases.
246b66d1acbSmrg    *644) cp_umask=133;;
247b66d1acbSmrg    *755) cp_umask=22;;
248b66d1acbSmrg
249b66d1acbSmrg    *[0-7])
250b66d1acbSmrg      if test -z "$stripcmd"; then
2514929c8f8Smrg        u_plus_rw=
252b66d1acbSmrg      else
2534929c8f8Smrg        u_plus_rw='% 200'
254b66d1acbSmrg      fi
255b66d1acbSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
256b66d1acbSmrg    *)
257b66d1acbSmrg      if test -z "$stripcmd"; then
2584929c8f8Smrg        u_plus_rw=
259b66d1acbSmrg      else
2604929c8f8Smrg        u_plus_rw=,u+rw
261b66d1acbSmrg      fi
262b66d1acbSmrg      cp_umask=$mode$u_plus_rw;;
263b66d1acbSmrg  esac
264b66d1acbSmrgfi
2654fb28925Smrg
266b66d1acbSmrgfor src
267b66d1acbSmrgdo
268df58931aSmrg  # Protect names problematic for 'test' and other utilities.
269b66d1acbSmrg  case $src in
270df58931aSmrg    -* | [=\(\)!]) src=./$src;;
271b66d1acbSmrg  esac
272b66d1acbSmrg
273b66d1acbSmrg  if test -n "$dir_arg"; then
274b66d1acbSmrg    dst=$src
275b66d1acbSmrg    dstdir=$dst
276b66d1acbSmrg    test -d "$dstdir"
277b66d1acbSmrg    dstdir_status=$?
2781c145c9fSmrg    # Don't chown directories that already exist.
2791c145c9fSmrg    if test $dstdir_status = 0; then
2801c145c9fSmrg      chowncmd=""
2811c145c9fSmrg    fi
282b66d1acbSmrg  else
283b66d1acbSmrg
284b66d1acbSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285b66d1acbSmrg    # might cause directories to be created, which would be especially bad
286b66d1acbSmrg    # if $src (and thus $dsttmp) contains '*'.
287b66d1acbSmrg    if test ! -f "$src" && test ! -d "$src"; then
288b66d1acbSmrg      echo "$0: $src does not exist." >&2
289b66d1acbSmrg      exit 1
290b66d1acbSmrg    fi
291b66d1acbSmrg
292b66d1acbSmrg    if test -z "$dst_arg"; then
293b66d1acbSmrg      echo "$0: no destination specified." >&2
294b66d1acbSmrg      exit 1
295b66d1acbSmrg    fi
296b66d1acbSmrg    dst=$dst_arg
2974fb28925Smrg
2981c145c9fSmrg    # If destination is a directory, append the input filename.
299b66d1acbSmrg    if test -d "$dst"; then
3004929c8f8Smrg      if test "$is_target_a_directory" = never; then
3014929c8f8Smrg        echo "$0: $dst_arg: Is a directory" >&2
3024929c8f8Smrg        exit 1
303b66d1acbSmrg      fi
304b66d1acbSmrg      dstdir=$dst
3051c145c9fSmrg      dstbase=`basename "$src"`
3061c145c9fSmrg      case $dst in
3071c145c9fSmrg	*/) dst=$dst$dstbase;;
3081c145c9fSmrg	*)  dst=$dst/$dstbase;;
3091c145c9fSmrg      esac
310b66d1acbSmrg      dstdir_status=0
311b66d1acbSmrg    else
3124929c8f8Smrg      dstdir=`dirname "$dst"`
313b66d1acbSmrg      test -d "$dstdir"
314b66d1acbSmrg      dstdir_status=$?
315b66d1acbSmrg    fi
316b66d1acbSmrg  fi
317b66d1acbSmrg
3181c145c9fSmrg  case $dstdir in
3191c145c9fSmrg    */) dstdirslash=$dstdir;;
3201c145c9fSmrg    *)  dstdirslash=$dstdir/;;
3211c145c9fSmrg  esac
3221c145c9fSmrg
323b66d1acbSmrg  obsolete_mkdir_used=false
324b66d1acbSmrg
325b66d1acbSmrg  if test $dstdir_status != 0; then
326b66d1acbSmrg    case $posix_mkdir in
327b66d1acbSmrg      '')
3284929c8f8Smrg        # With -d, create the new directory with the user-specified mode.
3294929c8f8Smrg        # Otherwise, rely on $mkdir_umask.
3304929c8f8Smrg        if test -n "$dir_arg"; then
3314929c8f8Smrg          mkdir_mode=-m$mode
3324929c8f8Smrg        else
3334929c8f8Smrg          mkdir_mode=
3344929c8f8Smrg        fi
3354929c8f8Smrg
3364929c8f8Smrg        posix_mkdir=false
3371c145c9fSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3381c145c9fSmrg	# here however when possible just to lower collision chance.
3391c145c9fSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3401c145c9fSmrg
3411c145c9fSmrg	trap '
3421c145c9fSmrg	  ret=$?
3431c145c9fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3441c145c9fSmrg	  exit $ret
3451c145c9fSmrg	' 0
3461c145c9fSmrg
3471c145c9fSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3481c145c9fSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3491c145c9fSmrg	# directory is successfully created first before we actually test
3501c145c9fSmrg	# 'mkdir -p'.
3511c145c9fSmrg	if (umask $mkdir_umask &&
3521c145c9fSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3531c145c9fSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3541c145c9fSmrg	then
3551c145c9fSmrg	  if test -z "$dir_arg" || {
3561c145c9fSmrg	       # Check for POSIX incompatibilities with -m.
3571c145c9fSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3581c145c9fSmrg	       # other-writable bit of parent directory when it shouldn't.
3591c145c9fSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3601c145c9fSmrg	       test_tmpdir="$tmpdir/a"
3611c145c9fSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3621c145c9fSmrg	       case $ls_ld_tmpdir in
3631c145c9fSmrg		 d????-?r-*) different_mode=700;;
3641c145c9fSmrg		 d????-?--*) different_mode=755;;
3651c145c9fSmrg		 *) false;;
3661c145c9fSmrg	       esac &&
3671c145c9fSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3681c145c9fSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3691c145c9fSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3701c145c9fSmrg	       }
3711c145c9fSmrg	     }
3721c145c9fSmrg	  then posix_mkdir=:
3731c145c9fSmrg	  fi
3741c145c9fSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3751c145c9fSmrg	else
3761c145c9fSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3771c145c9fSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3781c145c9fSmrg	fi
3791c145c9fSmrg	trap '' 0;;
380b66d1acbSmrg    esac
3814fb28925Smrg
382b66d1acbSmrg    if
383b66d1acbSmrg      $posix_mkdir && (
3844929c8f8Smrg        umask $mkdir_umask &&
3854929c8f8Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
386b66d1acbSmrg      )
387b66d1acbSmrg    then :
388b66d1acbSmrg    else
389b66d1acbSmrg
3901c145c9fSmrg      # mkdir does not conform to POSIX,
391b66d1acbSmrg      # or it failed possibly due to a race condition.  Create the
392b66d1acbSmrg      # directory the slow way, step by step, checking for races as we go.
393b66d1acbSmrg
394b66d1acbSmrg      case $dstdir in
3954929c8f8Smrg        /*) prefix='/';;
3964929c8f8Smrg        [-=\(\)!]*) prefix='./';;
3974929c8f8Smrg        *)  prefix='';;
398b66d1acbSmrg      esac
399b66d1acbSmrg
400b66d1acbSmrg      oIFS=$IFS
401b66d1acbSmrg      IFS=/
4024929c8f8Smrg      set -f
403b66d1acbSmrg      set fnord $dstdir
404b66d1acbSmrg      shift
4054929c8f8Smrg      set +f
406b66d1acbSmrg      IFS=$oIFS
407b66d1acbSmrg
408b66d1acbSmrg      prefixes=
409b66d1acbSmrg
410b66d1acbSmrg      for d
411b66d1acbSmrg      do
4124929c8f8Smrg        test X"$d" = X && continue
4134929c8f8Smrg
4144929c8f8Smrg        prefix=$prefix$d
4154929c8f8Smrg        if test -d "$prefix"; then
4164929c8f8Smrg          prefixes=
4174929c8f8Smrg        else
4184929c8f8Smrg          if $posix_mkdir; then
4191c145c9fSmrg            (umask $mkdir_umask &&
4204929c8f8Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4214929c8f8Smrg            # Don't fail if two instances are running concurrently.
4224929c8f8Smrg            test -d "$prefix" || exit 1
4234929c8f8Smrg          else
4244929c8f8Smrg            case $prefix in
4254929c8f8Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4264929c8f8Smrg              *) qprefix=$prefix;;
4274929c8f8Smrg            esac
4284929c8f8Smrg            prefixes="$prefixes '$qprefix'"
4294929c8f8Smrg          fi
4304929c8f8Smrg        fi
4314929c8f8Smrg        prefix=$prefix/
432b66d1acbSmrg      done
433b66d1acbSmrg
434b66d1acbSmrg      if test -n "$prefixes"; then
4354929c8f8Smrg        # Don't fail if two instances are running concurrently.
4364929c8f8Smrg        (umask $mkdir_umask &&
4374929c8f8Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4384929c8f8Smrg          test -d "$dstdir" || exit 1
4394929c8f8Smrg        obsolete_mkdir_used=true
440b66d1acbSmrg      fi
441b66d1acbSmrg    fi
442b66d1acbSmrg  fi
443b66d1acbSmrg
444b66d1acbSmrg  if test -n "$dir_arg"; then
445b66d1acbSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
446b66d1acbSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
447b66d1acbSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
448b66d1acbSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449b66d1acbSmrg  else
450b66d1acbSmrg
451b66d1acbSmrg    # Make a couple of temp file names in the proper directory.
4521c145c9fSmrg    dsttmp=${dstdirslash}_inst.$$_
4531c145c9fSmrg    rmtmp=${dstdirslash}_rm.$$_
454b66d1acbSmrg
455b66d1acbSmrg    # Trap to clean up those temp files at exit.
456b66d1acbSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457b66d1acbSmrg
458b66d1acbSmrg    # Copy the file name to the temp name.
4591c145c9fSmrg    (umask $cp_umask &&
4601c145c9fSmrg     { test -z "$stripcmd" || {
4611c145c9fSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4621c145c9fSmrg	 # which would cause strip to fail.
4631c145c9fSmrg	 if test -z "$doit"; then
4641c145c9fSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4651c145c9fSmrg	 else
4661c145c9fSmrg	   $doit touch "$dsttmp"
4671c145c9fSmrg	 fi
4681c145c9fSmrg       }
4691c145c9fSmrg     } &&
4701c145c9fSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471b66d1acbSmrg
472b66d1acbSmrg    # and set any options; do chmod last to preserve setuid bits.
473b66d1acbSmrg    #
474b66d1acbSmrg    # If any of these fail, we abort the whole thing.  If we want to
475b66d1acbSmrg    # ignore errors from any of these, just make sure not to ignore
476b66d1acbSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477b66d1acbSmrg    #
478b66d1acbSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
479b66d1acbSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
480b66d1acbSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
481b66d1acbSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
482b66d1acbSmrg
483b66d1acbSmrg    # If -C, don't bother to copy if it wouldn't change the file.
484b66d1acbSmrg    if $copy_on_change &&
4854929c8f8Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4864929c8f8Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4874929c8f8Smrg       set -f &&
488b66d1acbSmrg       set X $old && old=:$2:$4:$5:$6 &&
489b66d1acbSmrg       set X $new && new=:$2:$4:$5:$6 &&
4904929c8f8Smrg       set +f &&
491b66d1acbSmrg       test "$old" = "$new" &&
492b66d1acbSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
493b66d1acbSmrg    then
494b66d1acbSmrg      rm -f "$dsttmp"
495b66d1acbSmrg    else
4961c145c9fSmrg      # If $backupsuffix is set, and the file being installed
4971c145c9fSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4981c145c9fSmrg      # e.g., if mv doesn't support -f.
4991c145c9fSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5001c145c9fSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5011c145c9fSmrg      fi
5021c145c9fSmrg
503b66d1acbSmrg      # Rename the file to the real destination.
504b66d1acbSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
505b66d1acbSmrg
506b66d1acbSmrg      # The rename failed, perhaps because mv can't rename something else
507b66d1acbSmrg      # to itself, or perhaps because mv is so ancient that it does not
508b66d1acbSmrg      # support -f.
509b66d1acbSmrg      {
5104929c8f8Smrg        # Now remove or move aside any old file at destination location.
5114929c8f8Smrg        # We try this two ways since rm can't unlink itself on some
5124929c8f8Smrg        # systems and the destination file might be busy for other
5134929c8f8Smrg        # reasons.  In this case, the final cleanup might fail but the new
5144929c8f8Smrg        # file should still install successfully.
5154929c8f8Smrg        {
5164929c8f8Smrg          test ! -f "$dst" ||
5171c145c9fSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
5184929c8f8Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5191c145c9fSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5204929c8f8Smrg          } ||
5214929c8f8Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5224929c8f8Smrg            (exit 1); exit 1
5234929c8f8Smrg          }
5244929c8f8Smrg        } &&
5254929c8f8Smrg
5264929c8f8Smrg        # Now rename the file to the real destination.
5274929c8f8Smrg        $doit $mvcmd "$dsttmp" "$dst"
528b66d1acbSmrg      }
529b66d1acbSmrg    fi || exit 1
530b66d1acbSmrg
531b66d1acbSmrg    trap '' 0
532b66d1acbSmrg  fi
533b66d1acbSmrgdone
5344fb28925Smrg
535b66d1acbSmrg# Local variables:
5361c145c9fSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
537b66d1acbSmrg# time-stamp-start: "scriptversion="
538b66d1acbSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5391c145c9fSmrg# time-stamp-time-zone: "UTC0"
540b66d1acbSmrg# time-stamp-end: "; # UTC"
541b66d1acbSmrg# End:
542