18108eb18Smrg#!/bin/sh
28108eb18Smrg# install - install a program, script, or datafile
31a650d1eSmrg
499c4c48aSmrgscriptversion=2020-11-14.01; # UTC
51a650d1eSmrg
68108eb18Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
78108eb18Smrg# later released in X11R6 (xc/config/util/install.sh) with the
88108eb18Smrg# following copyright and license.
98108eb18Smrg#
108108eb18Smrg# Copyright (C) 1994 X Consortium
118108eb18Smrg#
128108eb18Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
138108eb18Smrg# of this software and associated documentation files (the "Software"), to
148108eb18Smrg# deal in the Software without restriction, including without limitation the
158108eb18Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
168108eb18Smrg# sell copies of the Software, and to permit persons to whom the Software is
178108eb18Smrg# furnished to do so, subject to the following conditions:
188108eb18Smrg#
198108eb18Smrg# The above copyright notice and this permission notice shall be included in
208108eb18Smrg# all copies or substantial portions of the Software.
218108eb18Smrg#
228108eb18Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
238108eb18Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
248108eb18Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
258108eb18Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
268108eb18Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
278108eb18Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
288108eb18Smrg#
298108eb18Smrg# Except as contained in this notice, the name of the X Consortium shall not
308108eb18Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
318108eb18Smrg# ings in this Software without prior written authorization from the X Consor-
328108eb18Smrg# tium.
338108eb18Smrg#
348108eb18Smrg#
358108eb18Smrg# FSF changes to this file are in the public domain.
368108eb18Smrg#
378108eb18Smrg# Calling this script install-sh is preferred over install.sh, to prevent
388a355152Smrg# 'make' implicit rules from creating a file called install from it
398108eb18Smrg# when there is no Makefile.
408108eb18Smrg#
418108eb18Smrg# This script is compatible with the BSD install script, but was written
421a650d1eSmrg# from scratch.
438108eb18Smrg
448a355152Smrgtab='	'
451a650d1eSmrgnl='
461a650d1eSmrg'
478a355152SmrgIFS=" $tab$nl"
488108eb18Smrg
498a355152Smrg# Set DOITPROG to "echo" to test this script.
508108eb18Smrg
511a650d1eSmrgdoit=${DOITPROG-}
528a355152Smrgdoit_exec=${doit:-exec}
538108eb18Smrg
541a650d1eSmrg# Put in absolute file names if you don't have them in your path;
551a650d1eSmrg# or use environment vars.
561a650d1eSmrg
571a650d1eSmrgchgrpprog=${CHGRPPROG-chgrp}
581a650d1eSmrgchmodprog=${CHMODPROG-chmod}
591a650d1eSmrgchownprog=${CHOWNPROG-chown}
601a650d1eSmrgcmpprog=${CMPPROG-cmp}
611a650d1eSmrgcpprog=${CPPROG-cp}
621a650d1eSmrgmkdirprog=${MKDIRPROG-mkdir}
631a650d1eSmrgmvprog=${MVPROG-mv}
641a650d1eSmrgrmprog=${RMPROG-rm}
651a650d1eSmrgstripprog=${STRIPPROG-strip}
661a650d1eSmrg
671a650d1eSmrgposix_mkdir=
681a650d1eSmrg
691a650d1eSmrg# Desired mode of installed file.
701a650d1eSmrgmode=0755
711a650d1eSmrg
7299c4c48aSmrg# Create dirs (including intermediate dirs) using mode 755.
7399c4c48aSmrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7499c4c48aSmrgmkdir_umask=22
7599c4c48aSmrg
7699c4c48aSmrgbackupsuffix=
771a650d1eSmrgchgrpcmd=
781a650d1eSmrgchmodcmd=$chmodprog
791a650d1eSmrgchowncmd=
801a650d1eSmrgmvcmd=$mvprog
811a650d1eSmrgrmcmd="$rmprog -f"
821a650d1eSmrgstripcmd=
838108eb18Smrg
841a650d1eSmrgsrc=
851a650d1eSmrgdst=
861a650d1eSmrgdir_arg=
871a650d1eSmrgdst_arg=
888108eb18Smrg
891a650d1eSmrgcopy_on_change=false
908a355152Smrgis_target_a_directory=possibly
918108eb18Smrg
921a650d1eSmrgusage="\
931a650d1eSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
941a650d1eSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
951a650d1eSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
961a650d1eSmrg   or: $0 [OPTION]... -d DIRECTORIES...
978108eb18Smrg
981a650d1eSmrgIn the 1st form, copy SRCFILE to DSTFILE.
991a650d1eSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1001a650d1eSmrgIn the 4th, create DIRECTORIES.
1018108eb18Smrg
1021a650d1eSmrgOptions:
1031a650d1eSmrg     --help     display this help and exit.
1041a650d1eSmrg     --version  display version info and exit.
1058108eb18Smrg
1061a650d1eSmrg  -c            (ignored)
10799c4c48aSmrg  -C            install only if different (preserve data modification time)
1081a650d1eSmrg  -d            create directories instead of installing files.
1091a650d1eSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1101a650d1eSmrg  -m MODE       $chmodprog installed files to MODE.
1111a650d1eSmrg  -o USER       $chownprog installed files to USER.
11299c4c48aSmrg  -p            pass -p to $cpprog.
1131a650d1eSmrg  -s            $stripprog installed files.
11499c4c48aSmrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1151a650d1eSmrg  -t DIRECTORY  install into DIRECTORY.
1161a650d1eSmrg  -T            report an error if DSTFILE is a directory.
1178108eb18Smrg
1181a650d1eSmrgEnvironment variables override the default commands:
1191a650d1eSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1201a650d1eSmrg  RMPROG STRIPPROG
12199c4c48aSmrg
12299c4c48aSmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12399c4c48aSmrgit's up to you to specify -f if you want it.
12499c4c48aSmrg
12599c4c48aSmrgIf -S is not specified, no backups are attempted.
12699c4c48aSmrg
12799c4c48aSmrgEmail bug reports to bug-automake@gnu.org.
12899c4c48aSmrgAutomake home page: https://www.gnu.org/software/automake/
1291a650d1eSmrg"
1308108eb18Smrg
1311a650d1eSmrgwhile test $# -ne 0; do
1321a650d1eSmrg  case $1 in
1331a650d1eSmrg    -c) ;;
1348108eb18Smrg
1351a650d1eSmrg    -C) copy_on_change=true;;
1368108eb18Smrg
1371a650d1eSmrg    -d) dir_arg=true;;
1388108eb18Smrg
1391a650d1eSmrg    -g) chgrpcmd="$chgrpprog $2"
1408a355152Smrg        shift;;
1418108eb18Smrg
1421a650d1eSmrg    --help) echo "$usage"; exit $?;;
1438108eb18Smrg
1441a650d1eSmrg    -m) mode=$2
1458a355152Smrg        case $mode in
1468a355152Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1478a355152Smrg            echo "$0: invalid mode: $mode" >&2
1488a355152Smrg            exit 1;;
1498a355152Smrg        esac
1508a355152Smrg        shift;;
1518108eb18Smrg
1521a650d1eSmrg    -o) chowncmd="$chownprog $2"
1538a355152Smrg        shift;;
1548108eb18Smrg
15599c4c48aSmrg    -p) cpprog="$cpprog -p";;
15699c4c48aSmrg
1571a650d1eSmrg    -s) stripcmd=$stripprog;;
1588108eb18Smrg
15999c4c48aSmrg    -S) backupsuffix="$2"
16099c4c48aSmrg        shift;;
16199c4c48aSmrg
1628a355152Smrg    -t)
1638a355152Smrg        is_target_a_directory=always
1648a355152Smrg        dst_arg=$2
1658a355152Smrg        # Protect names problematic for 'test' and other utilities.
1668a355152Smrg        case $dst_arg in
1678a355152Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1688a355152Smrg        esac
1698a355152Smrg        shift;;
1708108eb18Smrg
1718a355152Smrg    -T) is_target_a_directory=never;;
1728108eb18Smrg
1731a650d1eSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1748108eb18Smrg
1758a355152Smrg    --) shift
1768a355152Smrg        break;;
1778108eb18Smrg
1788a355152Smrg    -*) echo "$0: invalid option: $1" >&2
1798a355152Smrg        exit 1;;
1808108eb18Smrg
1811a650d1eSmrg    *)  break;;
1821a650d1eSmrg  esac
1831a650d1eSmrg  shift
1841a650d1eSmrgdone
1858108eb18Smrg
1868a355152Smrg# We allow the use of options -d and -T together, by making -d
1878a355152Smrg# take the precedence; this is for compatibility with GNU install.
1888a355152Smrg
1898a355152Smrgif test -n "$dir_arg"; then
1908a355152Smrg  if test -n "$dst_arg"; then
1918a355152Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1928a355152Smrg    exit 1
1938a355152Smrg  fi
1948a355152Smrgfi
1958a355152Smrg
1961a650d1eSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1971a650d1eSmrg  # When -d is used, all remaining arguments are directories to create.
1981a650d1eSmrg  # When -t is used, the destination is already specified.
1991a650d1eSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2001a650d1eSmrg  for arg
2011a650d1eSmrg  do
2021a650d1eSmrg    if test -n "$dst_arg"; then
2031a650d1eSmrg      # $@ is not empty: it contains at least $arg.
2041a650d1eSmrg      set fnord "$@" "$dst_arg"
2051a650d1eSmrg      shift # fnord
2061a650d1eSmrg    fi
2071a650d1eSmrg    shift # arg
2081a650d1eSmrg    dst_arg=$arg
2098a355152Smrg    # Protect names problematic for 'test' and other utilities.
2105977a007Smrg    case $dst_arg in
2115977a007Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
2125977a007Smrg    esac
2131a650d1eSmrg  done
2141a650d1eSmrgfi
2158108eb18Smrg
2161a650d1eSmrgif test $# -eq 0; then
2171a650d1eSmrg  if test -z "$dir_arg"; then
2181a650d1eSmrg    echo "$0: no input file specified." >&2
2191a650d1eSmrg    exit 1
2201a650d1eSmrg  fi
2218a355152Smrg  # It's OK to call 'install-sh -d' without argument.
2221a650d1eSmrg  # This can happen when creating conditional directories.
2231a650d1eSmrg  exit 0
2241a650d1eSmrgfi
2258108eb18Smrg
2268a355152Smrgif test -z "$dir_arg"; then
2278a355152Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2288a355152Smrg    if test ! -d "$dst_arg"; then
2298a355152Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2308a355152Smrg      exit 1
2318a355152Smrg    fi
2328a355152Smrg  fi
2338a355152Smrgfi
2348a355152Smrg
2351a650d1eSmrgif test -z "$dir_arg"; then
2365977a007Smrg  do_exit='(exit $ret); exit $ret'
2375977a007Smrg  trap "ret=129; $do_exit" 1
2385977a007Smrg  trap "ret=130; $do_exit" 2
2395977a007Smrg  trap "ret=141; $do_exit" 13
2405977a007Smrg  trap "ret=143; $do_exit" 15
2411a650d1eSmrg
2421a650d1eSmrg  # Set umask so as not to create temps with too-generous modes.
2431a650d1eSmrg  # However, 'strip' requires both read and write access to temps.
2441a650d1eSmrg  case $mode in
2451a650d1eSmrg    # Optimize common cases.
2461a650d1eSmrg    *644) cp_umask=133;;
2471a650d1eSmrg    *755) cp_umask=22;;
2481a650d1eSmrg
2491a650d1eSmrg    *[0-7])
2501a650d1eSmrg      if test -z "$stripcmd"; then
2518a355152Smrg        u_plus_rw=
2521a650d1eSmrg      else
2538a355152Smrg        u_plus_rw='% 200'
2541a650d1eSmrg      fi
2551a650d1eSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2561a650d1eSmrg    *)
2571a650d1eSmrg      if test -z "$stripcmd"; then
2588a355152Smrg        u_plus_rw=
2591a650d1eSmrg      else
2608a355152Smrg        u_plus_rw=,u+rw
2611a650d1eSmrg      fi
2621a650d1eSmrg      cp_umask=$mode$u_plus_rw;;
2631a650d1eSmrg  esac
2641a650d1eSmrgfi
2658108eb18Smrg
2661a650d1eSmrgfor src
2671a650d1eSmrgdo
2688a355152Smrg  # Protect names problematic for 'test' and other utilities.
2691a650d1eSmrg  case $src in
2705977a007Smrg    -* | [=\(\)!]) src=./$src;;
2711a650d1eSmrg  esac
2721a650d1eSmrg
2731a650d1eSmrg  if test -n "$dir_arg"; then
2741a650d1eSmrg    dst=$src
2751a650d1eSmrg    dstdir=$dst
2761a650d1eSmrg    test -d "$dstdir"
2771a650d1eSmrg    dstdir_status=$?
27899c4c48aSmrg    # Don't chown directories that already exist.
27999c4c48aSmrg    if test $dstdir_status = 0; then
28099c4c48aSmrg      chowncmd=""
28199c4c48aSmrg    fi
2821a650d1eSmrg  else
2831a650d1eSmrg
2841a650d1eSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2851a650d1eSmrg    # might cause directories to be created, which would be especially bad
2861a650d1eSmrg    # if $src (and thus $dsttmp) contains '*'.
2871a650d1eSmrg    if test ! -f "$src" && test ! -d "$src"; then
2881a650d1eSmrg      echo "$0: $src does not exist." >&2
2891a650d1eSmrg      exit 1
2901a650d1eSmrg    fi
2911a650d1eSmrg
2921a650d1eSmrg    if test -z "$dst_arg"; then
2931a650d1eSmrg      echo "$0: no destination specified." >&2
2941a650d1eSmrg      exit 1
2951a650d1eSmrg    fi
2961a650d1eSmrg    dst=$dst_arg
2978108eb18Smrg
29899c4c48aSmrg    # If destination is a directory, append the input filename.
2991a650d1eSmrg    if test -d "$dst"; then
3008a355152Smrg      if test "$is_target_a_directory" = never; then
3018a355152Smrg        echo "$0: $dst_arg: Is a directory" >&2
3028a355152Smrg        exit 1
3031a650d1eSmrg      fi
3041a650d1eSmrg      dstdir=$dst
30599c4c48aSmrg      dstbase=`basename "$src"`
30699c4c48aSmrg      case $dst in
30799c4c48aSmrg	*/) dst=$dst$dstbase;;
30899c4c48aSmrg	*)  dst=$dst/$dstbase;;
30999c4c48aSmrg      esac
3101a650d1eSmrg      dstdir_status=0
3111a650d1eSmrg    else
3128a355152Smrg      dstdir=`dirname "$dst"`
3131a650d1eSmrg      test -d "$dstdir"
3141a650d1eSmrg      dstdir_status=$?
3151a650d1eSmrg    fi
3161a650d1eSmrg  fi
3171a650d1eSmrg
31899c4c48aSmrg  case $dstdir in
31999c4c48aSmrg    */) dstdirslash=$dstdir;;
32099c4c48aSmrg    *)  dstdirslash=$dstdir/;;
32199c4c48aSmrg  esac
32299c4c48aSmrg
3231a650d1eSmrg  obsolete_mkdir_used=false
3241a650d1eSmrg
3251a650d1eSmrg  if test $dstdir_status != 0; then
3261a650d1eSmrg    case $posix_mkdir in
3271a650d1eSmrg      '')
3288a355152Smrg        # With -d, create the new directory with the user-specified mode.
3298a355152Smrg        # Otherwise, rely on $mkdir_umask.
3308a355152Smrg        if test -n "$dir_arg"; then
3318a355152Smrg          mkdir_mode=-m$mode
3328a355152Smrg        else
3338a355152Smrg          mkdir_mode=
3348a355152Smrg        fi
3358a355152Smrg
3368a355152Smrg        posix_mkdir=false
33799c4c48aSmrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33899c4c48aSmrg	# here however when possible just to lower collision chance.
33999c4c48aSmrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34099c4c48aSmrg
34199c4c48aSmrg	trap '
34299c4c48aSmrg	  ret=$?
34399c4c48aSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34499c4c48aSmrg	  exit $ret
34599c4c48aSmrg	' 0
34699c4c48aSmrg
34799c4c48aSmrg	# Because "mkdir -p" follows existing symlinks and we likely work
34899c4c48aSmrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34999c4c48aSmrg	# directory is successfully created first before we actually test
35099c4c48aSmrg	# 'mkdir -p'.
35199c4c48aSmrg	if (umask $mkdir_umask &&
35299c4c48aSmrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35399c4c48aSmrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35499c4c48aSmrg	then
35599c4c48aSmrg	  if test -z "$dir_arg" || {
35699c4c48aSmrg	       # Check for POSIX incompatibilities with -m.
35799c4c48aSmrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35899c4c48aSmrg	       # other-writable bit of parent directory when it shouldn't.
35999c4c48aSmrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36099c4c48aSmrg	       test_tmpdir="$tmpdir/a"
36199c4c48aSmrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36299c4c48aSmrg	       case $ls_ld_tmpdir in
36399c4c48aSmrg		 d????-?r-*) different_mode=700;;
36499c4c48aSmrg		 d????-?--*) different_mode=755;;
36599c4c48aSmrg		 *) false;;
36699c4c48aSmrg	       esac &&
36799c4c48aSmrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36899c4c48aSmrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36999c4c48aSmrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37099c4c48aSmrg	       }
37199c4c48aSmrg	     }
37299c4c48aSmrg	  then posix_mkdir=:
37399c4c48aSmrg	  fi
37499c4c48aSmrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37599c4c48aSmrg	else
37699c4c48aSmrg	  # Remove any dirs left behind by ancient mkdir implementations.
37799c4c48aSmrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37899c4c48aSmrg	fi
37999c4c48aSmrg	trap '' 0;;
3801a650d1eSmrg    esac
3818108eb18Smrg
3821a650d1eSmrg    if
3831a650d1eSmrg      $posix_mkdir && (
3848a355152Smrg        umask $mkdir_umask &&
3858a355152Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3861a650d1eSmrg      )
3871a650d1eSmrg    then :
3881a650d1eSmrg    else
3891a650d1eSmrg
39099c4c48aSmrg      # mkdir does not conform to POSIX,
3911a650d1eSmrg      # or it failed possibly due to a race condition.  Create the
3921a650d1eSmrg      # directory the slow way, step by step, checking for races as we go.
3931a650d1eSmrg
3941a650d1eSmrg      case $dstdir in
3958a355152Smrg        /*) prefix='/';;
3968a355152Smrg        [-=\(\)!]*) prefix='./';;
3978a355152Smrg        *)  prefix='';;
3981a650d1eSmrg      esac
3991a650d1eSmrg
4001a650d1eSmrg      oIFS=$IFS
4011a650d1eSmrg      IFS=/
4028a355152Smrg      set -f
4031a650d1eSmrg      set fnord $dstdir
4041a650d1eSmrg      shift
4058a355152Smrg      set +f
4061a650d1eSmrg      IFS=$oIFS
4071a650d1eSmrg
4081a650d1eSmrg      prefixes=
4091a650d1eSmrg
4101a650d1eSmrg      for d
4111a650d1eSmrg      do
4128a355152Smrg        test X"$d" = X && continue
4138a355152Smrg
4148a355152Smrg        prefix=$prefix$d
4158a355152Smrg        if test -d "$prefix"; then
4168a355152Smrg          prefixes=
4178a355152Smrg        else
4188a355152Smrg          if $posix_mkdir; then
41999c4c48aSmrg            (umask $mkdir_umask &&
4208a355152Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4218a355152Smrg            # Don't fail if two instances are running concurrently.
4228a355152Smrg            test -d "$prefix" || exit 1
4238a355152Smrg          else
4248a355152Smrg            case $prefix in
4258a355152Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4268a355152Smrg              *) qprefix=$prefix;;
4278a355152Smrg            esac
4288a355152Smrg            prefixes="$prefixes '$qprefix'"
4298a355152Smrg          fi
4308a355152Smrg        fi
4318a355152Smrg        prefix=$prefix/
4321a650d1eSmrg      done
4331a650d1eSmrg
4341a650d1eSmrg      if test -n "$prefixes"; then
4358a355152Smrg        # Don't fail if two instances are running concurrently.
4368a355152Smrg        (umask $mkdir_umask &&
4378a355152Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4388a355152Smrg          test -d "$dstdir" || exit 1
4398a355152Smrg        obsolete_mkdir_used=true
4401a650d1eSmrg      fi
4411a650d1eSmrg    fi
4421a650d1eSmrg  fi
4431a650d1eSmrg
4441a650d1eSmrg  if test -n "$dir_arg"; then
4451a650d1eSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4461a650d1eSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4471a650d1eSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4481a650d1eSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4491a650d1eSmrg  else
4501a650d1eSmrg
4511a650d1eSmrg    # Make a couple of temp file names in the proper directory.
45299c4c48aSmrg    dsttmp=${dstdirslash}_inst.$$_
45399c4c48aSmrg    rmtmp=${dstdirslash}_rm.$$_
4541a650d1eSmrg
4551a650d1eSmrg    # Trap to clean up those temp files at exit.
4561a650d1eSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4571a650d1eSmrg
4581a650d1eSmrg    # Copy the file name to the temp name.
45999c4c48aSmrg    (umask $cp_umask &&
46099c4c48aSmrg     { test -z "$stripcmd" || {
46199c4c48aSmrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46299c4c48aSmrg	 # which would cause strip to fail.
46399c4c48aSmrg	 if test -z "$doit"; then
46499c4c48aSmrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46599c4c48aSmrg	 else
46699c4c48aSmrg	   $doit touch "$dsttmp"
46799c4c48aSmrg	 fi
46899c4c48aSmrg       }
46999c4c48aSmrg     } &&
47099c4c48aSmrg     $doit_exec $cpprog "$src" "$dsttmp") &&
4711a650d1eSmrg
4721a650d1eSmrg    # and set any options; do chmod last to preserve setuid bits.
4731a650d1eSmrg    #
4741a650d1eSmrg    # If any of these fail, we abort the whole thing.  If we want to
4751a650d1eSmrg    # ignore errors from any of these, just make sure not to ignore
4761a650d1eSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4771a650d1eSmrg    #
4781a650d1eSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4791a650d1eSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4801a650d1eSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4811a650d1eSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4821a650d1eSmrg
4831a650d1eSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4841a650d1eSmrg    if $copy_on_change &&
4858a355152Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4868a355152Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4878a355152Smrg       set -f &&
4881a650d1eSmrg       set X $old && old=:$2:$4:$5:$6 &&
4891a650d1eSmrg       set X $new && new=:$2:$4:$5:$6 &&
4908a355152Smrg       set +f &&
4911a650d1eSmrg       test "$old" = "$new" &&
4921a650d1eSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4931a650d1eSmrg    then
4941a650d1eSmrg      rm -f "$dsttmp"
4951a650d1eSmrg    else
49699c4c48aSmrg      # If $backupsuffix is set, and the file being installed
49799c4c48aSmrg      # already exists, attempt a backup.  Don't worry if it fails,
49899c4c48aSmrg      # e.g., if mv doesn't support -f.
49999c4c48aSmrg      if test -n "$backupsuffix" && test -f "$dst"; then
50099c4c48aSmrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50199c4c48aSmrg      fi
50299c4c48aSmrg
5031a650d1eSmrg      # Rename the file to the real destination.
5041a650d1eSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5051a650d1eSmrg
5061a650d1eSmrg      # The rename failed, perhaps because mv can't rename something else
5071a650d1eSmrg      # to itself, or perhaps because mv is so ancient that it does not
5081a650d1eSmrg      # support -f.
5091a650d1eSmrg      {
5108a355152Smrg        # Now remove or move aside any old file at destination location.
5118a355152Smrg        # We try this two ways since rm can't unlink itself on some
5128a355152Smrg        # systems and the destination file might be busy for other
5138a355152Smrg        # reasons.  In this case, the final cleanup might fail but the new
5148a355152Smrg        # file should still install successfully.
5158a355152Smrg        {
5168a355152Smrg          test ! -f "$dst" ||
51799c4c48aSmrg          $doit $rmcmd "$dst" 2>/dev/null ||
5188a355152Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51999c4c48aSmrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5208a355152Smrg          } ||
5218a355152Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5228a355152Smrg            (exit 1); exit 1
5238a355152Smrg          }
5248a355152Smrg        } &&
5258a355152Smrg
5268a355152Smrg        # Now rename the file to the real destination.
5278a355152Smrg        $doit $mvcmd "$dsttmp" "$dst"
5281a650d1eSmrg      }
5291a650d1eSmrg    fi || exit 1
5301a650d1eSmrg
5311a650d1eSmrg    trap '' 0
5321a650d1eSmrg  fi
5331a650d1eSmrgdone
5348108eb18Smrg
5351a650d1eSmrg# Local variables:
53699c4c48aSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
5371a650d1eSmrg# time-stamp-start: "scriptversion="
5381a650d1eSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53999c4c48aSmrg# time-stamp-time-zone: "UTC0"
5401a650d1eSmrg# time-stamp-end: "; # UTC"
5411a650d1eSmrg# End:
542