176888252Smrg#!/bin/sh
276888252Smrg# install - install a program, script, or datafile
376888252Smrg
40814a2baSmrgscriptversion=2020-11-14.01; # UTC
576888252Smrg
676888252Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
776888252Smrg# later released in X11R6 (xc/config/util/install.sh) with the
876888252Smrg# following copyright and license.
976888252Smrg#
1076888252Smrg# Copyright (C) 1994 X Consortium
1176888252Smrg#
1276888252Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1376888252Smrg# of this software and associated documentation files (the "Software"), to
1476888252Smrg# deal in the Software without restriction, including without limitation the
1576888252Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1676888252Smrg# sell copies of the Software, and to permit persons to whom the Software is
1776888252Smrg# furnished to do so, subject to the following conditions:
1876888252Smrg#
1976888252Smrg# The above copyright notice and this permission notice shall be included in
2076888252Smrg# all copies or substantial portions of the Software.
2176888252Smrg#
2276888252Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2376888252Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2476888252Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2576888252Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2676888252Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2776888252Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2876888252Smrg#
2976888252Smrg# Except as contained in this notice, the name of the X Consortium shall not
3076888252Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3176888252Smrg# ings in this Software without prior written authorization from the X Consor-
3276888252Smrg# tium.
3376888252Smrg#
3476888252Smrg#
3576888252Smrg# FSF changes to this file are in the public domain.
3676888252Smrg#
3776888252Smrg# Calling this script install-sh is preferred over install.sh, to prevent
389586ba5aSmrg# 'make' implicit rules from creating a file called install from it
3976888252Smrg# when there is no Makefile.
4076888252Smrg#
4176888252Smrg# This script is compatible with the BSD install script, but was written
421ae1b5e8Smrg# from scratch.
431ae1b5e8Smrg
440814a2baSmrgtab='	'
451ae1b5e8Smrgnl='
461ae1b5e8Smrg'
470814a2baSmrgIFS=" $tab$nl"
4876888252Smrg
490814a2baSmrg# Set DOITPROG to "echo" to test this script.
5076888252Smrg
5186dafe34Smrgdoit=${DOITPROG-}
520814a2baSmrgdoit_exec=${doit:-exec}
5376888252Smrg
541ae1b5e8Smrg# Put in absolute file names if you don't have them in your path;
551ae1b5e8Smrg# or use environment vars.
5676888252Smrg
5786dafe34Smrgchgrpprog=${CHGRPPROG-chgrp}
5886dafe34Smrgchmodprog=${CHMODPROG-chmod}
5986dafe34Smrgchownprog=${CHOWNPROG-chown}
6086dafe34Smrgcmpprog=${CMPPROG-cmp}
6186dafe34Smrgcpprog=${CPPROG-cp}
6286dafe34Smrgmkdirprog=${MKDIRPROG-mkdir}
6386dafe34Smrgmvprog=${MVPROG-mv}
6486dafe34Smrgrmprog=${RMPROG-rm}
6586dafe34Smrgstripprog=${STRIPPROG-strip}
6686dafe34Smrg
671ae1b5e8Smrgposix_mkdir=
681ae1b5e8Smrg
691ae1b5e8Smrg# Desired mode of installed file.
701ae1b5e8Smrgmode=0755
711ae1b5e8Smrg
720814a2baSmrg# Create dirs (including intermediate dirs) using mode 755.
730814a2baSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
740814a2baSmrgmkdir_umask=22
750814a2baSmrg
760814a2baSmrgbackupsuffix=
7786dafe34Smrgchgrpcmd=
781ae1b5e8Smrgchmodcmd=$chmodprog
7976888252Smrgchowncmd=
8086dafe34Smrgmvcmd=$mvprog
8176888252Smrgrmcmd="$rmprog -f"
8286dafe34Smrgstripcmd=
8386dafe34Smrg
8476888252Smrgsrc=
8576888252Smrgdst=
8676888252Smrgdir_arg=
8786dafe34Smrgdst_arg=
8886dafe34Smrg
8986dafe34Smrgcopy_on_change=false
900814a2baSmrgis_target_a_directory=possibly
9176888252Smrg
9286dafe34Smrgusage="\
9386dafe34SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
9476888252Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9576888252Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9676888252Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9776888252Smrg
9876888252SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9976888252SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
10076888252SmrgIn the 4th, create DIRECTORIES.
10176888252Smrg
10276888252SmrgOptions:
10386dafe34Smrg     --help     display this help and exit.
10486dafe34Smrg     --version  display version info and exit.
10586dafe34Smrg
10686dafe34Smrg  -c            (ignored)
1070814a2baSmrg  -C            install only if different (preserve data modification time)
10886dafe34Smrg  -d            create directories instead of installing files.
10986dafe34Smrg  -g GROUP      $chgrpprog installed files to GROUP.
11086dafe34Smrg  -m MODE       $chmodprog installed files to MODE.
11186dafe34Smrg  -o USER       $chownprog installed files to USER.
1120814a2baSmrg  -p            pass -p to $cpprog.
11386dafe34Smrg  -s            $stripprog installed files.
1140814a2baSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
11586dafe34Smrg  -t DIRECTORY  install into DIRECTORY.
11686dafe34Smrg  -T            report an error if DSTFILE is a directory.
11776888252Smrg
11876888252SmrgEnvironment variables override the default commands:
11986dafe34Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12086dafe34Smrg  RMPROG STRIPPROG
1210814a2baSmrg
1220814a2baSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
1230814a2baSmrgit's up to you to specify -f if you want it.
1240814a2baSmrg
1250814a2baSmrgIf -S is not specified, no backups are attempted.
1260814a2baSmrg
1270814a2baSmrgEmail bug reports to bug-automake@gnu.org.
1280814a2baSmrgAutomake home page: https://www.gnu.org/software/automake/
12976888252Smrg"
13076888252Smrg
1311ae1b5e8Smrgwhile test $# -ne 0; do
13276888252Smrg  case $1 in
13386dafe34Smrg    -c) ;;
13486dafe34Smrg
13586dafe34Smrg    -C) copy_on_change=true;;
13676888252Smrg
13786dafe34Smrg    -d) dir_arg=true;;
13876888252Smrg
13976888252Smrg    -g) chgrpcmd="$chgrpprog $2"
1400814a2baSmrg        shift;;
14176888252Smrg
14276888252Smrg    --help) echo "$usage"; exit $?;;
14376888252Smrg
1441ae1b5e8Smrg    -m) mode=$2
1450814a2baSmrg        case $mode in
1460814a2baSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1470814a2baSmrg            echo "$0: invalid mode: $mode" >&2
1480814a2baSmrg            exit 1;;
1490814a2baSmrg        esac
1500814a2baSmrg        shift;;
15176888252Smrg
15276888252Smrg    -o) chowncmd="$chownprog $2"
1530814a2baSmrg        shift;;
1540814a2baSmrg
1550814a2baSmrg    -p) cpprog="$cpprog -p";;
15676888252Smrg
15786dafe34Smrg    -s) stripcmd=$stripprog;;
15876888252Smrg
1590814a2baSmrg    -S) backupsuffix="$2"
1600814a2baSmrg        shift;;
16176888252Smrg
1620814a2baSmrg    -t)
1630814a2baSmrg        is_target_a_directory=always
1640814a2baSmrg        dst_arg=$2
1650814a2baSmrg        # Protect names problematic for 'test' and other utilities.
1660814a2baSmrg        case $dst_arg in
1670814a2baSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1680814a2baSmrg        esac
1690814a2baSmrg        shift;;
1700814a2baSmrg
1710814a2baSmrg    -T) is_target_a_directory=never;;
17276888252Smrg
17376888252Smrg    --version) echo "$0 $scriptversion"; exit $?;;
17476888252Smrg
1750814a2baSmrg    --) shift
1760814a2baSmrg        break;;
1771ae1b5e8Smrg
1780814a2baSmrg    -*) echo "$0: invalid option: $1" >&2
1790814a2baSmrg        exit 1;;
1801ae1b5e8Smrg
1811ae1b5e8Smrg    *)  break;;
18276888252Smrg  esac
18386dafe34Smrg  shift
18476888252Smrgdone
18576888252Smrg
1860814a2baSmrg# We allow the use of options -d and -T together, by making -d
1870814a2baSmrg# take the precedence; this is for compatibility with GNU install.
1880814a2baSmrg
1890814a2baSmrgif test -n "$dir_arg"; then
1900814a2baSmrg  if test -n "$dst_arg"; then
1910814a2baSmrg    echo "$0: target directory not allowed when installing a directory." >&2
1920814a2baSmrg    exit 1
1930814a2baSmrg  fi
1940814a2baSmrgfi
1950814a2baSmrg
19686dafe34Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1971ae1b5e8Smrg  # When -d is used, all remaining arguments are directories to create.
1981ae1b5e8Smrg  # When -t is used, the destination is already specified.
1991ae1b5e8Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2001ae1b5e8Smrg  for arg
2011ae1b5e8Smrg  do
20286dafe34Smrg    if test -n "$dst_arg"; then
2031ae1b5e8Smrg      # $@ is not empty: it contains at least $arg.
20486dafe34Smrg      set fnord "$@" "$dst_arg"
2051ae1b5e8Smrg      shift # fnord
2061ae1b5e8Smrg    fi
2071ae1b5e8Smrg    shift # arg
20886dafe34Smrg    dst_arg=$arg
2099586ba5aSmrg    # Protect names problematic for 'test' and other utilities.
21063847c39Smrg    case $dst_arg in
21163847c39Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
21263847c39Smrg    esac
2131ae1b5e8Smrg  done
2141ae1b5e8Smrgfi
2151ae1b5e8Smrg
2161ae1b5e8Smrgif test $# -eq 0; then
21776888252Smrg  if test -z "$dir_arg"; then
21876888252Smrg    echo "$0: no input file specified." >&2
21976888252Smrg    exit 1
22076888252Smrg  fi
2219586ba5aSmrg  # It's OK to call 'install-sh -d' without argument.
22276888252Smrg  # This can happen when creating conditional directories.
22376888252Smrg  exit 0
22476888252Smrgfi
22576888252Smrg
2260814a2baSmrgif test -z "$dir_arg"; then
2270814a2baSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2280814a2baSmrg    if test ! -d "$dst_arg"; then
2290814a2baSmrg      echo "$0: $dst_arg: Is not a directory." >&2
2300814a2baSmrg      exit 1
2310814a2baSmrg    fi
2320814a2baSmrg  fi
2330814a2baSmrgfi
2340814a2baSmrg
2351ae1b5e8Smrgif test -z "$dir_arg"; then
23663847c39Smrg  do_exit='(exit $ret); exit $ret'
23763847c39Smrg  trap "ret=129; $do_exit" 1
23863847c39Smrg  trap "ret=130; $do_exit" 2
23963847c39Smrg  trap "ret=141; $do_exit" 13
24063847c39Smrg  trap "ret=143; $do_exit" 15
2411ae1b5e8Smrg
2421ae1b5e8Smrg  # Set umask so as not to create temps with too-generous modes.
2431ae1b5e8Smrg  # However, 'strip' requires both read and write access to temps.
2441ae1b5e8Smrg  case $mode in
2451ae1b5e8Smrg    # Optimize common cases.
2461ae1b5e8Smrg    *644) cp_umask=133;;
2471ae1b5e8Smrg    *755) cp_umask=22;;
2481ae1b5e8Smrg
2491ae1b5e8Smrg    *[0-7])
2501ae1b5e8Smrg      if test -z "$stripcmd"; then
2510814a2baSmrg        u_plus_rw=
2521ae1b5e8Smrg      else
2530814a2baSmrg        u_plus_rw='% 200'
2541ae1b5e8Smrg      fi
2551ae1b5e8Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2561ae1b5e8Smrg    *)
2571ae1b5e8Smrg      if test -z "$stripcmd"; then
2580814a2baSmrg        u_plus_rw=
2591ae1b5e8Smrg      else
2600814a2baSmrg        u_plus_rw=,u+rw
2611ae1b5e8Smrg      fi
2621ae1b5e8Smrg      cp_umask=$mode$u_plus_rw;;
2631ae1b5e8Smrg  esac
2641ae1b5e8Smrgfi
2651ae1b5e8Smrg
26676888252Smrgfor src
26776888252Smrgdo
2689586ba5aSmrg  # Protect names problematic for 'test' and other utilities.
26976888252Smrg  case $src in
27063847c39Smrg    -* | [=\(\)!]) src=./$src;;
27176888252Smrg  esac
27276888252Smrg
27376888252Smrg  if test -n "$dir_arg"; then
27476888252Smrg    dst=$src
2751ae1b5e8Smrg    dstdir=$dst
2761ae1b5e8Smrg    test -d "$dstdir"
2771ae1b5e8Smrg    dstdir_status=$?
2780814a2baSmrg    # Don't chown directories that already exist.
2790814a2baSmrg    if test $dstdir_status = 0; then
2800814a2baSmrg      chowncmd=""
2810814a2baSmrg    fi
28276888252Smrg  else
2831ae1b5e8Smrg
28476888252Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
28576888252Smrg    # might cause directories to be created, which would be especially bad
28676888252Smrg    # if $src (and thus $dsttmp) contains '*'.
28776888252Smrg    if test ! -f "$src" && test ! -d "$src"; then
28876888252Smrg      echo "$0: $src does not exist." >&2
28976888252Smrg      exit 1
29076888252Smrg    fi
29176888252Smrg
29286dafe34Smrg    if test -z "$dst_arg"; then
29376888252Smrg      echo "$0: no destination specified." >&2
29476888252Smrg      exit 1
29576888252Smrg    fi
29686dafe34Smrg    dst=$dst_arg
29776888252Smrg
2980814a2baSmrg    # If destination is a directory, append the input filename.
29976888252Smrg    if test -d "$dst"; then
3000814a2baSmrg      if test "$is_target_a_directory" = never; then
3010814a2baSmrg        echo "$0: $dst_arg: Is a directory" >&2
3020814a2baSmrg        exit 1
30376888252Smrg      fi
3041ae1b5e8Smrg      dstdir=$dst
3050814a2baSmrg      dstbase=`basename "$src"`
3060814a2baSmrg      case $dst in
3070814a2baSmrg	*/) dst=$dst$dstbase;;
3080814a2baSmrg	*)  dst=$dst/$dstbase;;
3090814a2baSmrg      esac
3101ae1b5e8Smrg      dstdir_status=0
3111ae1b5e8Smrg    else
3120814a2baSmrg      dstdir=`dirname "$dst"`
3131ae1b5e8Smrg      test -d "$dstdir"
3141ae1b5e8Smrg      dstdir_status=$?
31576888252Smrg    fi
31676888252Smrg  fi
31776888252Smrg
3180814a2baSmrg  case $dstdir in
3190814a2baSmrg    */) dstdirslash=$dstdir;;
3200814a2baSmrg    *)  dstdirslash=$dstdir/;;
3210814a2baSmrg  esac
3220814a2baSmrg
3231ae1b5e8Smrg  obsolete_mkdir_used=false
3241ae1b5e8Smrg
3251ae1b5e8Smrg  if test $dstdir_status != 0; then
3261ae1b5e8Smrg    case $posix_mkdir in
3271ae1b5e8Smrg      '')
3280814a2baSmrg        # With -d, create the new directory with the user-specified mode.
3290814a2baSmrg        # Otherwise, rely on $mkdir_umask.
3300814a2baSmrg        if test -n "$dir_arg"; then
3310814a2baSmrg          mkdir_mode=-m$mode
3320814a2baSmrg        else
3330814a2baSmrg          mkdir_mode=
3340814a2baSmrg        fi
3350814a2baSmrg
3360814a2baSmrg        posix_mkdir=false
3370814a2baSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
3380814a2baSmrg	# here however when possible just to lower collision chance.
3390814a2baSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3400814a2baSmrg
3410814a2baSmrg	trap '
3420814a2baSmrg	  ret=$?
3430814a2baSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
3440814a2baSmrg	  exit $ret
3450814a2baSmrg	' 0
3460814a2baSmrg
3470814a2baSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
3480814a2baSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
3490814a2baSmrg	# directory is successfully created first before we actually test
3500814a2baSmrg	# 'mkdir -p'.
3510814a2baSmrg	if (umask $mkdir_umask &&
3520814a2baSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
3530814a2baSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3540814a2baSmrg	then
3550814a2baSmrg	  if test -z "$dir_arg" || {
3560814a2baSmrg	       # Check for POSIX incompatibilities with -m.
3570814a2baSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3580814a2baSmrg	       # other-writable bit of parent directory when it shouldn't.
3590814a2baSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3600814a2baSmrg	       test_tmpdir="$tmpdir/a"
3610814a2baSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3620814a2baSmrg	       case $ls_ld_tmpdir in
3630814a2baSmrg		 d????-?r-*) different_mode=700;;
3640814a2baSmrg		 d????-?--*) different_mode=755;;
3650814a2baSmrg		 *) false;;
3660814a2baSmrg	       esac &&
3670814a2baSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3680814a2baSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3690814a2baSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3700814a2baSmrg	       }
3710814a2baSmrg	     }
3720814a2baSmrg	  then posix_mkdir=:
3730814a2baSmrg	  fi
3740814a2baSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3751ae1b5e8Smrg	else
3760814a2baSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
3770814a2baSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3781ae1b5e8Smrg	fi
3790814a2baSmrg	trap '' 0;;
3801ae1b5e8Smrg    esac
38176888252Smrg
3821ae1b5e8Smrg    if
3831ae1b5e8Smrg      $posix_mkdir && (
3840814a2baSmrg        umask $mkdir_umask &&
3850814a2baSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3861ae1b5e8Smrg      )
3871ae1b5e8Smrg    then :
3881ae1b5e8Smrg    else
38976888252Smrg
3900814a2baSmrg      # mkdir does not conform to POSIX,
3911ae1b5e8Smrg      # or it failed possibly due to a race condition.  Create the
3921ae1b5e8Smrg      # directory the slow way, step by step, checking for races as we go.
3931ae1b5e8Smrg
3941ae1b5e8Smrg      case $dstdir in
3950814a2baSmrg        /*) prefix='/';;
3960814a2baSmrg        [-=\(\)!]*) prefix='./';;
3970814a2baSmrg        *)  prefix='';;
3981ae1b5e8Smrg      esac
3991ae1b5e8Smrg
4001ae1b5e8Smrg      oIFS=$IFS
4011ae1b5e8Smrg      IFS=/
4020814a2baSmrg      set -f
4031ae1b5e8Smrg      set fnord $dstdir
40476888252Smrg      shift
4050814a2baSmrg      set +f
4061ae1b5e8Smrg      IFS=$oIFS
4071ae1b5e8Smrg
4081ae1b5e8Smrg      prefixes=
4091ae1b5e8Smrg
4101ae1b5e8Smrg      for d
4111ae1b5e8Smrg      do
4120814a2baSmrg        test X"$d" = X && continue
4130814a2baSmrg
4140814a2baSmrg        prefix=$prefix$d
4150814a2baSmrg        if test -d "$prefix"; then
4160814a2baSmrg          prefixes=
4170814a2baSmrg        else
4180814a2baSmrg          if $posix_mkdir; then
4190814a2baSmrg            (umask $mkdir_umask &&
4200814a2baSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4210814a2baSmrg            # Don't fail if two instances are running concurrently.
4220814a2baSmrg            test -d "$prefix" || exit 1
4230814a2baSmrg          else
4240814a2baSmrg            case $prefix in
4250814a2baSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4260814a2baSmrg              *) qprefix=$prefix;;
4270814a2baSmrg            esac
4280814a2baSmrg            prefixes="$prefixes '$qprefix'"
4290814a2baSmrg          fi
4300814a2baSmrg        fi
4310814a2baSmrg        prefix=$prefix/
4321ae1b5e8Smrg      done
4331ae1b5e8Smrg
4341ae1b5e8Smrg      if test -n "$prefixes"; then
4350814a2baSmrg        # Don't fail if two instances are running concurrently.
4360814a2baSmrg        (umask $mkdir_umask &&
4370814a2baSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4380814a2baSmrg          test -d "$dstdir" || exit 1
4390814a2baSmrg        obsolete_mkdir_used=true
44076888252Smrg      fi
4411ae1b5e8Smrg    fi
44276888252Smrg  fi
44376888252Smrg
44476888252Smrg  if test -n "$dir_arg"; then
4451ae1b5e8Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4461ae1b5e8Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4471ae1b5e8Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4481ae1b5e8Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44976888252Smrg  else
45076888252Smrg
45176888252Smrg    # Make a couple of temp file names in the proper directory.
4520814a2baSmrg    dsttmp=${dstdirslash}_inst.$$_
4530814a2baSmrg    rmtmp=${dstdirslash}_rm.$$_
45476888252Smrg
45576888252Smrg    # Trap to clean up those temp files at exit.
45676888252Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45776888252Smrg
45876888252Smrg    # Copy the file name to the temp name.
4590814a2baSmrg    (umask $cp_umask &&
4600814a2baSmrg     { test -z "$stripcmd" || {
4610814a2baSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
4620814a2baSmrg	 # which would cause strip to fail.
4630814a2baSmrg	 if test -z "$doit"; then
4640814a2baSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
4650814a2baSmrg	 else
4660814a2baSmrg	   $doit touch "$dsttmp"
4670814a2baSmrg	 fi
4680814a2baSmrg       }
4690814a2baSmrg     } &&
4700814a2baSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
47176888252Smrg
47276888252Smrg    # and set any options; do chmod last to preserve setuid bits.
47376888252Smrg    #
47476888252Smrg    # If any of these fail, we abort the whole thing.  If we want to
47576888252Smrg    # ignore errors from any of these, just make sure not to ignore
47676888252Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
47776888252Smrg    #
47886dafe34Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
47986dafe34Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
48086dafe34Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
48186dafe34Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
48286dafe34Smrg
48386dafe34Smrg    # If -C, don't bother to copy if it wouldn't change the file.
48486dafe34Smrg    if $copy_on_change &&
4850814a2baSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4860814a2baSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4870814a2baSmrg       set -f &&
48886dafe34Smrg       set X $old && old=:$2:$4:$5:$6 &&
48986dafe34Smrg       set X $new && new=:$2:$4:$5:$6 &&
4900814a2baSmrg       set +f &&
49186dafe34Smrg       test "$old" = "$new" &&
49286dafe34Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
49386dafe34Smrg    then
49486dafe34Smrg      rm -f "$dsttmp"
49586dafe34Smrg    else
4960814a2baSmrg      # If $backupsuffix is set, and the file being installed
4970814a2baSmrg      # already exists, attempt a backup.  Don't worry if it fails,
4980814a2baSmrg      # e.g., if mv doesn't support -f.
4990814a2baSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
5000814a2baSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
5010814a2baSmrg      fi
5020814a2baSmrg
50386dafe34Smrg      # Rename the file to the real destination.
50486dafe34Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
50586dafe34Smrg
50686dafe34Smrg      # The rename failed, perhaps because mv can't rename something else
50786dafe34Smrg      # to itself, or perhaps because mv is so ancient that it does not
50886dafe34Smrg      # support -f.
50986dafe34Smrg      {
5100814a2baSmrg        # Now remove or move aside any old file at destination location.
5110814a2baSmrg        # We try this two ways since rm can't unlink itself on some
5120814a2baSmrg        # systems and the destination file might be busy for other
5130814a2baSmrg        # reasons.  In this case, the final cleanup might fail but the new
5140814a2baSmrg        # file should still install successfully.
5150814a2baSmrg        {
5160814a2baSmrg          test ! -f "$dst" ||
5170814a2baSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
5180814a2baSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5190814a2baSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5200814a2baSmrg          } ||
5210814a2baSmrg          { echo "$0: cannot unlink or rename $dst" >&2
5220814a2baSmrg            (exit 1); exit 1
5230814a2baSmrg          }
5240814a2baSmrg        } &&
5250814a2baSmrg
5260814a2baSmrg        # Now rename the file to the real destination.
5270814a2baSmrg        $doit $mvcmd "$dsttmp" "$dst"
52886dafe34Smrg      }
52986dafe34Smrg    fi || exit 1
53076888252Smrg
5311ae1b5e8Smrg    trap '' 0
5321ae1b5e8Smrg  fi
5331ae1b5e8Smrgdone
53476888252Smrg
53576888252Smrg# Local variables:
5360814a2baSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
53776888252Smrg# time-stamp-start: "scriptversion="
53876888252Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5390814a2baSmrg# time-stamp-time-zone: "UTC0"
540d8556812Smrg# time-stamp-end: "; # UTC"
54176888252Smrg# End:
542