install-sh revision c3e29c03
14b0ead49Smrg#!/bin/sh
24b0ead49Smrg# install - install a program, script, or datafile
34b0ead49Smrg
4c3e29c03Smrgscriptversion=2020-11-14.01; # UTC
54b0ead49Smrg
64b0ead49Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
74b0ead49Smrg# later released in X11R6 (xc/config/util/install.sh) with the
84b0ead49Smrg# following copyright and license.
94b0ead49Smrg#
104b0ead49Smrg# Copyright (C) 1994 X Consortium
114b0ead49Smrg#
124b0ead49Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
134b0ead49Smrg# of this software and associated documentation files (the "Software"), to
144b0ead49Smrg# deal in the Software without restriction, including without limitation the
154b0ead49Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164b0ead49Smrg# sell copies of the Software, and to permit persons to whom the Software is
174b0ead49Smrg# furnished to do so, subject to the following conditions:
184b0ead49Smrg#
194b0ead49Smrg# The above copyright notice and this permission notice shall be included in
204b0ead49Smrg# all copies or substantial portions of the Software.
214b0ead49Smrg#
224b0ead49Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234b0ead49Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244b0ead49Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254b0ead49Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264b0ead49Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274b0ead49Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284b0ead49Smrg#
294b0ead49Smrg# Except as contained in this notice, the name of the X Consortium shall not
304b0ead49Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
314b0ead49Smrg# ings in this Software without prior written authorization from the X Consor-
324b0ead49Smrg# tium.
334b0ead49Smrg#
344b0ead49Smrg#
354b0ead49Smrg# FSF changes to this file are in the public domain.
364b0ead49Smrg#
374b0ead49Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3826372658Smrg# 'make' implicit rules from creating a file called install from it
394b0ead49Smrg# when there is no Makefile.
404b0ead49Smrg#
414b0ead49Smrg# This script is compatible with the BSD install script, but was written
4252b01e16Smrg# from scratch.
4352b01e16Smrg
4426372658Smrgtab='	'
4552b01e16Smrgnl='
4652b01e16Smrg'
4726372658SmrgIFS=" $tab$nl"
484b0ead49Smrg
4926372658Smrg# Set DOITPROG to "echo" to test this script.
504b0ead49Smrg
5152b01e16Smrgdoit=${DOITPROG-}
5226372658Smrgdoit_exec=${doit:-exec}
534b0ead49Smrg
5452b01e16Smrg# Put in absolute file names if you don't have them in your path;
5552b01e16Smrg# or use environment vars.
5652b01e16Smrg
5752b01e16Smrgchgrpprog=${CHGRPPROG-chgrp}
5852b01e16Smrgchmodprog=${CHMODPROG-chmod}
5952b01e16Smrgchownprog=${CHOWNPROG-chown}
6052b01e16Smrgcmpprog=${CMPPROG-cmp}
6152b01e16Smrgcpprog=${CPPROG-cp}
6252b01e16Smrgmkdirprog=${MKDIRPROG-mkdir}
6352b01e16Smrgmvprog=${MVPROG-mv}
6452b01e16Smrgrmprog=${RMPROG-rm}
6552b01e16Smrgstripprog=${STRIPPROG-strip}
6652b01e16Smrg
6752b01e16Smrgposix_mkdir=
6852b01e16Smrg
6952b01e16Smrg# Desired mode of installed file.
7052b01e16Smrgmode=0755
714b0ead49Smrg
72c3e29c03Smrg# Create dirs (including intermediate dirs) using mode 755.
73c3e29c03Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
74c3e29c03Smrgmkdir_umask=22
75c3e29c03Smrg
76c3e29c03Smrgbackupsuffix=
774b0ead49Smrgchgrpcmd=
7852b01e16Smrgchmodcmd=$chmodprog
7952b01e16Smrgchowncmd=
8052b01e16Smrgmvcmd=$mvprog
814b0ead49Smrgrmcmd="$rmprog -f"
8252b01e16Smrgstripcmd=
8352b01e16Smrg
844b0ead49Smrgsrc=
854b0ead49Smrgdst=
864b0ead49Smrgdir_arg=
8752b01e16Smrgdst_arg=
8852b01e16Smrg
8952b01e16Smrgcopy_on_change=false
9026372658Smrgis_target_a_directory=possibly
914b0ead49Smrg
9252b01e16Smrgusage="\
9352b01e16SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
944b0ead49Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
954b0ead49Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
964b0ead49Smrg   or: $0 [OPTION]... -d DIRECTORIES...
974b0ead49Smrg
984b0ead49SmrgIn the 1st form, copy SRCFILE to DSTFILE.
994b0ead49SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1004b0ead49SmrgIn the 4th, create DIRECTORIES.
1014b0ead49Smrg
1024b0ead49SmrgOptions:
10352b01e16Smrg     --help     display this help and exit.
10452b01e16Smrg     --version  display version info and exit.
10552b01e16Smrg
10652b01e16Smrg  -c            (ignored)
107c3e29c03Smrg  -C            install only if different (preserve data modification time)
10852b01e16Smrg  -d            create directories instead of installing files.
10952b01e16Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11052b01e16Smrg  -m MODE       $chmodprog installed files to MODE.
11152b01e16Smrg  -o USER       $chownprog installed files to USER.
112c3e29c03Smrg  -p            pass -p to $cpprog.
11352b01e16Smrg  -s            $stripprog installed files.
114c3e29c03Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11552b01e16Smrg  -t DIRECTORY  install into DIRECTORY.
11652b01e16Smrg  -T            report an error if DSTFILE is a directory.
1174b0ead49Smrg
1184b0ead49SmrgEnvironment variables override the default commands:
11952b01e16Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12052b01e16Smrg  RMPROG STRIPPROG
121c3e29c03Smrg
122c3e29c03SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
123c3e29c03Smrgit's up to you to specify -f if you want it.
124c3e29c03Smrg
125c3e29c03SmrgIf -S is not specified, no backups are attempted.
126c3e29c03Smrg
127c3e29c03SmrgEmail bug reports to bug-automake@gnu.org.
128c3e29c03SmrgAutomake home page: https://www.gnu.org/software/automake/
1294b0ead49Smrg"
1304b0ead49Smrg
13152b01e16Smrgwhile test $# -ne 0; do
1324b0ead49Smrg  case $1 in
13352b01e16Smrg    -c) ;;
13452b01e16Smrg
13552b01e16Smrg    -C) copy_on_change=true;;
1364b0ead49Smrg
13752b01e16Smrg    -d) dir_arg=true;;
1384b0ead49Smrg
1394b0ead49Smrg    -g) chgrpcmd="$chgrpprog $2"
14026372658Smrg        shift;;
1414b0ead49Smrg
1424b0ead49Smrg    --help) echo "$usage"; exit $?;;
1434b0ead49Smrg
14452b01e16Smrg    -m) mode=$2
14526372658Smrg        case $mode in
14626372658Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
14726372658Smrg            echo "$0: invalid mode: $mode" >&2
14826372658Smrg            exit 1;;
14926372658Smrg        esac
15026372658Smrg        shift;;
1514b0ead49Smrg
1524b0ead49Smrg    -o) chowncmd="$chownprog $2"
15326372658Smrg        shift;;
1544b0ead49Smrg
155c3e29c03Smrg    -p) cpprog="$cpprog -p";;
156c3e29c03Smrg
15752b01e16Smrg    -s) stripcmd=$stripprog;;
1584b0ead49Smrg
159c3e29c03Smrg    -S) backupsuffix="$2"
160c3e29c03Smrg        shift;;
161c3e29c03Smrg
16226372658Smrg    -t)
16326372658Smrg        is_target_a_directory=always
16426372658Smrg        dst_arg=$2
16526372658Smrg        # Protect names problematic for 'test' and other utilities.
16626372658Smrg        case $dst_arg in
16726372658Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
16826372658Smrg        esac
16926372658Smrg        shift;;
1704b0ead49Smrg
17126372658Smrg    -T) is_target_a_directory=never;;
1724b0ead49Smrg
1734b0ead49Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1744b0ead49Smrg
17526372658Smrg    --) shift
17626372658Smrg        break;;
17752b01e16Smrg
17826372658Smrg    -*) echo "$0: invalid option: $1" >&2
17926372658Smrg        exit 1;;
18052b01e16Smrg
18152b01e16Smrg    *)  break;;
1824b0ead49Smrg  esac
18352b01e16Smrg  shift
1844b0ead49Smrgdone
1854b0ead49Smrg
18626372658Smrg# We allow the use of options -d and -T together, by making -d
18726372658Smrg# take the precedence; this is for compatibility with GNU install.
18826372658Smrg
18926372658Smrgif test -n "$dir_arg"; then
19026372658Smrg  if test -n "$dst_arg"; then
19126372658Smrg    echo "$0: target directory not allowed when installing a directory." >&2
19226372658Smrg    exit 1
19326372658Smrg  fi
19426372658Smrgfi
19526372658Smrg
19652b01e16Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
19752b01e16Smrg  # When -d is used, all remaining arguments are directories to create.
19852b01e16Smrg  # When -t is used, the destination is already specified.
19952b01e16Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
20052b01e16Smrg  for arg
20152b01e16Smrg  do
20252b01e16Smrg    if test -n "$dst_arg"; then
20352b01e16Smrg      # $@ is not empty: it contains at least $arg.
20452b01e16Smrg      set fnord "$@" "$dst_arg"
20552b01e16Smrg      shift # fnord
20652b01e16Smrg    fi
20752b01e16Smrg    shift # arg
20852b01e16Smrg    dst_arg=$arg
20926372658Smrg    # Protect names problematic for 'test' and other utilities.
2104e7590efSmrg    case $dst_arg in
2114e7590efSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2124e7590efSmrg    esac
21352b01e16Smrg  done
21452b01e16Smrgfi
21552b01e16Smrg
21652b01e16Smrgif test $# -eq 0; then
2174b0ead49Smrg  if test -z "$dir_arg"; then
2184b0ead49Smrg    echo "$0: no input file specified." >&2
2194b0ead49Smrg    exit 1
2204b0ead49Smrg  fi
22126372658Smrg  # It's OK to call 'install-sh -d' without argument.
2224b0ead49Smrg  # This can happen when creating conditional directories.
2234b0ead49Smrg  exit 0
2244b0ead49Smrgfi
2254b0ead49Smrg
22626372658Smrgif test -z "$dir_arg"; then
22726372658Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
22826372658Smrg    if test ! -d "$dst_arg"; then
22926372658Smrg      echo "$0: $dst_arg: Is not a directory." >&2
23026372658Smrg      exit 1
23126372658Smrg    fi
23226372658Smrg  fi
23326372658Smrgfi
23426372658Smrg
23552b01e16Smrgif test -z "$dir_arg"; then
2364e7590efSmrg  do_exit='(exit $ret); exit $ret'
2374e7590efSmrg  trap "ret=129; $do_exit" 1
2384e7590efSmrg  trap "ret=130; $do_exit" 2
2394e7590efSmrg  trap "ret=141; $do_exit" 13
2404e7590efSmrg  trap "ret=143; $do_exit" 15
24152b01e16Smrg
24252b01e16Smrg  # Set umask so as not to create temps with too-generous modes.
24352b01e16Smrg  # However, 'strip' requires both read and write access to temps.
24452b01e16Smrg  case $mode in
24552b01e16Smrg    # Optimize common cases.
24652b01e16Smrg    *644) cp_umask=133;;
24752b01e16Smrg    *755) cp_umask=22;;
24852b01e16Smrg
24952b01e16Smrg    *[0-7])
25052b01e16Smrg      if test -z "$stripcmd"; then
25126372658Smrg        u_plus_rw=
25252b01e16Smrg      else
25326372658Smrg        u_plus_rw='% 200'
25452b01e16Smrg      fi
25552b01e16Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
25652b01e16Smrg    *)
25752b01e16Smrg      if test -z "$stripcmd"; then
25826372658Smrg        u_plus_rw=
25952b01e16Smrg      else
26026372658Smrg        u_plus_rw=,u+rw
26152b01e16Smrg      fi
26252b01e16Smrg      cp_umask=$mode$u_plus_rw;;
26352b01e16Smrg  esac
26452b01e16Smrgfi
26552b01e16Smrg
2664b0ead49Smrgfor src
2674b0ead49Smrgdo
26826372658Smrg  # Protect names problematic for 'test' and other utilities.
2694b0ead49Smrg  case $src in
2704e7590efSmrg    -* | [=\(\)!]) src=./$src;;
2714b0ead49Smrg  esac
2724b0ead49Smrg
2734b0ead49Smrg  if test -n "$dir_arg"; then
2744b0ead49Smrg    dst=$src
27552b01e16Smrg    dstdir=$dst
27652b01e16Smrg    test -d "$dstdir"
27752b01e16Smrg    dstdir_status=$?
278c3e29c03Smrg    # Don't chown directories that already exist.
279c3e29c03Smrg    if test $dstdir_status = 0; then
280c3e29c03Smrg      chowncmd=""
281c3e29c03Smrg    fi
2824b0ead49Smrg  else
28352b01e16Smrg
2844b0ead49Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2854b0ead49Smrg    # might cause directories to be created, which would be especially bad
2864b0ead49Smrg    # if $src (and thus $dsttmp) contains '*'.
2874b0ead49Smrg    if test ! -f "$src" && test ! -d "$src"; then
2884b0ead49Smrg      echo "$0: $src does not exist." >&2
2894b0ead49Smrg      exit 1
2904b0ead49Smrg    fi
2914b0ead49Smrg
29252b01e16Smrg    if test -z "$dst_arg"; then
2934b0ead49Smrg      echo "$0: no destination specified." >&2
2944b0ead49Smrg      exit 1
2954b0ead49Smrg    fi
29652b01e16Smrg    dst=$dst_arg
2974b0ead49Smrg
298c3e29c03Smrg    # If destination is a directory, append the input filename.
2994b0ead49Smrg    if test -d "$dst"; then
30026372658Smrg      if test "$is_target_a_directory" = never; then
30126372658Smrg        echo "$0: $dst_arg: Is a directory" >&2
30226372658Smrg        exit 1
3034b0ead49Smrg      fi
30452b01e16Smrg      dstdir=$dst
305c3e29c03Smrg      dstbase=`basename "$src"`
306c3e29c03Smrg      case $dst in
307c3e29c03Smrg	*/) dst=$dst$dstbase;;
308c3e29c03Smrg	*)  dst=$dst/$dstbase;;
309c3e29c03Smrg      esac
31052b01e16Smrg      dstdir_status=0
31152b01e16Smrg    else
31226372658Smrg      dstdir=`dirname "$dst"`
31352b01e16Smrg      test -d "$dstdir"
31452b01e16Smrg      dstdir_status=$?
3154b0ead49Smrg    fi
3164b0ead49Smrg  fi
3174b0ead49Smrg
318c3e29c03Smrg  case $dstdir in
319c3e29c03Smrg    */) dstdirslash=$dstdir;;
320c3e29c03Smrg    *)  dstdirslash=$dstdir/;;
321c3e29c03Smrg  esac
322c3e29c03Smrg
32352b01e16Smrg  obsolete_mkdir_used=false
32452b01e16Smrg
32552b01e16Smrg  if test $dstdir_status != 0; then
32652b01e16Smrg    case $posix_mkdir in
32752b01e16Smrg      '')
32826372658Smrg        # With -d, create the new directory with the user-specified mode.
32926372658Smrg        # Otherwise, rely on $mkdir_umask.
33026372658Smrg        if test -n "$dir_arg"; then
33126372658Smrg          mkdir_mode=-m$mode
33226372658Smrg        else
33326372658Smrg          mkdir_mode=
33426372658Smrg        fi
33526372658Smrg
33626372658Smrg        posix_mkdir=false
337c3e29c03Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
338c3e29c03Smrg	# here however when possible just to lower collision chance.
339c3e29c03Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
340c3e29c03Smrg
341c3e29c03Smrg	trap '
342c3e29c03Smrg	  ret=$?
343c3e29c03Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
344c3e29c03Smrg	  exit $ret
345c3e29c03Smrg	' 0
346c3e29c03Smrg
347c3e29c03Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
348c3e29c03Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
349c3e29c03Smrg	# directory is successfully created first before we actually test
350c3e29c03Smrg	# 'mkdir -p'.
351c3e29c03Smrg	if (umask $mkdir_umask &&
352c3e29c03Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
353c3e29c03Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
354c3e29c03Smrg	then
355c3e29c03Smrg	  if test -z "$dir_arg" || {
356c3e29c03Smrg	       # Check for POSIX incompatibilities with -m.
357c3e29c03Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
358c3e29c03Smrg	       # other-writable bit of parent directory when it shouldn't.
359c3e29c03Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
360c3e29c03Smrg	       test_tmpdir="$tmpdir/a"
361c3e29c03Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
362c3e29c03Smrg	       case $ls_ld_tmpdir in
363c3e29c03Smrg		 d????-?r-*) different_mode=700;;
364c3e29c03Smrg		 d????-?--*) different_mode=755;;
365c3e29c03Smrg		 *) false;;
366c3e29c03Smrg	       esac &&
367c3e29c03Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
368c3e29c03Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
369c3e29c03Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
370c3e29c03Smrg	       }
371c3e29c03Smrg	     }
372c3e29c03Smrg	  then posix_mkdir=:
373c3e29c03Smrg	  fi
374c3e29c03Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
375c3e29c03Smrg	else
376c3e29c03Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
377c3e29c03Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
378c3e29c03Smrg	fi
379c3e29c03Smrg	trap '' 0;;
38052b01e16Smrg    esac
3814b0ead49Smrg
38252b01e16Smrg    if
38352b01e16Smrg      $posix_mkdir && (
38426372658Smrg        umask $mkdir_umask &&
38526372658Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38652b01e16Smrg      )
38752b01e16Smrg    then :
38852b01e16Smrg    else
3894b0ead49Smrg
390c3e29c03Smrg      # mkdir does not conform to POSIX,
39152b01e16Smrg      # or it failed possibly due to a race condition.  Create the
39252b01e16Smrg      # directory the slow way, step by step, checking for races as we go.
3934b0ead49Smrg
39452b01e16Smrg      case $dstdir in
39526372658Smrg        /*) prefix='/';;
39626372658Smrg        [-=\(\)!]*) prefix='./';;
39726372658Smrg        *)  prefix='';;
39852b01e16Smrg      esac
3994b0ead49Smrg
40052b01e16Smrg      oIFS=$IFS
40152b01e16Smrg      IFS=/
40226372658Smrg      set -f
40352b01e16Smrg      set fnord $dstdir
4044b0ead49Smrg      shift
40526372658Smrg      set +f
40652b01e16Smrg      IFS=$oIFS
40752b01e16Smrg
40852b01e16Smrg      prefixes=
40952b01e16Smrg
41052b01e16Smrg      for d
41152b01e16Smrg      do
41226372658Smrg        test X"$d" = X && continue
41326372658Smrg
41426372658Smrg        prefix=$prefix$d
41526372658Smrg        if test -d "$prefix"; then
41626372658Smrg          prefixes=
41726372658Smrg        else
41826372658Smrg          if $posix_mkdir; then
419c3e29c03Smrg            (umask $mkdir_umask &&
42026372658Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42126372658Smrg            # Don't fail if two instances are running concurrently.
42226372658Smrg            test -d "$prefix" || exit 1
42326372658Smrg          else
42426372658Smrg            case $prefix in
42526372658Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42626372658Smrg              *) qprefix=$prefix;;
42726372658Smrg            esac
42826372658Smrg            prefixes="$prefixes '$qprefix'"
42926372658Smrg          fi
43026372658Smrg        fi
43126372658Smrg        prefix=$prefix/
43252b01e16Smrg      done
43352b01e16Smrg
43452b01e16Smrg      if test -n "$prefixes"; then
43526372658Smrg        # Don't fail if two instances are running concurrently.
43626372658Smrg        (umask $mkdir_umask &&
43726372658Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43826372658Smrg          test -d "$dstdir" || exit 1
43926372658Smrg        obsolete_mkdir_used=true
4404b0ead49Smrg      fi
44152b01e16Smrg    fi
4424b0ead49Smrg  fi
4434b0ead49Smrg
4444b0ead49Smrg  if test -n "$dir_arg"; then
44552b01e16Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44652b01e16Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44752b01e16Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44852b01e16Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4494b0ead49Smrg  else
4504b0ead49Smrg
4514b0ead49Smrg    # Make a couple of temp file names in the proper directory.
452c3e29c03Smrg    dsttmp=${dstdirslash}_inst.$$_
453c3e29c03Smrg    rmtmp=${dstdirslash}_rm.$$_
4544b0ead49Smrg
4554b0ead49Smrg    # Trap to clean up those temp files at exit.
4564b0ead49Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4574b0ead49Smrg
4584b0ead49Smrg    # Copy the file name to the temp name.
459c3e29c03Smrg    (umask $cp_umask &&
460c3e29c03Smrg     { test -z "$stripcmd" || {
461c3e29c03Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
462c3e29c03Smrg	 # which would cause strip to fail.
463c3e29c03Smrg	 if test -z "$doit"; then
464c3e29c03Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
465c3e29c03Smrg	 else
466c3e29c03Smrg	   $doit touch "$dsttmp"
467c3e29c03Smrg	 fi
468c3e29c03Smrg       }
469c3e29c03Smrg     } &&
470c3e29c03Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4714b0ead49Smrg
4724b0ead49Smrg    # and set any options; do chmod last to preserve setuid bits.
4734b0ead49Smrg    #
4744b0ead49Smrg    # If any of these fail, we abort the whole thing.  If we want to
4754b0ead49Smrg    # ignore errors from any of these, just make sure not to ignore
4764b0ead49Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4774b0ead49Smrg    #
47852b01e16Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47952b01e16Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48052b01e16Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48152b01e16Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48252b01e16Smrg
48352b01e16Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48452b01e16Smrg    if $copy_on_change &&
48526372658Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
48626372658Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
48726372658Smrg       set -f &&
48852b01e16Smrg       set X $old && old=:$2:$4:$5:$6 &&
48952b01e16Smrg       set X $new && new=:$2:$4:$5:$6 &&
49026372658Smrg       set +f &&
49152b01e16Smrg       test "$old" = "$new" &&
49252b01e16Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49352b01e16Smrg    then
49452b01e16Smrg      rm -f "$dsttmp"
49552b01e16Smrg    else
496c3e29c03Smrg      # If $backupsuffix is set, and the file being installed
497c3e29c03Smrg      # already exists, attempt a backup.  Don't worry if it fails,
498c3e29c03Smrg      # e.g., if mv doesn't support -f.
499c3e29c03Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
500c3e29c03Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
501c3e29c03Smrg      fi
502c3e29c03Smrg
50352b01e16Smrg      # Rename the file to the real destination.
50452b01e16Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50552b01e16Smrg
50652b01e16Smrg      # The rename failed, perhaps because mv can't rename something else
50752b01e16Smrg      # to itself, or perhaps because mv is so ancient that it does not
50852b01e16Smrg      # support -f.
50952b01e16Smrg      {
51026372658Smrg        # Now remove or move aside any old file at destination location.
51126372658Smrg        # We try this two ways since rm can't unlink itself on some
51226372658Smrg        # systems and the destination file might be busy for other
51326372658Smrg        # reasons.  In this case, the final cleanup might fail but the new
51426372658Smrg        # file should still install successfully.
51526372658Smrg        {
51626372658Smrg          test ! -f "$dst" ||
517c3e29c03Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
51826372658Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
519c3e29c03Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
52026372658Smrg          } ||
52126372658Smrg          { echo "$0: cannot unlink or rename $dst" >&2
52226372658Smrg            (exit 1); exit 1
52326372658Smrg          }
52426372658Smrg        } &&
52526372658Smrg
52626372658Smrg        # Now rename the file to the real destination.
52726372658Smrg        $doit $mvcmd "$dsttmp" "$dst"
52852b01e16Smrg      }
52952b01e16Smrg    fi || exit 1
53052b01e16Smrg
53152b01e16Smrg    trap '' 0
53252b01e16Smrg  fi
5334b0ead49Smrgdone
5344b0ead49Smrg
5354b0ead49Smrg# Local variables:
536c3e29c03Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
5374b0ead49Smrg# time-stamp-start: "scriptversion="
5384b0ead49Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
539c3e29c03Smrg# time-stamp-time-zone: "UTC0"
54052b01e16Smrg# time-stamp-end: "; # UTC"
5414b0ead49Smrg# End:
542