install-sh revision f403ce92
13294c433Smrg#!/bin/sh
23294c433Smrg# install - install a program, script, or datafile
39bc46bd4Smrg
4f403ce92Smrgscriptversion=2020-11-14.01; # UTC
59bc46bd4Smrg
63294c433Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
73294c433Smrg# later released in X11R6 (xc/config/util/install.sh) with the
83294c433Smrg# following copyright and license.
93294c433Smrg#
103294c433Smrg# Copyright (C) 1994 X Consortium
113294c433Smrg#
123294c433Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
133294c433Smrg# of this software and associated documentation files (the "Software"), to
143294c433Smrg# deal in the Software without restriction, including without limitation the
153294c433Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
163294c433Smrg# sell copies of the Software, and to permit persons to whom the Software is
173294c433Smrg# furnished to do so, subject to the following conditions:
183294c433Smrg#
193294c433Smrg# The above copyright notice and this permission notice shall be included in
203294c433Smrg# all copies or substantial portions of the Software.
213294c433Smrg#
223294c433Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
233294c433Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
243294c433Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
253294c433Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
263294c433Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
273294c433Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
283294c433Smrg#
293294c433Smrg# Except as contained in this notice, the name of the X Consortium shall not
303294c433Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
313294c433Smrg# ings in this Software without prior written authorization from the X Consor-
323294c433Smrg# tium.
333294c433Smrg#
343294c433Smrg#
353294c433Smrg# FSF changes to this file are in the public domain.
363294c433Smrg#
373294c433Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38f403ce92Smrg# 'make' implicit rules from creating a file called install from it
393294c433Smrg# when there is no Makefile.
403294c433Smrg#
413294c433Smrg# This script is compatible with the BSD install script, but was written
429bc46bd4Smrg# from scratch.
433294c433Smrg
44f403ce92Smrgtab='	'
459bc46bd4Smrgnl='
469bc46bd4Smrg'
47f403ce92SmrgIFS=" $tab$nl"
483294c433Smrg
49f403ce92Smrg# Set DOITPROG to "echo" to test this script.
503294c433Smrg
519bc46bd4Smrgdoit=${DOITPROG-}
52f403ce92Smrgdoit_exec=${doit:-exec}
533294c433Smrg
549bc46bd4Smrg# Put in absolute file names if you don't have them in your path;
559bc46bd4Smrg# or use environment vars.
569bc46bd4Smrg
579bc46bd4Smrgchgrpprog=${CHGRPPROG-chgrp}
589bc46bd4Smrgchmodprog=${CHMODPROG-chmod}
599bc46bd4Smrgchownprog=${CHOWNPROG-chown}
609bc46bd4Smrgcmpprog=${CMPPROG-cmp}
619bc46bd4Smrgcpprog=${CPPROG-cp}
629bc46bd4Smrgmkdirprog=${MKDIRPROG-mkdir}
639bc46bd4Smrgmvprog=${MVPROG-mv}
649bc46bd4Smrgrmprog=${RMPROG-rm}
659bc46bd4Smrgstripprog=${STRIPPROG-strip}
669bc46bd4Smrg
679bc46bd4Smrgposix_mkdir=
689bc46bd4Smrg
699bc46bd4Smrg# Desired mode of installed file.
709bc46bd4Smrgmode=0755
719bc46bd4Smrg
72f403ce92Smrg# Create dirs (including intermediate dirs) using mode 755.
73f403ce92Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74f403ce92Smrgmkdir_umask=22
75f403ce92Smrg
76f403ce92Smrgbackupsuffix=
779bc46bd4Smrgchgrpcmd=
789bc46bd4Smrgchmodcmd=$chmodprog
799bc46bd4Smrgchowncmd=
809bc46bd4Smrgmvcmd=$mvprog
819bc46bd4Smrgrmcmd="$rmprog -f"
829bc46bd4Smrgstripcmd=
833294c433Smrg
849bc46bd4Smrgsrc=
859bc46bd4Smrgdst=
869bc46bd4Smrgdir_arg=
879bc46bd4Smrgdst_arg=
883294c433Smrg
899bc46bd4Smrgcopy_on_change=false
90f403ce92Smrgis_target_a_directory=possibly
913294c433Smrg
929bc46bd4Smrgusage="\
939bc46bd4SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
949bc46bd4Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
959bc46bd4Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
969bc46bd4Smrg   or: $0 [OPTION]... -d DIRECTORIES...
973294c433Smrg
989bc46bd4SmrgIn the 1st form, copy SRCFILE to DSTFILE.
999bc46bd4SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1009bc46bd4SmrgIn the 4th, create DIRECTORIES.
1013294c433Smrg
1029bc46bd4SmrgOptions:
1039bc46bd4Smrg     --help     display this help and exit.
1049bc46bd4Smrg     --version  display version info and exit.
1053294c433Smrg
1069bc46bd4Smrg  -c            (ignored)
107f403ce92Smrg  -C            install only if different (preserve data modification time)
1089bc46bd4Smrg  -d            create directories instead of installing files.
1099bc46bd4Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1109bc46bd4Smrg  -m MODE       $chmodprog installed files to MODE.
1119bc46bd4Smrg  -o USER       $chownprog installed files to USER.
112f403ce92Smrg  -p            pass -p to $cpprog.
1139bc46bd4Smrg  -s            $stripprog installed files.
114f403ce92Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1159bc46bd4Smrg  -t DIRECTORY  install into DIRECTORY.
1169bc46bd4Smrg  -T            report an error if DSTFILE is a directory.
1173294c433Smrg
1189bc46bd4SmrgEnvironment variables override the default commands:
1199bc46bd4Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1209bc46bd4Smrg  RMPROG STRIPPROG
121f403ce92Smrg
122f403ce92SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123f403ce92Smrgit's up to you to specify -f if you want it.
124f403ce92Smrg
125f403ce92SmrgIf -S is not specified, no backups are attempted.
126f403ce92Smrg
127f403ce92SmrgEmail bug reports to bug-automake@gnu.org.
128f403ce92SmrgAutomake home page: https://www.gnu.org/software/automake/
1299bc46bd4Smrg"
1303294c433Smrg
1319bc46bd4Smrgwhile test $# -ne 0; do
1329bc46bd4Smrg  case $1 in
1339bc46bd4Smrg    -c) ;;
1343294c433Smrg
1359bc46bd4Smrg    -C) copy_on_change=true;;
1363294c433Smrg
1379bc46bd4Smrg    -d) dir_arg=true;;
1383294c433Smrg
1399bc46bd4Smrg    -g) chgrpcmd="$chgrpprog $2"
140f403ce92Smrg        shift;;
1413294c433Smrg
1429bc46bd4Smrg    --help) echo "$usage"; exit $?;;
1433294c433Smrg
1449bc46bd4Smrg    -m) mode=$2
145f403ce92Smrg        case $mode in
146f403ce92Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
147f403ce92Smrg            echo "$0: invalid mode: $mode" >&2
148f403ce92Smrg            exit 1;;
149f403ce92Smrg        esac
150f403ce92Smrg        shift;;
1513294c433Smrg
1529bc46bd4Smrg    -o) chowncmd="$chownprog $2"
153f403ce92Smrg        shift;;
154f403ce92Smrg
155f403ce92Smrg    -p) cpprog="$cpprog -p";;
1563294c433Smrg
1579bc46bd4Smrg    -s) stripcmd=$stripprog;;
1583294c433Smrg
159f403ce92Smrg    -S) backupsuffix="$2"
160f403ce92Smrg        shift;;
161f403ce92Smrg
162f403ce92Smrg    -t)
163f403ce92Smrg        is_target_a_directory=always
164f403ce92Smrg        dst_arg=$2
165f403ce92Smrg        # Protect names problematic for 'test' and other utilities.
166f403ce92Smrg        case $dst_arg in
167f403ce92Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
168f403ce92Smrg        esac
169f403ce92Smrg        shift;;
1703294c433Smrg
171f403ce92Smrg    -T) is_target_a_directory=never;;
1723294c433Smrg
1739bc46bd4Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1743294c433Smrg
175f403ce92Smrg    --) shift
176f403ce92Smrg        break;;
1773294c433Smrg
178f403ce92Smrg    -*) echo "$0: invalid option: $1" >&2
179f403ce92Smrg        exit 1;;
1803294c433Smrg
1819bc46bd4Smrg    *)  break;;
1829bc46bd4Smrg  esac
1839bc46bd4Smrg  shift
1849bc46bd4Smrgdone
1853294c433Smrg
186f403ce92Smrg# We allow the use of options -d and -T together, by making -d
187f403ce92Smrg# take the precedence; this is for compatibility with GNU install.
188f403ce92Smrg
189f403ce92Smrgif test -n "$dir_arg"; then
190f403ce92Smrg  if test -n "$dst_arg"; then
191f403ce92Smrg    echo "$0: target directory not allowed when installing a directory." >&2
192f403ce92Smrg    exit 1
193f403ce92Smrg  fi
194f403ce92Smrgfi
195f403ce92Smrg
1969bc46bd4Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1979bc46bd4Smrg  # When -d is used, all remaining arguments are directories to create.
1989bc46bd4Smrg  # When -t is used, the destination is already specified.
1999bc46bd4Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2009bc46bd4Smrg  for arg
2019bc46bd4Smrg  do
2029bc46bd4Smrg    if test -n "$dst_arg"; then
2039bc46bd4Smrg      # $@ is not empty: it contains at least $arg.
2049bc46bd4Smrg      set fnord "$@" "$dst_arg"
2059bc46bd4Smrg      shift # fnord
2069bc46bd4Smrg    fi
2079bc46bd4Smrg    shift # arg
2089bc46bd4Smrg    dst_arg=$arg
209f403ce92Smrg    # Protect names problematic for 'test' and other utilities.
210f403ce92Smrg    case $dst_arg in
211f403ce92Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212f403ce92Smrg    esac
2139bc46bd4Smrg  done
2149bc46bd4Smrgfi
2153294c433Smrg
2169bc46bd4Smrgif test $# -eq 0; then
2179bc46bd4Smrg  if test -z "$dir_arg"; then
2189bc46bd4Smrg    echo "$0: no input file specified." >&2
2199bc46bd4Smrg    exit 1
2209bc46bd4Smrg  fi
221f403ce92Smrg  # It's OK to call 'install-sh -d' without argument.
2229bc46bd4Smrg  # This can happen when creating conditional directories.
2239bc46bd4Smrg  exit 0
2249bc46bd4Smrgfi
2253294c433Smrg
2269bc46bd4Smrgif test -z "$dir_arg"; then
227f403ce92Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
228f403ce92Smrg    if test ! -d "$dst_arg"; then
229f403ce92Smrg      echo "$0: $dst_arg: Is not a directory." >&2
230f403ce92Smrg      exit 1
231f403ce92Smrg    fi
232f403ce92Smrg  fi
233f403ce92Smrgfi
234f403ce92Smrg
235f403ce92Smrgif test -z "$dir_arg"; then
236f403ce92Smrg  do_exit='(exit $ret); exit $ret'
237f403ce92Smrg  trap "ret=129; $do_exit" 1
238f403ce92Smrg  trap "ret=130; $do_exit" 2
239f403ce92Smrg  trap "ret=141; $do_exit" 13
240f403ce92Smrg  trap "ret=143; $do_exit" 15
2419bc46bd4Smrg
2429bc46bd4Smrg  # Set umask so as not to create temps with too-generous modes.
2439bc46bd4Smrg  # However, 'strip' requires both read and write access to temps.
2449bc46bd4Smrg  case $mode in
2459bc46bd4Smrg    # Optimize common cases.
2469bc46bd4Smrg    *644) cp_umask=133;;
2479bc46bd4Smrg    *755) cp_umask=22;;
2489bc46bd4Smrg
2499bc46bd4Smrg    *[0-7])
2509bc46bd4Smrg      if test -z "$stripcmd"; then
251f403ce92Smrg        u_plus_rw=
2529bc46bd4Smrg      else
253f403ce92Smrg        u_plus_rw='% 200'
2549bc46bd4Smrg      fi
2559bc46bd4Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2569bc46bd4Smrg    *)
2579bc46bd4Smrg      if test -z "$stripcmd"; then
258f403ce92Smrg        u_plus_rw=
2599bc46bd4Smrg      else
260f403ce92Smrg        u_plus_rw=,u+rw
2619bc46bd4Smrg      fi
2629bc46bd4Smrg      cp_umask=$mode$u_plus_rw;;
2639bc46bd4Smrg  esac
2649bc46bd4Smrgfi
2653294c433Smrg
2669bc46bd4Smrgfor src
2679bc46bd4Smrgdo
268f403ce92Smrg  # Protect names problematic for 'test' and other utilities.
2699bc46bd4Smrg  case $src in
270f403ce92Smrg    -* | [=\(\)!]) src=./$src;;
2719bc46bd4Smrg  esac
2729bc46bd4Smrg
2739bc46bd4Smrg  if test -n "$dir_arg"; then
2749bc46bd4Smrg    dst=$src
2759bc46bd4Smrg    dstdir=$dst
2769bc46bd4Smrg    test -d "$dstdir"
2779bc46bd4Smrg    dstdir_status=$?
278f403ce92Smrg    # Don't chown directories that already exist.
279f403ce92Smrg    if test $dstdir_status = 0; then
280f403ce92Smrg      chowncmd=""
281f403ce92Smrg    fi
2829bc46bd4Smrg  else
2839bc46bd4Smrg
2849bc46bd4Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2859bc46bd4Smrg    # might cause directories to be created, which would be especially bad
2869bc46bd4Smrg    # if $src (and thus $dsttmp) contains '*'.
2879bc46bd4Smrg    if test ! -f "$src" && test ! -d "$src"; then
2889bc46bd4Smrg      echo "$0: $src does not exist." >&2
2899bc46bd4Smrg      exit 1
2909bc46bd4Smrg    fi
2919bc46bd4Smrg
2929bc46bd4Smrg    if test -z "$dst_arg"; then
2939bc46bd4Smrg      echo "$0: no destination specified." >&2
2949bc46bd4Smrg      exit 1
2959bc46bd4Smrg    fi
2969bc46bd4Smrg    dst=$dst_arg
2973294c433Smrg
298f403ce92Smrg    # If destination is a directory, append the input filename.
2999bc46bd4Smrg    if test -d "$dst"; then
300f403ce92Smrg      if test "$is_target_a_directory" = never; then
301f403ce92Smrg        echo "$0: $dst_arg: Is a directory" >&2
302f403ce92Smrg        exit 1
3039bc46bd4Smrg      fi
3049bc46bd4Smrg      dstdir=$dst
305f403ce92Smrg      dstbase=`basename "$src"`
306f403ce92Smrg      case $dst in
307f403ce92Smrg	*/) dst=$dst$dstbase;;
308f403ce92Smrg	*)  dst=$dst/$dstbase;;
309f403ce92Smrg      esac
3109bc46bd4Smrg      dstdir_status=0
3119bc46bd4Smrg    else
312f403ce92Smrg      dstdir=`dirname "$dst"`
3139bc46bd4Smrg      test -d "$dstdir"
3149bc46bd4Smrg      dstdir_status=$?
3159bc46bd4Smrg    fi
3169bc46bd4Smrg  fi
3179bc46bd4Smrg
318f403ce92Smrg  case $dstdir in
319f403ce92Smrg    */) dstdirslash=$dstdir;;
320f403ce92Smrg    *)  dstdirslash=$dstdir/;;
321f403ce92Smrg  esac
322f403ce92Smrg
3239bc46bd4Smrg  obsolete_mkdir_used=false
3249bc46bd4Smrg
3259bc46bd4Smrg  if test $dstdir_status != 0; then
3269bc46bd4Smrg    case $posix_mkdir in
3279bc46bd4Smrg      '')
328f403ce92Smrg        # With -d, create the new directory with the user-specified mode.
329f403ce92Smrg        # Otherwise, rely on $mkdir_umask.
330f403ce92Smrg        if test -n "$dir_arg"; then
331f403ce92Smrg          mkdir_mode=-m$mode
332f403ce92Smrg        else
333f403ce92Smrg          mkdir_mode=
334f403ce92Smrg        fi
335f403ce92Smrg
336f403ce92Smrg        posix_mkdir=false
337f403ce92Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338f403ce92Smrg	# here however when possible just to lower collision chance.
339f403ce92Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340f403ce92Smrg
341f403ce92Smrg	trap '
342f403ce92Smrg	  ret=$?
343f403ce92Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344f403ce92Smrg	  exit $ret
345f403ce92Smrg	' 0
346f403ce92Smrg
347f403ce92Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348f403ce92Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349f403ce92Smrg	# directory is successfully created first before we actually test
350f403ce92Smrg	# 'mkdir -p'.
351f403ce92Smrg	if (umask $mkdir_umask &&
352f403ce92Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353f403ce92Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354f403ce92Smrg	then
355f403ce92Smrg	  if test -z "$dir_arg" || {
356f403ce92Smrg	       # Check for POSIX incompatibilities with -m.
357f403ce92Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358f403ce92Smrg	       # other-writable bit of parent directory when it shouldn't.
359f403ce92Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360f403ce92Smrg	       test_tmpdir="$tmpdir/a"
361f403ce92Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362f403ce92Smrg	       case $ls_ld_tmpdir in
363f403ce92Smrg		 d????-?r-*) different_mode=700;;
364f403ce92Smrg		 d????-?--*) different_mode=755;;
365f403ce92Smrg		 *) false;;
366f403ce92Smrg	       esac &&
367f403ce92Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368f403ce92Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369f403ce92Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370f403ce92Smrg	       }
371f403ce92Smrg	     }
372f403ce92Smrg	  then posix_mkdir=:
373f403ce92Smrg	  fi
374f403ce92Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3753294c433Smrg	else
376f403ce92Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377f403ce92Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3783294c433Smrg	fi
379f403ce92Smrg	trap '' 0;;
3809bc46bd4Smrg    esac
3813294c433Smrg
3829bc46bd4Smrg    if
3839bc46bd4Smrg      $posix_mkdir && (
384f403ce92Smrg        umask $mkdir_umask &&
385f403ce92Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3869bc46bd4Smrg      )
3879bc46bd4Smrg    then :
3889bc46bd4Smrg    else
3899bc46bd4Smrg
390f403ce92Smrg      # mkdir does not conform to POSIX,
3919bc46bd4Smrg      # or it failed possibly due to a race condition.  Create the
3929bc46bd4Smrg      # directory the slow way, step by step, checking for races as we go.
3939bc46bd4Smrg
3949bc46bd4Smrg      case $dstdir in
395f403ce92Smrg        /*) prefix='/';;
396f403ce92Smrg        [-=\(\)!]*) prefix='./';;
397f403ce92Smrg        *)  prefix='';;
3989bc46bd4Smrg      esac
3999bc46bd4Smrg
4009bc46bd4Smrg      oIFS=$IFS
4019bc46bd4Smrg      IFS=/
402f403ce92Smrg      set -f
4039bc46bd4Smrg      set fnord $dstdir
4049bc46bd4Smrg      shift
405f403ce92Smrg      set +f
4069bc46bd4Smrg      IFS=$oIFS
4079bc46bd4Smrg
4089bc46bd4Smrg      prefixes=
4099bc46bd4Smrg
4109bc46bd4Smrg      for d
4119bc46bd4Smrg      do
412f403ce92Smrg        test X"$d" = X && continue
413f403ce92Smrg
414f403ce92Smrg        prefix=$prefix$d
415f403ce92Smrg        if test -d "$prefix"; then
416f403ce92Smrg          prefixes=
417f403ce92Smrg        else
418f403ce92Smrg          if $posix_mkdir; then
419f403ce92Smrg            (umask $mkdir_umask &&
420f403ce92Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
421f403ce92Smrg            # Don't fail if two instances are running concurrently.
422f403ce92Smrg            test -d "$prefix" || exit 1
423f403ce92Smrg          else
424f403ce92Smrg            case $prefix in
425f403ce92Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
426f403ce92Smrg              *) qprefix=$prefix;;
427f403ce92Smrg            esac
428f403ce92Smrg            prefixes="$prefixes '$qprefix'"
429f403ce92Smrg          fi
430f403ce92Smrg        fi
431f403ce92Smrg        prefix=$prefix/
4329bc46bd4Smrg      done
4339bc46bd4Smrg
4349bc46bd4Smrg      if test -n "$prefixes"; then
435f403ce92Smrg        # Don't fail if two instances are running concurrently.
436f403ce92Smrg        (umask $mkdir_umask &&
437f403ce92Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
438f403ce92Smrg          test -d "$dstdir" || exit 1
439f403ce92Smrg        obsolete_mkdir_used=true
4409bc46bd4Smrg      fi
4419bc46bd4Smrg    fi
4429bc46bd4Smrg  fi
4439bc46bd4Smrg
4449bc46bd4Smrg  if test -n "$dir_arg"; then
4459bc46bd4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4469bc46bd4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4479bc46bd4Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4489bc46bd4Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4499bc46bd4Smrg  else
4509bc46bd4Smrg
4519bc46bd4Smrg    # Make a couple of temp file names in the proper directory.
452f403ce92Smrg    dsttmp=${dstdirslash}_inst.$$_
453f403ce92Smrg    rmtmp=${dstdirslash}_rm.$$_
4549bc46bd4Smrg
4559bc46bd4Smrg    # Trap to clean up those temp files at exit.
4569bc46bd4Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4579bc46bd4Smrg
4589bc46bd4Smrg    # Copy the file name to the temp name.
459f403ce92Smrg    (umask $cp_umask &&
460f403ce92Smrg     { test -z "$stripcmd" || {
461f403ce92Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462f403ce92Smrg	 # which would cause strip to fail.
463f403ce92Smrg	 if test -z "$doit"; then
464f403ce92Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465f403ce92Smrg	 else
466f403ce92Smrg	   $doit touch "$dsttmp"
467f403ce92Smrg	 fi
468f403ce92Smrg       }
469f403ce92Smrg     } &&
470f403ce92Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4719bc46bd4Smrg
4729bc46bd4Smrg    # and set any options; do chmod last to preserve setuid bits.
4739bc46bd4Smrg    #
4749bc46bd4Smrg    # If any of these fail, we abort the whole thing.  If we want to
4759bc46bd4Smrg    # ignore errors from any of these, just make sure not to ignore
4769bc46bd4Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4779bc46bd4Smrg    #
4789bc46bd4Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4799bc46bd4Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4809bc46bd4Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4819bc46bd4Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4829bc46bd4Smrg
4839bc46bd4Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4849bc46bd4Smrg    if $copy_on_change &&
485f403ce92Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
486f403ce92Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
487f403ce92Smrg       set -f &&
4889bc46bd4Smrg       set X $old && old=:$2:$4:$5:$6 &&
4899bc46bd4Smrg       set X $new && new=:$2:$4:$5:$6 &&
490f403ce92Smrg       set +f &&
4919bc46bd4Smrg       test "$old" = "$new" &&
4929bc46bd4Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4939bc46bd4Smrg    then
4949bc46bd4Smrg      rm -f "$dsttmp"
4959bc46bd4Smrg    else
496f403ce92Smrg      # If $backupsuffix is set, and the file being installed
497f403ce92Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498f403ce92Smrg      # e.g., if mv doesn't support -f.
499f403ce92Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500f403ce92Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501f403ce92Smrg      fi
502f403ce92Smrg
5039bc46bd4Smrg      # Rename the file to the real destination.
5049bc46bd4Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5059bc46bd4Smrg
5069bc46bd4Smrg      # The rename failed, perhaps because mv can't rename something else
5079bc46bd4Smrg      # to itself, or perhaps because mv is so ancient that it does not
5089bc46bd4Smrg      # support -f.
5099bc46bd4Smrg      {
510f403ce92Smrg        # Now remove or move aside any old file at destination location.
511f403ce92Smrg        # We try this two ways since rm can't unlink itself on some
512f403ce92Smrg        # systems and the destination file might be busy for other
513f403ce92Smrg        # reasons.  In this case, the final cleanup might fail but the new
514f403ce92Smrg        # file should still install successfully.
515f403ce92Smrg        {
516f403ce92Smrg          test ! -f "$dst" ||
517f403ce92Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
518f403ce92Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519f403ce92Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
520f403ce92Smrg          } ||
521f403ce92Smrg          { echo "$0: cannot unlink or rename $dst" >&2
522f403ce92Smrg            (exit 1); exit 1
523f403ce92Smrg          }
524f403ce92Smrg        } &&
525f403ce92Smrg
526f403ce92Smrg        # Now rename the file to the real destination.
527f403ce92Smrg        $doit $mvcmd "$dsttmp" "$dst"
5289bc46bd4Smrg      }
5299bc46bd4Smrg    fi || exit 1
5309bc46bd4Smrg
5319bc46bd4Smrg    trap '' 0
5329bc46bd4Smrg  fi
5339bc46bd4Smrgdone
5343294c433Smrg
5359bc46bd4Smrg# Local variables:
536f403ce92Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5379bc46bd4Smrg# time-stamp-start: "scriptversion="
5389bc46bd4Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539f403ce92Smrg# time-stamp-time-zone: "UTC0"
5409bc46bd4Smrg# time-stamp-end: "; # UTC"
5419bc46bd4Smrg# End:
542