1e6232409Smrg#!/bin/sh
2e6232409Smrg# install - install a program, script, or datafile
3e6232409Smrg
46d1874b2Smrgscriptversion=2020-11-14.01; # UTC
5e6232409Smrg
6e6232409Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7e6232409Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8e6232409Smrg# following copyright and license.
9e6232409Smrg#
10e6232409Smrg# Copyright (C) 1994 X Consortium
11e6232409Smrg#
12e6232409Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13e6232409Smrg# of this software and associated documentation files (the "Software"), to
14e6232409Smrg# deal in the Software without restriction, including without limitation the
15e6232409Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16e6232409Smrg# sell copies of the Software, and to permit persons to whom the Software is
17e6232409Smrg# furnished to do so, subject to the following conditions:
18e6232409Smrg#
19e6232409Smrg# The above copyright notice and this permission notice shall be included in
20e6232409Smrg# all copies or substantial portions of the Software.
21e6232409Smrg#
22e6232409Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23e6232409Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24e6232409Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25e6232409Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26e6232409Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27e6232409Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28e6232409Smrg#
29e6232409Smrg# Except as contained in this notice, the name of the X Consortium shall not
30e6232409Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31e6232409Smrg# ings in this Software without prior written authorization from the X Consor-
32e6232409Smrg# tium.
33e6232409Smrg#
34e6232409Smrg#
35e6232409Smrg# FSF changes to this file are in the public domain.
36e6232409Smrg#
37e6232409Smrg# Calling this script install-sh is preferred over install.sh, to prevent
386a45684fSmrg# 'make' implicit rules from creating a file called install from it
39e6232409Smrg# when there is no Makefile.
40e6232409Smrg#
41e6232409Smrg# This script is compatible with the BSD install script, but was written
428fff3f40Smrg# from scratch.
438fff3f40Smrg
446a45684fSmrgtab='	'
458fff3f40Smrgnl='
468fff3f40Smrg'
476a45684fSmrgIFS=" $tab$nl"
48e6232409Smrg
496a45684fSmrg# Set DOITPROG to "echo" to test this script.
50e6232409Smrg
518fff3f40Smrgdoit=${DOITPROG-}
526a45684fSmrgdoit_exec=${doit:-exec}
53e6232409Smrg
548fff3f40Smrg# Put in absolute file names if you don't have them in your path;
558fff3f40Smrg# or use environment vars.
568fff3f40Smrg
578fff3f40Smrgchgrpprog=${CHGRPPROG-chgrp}
588fff3f40Smrgchmodprog=${CHMODPROG-chmod}
598fff3f40Smrgchownprog=${CHOWNPROG-chown}
608fff3f40Smrgcmpprog=${CMPPROG-cmp}
618fff3f40Smrgcpprog=${CPPROG-cp}
628fff3f40Smrgmkdirprog=${MKDIRPROG-mkdir}
638fff3f40Smrgmvprog=${MVPROG-mv}
648fff3f40Smrgrmprog=${RMPROG-rm}
658fff3f40Smrgstripprog=${STRIPPROG-strip}
668fff3f40Smrg
678fff3f40Smrgposix_mkdir=
688fff3f40Smrg
698fff3f40Smrg# Desired mode of installed file.
708fff3f40Smrgmode=0755
71e6232409Smrg
726d1874b2Smrg# Create dirs (including intermediate dirs) using mode 755.
736d1874b2Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
746d1874b2Smrgmkdir_umask=22
756d1874b2Smrg
766d1874b2Smrgbackupsuffix=
77e6232409Smrgchgrpcmd=
788fff3f40Smrgchmodcmd=$chmodprog
798fff3f40Smrgchowncmd=
808fff3f40Smrgmvcmd=$mvprog
81e6232409Smrgrmcmd="$rmprog -f"
828fff3f40Smrgstripcmd=
838fff3f40Smrg
84e6232409Smrgsrc=
85e6232409Smrgdst=
86e6232409Smrgdir_arg=
878fff3f40Smrgdst_arg=
888fff3f40Smrg
898fff3f40Smrgcopy_on_change=false
906a45684fSmrgis_target_a_directory=possibly
91e6232409Smrg
928fff3f40Smrgusage="\
938fff3f40SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94e6232409Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95e6232409Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96e6232409Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97e6232409Smrg
98e6232409SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99e6232409SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100e6232409SmrgIn the 4th, create DIRECTORIES.
101e6232409Smrg
102e6232409SmrgOptions:
1038fff3f40Smrg     --help     display this help and exit.
1048fff3f40Smrg     --version  display version info and exit.
1058fff3f40Smrg
1068fff3f40Smrg  -c            (ignored)
1076d1874b2Smrg  -C            install only if different (preserve data modification time)
1088fff3f40Smrg  -d            create directories instead of installing files.
1098fff3f40Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1108fff3f40Smrg  -m MODE       $chmodprog installed files to MODE.
1118fff3f40Smrg  -o USER       $chownprog installed files to USER.
1126d1874b2Smrg  -p            pass -p to $cpprog.
1138fff3f40Smrg  -s            $stripprog installed files.
1146d1874b2Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1158fff3f40Smrg  -t DIRECTORY  install into DIRECTORY.
1168fff3f40Smrg  -T            report an error if DSTFILE is a directory.
117e6232409Smrg
118e6232409SmrgEnvironment variables override the default commands:
1198fff3f40Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1208fff3f40Smrg  RMPROG STRIPPROG
1216d1874b2Smrg
1226d1874b2SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1236d1874b2Smrgit's up to you to specify -f if you want it.
1246d1874b2Smrg
1256d1874b2SmrgIf -S is not specified, no backups are attempted.
1266d1874b2Smrg
1276d1874b2SmrgEmail bug reports to bug-automake@gnu.org.
1286d1874b2SmrgAutomake home page: https://www.gnu.org/software/automake/
129e6232409Smrg"
130e6232409Smrg
1318fff3f40Smrgwhile test $# -ne 0; do
132e6232409Smrg  case $1 in
1338fff3f40Smrg    -c) ;;
1348fff3f40Smrg
1358fff3f40Smrg    -C) copy_on_change=true;;
136e6232409Smrg
1378fff3f40Smrg    -d) dir_arg=true;;
138e6232409Smrg
139e6232409Smrg    -g) chgrpcmd="$chgrpprog $2"
1406a45684fSmrg        shift;;
141e6232409Smrg
142e6232409Smrg    --help) echo "$usage"; exit $?;;
143e6232409Smrg
1448fff3f40Smrg    -m) mode=$2
1456a45684fSmrg        case $mode in
1466a45684fSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1476a45684fSmrg            echo "$0: invalid mode: $mode" >&2
1486a45684fSmrg            exit 1;;
1496a45684fSmrg        esac
1506a45684fSmrg        shift;;
151e6232409Smrg
152e6232409Smrg    -o) chowncmd="$chownprog $2"
1536a45684fSmrg        shift;;
154e6232409Smrg
1556d1874b2Smrg    -p) cpprog="$cpprog -p";;
1566d1874b2Smrg
1578fff3f40Smrg    -s) stripcmd=$stripprog;;
158e6232409Smrg
1596d1874b2Smrg    -S) backupsuffix="$2"
1606d1874b2Smrg        shift;;
1616d1874b2Smrg
1626a45684fSmrg    -t)
1636a45684fSmrg        is_target_a_directory=always
1646a45684fSmrg        dst_arg=$2
1656a45684fSmrg        # Protect names problematic for 'test' and other utilities.
1666a45684fSmrg        case $dst_arg in
1676a45684fSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1686a45684fSmrg        esac
1696a45684fSmrg        shift;;
170e6232409Smrg
1716a45684fSmrg    -T) is_target_a_directory=never;;
172e6232409Smrg
173e6232409Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174e6232409Smrg
1756a45684fSmrg    --) shift
1766a45684fSmrg        break;;
1778fff3f40Smrg
1786a45684fSmrg    -*) echo "$0: invalid option: $1" >&2
1796a45684fSmrg        exit 1;;
1808fff3f40Smrg
1818fff3f40Smrg    *)  break;;
182e6232409Smrg  esac
1838fff3f40Smrg  shift
184e6232409Smrgdone
185e6232409Smrg
1866a45684fSmrg# We allow the use of options -d and -T together, by making -d
1876a45684fSmrg# take the precedence; this is for compatibility with GNU install.
1886a45684fSmrg
1896a45684fSmrgif test -n "$dir_arg"; then
1906a45684fSmrg  if test -n "$dst_arg"; then
1916a45684fSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1926a45684fSmrg    exit 1
1936a45684fSmrg  fi
1946a45684fSmrgfi
1956a45684fSmrg
1968fff3f40Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1978fff3f40Smrg  # When -d is used, all remaining arguments are directories to create.
1988fff3f40Smrg  # When -t is used, the destination is already specified.
1998fff3f40Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2008fff3f40Smrg  for arg
2018fff3f40Smrg  do
2028fff3f40Smrg    if test -n "$dst_arg"; then
2038fff3f40Smrg      # $@ is not empty: it contains at least $arg.
2048fff3f40Smrg      set fnord "$@" "$dst_arg"
2058fff3f40Smrg      shift # fnord
2068fff3f40Smrg    fi
2078fff3f40Smrg    shift # arg
2088fff3f40Smrg    dst_arg=$arg
2096a45684fSmrg    # Protect names problematic for 'test' and other utilities.
21094a251fdSmrg    case $dst_arg in
21194a251fdSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21294a251fdSmrg    esac
2138fff3f40Smrg  done
2148fff3f40Smrgfi
2158fff3f40Smrg
2168fff3f40Smrgif test $# -eq 0; then
217e6232409Smrg  if test -z "$dir_arg"; then
218e6232409Smrg    echo "$0: no input file specified." >&2
219e6232409Smrg    exit 1
220e6232409Smrg  fi
2216a45684fSmrg  # It's OK to call 'install-sh -d' without argument.
222e6232409Smrg  # This can happen when creating conditional directories.
223e6232409Smrg  exit 0
224e6232409Smrgfi
225e6232409Smrg
2266a45684fSmrgif test -z "$dir_arg"; then
2276a45684fSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2286a45684fSmrg    if test ! -d "$dst_arg"; then
2296a45684fSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2306a45684fSmrg      exit 1
2316a45684fSmrg    fi
2326a45684fSmrg  fi
2336a45684fSmrgfi
2346a45684fSmrg
2358fff3f40Smrgif test -z "$dir_arg"; then
23694a251fdSmrg  do_exit='(exit $ret); exit $ret'
23794a251fdSmrg  trap "ret=129; $do_exit" 1
23894a251fdSmrg  trap "ret=130; $do_exit" 2
23994a251fdSmrg  trap "ret=141; $do_exit" 13
24094a251fdSmrg  trap "ret=143; $do_exit" 15
2418fff3f40Smrg
2428fff3f40Smrg  # Set umask so as not to create temps with too-generous modes.
2438fff3f40Smrg  # However, 'strip' requires both read and write access to temps.
2448fff3f40Smrg  case $mode in
2458fff3f40Smrg    # Optimize common cases.
2468fff3f40Smrg    *644) cp_umask=133;;
2478fff3f40Smrg    *755) cp_umask=22;;
2488fff3f40Smrg
2498fff3f40Smrg    *[0-7])
2508fff3f40Smrg      if test -z "$stripcmd"; then
2516a45684fSmrg        u_plus_rw=
2528fff3f40Smrg      else
2536a45684fSmrg        u_plus_rw='% 200'
2548fff3f40Smrg      fi
2558fff3f40Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2568fff3f40Smrg    *)
2578fff3f40Smrg      if test -z "$stripcmd"; then
2586a45684fSmrg        u_plus_rw=
2598fff3f40Smrg      else
2606a45684fSmrg        u_plus_rw=,u+rw
2618fff3f40Smrg      fi
2628fff3f40Smrg      cp_umask=$mode$u_plus_rw;;
2638fff3f40Smrg  esac
2648fff3f40Smrgfi
2658fff3f40Smrg
266e6232409Smrgfor src
267e6232409Smrgdo
2686a45684fSmrg  # Protect names problematic for 'test' and other utilities.
269e6232409Smrg  case $src in
27094a251fdSmrg    -* | [=\(\)!]) src=./$src;;
271e6232409Smrg  esac
272e6232409Smrg
273e6232409Smrg  if test -n "$dir_arg"; then
274e6232409Smrg    dst=$src
2758fff3f40Smrg    dstdir=$dst
2768fff3f40Smrg    test -d "$dstdir"
2778fff3f40Smrg    dstdir_status=$?
2786d1874b2Smrg    # Don't chown directories that already exist.
2796d1874b2Smrg    if test $dstdir_status = 0; then
2806d1874b2Smrg      chowncmd=""
2816d1874b2Smrg    fi
282e6232409Smrg  else
2838fff3f40Smrg
284e6232409Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285e6232409Smrg    # might cause directories to be created, which would be especially bad
286e6232409Smrg    # if $src (and thus $dsttmp) contains '*'.
287e6232409Smrg    if test ! -f "$src" && test ! -d "$src"; then
288e6232409Smrg      echo "$0: $src does not exist." >&2
289e6232409Smrg      exit 1
290e6232409Smrg    fi
291e6232409Smrg
2928fff3f40Smrg    if test -z "$dst_arg"; then
293e6232409Smrg      echo "$0: no destination specified." >&2
294e6232409Smrg      exit 1
295e6232409Smrg    fi
2968fff3f40Smrg    dst=$dst_arg
297e6232409Smrg
2986d1874b2Smrg    # If destination is a directory, append the input filename.
299e6232409Smrg    if test -d "$dst"; then
3006a45684fSmrg      if test "$is_target_a_directory" = never; then
3016a45684fSmrg        echo "$0: $dst_arg: Is a directory" >&2
3026a45684fSmrg        exit 1
303e6232409Smrg      fi
3048fff3f40Smrg      dstdir=$dst
3056d1874b2Smrg      dstbase=`basename "$src"`
3066d1874b2Smrg      case $dst in
3076d1874b2Smrg	*/) dst=$dst$dstbase;;
3086d1874b2Smrg	*)  dst=$dst/$dstbase;;
3096d1874b2Smrg      esac
3108fff3f40Smrg      dstdir_status=0
3118fff3f40Smrg    else
3126a45684fSmrg      dstdir=`dirname "$dst"`
3138fff3f40Smrg      test -d "$dstdir"
3148fff3f40Smrg      dstdir_status=$?
315e6232409Smrg    fi
316e6232409Smrg  fi
317e6232409Smrg
3186d1874b2Smrg  case $dstdir in
3196d1874b2Smrg    */) dstdirslash=$dstdir;;
3206d1874b2Smrg    *)  dstdirslash=$dstdir/;;
3216d1874b2Smrg  esac
3226d1874b2Smrg
3238fff3f40Smrg  obsolete_mkdir_used=false
3248fff3f40Smrg
3258fff3f40Smrg  if test $dstdir_status != 0; then
3268fff3f40Smrg    case $posix_mkdir in
3278fff3f40Smrg      '')
3286a45684fSmrg        # With -d, create the new directory with the user-specified mode.
3296a45684fSmrg        # Otherwise, rely on $mkdir_umask.
3306a45684fSmrg        if test -n "$dir_arg"; then
3316a45684fSmrg          mkdir_mode=-m$mode
3326a45684fSmrg        else
3336a45684fSmrg          mkdir_mode=
3346a45684fSmrg        fi
3356a45684fSmrg
3366a45684fSmrg        posix_mkdir=false
3376d1874b2Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3386d1874b2Smrg	# here however when possible just to lower collision chance.
3396d1874b2Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3406d1874b2Smrg
3416d1874b2Smrg	trap '
3426d1874b2Smrg	  ret=$?
3436d1874b2Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3446d1874b2Smrg	  exit $ret
3456d1874b2Smrg	' 0
3466d1874b2Smrg
3476d1874b2Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
3486d1874b2Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3496d1874b2Smrg	# directory is successfully created first before we actually test
3506d1874b2Smrg	# 'mkdir -p'.
3516d1874b2Smrg	if (umask $mkdir_umask &&
3526d1874b2Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3536d1874b2Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3546d1874b2Smrg	then
3556d1874b2Smrg	  if test -z "$dir_arg" || {
3566d1874b2Smrg	       # Check for POSIX incompatibilities with -m.
3576d1874b2Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3586d1874b2Smrg	       # other-writable bit of parent directory when it shouldn't.
3596d1874b2Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3606d1874b2Smrg	       test_tmpdir="$tmpdir/a"
3616d1874b2Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3626d1874b2Smrg	       case $ls_ld_tmpdir in
3636d1874b2Smrg		 d????-?r-*) different_mode=700;;
3646d1874b2Smrg		 d????-?--*) different_mode=755;;
3656d1874b2Smrg		 *) false;;
3666d1874b2Smrg	       esac &&
3676d1874b2Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3686d1874b2Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3696d1874b2Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3706d1874b2Smrg	       }
3716d1874b2Smrg	     }
3726d1874b2Smrg	  then posix_mkdir=:
3736d1874b2Smrg	  fi
3746d1874b2Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3756d1874b2Smrg	else
3766d1874b2Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
3776d1874b2Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3786d1874b2Smrg	fi
3796d1874b2Smrg	trap '' 0;;
3808fff3f40Smrg    esac
381e6232409Smrg
3828fff3f40Smrg    if
3838fff3f40Smrg      $posix_mkdir && (
3846a45684fSmrg        umask $mkdir_umask &&
3856a45684fSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3868fff3f40Smrg      )
3878fff3f40Smrg    then :
3888fff3f40Smrg    else
389e6232409Smrg
3906d1874b2Smrg      # mkdir does not conform to POSIX,
3918fff3f40Smrg      # or it failed possibly due to a race condition.  Create the
3928fff3f40Smrg      # directory the slow way, step by step, checking for races as we go.
393e6232409Smrg
3948fff3f40Smrg      case $dstdir in
3956a45684fSmrg        /*) prefix='/';;
3966a45684fSmrg        [-=\(\)!]*) prefix='./';;
3976a45684fSmrg        *)  prefix='';;
3988fff3f40Smrg      esac
399e6232409Smrg
4008fff3f40Smrg      oIFS=$IFS
4018fff3f40Smrg      IFS=/
4026a45684fSmrg      set -f
4038fff3f40Smrg      set fnord $dstdir
404e6232409Smrg      shift
4056a45684fSmrg      set +f
4068fff3f40Smrg      IFS=$oIFS
4078fff3f40Smrg
4088fff3f40Smrg      prefixes=
4098fff3f40Smrg
4108fff3f40Smrg      for d
4118fff3f40Smrg      do
4126a45684fSmrg        test X"$d" = X && continue
4136a45684fSmrg
4146a45684fSmrg        prefix=$prefix$d
4156a45684fSmrg        if test -d "$prefix"; then
4166a45684fSmrg          prefixes=
4176a45684fSmrg        else
4186a45684fSmrg          if $posix_mkdir; then
4196d1874b2Smrg            (umask $mkdir_umask &&
4206a45684fSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4216a45684fSmrg            # Don't fail if two instances are running concurrently.
4226a45684fSmrg            test -d "$prefix" || exit 1
4236a45684fSmrg          else
4246a45684fSmrg            case $prefix in
4256a45684fSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4266a45684fSmrg              *) qprefix=$prefix;;
4276a45684fSmrg            esac
4286a45684fSmrg            prefixes="$prefixes '$qprefix'"
4296a45684fSmrg          fi
4306a45684fSmrg        fi
4316a45684fSmrg        prefix=$prefix/
4328fff3f40Smrg      done
4338fff3f40Smrg
4348fff3f40Smrg      if test -n "$prefixes"; then
4356a45684fSmrg        # Don't fail if two instances are running concurrently.
4366a45684fSmrg        (umask $mkdir_umask &&
4376a45684fSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4386a45684fSmrg          test -d "$dstdir" || exit 1
4396a45684fSmrg        obsolete_mkdir_used=true
440e6232409Smrg      fi
4418fff3f40Smrg    fi
442e6232409Smrg  fi
443e6232409Smrg
444e6232409Smrg  if test -n "$dir_arg"; then
4458fff3f40Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4468fff3f40Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4478fff3f40Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4488fff3f40Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449e6232409Smrg  else
450e6232409Smrg
451e6232409Smrg    # Make a couple of temp file names in the proper directory.
4526d1874b2Smrg    dsttmp=${dstdirslash}_inst.$$_
4536d1874b2Smrg    rmtmp=${dstdirslash}_rm.$$_
454e6232409Smrg
455e6232409Smrg    # Trap to clean up those temp files at exit.
456e6232409Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457e6232409Smrg
458e6232409Smrg    # Copy the file name to the temp name.
4596d1874b2Smrg    (umask $cp_umask &&
4606d1874b2Smrg     { test -z "$stripcmd" || {
4616d1874b2Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4626d1874b2Smrg	 # which would cause strip to fail.
4636d1874b2Smrg	 if test -z "$doit"; then
4646d1874b2Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4656d1874b2Smrg	 else
4666d1874b2Smrg	   $doit touch "$dsttmp"
4676d1874b2Smrg	 fi
4686d1874b2Smrg       }
4696d1874b2Smrg     } &&
4706d1874b2Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471e6232409Smrg
472e6232409Smrg    # and set any options; do chmod last to preserve setuid bits.
473e6232409Smrg    #
474e6232409Smrg    # If any of these fail, we abort the whole thing.  If we want to
475e6232409Smrg    # ignore errors from any of these, just make sure not to ignore
476e6232409Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477e6232409Smrg    #
4788fff3f40Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4798fff3f40Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4808fff3f40Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4818fff3f40Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4828fff3f40Smrg
4838fff3f40Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4848fff3f40Smrg    if $copy_on_change &&
4856a45684fSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4866a45684fSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4876a45684fSmrg       set -f &&
4888fff3f40Smrg       set X $old && old=:$2:$4:$5:$6 &&
4898fff3f40Smrg       set X $new && new=:$2:$4:$5:$6 &&
4906a45684fSmrg       set +f &&
4918fff3f40Smrg       test "$old" = "$new" &&
4928fff3f40Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4938fff3f40Smrg    then
4948fff3f40Smrg      rm -f "$dsttmp"
4958fff3f40Smrg    else
4966d1874b2Smrg      # If $backupsuffix is set, and the file being installed
4976d1874b2Smrg      # already exists, attempt a backup.  Don't worry if it fails,
4986d1874b2Smrg      # e.g., if mv doesn't support -f.
4996d1874b2Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
5006d1874b2Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5016d1874b2Smrg      fi
5026d1874b2Smrg
5038fff3f40Smrg      # Rename the file to the real destination.
5048fff3f40Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5058fff3f40Smrg
5068fff3f40Smrg      # The rename failed, perhaps because mv can't rename something else
5078fff3f40Smrg      # to itself, or perhaps because mv is so ancient that it does not
5088fff3f40Smrg      # support -f.
5098fff3f40Smrg      {
5106a45684fSmrg        # Now remove or move aside any old file at destination location.
5116a45684fSmrg        # We try this two ways since rm can't unlink itself on some
5126a45684fSmrg        # systems and the destination file might be busy for other
5136a45684fSmrg        # reasons.  In this case, the final cleanup might fail but the new
5146a45684fSmrg        # file should still install successfully.
5156a45684fSmrg        {
5166a45684fSmrg          test ! -f "$dst" ||
5176d1874b2Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5186a45684fSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5196d1874b2Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5206a45684fSmrg          } ||
5216a45684fSmrg          { echo "$0: cannot unlink or rename $dst" >&2
5226a45684fSmrg            (exit 1); exit 1
5236a45684fSmrg          }
5246a45684fSmrg        } &&
5256a45684fSmrg
5266a45684fSmrg        # Now rename the file to the real destination.
5276a45684fSmrg        $doit $mvcmd "$dsttmp" "$dst"
5288fff3f40Smrg      }
5298fff3f40Smrg    fi || exit 1
5308fff3f40Smrg
5318fff3f40Smrg    trap '' 0
5328fff3f40Smrg  fi
533e6232409Smrgdone
534e6232409Smrg
535e6232409Smrg# Local variables:
5366d1874b2Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537e6232409Smrg# time-stamp-start: "scriptversion="
538e6232409Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5396d1874b2Smrg# time-stamp-time-zone: "UTC0"
5408fff3f40Smrg# time-stamp-end: "; # UTC"
541e6232409Smrg# End:
542