install-sh revision 44256261
1c06b6b69Smrg#!/bin/sh
2c06b6b69Smrg# install - install a program, script, or datafile
3c06b6b69Smrg
444256261Smrgscriptversion=2020-11-14.01; # UTC
5c06b6b69Smrg
6c06b6b69Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c06b6b69Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8c06b6b69Smrg# following copyright and license.
9c06b6b69Smrg#
10c06b6b69Smrg# Copyright (C) 1994 X Consortium
11c06b6b69Smrg#
12c06b6b69Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c06b6b69Smrg# of this software and associated documentation files (the "Software"), to
14c06b6b69Smrg# deal in the Software without restriction, including without limitation the
15c06b6b69Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c06b6b69Smrg# sell copies of the Software, and to permit persons to whom the Software is
17c06b6b69Smrg# furnished to do so, subject to the following conditions:
18c06b6b69Smrg#
19c06b6b69Smrg# The above copyright notice and this permission notice shall be included in
20c06b6b69Smrg# all copies or substantial portions of the Software.
21c06b6b69Smrg#
22c06b6b69Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c06b6b69Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c06b6b69Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c06b6b69Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c06b6b69Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c06b6b69Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c06b6b69Smrg#
29c06b6b69Smrg# Except as contained in this notice, the name of the X Consortium shall not
30c06b6b69Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c06b6b69Smrg# ings in this Software without prior written authorization from the X Consor-
32c06b6b69Smrg# tium.
33c06b6b69Smrg#
34c06b6b69Smrg#
35c06b6b69Smrg# FSF changes to this file are in the public domain.
36c06b6b69Smrg#
37c06b6b69Smrg# Calling this script install-sh is preferred over install.sh, to prevent
387a5ec427Smrg# 'make' implicit rules from creating a file called install from it
39c06b6b69Smrg# when there is no Makefile.
40c06b6b69Smrg#
41c06b6b69Smrg# This script is compatible with the BSD install script, but was written
429f4658d1Smrg# from scratch.
439f4658d1Smrg
447a5ec427Smrgtab='	'
459f4658d1Smrgnl='
469f4658d1Smrg'
477a5ec427SmrgIFS=" $tab$nl"
48c06b6b69Smrg
497a5ec427Smrg# Set DOITPROG to "echo" to test this script.
50c06b6b69Smrg
519f4658d1Smrgdoit=${DOITPROG-}
527a5ec427Smrgdoit_exec=${doit:-exec}
53c06b6b69Smrg
549f4658d1Smrg# Put in absolute file names if you don't have them in your path;
559f4658d1Smrg# or use environment vars.
569f4658d1Smrg
579f4658d1Smrgchgrpprog=${CHGRPPROG-chgrp}
589f4658d1Smrgchmodprog=${CHMODPROG-chmod}
599f4658d1Smrgchownprog=${CHOWNPROG-chown}
609f4658d1Smrgcmpprog=${CMPPROG-cmp}
619f4658d1Smrgcpprog=${CPPROG-cp}
629f4658d1Smrgmkdirprog=${MKDIRPROG-mkdir}
639f4658d1Smrgmvprog=${MVPROG-mv}
649f4658d1Smrgrmprog=${RMPROG-rm}
659f4658d1Smrgstripprog=${STRIPPROG-strip}
669f4658d1Smrg
679f4658d1Smrgposix_mkdir=
689f4658d1Smrg
699f4658d1Smrg# Desired mode of installed file.
709f4658d1Smrgmode=0755
71c06b6b69Smrg
7244256261Smrg# Create dirs (including intermediate dirs) using mode 755.
7344256261Smrg# This is like GNU 'install' as of coreutils 8.32 (2020).
7444256261Smrgmkdir_umask=22
7544256261Smrg
7644256261Smrgbackupsuffix=
77c06b6b69Smrgchgrpcmd=
789f4658d1Smrgchmodcmd=$chmodprog
799f4658d1Smrgchowncmd=
809f4658d1Smrgmvcmd=$mvprog
81c06b6b69Smrgrmcmd="$rmprog -f"
829f4658d1Smrgstripcmd=
839f4658d1Smrg
84c06b6b69Smrgsrc=
85c06b6b69Smrgdst=
86c06b6b69Smrgdir_arg=
879f4658d1Smrgdst_arg=
889f4658d1Smrg
899f4658d1Smrgcopy_on_change=false
907a5ec427Smrgis_target_a_directory=possibly
91c06b6b69Smrg
929f4658d1Smrgusage="\
939f4658d1SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
94c06b6b69Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
95c06b6b69Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
96c06b6b69Smrg   or: $0 [OPTION]... -d DIRECTORIES...
97c06b6b69Smrg
98c06b6b69SmrgIn the 1st form, copy SRCFILE to DSTFILE.
99c06b6b69SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
100c06b6b69SmrgIn the 4th, create DIRECTORIES.
101c06b6b69Smrg
102c06b6b69SmrgOptions:
1039f4658d1Smrg     --help     display this help and exit.
1049f4658d1Smrg     --version  display version info and exit.
1059f4658d1Smrg
1069f4658d1Smrg  -c            (ignored)
10744256261Smrg  -C            install only if different (preserve data modification time)
1089f4658d1Smrg  -d            create directories instead of installing files.
1099f4658d1Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1109f4658d1Smrg  -m MODE       $chmodprog installed files to MODE.
1119f4658d1Smrg  -o USER       $chownprog installed files to USER.
11244256261Smrg  -p            pass -p to $cpprog.
1139f4658d1Smrg  -s            $stripprog installed files.
11444256261Smrg  -S SUFFIX     attempt to back up existing files, with suffix SUFFIX.
1159f4658d1Smrg  -t DIRECTORY  install into DIRECTORY.
1169f4658d1Smrg  -T            report an error if DSTFILE is a directory.
117c06b6b69Smrg
118c06b6b69SmrgEnvironment variables override the default commands:
1199f4658d1Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1209f4658d1Smrg  RMPROG STRIPPROG
12144256261Smrg
12244256261SmrgBy default, rm is invoked with -f; when overridden with RMPROG,
12344256261Smrgit's up to you to specify -f if you want it.
12444256261Smrg
12544256261SmrgIf -S is not specified, no backups are attempted.
12644256261Smrg
12744256261SmrgEmail bug reports to bug-automake@gnu.org.
12844256261SmrgAutomake home page: https://www.gnu.org/software/automake/
129c06b6b69Smrg"
130c06b6b69Smrg
1319f4658d1Smrgwhile test $# -ne 0; do
132c06b6b69Smrg  case $1 in
1339f4658d1Smrg    -c) ;;
1349f4658d1Smrg
1359f4658d1Smrg    -C) copy_on_change=true;;
136c06b6b69Smrg
1379f4658d1Smrg    -d) dir_arg=true;;
138c06b6b69Smrg
139c06b6b69Smrg    -g) chgrpcmd="$chgrpprog $2"
1407a5ec427Smrg        shift;;
141c06b6b69Smrg
142c06b6b69Smrg    --help) echo "$usage"; exit $?;;
143c06b6b69Smrg
1449f4658d1Smrg    -m) mode=$2
1457a5ec427Smrg        case $mode in
1467a5ec427Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1477a5ec427Smrg            echo "$0: invalid mode: $mode" >&2
1487a5ec427Smrg            exit 1;;
1497a5ec427Smrg        esac
1507a5ec427Smrg        shift;;
151c06b6b69Smrg
152c06b6b69Smrg    -o) chowncmd="$chownprog $2"
1537a5ec427Smrg        shift;;
154c06b6b69Smrg
15544256261Smrg    -p) cpprog="$cpprog -p";;
15644256261Smrg
1579f4658d1Smrg    -s) stripcmd=$stripprog;;
158c06b6b69Smrg
15944256261Smrg    -S) backupsuffix="$2"
16044256261Smrg        shift;;
16144256261Smrg
1627a5ec427Smrg    -t)
1637a5ec427Smrg        is_target_a_directory=always
1647a5ec427Smrg        dst_arg=$2
1657a5ec427Smrg        # Protect names problematic for 'test' and other utilities.
1667a5ec427Smrg        case $dst_arg in
1677a5ec427Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1687a5ec427Smrg        esac
1697a5ec427Smrg        shift;;
170c06b6b69Smrg
1717a5ec427Smrg    -T) is_target_a_directory=never;;
172c06b6b69Smrg
173c06b6b69Smrg    --version) echo "$0 $scriptversion"; exit $?;;
174c06b6b69Smrg
1757a5ec427Smrg    --) shift
1767a5ec427Smrg        break;;
1779f4658d1Smrg
1787a5ec427Smrg    -*) echo "$0: invalid option: $1" >&2
1797a5ec427Smrg        exit 1;;
1809f4658d1Smrg
1819f4658d1Smrg    *)  break;;
182c06b6b69Smrg  esac
1839f4658d1Smrg  shift
184c06b6b69Smrgdone
185c06b6b69Smrg
1867a5ec427Smrg# We allow the use of options -d and -T together, by making -d
1877a5ec427Smrg# take the precedence; this is for compatibility with GNU install.
1887a5ec427Smrg
1897a5ec427Smrgif test -n "$dir_arg"; then
1907a5ec427Smrg  if test -n "$dst_arg"; then
1917a5ec427Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1927a5ec427Smrg    exit 1
1937a5ec427Smrg  fi
1947a5ec427Smrgfi
1957a5ec427Smrg
1969f4658d1Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1979f4658d1Smrg  # When -d is used, all remaining arguments are directories to create.
1989f4658d1Smrg  # When -t is used, the destination is already specified.
1999f4658d1Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
2009f4658d1Smrg  for arg
2019f4658d1Smrg  do
2029f4658d1Smrg    if test -n "$dst_arg"; then
2039f4658d1Smrg      # $@ is not empty: it contains at least $arg.
2049f4658d1Smrg      set fnord "$@" "$dst_arg"
2059f4658d1Smrg      shift # fnord
2069f4658d1Smrg    fi
2079f4658d1Smrg    shift # arg
2089f4658d1Smrg    dst_arg=$arg
2097a5ec427Smrg    # Protect names problematic for 'test' and other utilities.
210a1d73b4bSmrg    case $dst_arg in
211a1d73b4bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
212a1d73b4bSmrg    esac
2139f4658d1Smrg  done
2149f4658d1Smrgfi
2159f4658d1Smrg
2169f4658d1Smrgif test $# -eq 0; then
217c06b6b69Smrg  if test -z "$dir_arg"; then
218c06b6b69Smrg    echo "$0: no input file specified." >&2
219c06b6b69Smrg    exit 1
220c06b6b69Smrg  fi
2217a5ec427Smrg  # It's OK to call 'install-sh -d' without argument.
222c06b6b69Smrg  # This can happen when creating conditional directories.
223c06b6b69Smrg  exit 0
224c06b6b69Smrgfi
225c06b6b69Smrg
2267a5ec427Smrgif test -z "$dir_arg"; then
2277a5ec427Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2287a5ec427Smrg    if test ! -d "$dst_arg"; then
2297a5ec427Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2307a5ec427Smrg      exit 1
2317a5ec427Smrg    fi
2327a5ec427Smrg  fi
2337a5ec427Smrgfi
2347a5ec427Smrg
2359f4658d1Smrgif test -z "$dir_arg"; then
236a1d73b4bSmrg  do_exit='(exit $ret); exit $ret'
237a1d73b4bSmrg  trap "ret=129; $do_exit" 1
238a1d73b4bSmrg  trap "ret=130; $do_exit" 2
239a1d73b4bSmrg  trap "ret=141; $do_exit" 13
240a1d73b4bSmrg  trap "ret=143; $do_exit" 15
2419f4658d1Smrg
2429f4658d1Smrg  # Set umask so as not to create temps with too-generous modes.
2439f4658d1Smrg  # However, 'strip' requires both read and write access to temps.
2449f4658d1Smrg  case $mode in
2459f4658d1Smrg    # Optimize common cases.
2469f4658d1Smrg    *644) cp_umask=133;;
2479f4658d1Smrg    *755) cp_umask=22;;
2489f4658d1Smrg
2499f4658d1Smrg    *[0-7])
2509f4658d1Smrg      if test -z "$stripcmd"; then
2517a5ec427Smrg        u_plus_rw=
2529f4658d1Smrg      else
2537a5ec427Smrg        u_plus_rw='% 200'
2549f4658d1Smrg      fi
2559f4658d1Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2569f4658d1Smrg    *)
2579f4658d1Smrg      if test -z "$stripcmd"; then
2587a5ec427Smrg        u_plus_rw=
2599f4658d1Smrg      else
2607a5ec427Smrg        u_plus_rw=,u+rw
2619f4658d1Smrg      fi
2629f4658d1Smrg      cp_umask=$mode$u_plus_rw;;
2639f4658d1Smrg  esac
2649f4658d1Smrgfi
2659f4658d1Smrg
266c06b6b69Smrgfor src
267c06b6b69Smrgdo
2687a5ec427Smrg  # Protect names problematic for 'test' and other utilities.
269c06b6b69Smrg  case $src in
270a1d73b4bSmrg    -* | [=\(\)!]) src=./$src;;
271c06b6b69Smrg  esac
272c06b6b69Smrg
273c06b6b69Smrg  if test -n "$dir_arg"; then
274c06b6b69Smrg    dst=$src
2759f4658d1Smrg    dstdir=$dst
2769f4658d1Smrg    test -d "$dstdir"
2779f4658d1Smrg    dstdir_status=$?
27844256261Smrg    # Don't chown directories that already exist.
27944256261Smrg    if test $dstdir_status = 0; then
28044256261Smrg      chowncmd=""
28144256261Smrg    fi
282c06b6b69Smrg  else
2839f4658d1Smrg
284c06b6b69Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
285c06b6b69Smrg    # might cause directories to be created, which would be especially bad
286c06b6b69Smrg    # if $src (and thus $dsttmp) contains '*'.
287c06b6b69Smrg    if test ! -f "$src" && test ! -d "$src"; then
288c06b6b69Smrg      echo "$0: $src does not exist." >&2
289c06b6b69Smrg      exit 1
290c06b6b69Smrg    fi
291c06b6b69Smrg
2929f4658d1Smrg    if test -z "$dst_arg"; then
293c06b6b69Smrg      echo "$0: no destination specified." >&2
294c06b6b69Smrg      exit 1
295c06b6b69Smrg    fi
2969f4658d1Smrg    dst=$dst_arg
297c06b6b69Smrg
29844256261Smrg    # If destination is a directory, append the input filename.
299c06b6b69Smrg    if test -d "$dst"; then
3007a5ec427Smrg      if test "$is_target_a_directory" = never; then
3017a5ec427Smrg        echo "$0: $dst_arg: Is a directory" >&2
3027a5ec427Smrg        exit 1
303c06b6b69Smrg      fi
3049f4658d1Smrg      dstdir=$dst
30544256261Smrg      dstbase=`basename "$src"`
30644256261Smrg      case $dst in
30744256261Smrg	*/) dst=$dst$dstbase;;
30844256261Smrg	*)  dst=$dst/$dstbase;;
30944256261Smrg      esac
3109f4658d1Smrg      dstdir_status=0
3119f4658d1Smrg    else
3127a5ec427Smrg      dstdir=`dirname "$dst"`
3139f4658d1Smrg      test -d "$dstdir"
3149f4658d1Smrg      dstdir_status=$?
315c06b6b69Smrg    fi
316c06b6b69Smrg  fi
317c06b6b69Smrg
31844256261Smrg  case $dstdir in
31944256261Smrg    */) dstdirslash=$dstdir;;
32044256261Smrg    *)  dstdirslash=$dstdir/;;
32144256261Smrg  esac
32244256261Smrg
3239f4658d1Smrg  obsolete_mkdir_used=false
3249f4658d1Smrg
3259f4658d1Smrg  if test $dstdir_status != 0; then
3269f4658d1Smrg    case $posix_mkdir in
3279f4658d1Smrg      '')
3287a5ec427Smrg        # With -d, create the new directory with the user-specified mode.
3297a5ec427Smrg        # Otherwise, rely on $mkdir_umask.
3307a5ec427Smrg        if test -n "$dir_arg"; then
3317a5ec427Smrg          mkdir_mode=-m$mode
3327a5ec427Smrg        else
3337a5ec427Smrg          mkdir_mode=
3347a5ec427Smrg        fi
3357a5ec427Smrg
3367a5ec427Smrg        posix_mkdir=false
33744256261Smrg	# The $RANDOM variable is not portable (e.g., dash).  Use it
33844256261Smrg	# here however when possible just to lower collision chance.
33944256261Smrg	tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34044256261Smrg
34144256261Smrg	trap '
34244256261Smrg	  ret=$?
34344256261Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null
34444256261Smrg	  exit $ret
34544256261Smrg	' 0
34644256261Smrg
34744256261Smrg	# Because "mkdir -p" follows existing symlinks and we likely work
34844256261Smrg	# directly in world-writeable /tmp, make sure that the '$tmpdir'
34944256261Smrg	# directory is successfully created first before we actually test
35044256261Smrg	# 'mkdir -p'.
35144256261Smrg	if (umask $mkdir_umask &&
35244256261Smrg	    $mkdirprog $mkdir_mode "$tmpdir" &&
35344256261Smrg	    exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
35444256261Smrg	then
35544256261Smrg	  if test -z "$dir_arg" || {
35644256261Smrg	       # Check for POSIX incompatibilities with -m.
35744256261Smrg	       # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35844256261Smrg	       # other-writable bit of parent directory when it shouldn't.
35944256261Smrg	       # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
36044256261Smrg	       test_tmpdir="$tmpdir/a"
36144256261Smrg	       ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
36244256261Smrg	       case $ls_ld_tmpdir in
36344256261Smrg		 d????-?r-*) different_mode=700;;
36444256261Smrg		 d????-?--*) different_mode=755;;
36544256261Smrg		 *) false;;
36644256261Smrg	       esac &&
36744256261Smrg	       $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
36844256261Smrg		 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36944256261Smrg		 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
37044256261Smrg	       }
37144256261Smrg	     }
37244256261Smrg	  then posix_mkdir=:
37344256261Smrg	  fi
37444256261Smrg	  rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
37544256261Smrg	else
37644256261Smrg	  # Remove any dirs left behind by ancient mkdir implementations.
37744256261Smrg	  rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37844256261Smrg	fi
37944256261Smrg	trap '' 0;;
3809f4658d1Smrg    esac
381c06b6b69Smrg
3829f4658d1Smrg    if
3839f4658d1Smrg      $posix_mkdir && (
3847a5ec427Smrg        umask $mkdir_umask &&
3857a5ec427Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3869f4658d1Smrg      )
3879f4658d1Smrg    then :
3889f4658d1Smrg    else
389c06b6b69Smrg
39044256261Smrg      # mkdir does not conform to POSIX,
3919f4658d1Smrg      # or it failed possibly due to a race condition.  Create the
3929f4658d1Smrg      # directory the slow way, step by step, checking for races as we go.
393c06b6b69Smrg
3949f4658d1Smrg      case $dstdir in
3957a5ec427Smrg        /*) prefix='/';;
3967a5ec427Smrg        [-=\(\)!]*) prefix='./';;
3977a5ec427Smrg        *)  prefix='';;
3989f4658d1Smrg      esac
399c06b6b69Smrg
4009f4658d1Smrg      oIFS=$IFS
4019f4658d1Smrg      IFS=/
4027a5ec427Smrg      set -f
4039f4658d1Smrg      set fnord $dstdir
404c06b6b69Smrg      shift
4057a5ec427Smrg      set +f
4069f4658d1Smrg      IFS=$oIFS
4079f4658d1Smrg
4089f4658d1Smrg      prefixes=
4099f4658d1Smrg
4109f4658d1Smrg      for d
4119f4658d1Smrg      do
4127a5ec427Smrg        test X"$d" = X && continue
4137a5ec427Smrg
4147a5ec427Smrg        prefix=$prefix$d
4157a5ec427Smrg        if test -d "$prefix"; then
4167a5ec427Smrg          prefixes=
4177a5ec427Smrg        else
4187a5ec427Smrg          if $posix_mkdir; then
41944256261Smrg            (umask $mkdir_umask &&
4207a5ec427Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4217a5ec427Smrg            # Don't fail if two instances are running concurrently.
4227a5ec427Smrg            test -d "$prefix" || exit 1
4237a5ec427Smrg          else
4247a5ec427Smrg            case $prefix in
4257a5ec427Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4267a5ec427Smrg              *) qprefix=$prefix;;
4277a5ec427Smrg            esac
4287a5ec427Smrg            prefixes="$prefixes '$qprefix'"
4297a5ec427Smrg          fi
4307a5ec427Smrg        fi
4317a5ec427Smrg        prefix=$prefix/
4329f4658d1Smrg      done
4339f4658d1Smrg
4349f4658d1Smrg      if test -n "$prefixes"; then
4357a5ec427Smrg        # Don't fail if two instances are running concurrently.
4367a5ec427Smrg        (umask $mkdir_umask &&
4377a5ec427Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4387a5ec427Smrg          test -d "$dstdir" || exit 1
4397a5ec427Smrg        obsolete_mkdir_used=true
440c06b6b69Smrg      fi
4419f4658d1Smrg    fi
442c06b6b69Smrg  fi
443c06b6b69Smrg
444c06b6b69Smrg  if test -n "$dir_arg"; then
4459f4658d1Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4469f4658d1Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4479f4658d1Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4489f4658d1Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
449c06b6b69Smrg  else
450c06b6b69Smrg
451c06b6b69Smrg    # Make a couple of temp file names in the proper directory.
45244256261Smrg    dsttmp=${dstdirslash}_inst.$$_
45344256261Smrg    rmtmp=${dstdirslash}_rm.$$_
454c06b6b69Smrg
455c06b6b69Smrg    # Trap to clean up those temp files at exit.
456c06b6b69Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
457c06b6b69Smrg
458c06b6b69Smrg    # Copy the file name to the temp name.
45944256261Smrg    (umask $cp_umask &&
46044256261Smrg     { test -z "$stripcmd" || {
46144256261Smrg	 # Create $dsttmp read-write so that cp doesn't create it read-only,
46244256261Smrg	 # which would cause strip to fail.
46344256261Smrg	 if test -z "$doit"; then
46444256261Smrg	   : >"$dsttmp" # No need to fork-exec 'touch'.
46544256261Smrg	 else
46644256261Smrg	   $doit touch "$dsttmp"
46744256261Smrg	 fi
46844256261Smrg       }
46944256261Smrg     } &&
47044256261Smrg     $doit_exec $cpprog "$src" "$dsttmp") &&
471c06b6b69Smrg
472c06b6b69Smrg    # and set any options; do chmod last to preserve setuid bits.
473c06b6b69Smrg    #
474c06b6b69Smrg    # If any of these fail, we abort the whole thing.  If we want to
475c06b6b69Smrg    # ignore errors from any of these, just make sure not to ignore
476c06b6b69Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
477c06b6b69Smrg    #
4789f4658d1Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4799f4658d1Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4809f4658d1Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4819f4658d1Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4829f4658d1Smrg
4839f4658d1Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4849f4658d1Smrg    if $copy_on_change &&
4857a5ec427Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4867a5ec427Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4877a5ec427Smrg       set -f &&
4889f4658d1Smrg       set X $old && old=:$2:$4:$5:$6 &&
4899f4658d1Smrg       set X $new && new=:$2:$4:$5:$6 &&
4907a5ec427Smrg       set +f &&
4919f4658d1Smrg       test "$old" = "$new" &&
4929f4658d1Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4939f4658d1Smrg    then
4949f4658d1Smrg      rm -f "$dsttmp"
4959f4658d1Smrg    else
49644256261Smrg      # If $backupsuffix is set, and the file being installed
49744256261Smrg      # already exists, attempt a backup.  Don't worry if it fails,
49844256261Smrg      # e.g., if mv doesn't support -f.
49944256261Smrg      if test -n "$backupsuffix" && test -f "$dst"; then
50044256261Smrg        $doit $mvcmd -f "$dst" "$dst$backupsuffix" 2>/dev/null
50144256261Smrg      fi
50244256261Smrg
5039f4658d1Smrg      # Rename the file to the real destination.
5049f4658d1Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
5059f4658d1Smrg
5069f4658d1Smrg      # The rename failed, perhaps because mv can't rename something else
5079f4658d1Smrg      # to itself, or perhaps because mv is so ancient that it does not
5089f4658d1Smrg      # support -f.
5099f4658d1Smrg      {
5107a5ec427Smrg        # Now remove or move aside any old file at destination location.
5117a5ec427Smrg        # We try this two ways since rm can't unlink itself on some
5127a5ec427Smrg        # systems and the destination file might be busy for other
5137a5ec427Smrg        # reasons.  In this case, the final cleanup might fail but the new
5147a5ec427Smrg        # file should still install successfully.
5157a5ec427Smrg        {
5167a5ec427Smrg          test ! -f "$dst" ||
51744256261Smrg          $doit $rmcmd "$dst" 2>/dev/null ||
5187a5ec427Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
51944256261Smrg            { $doit $rmcmd "$rmtmp" 2>/dev/null; :; }
5207a5ec427Smrg          } ||
5217a5ec427Smrg          { echo "$0: cannot unlink or rename $dst" >&2
5227a5ec427Smrg            (exit 1); exit 1
5237a5ec427Smrg          }
5247a5ec427Smrg        } &&
5257a5ec427Smrg
5267a5ec427Smrg        # Now rename the file to the real destination.
5277a5ec427Smrg        $doit $mvcmd "$dsttmp" "$dst"
5289f4658d1Smrg      }
5299f4658d1Smrg    fi || exit 1
5309f4658d1Smrg
5319f4658d1Smrg    trap '' 0
5329f4658d1Smrg  fi
533c06b6b69Smrgdone
534c06b6b69Smrg
535c06b6b69Smrg# Local variables:
53644256261Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
537c06b6b69Smrg# time-stamp-start: "scriptversion="
538c06b6b69Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
53944256261Smrg# time-stamp-time-zone: "UTC0"
5407d17d341Smrg# time-stamp-end: "; # UTC"
541c06b6b69Smrg# End:
542