install-sh revision a0c41156
145bc899bSmrg#!/bin/sh
245bc899bSmrg# install - install a program, script, or datafile
345bc899bSmrg
4a0c41156Smrgscriptversion=2018-03-11.20; # UTC
545bc899bSmrg
645bc899bSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
745bc899bSmrg# later released in X11R6 (xc/config/util/install.sh) with the
845bc899bSmrg# following copyright and license.
945bc899bSmrg#
1045bc899bSmrg# Copyright (C) 1994 X Consortium
1145bc899bSmrg#
1245bc899bSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1345bc899bSmrg# of this software and associated documentation files (the "Software"), to
1445bc899bSmrg# deal in the Software without restriction, including without limitation the
1545bc899bSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1645bc899bSmrg# sell copies of the Software, and to permit persons to whom the Software is
1745bc899bSmrg# furnished to do so, subject to the following conditions:
1845bc899bSmrg#
1945bc899bSmrg# The above copyright notice and this permission notice shall be included in
2045bc899bSmrg# all copies or substantial portions of the Software.
2145bc899bSmrg#
2245bc899bSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2345bc899bSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2445bc899bSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2545bc899bSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2645bc899bSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2745bc899bSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2845bc899bSmrg#
2945bc899bSmrg# Except as contained in this notice, the name of the X Consortium shall not
3045bc899bSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3145bc899bSmrg# ings in this Software without prior written authorization from the X Consor-
3245bc899bSmrg# tium.
3345bc899bSmrg#
3445bc899bSmrg#
3545bc899bSmrg# FSF changes to this file are in the public domain.
3645bc899bSmrg#
3745bc899bSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38c97b1c41Smrg# 'make' implicit rules from creating a file called install from it
3945bc899bSmrg# when there is no Makefile.
4045bc899bSmrg#
4145bc899bSmrg# This script is compatible with the BSD install script, but was written
4250f2e948Smrg# from scratch.
4350f2e948Smrg
4457ba8e8bSmrgtab='	'
4550f2e948Smrgnl='
4650f2e948Smrg'
4757ba8e8bSmrgIFS=" $tab$nl"
4845bc899bSmrg
4957ba8e8bSmrg# Set DOITPROG to "echo" to test this script.
5045bc899bSmrg
5150f2e948Smrgdoit=${DOITPROG-}
5257ba8e8bSmrgdoit_exec=${doit:-exec}
5345bc899bSmrg
5450f2e948Smrg# Put in absolute file names if you don't have them in your path;
5550f2e948Smrg# or use environment vars.
5650f2e948Smrg
5750f2e948Smrgchgrpprog=${CHGRPPROG-chgrp}
5850f2e948Smrgchmodprog=${CHMODPROG-chmod}
5950f2e948Smrgchownprog=${CHOWNPROG-chown}
6050f2e948Smrgcmpprog=${CMPPROG-cmp}
6150f2e948Smrgcpprog=${CPPROG-cp}
6250f2e948Smrgmkdirprog=${MKDIRPROG-mkdir}
6350f2e948Smrgmvprog=${MVPROG-mv}
6450f2e948Smrgrmprog=${RMPROG-rm}
6550f2e948Smrgstripprog=${STRIPPROG-strip}
6650f2e948Smrg
6750f2e948Smrgposix_mkdir=
6850f2e948Smrg
6950f2e948Smrg# Desired mode of installed file.
7050f2e948Smrgmode=0755
7145bc899bSmrg
72226fade8Smrgchgrpcmd=
7350f2e948Smrgchmodcmd=$chmodprog
7450f2e948Smrgchowncmd=
7550f2e948Smrgmvcmd=$mvprog
76226fade8Smrgrmcmd="$rmprog -f"
7750f2e948Smrgstripcmd=
7850f2e948Smrg
7945bc899bSmrgsrc=
8045bc899bSmrgdst=
8145bc899bSmrgdir_arg=
8250f2e948Smrgdst_arg=
8350f2e948Smrg
8450f2e948Smrgcopy_on_change=false
8557ba8e8bSmrgis_target_a_directory=possibly
8645bc899bSmrg
8750f2e948Smrgusage="\
8850f2e948SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8945bc899bSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9045bc899bSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9145bc899bSmrg   or: $0 [OPTION]... -d DIRECTORIES...
9245bc899bSmrg
9345bc899bSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9445bc899bSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9545bc899bSmrgIn the 4th, create DIRECTORIES.
9645bc899bSmrg
9745bc899bSmrgOptions:
9850f2e948Smrg     --help     display this help and exit.
9950f2e948Smrg     --version  display version info and exit.
10050f2e948Smrg
10150f2e948Smrg  -c            (ignored)
10250f2e948Smrg  -C            install only if different (preserve the last data modification time)
10350f2e948Smrg  -d            create directories instead of installing files.
10450f2e948Smrg  -g GROUP      $chgrpprog installed files to GROUP.
10550f2e948Smrg  -m MODE       $chmodprog installed files to MODE.
10650f2e948Smrg  -o USER       $chownprog installed files to USER.
10750f2e948Smrg  -s            $stripprog installed files.
10850f2e948Smrg  -t DIRECTORY  install into DIRECTORY.
10950f2e948Smrg  -T            report an error if DSTFILE is a directory.
11045bc899bSmrg
11145bc899bSmrgEnvironment variables override the default commands:
11250f2e948Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11350f2e948Smrg  RMPROG STRIPPROG
11445bc899bSmrg"
11545bc899bSmrg
11650f2e948Smrgwhile test $# -ne 0; do
11745bc899bSmrg  case $1 in
11850f2e948Smrg    -c) ;;
11950f2e948Smrg
12050f2e948Smrg    -C) copy_on_change=true;;
12145bc899bSmrg
12250f2e948Smrg    -d) dir_arg=true;;
12345bc899bSmrg
12445bc899bSmrg    -g) chgrpcmd="$chgrpprog $2"
12557ba8e8bSmrg        shift;;
12645bc899bSmrg
12745bc899bSmrg    --help) echo "$usage"; exit $?;;
12845bc899bSmrg
12950f2e948Smrg    -m) mode=$2
13057ba8e8bSmrg        case $mode in
13157ba8e8bSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13257ba8e8bSmrg            echo "$0: invalid mode: $mode" >&2
13357ba8e8bSmrg            exit 1;;
13457ba8e8bSmrg        esac
13557ba8e8bSmrg        shift;;
13645bc899bSmrg
13745bc899bSmrg    -o) chowncmd="$chownprog $2"
13857ba8e8bSmrg        shift;;
13945bc899bSmrg
14050f2e948Smrg    -s) stripcmd=$stripprog;;
14145bc899bSmrg
14257ba8e8bSmrg    -t)
14357ba8e8bSmrg        is_target_a_directory=always
14457ba8e8bSmrg        dst_arg=$2
14557ba8e8bSmrg        # Protect names problematic for 'test' and other utilities.
14657ba8e8bSmrg        case $dst_arg in
14757ba8e8bSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14857ba8e8bSmrg        esac
14957ba8e8bSmrg        shift;;
15045bc899bSmrg
15157ba8e8bSmrg    -T) is_target_a_directory=never;;
15245bc899bSmrg
15345bc899bSmrg    --version) echo "$0 $scriptversion"; exit $?;;
15445bc899bSmrg
15557ba8e8bSmrg    --) shift
15657ba8e8bSmrg        break;;
15750f2e948Smrg
15857ba8e8bSmrg    -*) echo "$0: invalid option: $1" >&2
15957ba8e8bSmrg        exit 1;;
16050f2e948Smrg
16150f2e948Smrg    *)  break;;
16245bc899bSmrg  esac
16350f2e948Smrg  shift
16445bc899bSmrgdone
16545bc899bSmrg
16657ba8e8bSmrg# We allow the use of options -d and -T together, by making -d
16757ba8e8bSmrg# take the precedence; this is for compatibility with GNU install.
16857ba8e8bSmrg
16957ba8e8bSmrgif test -n "$dir_arg"; then
17057ba8e8bSmrg  if test -n "$dst_arg"; then
17157ba8e8bSmrg    echo "$0: target directory not allowed when installing a directory." >&2
17257ba8e8bSmrg    exit 1
17357ba8e8bSmrg  fi
17457ba8e8bSmrgfi
17557ba8e8bSmrg
17650f2e948Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17750f2e948Smrg  # When -d is used, all remaining arguments are directories to create.
17850f2e948Smrg  # When -t is used, the destination is already specified.
17950f2e948Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18050f2e948Smrg  for arg
18150f2e948Smrg  do
18250f2e948Smrg    if test -n "$dst_arg"; then
18350f2e948Smrg      # $@ is not empty: it contains at least $arg.
18450f2e948Smrg      set fnord "$@" "$dst_arg"
18550f2e948Smrg      shift # fnord
18650f2e948Smrg    fi
18750f2e948Smrg    shift # arg
18850f2e948Smrg    dst_arg=$arg
189c97b1c41Smrg    # Protect names problematic for 'test' and other utilities.
190b40a6198Smrg    case $dst_arg in
191b40a6198Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192b40a6198Smrg    esac
19350f2e948Smrg  done
19450f2e948Smrgfi
19550f2e948Smrg
19650f2e948Smrgif test $# -eq 0; then
19745bc899bSmrg  if test -z "$dir_arg"; then
19845bc899bSmrg    echo "$0: no input file specified." >&2
19945bc899bSmrg    exit 1
20045bc899bSmrg  fi
201c97b1c41Smrg  # It's OK to call 'install-sh -d' without argument.
20245bc899bSmrg  # This can happen when creating conditional directories.
20345bc899bSmrg  exit 0
20445bc899bSmrgfi
20545bc899bSmrg
20657ba8e8bSmrgif test -z "$dir_arg"; then
20757ba8e8bSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20857ba8e8bSmrg    if test ! -d "$dst_arg"; then
20957ba8e8bSmrg      echo "$0: $dst_arg: Is not a directory." >&2
21057ba8e8bSmrg      exit 1
21157ba8e8bSmrg    fi
21257ba8e8bSmrg  fi
21357ba8e8bSmrgfi
21457ba8e8bSmrg
21550f2e948Smrgif test -z "$dir_arg"; then
216b40a6198Smrg  do_exit='(exit $ret); exit $ret'
217b40a6198Smrg  trap "ret=129; $do_exit" 1
218b40a6198Smrg  trap "ret=130; $do_exit" 2
219b40a6198Smrg  trap "ret=141; $do_exit" 13
220b40a6198Smrg  trap "ret=143; $do_exit" 15
22150f2e948Smrg
22250f2e948Smrg  # Set umask so as not to create temps with too-generous modes.
22350f2e948Smrg  # However, 'strip' requires both read and write access to temps.
22450f2e948Smrg  case $mode in
22550f2e948Smrg    # Optimize common cases.
22650f2e948Smrg    *644) cp_umask=133;;
22750f2e948Smrg    *755) cp_umask=22;;
22850f2e948Smrg
22950f2e948Smrg    *[0-7])
23050f2e948Smrg      if test -z "$stripcmd"; then
23157ba8e8bSmrg        u_plus_rw=
23250f2e948Smrg      else
23357ba8e8bSmrg        u_plus_rw='% 200'
23450f2e948Smrg      fi
23550f2e948Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23650f2e948Smrg    *)
23750f2e948Smrg      if test -z "$stripcmd"; then
23857ba8e8bSmrg        u_plus_rw=
23950f2e948Smrg      else
24057ba8e8bSmrg        u_plus_rw=,u+rw
24150f2e948Smrg      fi
24250f2e948Smrg      cp_umask=$mode$u_plus_rw;;
24350f2e948Smrg  esac
24450f2e948Smrgfi
24550f2e948Smrg
24645bc899bSmrgfor src
24745bc899bSmrgdo
248c97b1c41Smrg  # Protect names problematic for 'test' and other utilities.
24945bc899bSmrg  case $src in
250b40a6198Smrg    -* | [=\(\)!]) src=./$src;;
25145bc899bSmrg  esac
25245bc899bSmrg
25345bc899bSmrg  if test -n "$dir_arg"; then
25445bc899bSmrg    dst=$src
25550f2e948Smrg    dstdir=$dst
25650f2e948Smrg    test -d "$dstdir"
25750f2e948Smrg    dstdir_status=$?
258226fade8Smrg  else
25950f2e948Smrg
26045bc899bSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26145bc899bSmrg    # might cause directories to be created, which would be especially bad
26245bc899bSmrg    # if $src (and thus $dsttmp) contains '*'.
26345bc899bSmrg    if test ! -f "$src" && test ! -d "$src"; then
26445bc899bSmrg      echo "$0: $src does not exist." >&2
26545bc899bSmrg      exit 1
26645bc899bSmrg    fi
26745bc899bSmrg
26850f2e948Smrg    if test -z "$dst_arg"; then
26945bc899bSmrg      echo "$0: no destination specified." >&2
27045bc899bSmrg      exit 1
27145bc899bSmrg    fi
27250f2e948Smrg    dst=$dst_arg
27345bc899bSmrg
274a0c41156Smrg    # If destination is a directory, append the input filename.
27545bc899bSmrg    if test -d "$dst"; then
27657ba8e8bSmrg      if test "$is_target_a_directory" = never; then
27757ba8e8bSmrg        echo "$0: $dst_arg: Is a directory" >&2
27857ba8e8bSmrg        exit 1
27945bc899bSmrg      fi
28050f2e948Smrg      dstdir=$dst
281a0c41156Smrg      dstbase=`basename "$src"`
282a0c41156Smrg      case $dst in
283a0c41156Smrg	*/) dst=$dst$dstbase;;
284a0c41156Smrg	*)  dst=$dst/$dstbase;;
285a0c41156Smrg      esac
28650f2e948Smrg      dstdir_status=0
28750f2e948Smrg    else
28857ba8e8bSmrg      dstdir=`dirname "$dst"`
28950f2e948Smrg      test -d "$dstdir"
29050f2e948Smrg      dstdir_status=$?
29145bc899bSmrg    fi
29245bc899bSmrg  fi
29345bc899bSmrg
294a0c41156Smrg  case $dstdir in
295a0c41156Smrg    */) dstdirslash=$dstdir;;
296a0c41156Smrg    *)  dstdirslash=$dstdir/;;
297a0c41156Smrg  esac
298a0c41156Smrg
29950f2e948Smrg  obsolete_mkdir_used=false
30050f2e948Smrg
30150f2e948Smrg  if test $dstdir_status != 0; then
30250f2e948Smrg    case $posix_mkdir in
30350f2e948Smrg      '')
30457ba8e8bSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
30557ba8e8bSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
30657ba8e8bSmrg        umask=`umask`
30757ba8e8bSmrg        case $stripcmd.$umask in
30857ba8e8bSmrg          # Optimize common cases.
30957ba8e8bSmrg          *[2367][2367]) mkdir_umask=$umask;;
31057ba8e8bSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31157ba8e8bSmrg
31257ba8e8bSmrg          *[0-7])
31357ba8e8bSmrg            mkdir_umask=`expr $umask + 22 \
31457ba8e8bSmrg              - $umask % 100 % 40 + $umask % 20 \
31557ba8e8bSmrg              - $umask % 10 % 4 + $umask % 2
31657ba8e8bSmrg            `;;
31757ba8e8bSmrg          *) mkdir_umask=$umask,go-w;;
31857ba8e8bSmrg        esac
31957ba8e8bSmrg
32057ba8e8bSmrg        # With -d, create the new directory with the user-specified mode.
32157ba8e8bSmrg        # Otherwise, rely on $mkdir_umask.
32257ba8e8bSmrg        if test -n "$dir_arg"; then
32357ba8e8bSmrg          mkdir_mode=-m$mode
32457ba8e8bSmrg        else
32557ba8e8bSmrg          mkdir_mode=
32657ba8e8bSmrg        fi
32757ba8e8bSmrg
32857ba8e8bSmrg        posix_mkdir=false
32957ba8e8bSmrg        case $umask in
33057ba8e8bSmrg          *[123567][0-7][0-7])
33157ba8e8bSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
33257ba8e8bSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33357ba8e8bSmrg            ;;
33457ba8e8bSmrg          *)
335a0c41156Smrg            # Note that $RANDOM variable is not portable (e.g. dash);  Use it
336a0c41156Smrg            # here however when possible just to lower collision chance.
33757ba8e8bSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
33857ba8e8bSmrg
339a0c41156Smrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
340a0c41156Smrg
341a0c41156Smrg            # Because "mkdir -p" follows existing symlinks and we likely work
342a0c41156Smrg            # directly in world-writeable /tmp, make sure that the '$tmpdir'
343a0c41156Smrg            # directory is successfully created first before we actually test
344a0c41156Smrg            # 'mkdir -p' feature.
34557ba8e8bSmrg            if (umask $mkdir_umask &&
346a0c41156Smrg                $mkdirprog $mkdir_mode "$tmpdir" &&
347a0c41156Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
34857ba8e8bSmrg            then
34957ba8e8bSmrg              if test -z "$dir_arg" || {
35057ba8e8bSmrg                   # Check for POSIX incompatibilities with -m.
35157ba8e8bSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35257ba8e8bSmrg                   # other-writable bit of parent directory when it shouldn't.
35357ba8e8bSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
354a0c41156Smrg                   test_tmpdir="$tmpdir/a"
355a0c41156Smrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
35657ba8e8bSmrg                   case $ls_ld_tmpdir in
35757ba8e8bSmrg                     d????-?r-*) different_mode=700;;
35857ba8e8bSmrg                     d????-?--*) different_mode=755;;
35957ba8e8bSmrg                     *) false;;
36057ba8e8bSmrg                   esac &&
361a0c41156Smrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
362a0c41156Smrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
36357ba8e8bSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36457ba8e8bSmrg                   }
36557ba8e8bSmrg                 }
36657ba8e8bSmrg              then posix_mkdir=:
36757ba8e8bSmrg              fi
368a0c41156Smrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
36957ba8e8bSmrg            else
37057ba8e8bSmrg              # Remove any dirs left behind by ancient mkdir implementations.
371a0c41156Smrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
37257ba8e8bSmrg            fi
37357ba8e8bSmrg            trap '' 0;;
37457ba8e8bSmrg        esac;;
37550f2e948Smrg    esac
37645bc899bSmrg
37750f2e948Smrg    if
37850f2e948Smrg      $posix_mkdir && (
37957ba8e8bSmrg        umask $mkdir_umask &&
38057ba8e8bSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38150f2e948Smrg      )
38250f2e948Smrg    then :
38350f2e948Smrg    else
38445bc899bSmrg
38550f2e948Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38650f2e948Smrg      # or it failed possibly due to a race condition.  Create the
38750f2e948Smrg      # directory the slow way, step by step, checking for races as we go.
38845bc899bSmrg
38950f2e948Smrg      case $dstdir in
39057ba8e8bSmrg        /*) prefix='/';;
39157ba8e8bSmrg        [-=\(\)!]*) prefix='./';;
39257ba8e8bSmrg        *)  prefix='';;
39350f2e948Smrg      esac
39445bc899bSmrg
39550f2e948Smrg      oIFS=$IFS
39650f2e948Smrg      IFS=/
39757ba8e8bSmrg      set -f
39850f2e948Smrg      set fnord $dstdir
39945bc899bSmrg      shift
40057ba8e8bSmrg      set +f
40150f2e948Smrg      IFS=$oIFS
40250f2e948Smrg
40350f2e948Smrg      prefixes=
40450f2e948Smrg
40550f2e948Smrg      for d
40650f2e948Smrg      do
40757ba8e8bSmrg        test X"$d" = X && continue
40857ba8e8bSmrg
40957ba8e8bSmrg        prefix=$prefix$d
41057ba8e8bSmrg        if test -d "$prefix"; then
41157ba8e8bSmrg          prefixes=
41257ba8e8bSmrg        else
41357ba8e8bSmrg          if $posix_mkdir; then
41457ba8e8bSmrg            (umask=$mkdir_umask &&
41557ba8e8bSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41657ba8e8bSmrg            # Don't fail if two instances are running concurrently.
41757ba8e8bSmrg            test -d "$prefix" || exit 1
41857ba8e8bSmrg          else
41957ba8e8bSmrg            case $prefix in
42057ba8e8bSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42157ba8e8bSmrg              *) qprefix=$prefix;;
42257ba8e8bSmrg            esac
42357ba8e8bSmrg            prefixes="$prefixes '$qprefix'"
42457ba8e8bSmrg          fi
42557ba8e8bSmrg        fi
42657ba8e8bSmrg        prefix=$prefix/
42750f2e948Smrg      done
42850f2e948Smrg
42950f2e948Smrg      if test -n "$prefixes"; then
43057ba8e8bSmrg        # Don't fail if two instances are running concurrently.
43157ba8e8bSmrg        (umask $mkdir_umask &&
43257ba8e8bSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
43357ba8e8bSmrg          test -d "$dstdir" || exit 1
43457ba8e8bSmrg        obsolete_mkdir_used=true
43545bc899bSmrg      fi
43650f2e948Smrg    fi
43745bc899bSmrg  fi
43845bc899bSmrg
43945bc899bSmrg  if test -n "$dir_arg"; then
44050f2e948Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44150f2e948Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44250f2e948Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44350f2e948Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44445bc899bSmrg  else
44545bc899bSmrg
44645bc899bSmrg    # Make a couple of temp file names in the proper directory.
447a0c41156Smrg    dsttmp=${dstdirslash}_inst.$$_
448a0c41156Smrg    rmtmp=${dstdirslash}_rm.$$_
44945bc899bSmrg
45045bc899bSmrg    # Trap to clean up those temp files at exit.
45145bc899bSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45245bc899bSmrg
45345bc899bSmrg    # Copy the file name to the temp name.
45450f2e948Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45545bc899bSmrg
45645bc899bSmrg    # and set any options; do chmod last to preserve setuid bits.
45745bc899bSmrg    #
45845bc899bSmrg    # If any of these fail, we abort the whole thing.  If we want to
45945bc899bSmrg    # ignore errors from any of these, just make sure not to ignore
46045bc899bSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46145bc899bSmrg    #
46250f2e948Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46350f2e948Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46450f2e948Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46550f2e948Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46650f2e948Smrg
46750f2e948Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46850f2e948Smrg    if $copy_on_change &&
46957ba8e8bSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
47057ba8e8bSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
47157ba8e8bSmrg       set -f &&
47250f2e948Smrg       set X $old && old=:$2:$4:$5:$6 &&
47350f2e948Smrg       set X $new && new=:$2:$4:$5:$6 &&
47457ba8e8bSmrg       set +f &&
47550f2e948Smrg       test "$old" = "$new" &&
47650f2e948Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47750f2e948Smrg    then
47850f2e948Smrg      rm -f "$dsttmp"
47950f2e948Smrg    else
48050f2e948Smrg      # Rename the file to the real destination.
48150f2e948Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48250f2e948Smrg
48350f2e948Smrg      # The rename failed, perhaps because mv can't rename something else
48450f2e948Smrg      # to itself, or perhaps because mv is so ancient that it does not
48550f2e948Smrg      # support -f.
48650f2e948Smrg      {
48757ba8e8bSmrg        # Now remove or move aside any old file at destination location.
48857ba8e8bSmrg        # We try this two ways since rm can't unlink itself on some
48957ba8e8bSmrg        # systems and the destination file might be busy for other
49057ba8e8bSmrg        # reasons.  In this case, the final cleanup might fail but the new
49157ba8e8bSmrg        # file should still install successfully.
49257ba8e8bSmrg        {
49357ba8e8bSmrg          test ! -f "$dst" ||
49457ba8e8bSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
49557ba8e8bSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49657ba8e8bSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49757ba8e8bSmrg          } ||
49857ba8e8bSmrg          { echo "$0: cannot unlink or rename $dst" >&2
49957ba8e8bSmrg            (exit 1); exit 1
50057ba8e8bSmrg          }
50157ba8e8bSmrg        } &&
50257ba8e8bSmrg
50357ba8e8bSmrg        # Now rename the file to the real destination.
50457ba8e8bSmrg        $doit $mvcmd "$dsttmp" "$dst"
50550f2e948Smrg      }
50650f2e948Smrg    fi || exit 1
50750f2e948Smrg
50850f2e948Smrg    trap '' 0
50950f2e948Smrg  fi
51045bc899bSmrgdone
51145bc899bSmrg
51245bc899bSmrg# Local variables:
513a0c41156Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
51445bc899bSmrg# time-stamp-start: "scriptversion="
51545bc899bSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
516f2408745Smrg# time-stamp-time-zone: "UTC0"
517b40a6198Smrg# time-stamp-end: "; # UTC"
51845bc899bSmrg# End:
519