install-sh revision b40a6198
145bc899bSmrg#!/bin/sh
245bc899bSmrg# install - install a program, script, or datafile
345bc899bSmrg
4b40a6198Smrgscriptversion=2011-01-19.21; # 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
3845bc899bSmrg# `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
4450f2e948Smrgnl='
4550f2e948Smrg'
4650f2e948SmrgIFS=" ""	$nl"
4745bc899bSmrg
4845bc899bSmrg# set DOITPROG to echo to test this script
4945bc899bSmrg
5045bc899bSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5150f2e948Smrgdoit=${DOITPROG-}
5250f2e948Smrgif test -z "$doit"; then
5350f2e948Smrg  doit_exec=exec
5450f2e948Smrgelse
5550f2e948Smrg  doit_exec=$doit
5650f2e948Smrgfi
5745bc899bSmrg
5850f2e948Smrg# Put in absolute file names if you don't have them in your path;
5950f2e948Smrg# or use environment vars.
6050f2e948Smrg
6150f2e948Smrgchgrpprog=${CHGRPPROG-chgrp}
6250f2e948Smrgchmodprog=${CHMODPROG-chmod}
6350f2e948Smrgchownprog=${CHOWNPROG-chown}
6450f2e948Smrgcmpprog=${CMPPROG-cmp}
6550f2e948Smrgcpprog=${CPPROG-cp}
6650f2e948Smrgmkdirprog=${MKDIRPROG-mkdir}
6750f2e948Smrgmvprog=${MVPROG-mv}
6850f2e948Smrgrmprog=${RMPROG-rm}
6950f2e948Smrgstripprog=${STRIPPROG-strip}
7050f2e948Smrg
7150f2e948Smrgposix_glob='?'
7250f2e948Smrginitialize_posix_glob='
7350f2e948Smrg  test "$posix_glob" != "?" || {
7450f2e948Smrg    if (set -f) 2>/dev/null; then
7550f2e948Smrg      posix_glob=
7650f2e948Smrg    else
7750f2e948Smrg      posix_glob=:
7850f2e948Smrg    fi
7950f2e948Smrg  }
8050f2e948Smrg'
811ac89addSmrg
8250f2e948Smrgposix_mkdir=
8350f2e948Smrg
8450f2e948Smrg# Desired mode of installed file.
8550f2e948Smrgmode=0755
8645bc899bSmrg
87226fade8Smrgchgrpcmd=
8850f2e948Smrgchmodcmd=$chmodprog
8950f2e948Smrgchowncmd=
9050f2e948Smrgmvcmd=$mvprog
91226fade8Smrgrmcmd="$rmprog -f"
9250f2e948Smrgstripcmd=
9350f2e948Smrg
9445bc899bSmrgsrc=
9545bc899bSmrgdst=
9645bc899bSmrgdir_arg=
9750f2e948Smrgdst_arg=
9850f2e948Smrg
9950f2e948Smrgcopy_on_change=false
10045bc899bSmrgno_target_directory=
10145bc899bSmrg
10250f2e948Smrgusage="\
10350f2e948SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10445bc899bSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10545bc899bSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10645bc899bSmrg   or: $0 [OPTION]... -d DIRECTORIES...
10745bc899bSmrg
10845bc899bSmrgIn the 1st form, copy SRCFILE to DSTFILE.
10945bc899bSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11045bc899bSmrgIn the 4th, create DIRECTORIES.
11145bc899bSmrg
11245bc899bSmrgOptions:
11350f2e948Smrg     --help     display this help and exit.
11450f2e948Smrg     --version  display version info and exit.
11550f2e948Smrg
11650f2e948Smrg  -c            (ignored)
11750f2e948Smrg  -C            install only if different (preserve the last data modification time)
11850f2e948Smrg  -d            create directories instead of installing files.
11950f2e948Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12050f2e948Smrg  -m MODE       $chmodprog installed files to MODE.
12150f2e948Smrg  -o USER       $chownprog installed files to USER.
12250f2e948Smrg  -s            $stripprog installed files.
12350f2e948Smrg  -t DIRECTORY  install into DIRECTORY.
12450f2e948Smrg  -T            report an error if DSTFILE is a directory.
12545bc899bSmrg
12645bc899bSmrgEnvironment variables override the default commands:
12750f2e948Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12850f2e948Smrg  RMPROG STRIPPROG
12945bc899bSmrg"
13045bc899bSmrg
13150f2e948Smrgwhile test $# -ne 0; do
13245bc899bSmrg  case $1 in
13350f2e948Smrg    -c) ;;
13450f2e948Smrg
13550f2e948Smrg    -C) copy_on_change=true;;
13645bc899bSmrg
13750f2e948Smrg    -d) dir_arg=true;;
13845bc899bSmrg
13945bc899bSmrg    -g) chgrpcmd="$chgrpprog $2"
14050f2e948Smrg	shift;;
14145bc899bSmrg
14245bc899bSmrg    --help) echo "$usage"; exit $?;;
14345bc899bSmrg
14450f2e948Smrg    -m) mode=$2
14550f2e948Smrg	case $mode in
14650f2e948Smrg	  *' '* | *'	'* | *'
14750f2e948Smrg'*	  | *'*'* | *'?'* | *'['*)
14850f2e948Smrg	    echo "$0: invalid mode: $mode" >&2
14950f2e948Smrg	    exit 1;;
15050f2e948Smrg	esac
15150f2e948Smrg	shift;;
15245bc899bSmrg
15345bc899bSmrg    -o) chowncmd="$chownprog $2"
15450f2e948Smrg	shift;;
15545bc899bSmrg
15650f2e948Smrg    -s) stripcmd=$stripprog;;
15745bc899bSmrg
15850f2e948Smrg    -t) dst_arg=$2
159b40a6198Smrg	# Protect names problematic for `test' and other utilities.
160b40a6198Smrg	case $dst_arg in
161b40a6198Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162b40a6198Smrg	esac
16350f2e948Smrg	shift;;
16445bc899bSmrg
16550f2e948Smrg    -T) no_target_directory=true;;
16645bc899bSmrg
16745bc899bSmrg    --version) echo "$0 $scriptversion"; exit $?;;
16845bc899bSmrg
16950f2e948Smrg    --)	shift
17045bc899bSmrg	break;;
17150f2e948Smrg
17250f2e948Smrg    -*)	echo "$0: invalid option: $1" >&2
17350f2e948Smrg	exit 1;;
17450f2e948Smrg
17550f2e948Smrg    *)  break;;
17645bc899bSmrg  esac
17750f2e948Smrg  shift
17845bc899bSmrgdone
17945bc899bSmrg
18050f2e948Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
18150f2e948Smrg  # When -d is used, all remaining arguments are directories to create.
18250f2e948Smrg  # When -t is used, the destination is already specified.
18350f2e948Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18450f2e948Smrg  for arg
18550f2e948Smrg  do
18650f2e948Smrg    if test -n "$dst_arg"; then
18750f2e948Smrg      # $@ is not empty: it contains at least $arg.
18850f2e948Smrg      set fnord "$@" "$dst_arg"
18950f2e948Smrg      shift # fnord
19050f2e948Smrg    fi
19150f2e948Smrg    shift # arg
19250f2e948Smrg    dst_arg=$arg
193b40a6198Smrg    # Protect names problematic for `test' and other utilities.
194b40a6198Smrg    case $dst_arg in
195b40a6198Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196b40a6198Smrg    esac
19750f2e948Smrg  done
19850f2e948Smrgfi
19950f2e948Smrg
20050f2e948Smrgif test $# -eq 0; then
20145bc899bSmrg  if test -z "$dir_arg"; then
20245bc899bSmrg    echo "$0: no input file specified." >&2
20345bc899bSmrg    exit 1
20445bc899bSmrg  fi
20545bc899bSmrg  # It's OK to call `install-sh -d' without argument.
20645bc899bSmrg  # This can happen when creating conditional directories.
20745bc899bSmrg  exit 0
20845bc899bSmrgfi
20945bc899bSmrg
21050f2e948Smrgif test -z "$dir_arg"; then
211b40a6198Smrg  do_exit='(exit $ret); exit $ret'
212b40a6198Smrg  trap "ret=129; $do_exit" 1
213b40a6198Smrg  trap "ret=130; $do_exit" 2
214b40a6198Smrg  trap "ret=141; $do_exit" 13
215b40a6198Smrg  trap "ret=143; $do_exit" 15
21650f2e948Smrg
21750f2e948Smrg  # Set umask so as not to create temps with too-generous modes.
21850f2e948Smrg  # However, 'strip' requires both read and write access to temps.
21950f2e948Smrg  case $mode in
22050f2e948Smrg    # Optimize common cases.
22150f2e948Smrg    *644) cp_umask=133;;
22250f2e948Smrg    *755) cp_umask=22;;
22350f2e948Smrg
22450f2e948Smrg    *[0-7])
22550f2e948Smrg      if test -z "$stripcmd"; then
22650f2e948Smrg	u_plus_rw=
22750f2e948Smrg      else
22850f2e948Smrg	u_plus_rw='% 200'
22950f2e948Smrg      fi
23050f2e948Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23150f2e948Smrg    *)
23250f2e948Smrg      if test -z "$stripcmd"; then
23350f2e948Smrg	u_plus_rw=
23450f2e948Smrg      else
23550f2e948Smrg	u_plus_rw=,u+rw
23650f2e948Smrg      fi
23750f2e948Smrg      cp_umask=$mode$u_plus_rw;;
23850f2e948Smrg  esac
23950f2e948Smrgfi
24050f2e948Smrg
24145bc899bSmrgfor src
24245bc899bSmrgdo
243b40a6198Smrg  # Protect names problematic for `test' and other utilities.
24445bc899bSmrg  case $src in
245b40a6198Smrg    -* | [=\(\)!]) src=./$src;;
24645bc899bSmrg  esac
24745bc899bSmrg
24845bc899bSmrg  if test -n "$dir_arg"; then
24945bc899bSmrg    dst=$src
25050f2e948Smrg    dstdir=$dst
25150f2e948Smrg    test -d "$dstdir"
25250f2e948Smrg    dstdir_status=$?
253226fade8Smrg  else
25450f2e948Smrg
25545bc899bSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
25645bc899bSmrg    # might cause directories to be created, which would be especially bad
25745bc899bSmrg    # if $src (and thus $dsttmp) contains '*'.
25845bc899bSmrg    if test ! -f "$src" && test ! -d "$src"; then
25945bc899bSmrg      echo "$0: $src does not exist." >&2
26045bc899bSmrg      exit 1
26145bc899bSmrg    fi
26245bc899bSmrg
26350f2e948Smrg    if test -z "$dst_arg"; then
26445bc899bSmrg      echo "$0: no destination specified." >&2
26545bc899bSmrg      exit 1
26645bc899bSmrg    fi
26750f2e948Smrg    dst=$dst_arg
26845bc899bSmrg
26945bc899bSmrg    # If destination is a directory, append the input filename; won't work
27045bc899bSmrg    # if double slashes aren't ignored.
27145bc899bSmrg    if test -d "$dst"; then
27245bc899bSmrg      if test -n "$no_target_directory"; then
27350f2e948Smrg	echo "$0: $dst_arg: Is a directory" >&2
27445bc899bSmrg	exit 1
27545bc899bSmrg      fi
27650f2e948Smrg      dstdir=$dst
27750f2e948Smrg      dst=$dstdir/`basename "$src"`
27850f2e948Smrg      dstdir_status=0
27950f2e948Smrg    else
28050f2e948Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
28150f2e948Smrg      dstdir=`
28250f2e948Smrg	(dirname "$dst") 2>/dev/null ||
28350f2e948Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
28450f2e948Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
28550f2e948Smrg	     X"$dst" : 'X\(//\)$' \| \
28650f2e948Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28750f2e948Smrg	echo X"$dst" |
28850f2e948Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28950f2e948Smrg		   s//\1/
29050f2e948Smrg		   q
29150f2e948Smrg		 }
29250f2e948Smrg		 /^X\(\/\/\)[^/].*/{
29350f2e948Smrg		   s//\1/
29450f2e948Smrg		   q
29550f2e948Smrg		 }
29650f2e948Smrg		 /^X\(\/\/\)$/{
29750f2e948Smrg		   s//\1/
29850f2e948Smrg		   q
29950f2e948Smrg		 }
30050f2e948Smrg		 /^X\(\/\).*/{
30150f2e948Smrg		   s//\1/
30250f2e948Smrg		   q
30350f2e948Smrg		 }
30450f2e948Smrg		 s/.*/./; q'
30550f2e948Smrg      `
30650f2e948Smrg
30750f2e948Smrg      test -d "$dstdir"
30850f2e948Smrg      dstdir_status=$?
30945bc899bSmrg    fi
31045bc899bSmrg  fi
31145bc899bSmrg
31250f2e948Smrg  obsolete_mkdir_used=false
31350f2e948Smrg
31450f2e948Smrg  if test $dstdir_status != 0; then
31550f2e948Smrg    case $posix_mkdir in
31650f2e948Smrg      '')
31750f2e948Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31850f2e948Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31950f2e948Smrg	umask=`umask`
32050f2e948Smrg	case $stripcmd.$umask in
32150f2e948Smrg	  # Optimize common cases.
32250f2e948Smrg	  *[2367][2367]) mkdir_umask=$umask;;
32350f2e948Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
32450f2e948Smrg
32550f2e948Smrg	  *[0-7])
32650f2e948Smrg	    mkdir_umask=`expr $umask + 22 \
32750f2e948Smrg	      - $umask % 100 % 40 + $umask % 20 \
32850f2e948Smrg	      - $umask % 10 % 4 + $umask % 2
32950f2e948Smrg	    `;;
33050f2e948Smrg	  *) mkdir_umask=$umask,go-w;;
33150f2e948Smrg	esac
33250f2e948Smrg
33350f2e948Smrg	# With -d, create the new directory with the user-specified mode.
33450f2e948Smrg	# Otherwise, rely on $mkdir_umask.
33550f2e948Smrg	if test -n "$dir_arg"; then
33650f2e948Smrg	  mkdir_mode=-m$mode
33750f2e948Smrg	else
33850f2e948Smrg	  mkdir_mode=
33950f2e948Smrg	fi
34050f2e948Smrg
34150f2e948Smrg	posix_mkdir=false
34250f2e948Smrg	case $umask in
34350f2e948Smrg	  *[123567][0-7][0-7])
34450f2e948Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
34550f2e948Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
34650f2e948Smrg	    ;;
34750f2e948Smrg	  *)
34850f2e948Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34950f2e948Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
35050f2e948Smrg
35150f2e948Smrg	    if (umask $mkdir_umask &&
35250f2e948Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
35350f2e948Smrg	    then
35450f2e948Smrg	      if test -z "$dir_arg" || {
35550f2e948Smrg		   # Check for POSIX incompatibilities with -m.
35650f2e948Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35750f2e948Smrg		   # other-writeable bit of parent directory when it shouldn't.
35850f2e948Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35950f2e948Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
36050f2e948Smrg		   case $ls_ld_tmpdir in
36150f2e948Smrg		     d????-?r-*) different_mode=700;;
36250f2e948Smrg		     d????-?--*) different_mode=755;;
36350f2e948Smrg		     *) false;;
36450f2e948Smrg		   esac &&
36550f2e948Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
36650f2e948Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36750f2e948Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36850f2e948Smrg		   }
36950f2e948Smrg		 }
37050f2e948Smrg	      then posix_mkdir=:
37150f2e948Smrg	      fi
37250f2e948Smrg	      rmdir "$tmpdir/d" "$tmpdir"
37350f2e948Smrg	    else
37450f2e948Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
37550f2e948Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
37650f2e948Smrg	    fi
37750f2e948Smrg	    trap '' 0;;
37850f2e948Smrg	esac;;
37950f2e948Smrg    esac
38045bc899bSmrg
38150f2e948Smrg    if
38250f2e948Smrg      $posix_mkdir && (
38350f2e948Smrg	umask $mkdir_umask &&
38450f2e948Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38550f2e948Smrg      )
38650f2e948Smrg    then :
38750f2e948Smrg    else
38845bc899bSmrg
38950f2e948Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
39050f2e948Smrg      # or it failed possibly due to a race condition.  Create the
39150f2e948Smrg      # directory the slow way, step by step, checking for races as we go.
39245bc899bSmrg
39350f2e948Smrg      case $dstdir in
39450f2e948Smrg	/*) prefix='/';;
395b40a6198Smrg	[-=\(\)!]*) prefix='./';;
39650f2e948Smrg	*)  prefix='';;
39750f2e948Smrg      esac
39845bc899bSmrg
39950f2e948Smrg      eval "$initialize_posix_glob"
40045bc899bSmrg
40150f2e948Smrg      oIFS=$IFS
40250f2e948Smrg      IFS=/
40350f2e948Smrg      $posix_glob set -f
40450f2e948Smrg      set fnord $dstdir
40545bc899bSmrg      shift
40650f2e948Smrg      $posix_glob set +f
40750f2e948Smrg      IFS=$oIFS
40850f2e948Smrg
40950f2e948Smrg      prefixes=
41050f2e948Smrg
41150f2e948Smrg      for d
41250f2e948Smrg      do
413b40a6198Smrg	test X"$d" = X && continue
41450f2e948Smrg
41550f2e948Smrg	prefix=$prefix$d
41650f2e948Smrg	if test -d "$prefix"; then
41750f2e948Smrg	  prefixes=
41850f2e948Smrg	else
41950f2e948Smrg	  if $posix_mkdir; then
42050f2e948Smrg	    (umask=$mkdir_umask &&
42150f2e948Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42250f2e948Smrg	    # Don't fail if two instances are running concurrently.
42350f2e948Smrg	    test -d "$prefix" || exit 1
42450f2e948Smrg	  else
42550f2e948Smrg	    case $prefix in
42650f2e948Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42750f2e948Smrg	      *) qprefix=$prefix;;
42850f2e948Smrg	    esac
42950f2e948Smrg	    prefixes="$prefixes '$qprefix'"
43050f2e948Smrg	  fi
43150f2e948Smrg	fi
43250f2e948Smrg	prefix=$prefix/
43350f2e948Smrg      done
43450f2e948Smrg
43550f2e948Smrg      if test -n "$prefixes"; then
43650f2e948Smrg	# Don't fail if two instances are running concurrently.
43750f2e948Smrg	(umask $mkdir_umask &&
43850f2e948Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43950f2e948Smrg	  test -d "$dstdir" || exit 1
44050f2e948Smrg	obsolete_mkdir_used=true
44145bc899bSmrg      fi
44250f2e948Smrg    fi
44345bc899bSmrg  fi
44445bc899bSmrg
44545bc899bSmrg  if test -n "$dir_arg"; then
44650f2e948Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44750f2e948Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44850f2e948Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44950f2e948Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
45045bc899bSmrg  else
45145bc899bSmrg
45245bc899bSmrg    # Make a couple of temp file names in the proper directory.
45345bc899bSmrg    dsttmp=$dstdir/_inst.$$_
45445bc899bSmrg    rmtmp=$dstdir/_rm.$$_
45545bc899bSmrg
45645bc899bSmrg    # Trap to clean up those temp files at exit.
45745bc899bSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45845bc899bSmrg
45945bc899bSmrg    # Copy the file name to the temp name.
46050f2e948Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
46145bc899bSmrg
46245bc899bSmrg    # and set any options; do chmod last to preserve setuid bits.
46345bc899bSmrg    #
46445bc899bSmrg    # If any of these fail, we abort the whole thing.  If we want to
46545bc899bSmrg    # ignore errors from any of these, just make sure not to ignore
46645bc899bSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46745bc899bSmrg    #
46850f2e948Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46950f2e948Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
47050f2e948Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
47150f2e948Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
47250f2e948Smrg
47350f2e948Smrg    # If -C, don't bother to copy if it wouldn't change the file.
47450f2e948Smrg    if $copy_on_change &&
47550f2e948Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
47650f2e948Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47750f2e948Smrg
47850f2e948Smrg       eval "$initialize_posix_glob" &&
47950f2e948Smrg       $posix_glob set -f &&
48050f2e948Smrg       set X $old && old=:$2:$4:$5:$6 &&
48150f2e948Smrg       set X $new && new=:$2:$4:$5:$6 &&
48250f2e948Smrg       $posix_glob set +f &&
48350f2e948Smrg
48450f2e948Smrg       test "$old" = "$new" &&
48550f2e948Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
48650f2e948Smrg    then
48750f2e948Smrg      rm -f "$dsttmp"
48850f2e948Smrg    else
48950f2e948Smrg      # Rename the file to the real destination.
49050f2e948Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
49150f2e948Smrg
49250f2e948Smrg      # The rename failed, perhaps because mv can't rename something else
49350f2e948Smrg      # to itself, or perhaps because mv is so ancient that it does not
49450f2e948Smrg      # support -f.
49550f2e948Smrg      {
49650f2e948Smrg	# Now remove or move aside any old file at destination location.
49750f2e948Smrg	# We try this two ways since rm can't unlink itself on some
49850f2e948Smrg	# systems and the destination file might be busy for other
49950f2e948Smrg	# reasons.  In this case, the final cleanup might fail but the new
50050f2e948Smrg	# file should still install successfully.
50150f2e948Smrg	{
50250f2e948Smrg	  test ! -f "$dst" ||
50350f2e948Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
50450f2e948Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
50550f2e948Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
50650f2e948Smrg	  } ||
50750f2e948Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50850f2e948Smrg	    (exit 1); exit 1
50950f2e948Smrg	  }
51050f2e948Smrg	} &&
51150f2e948Smrg
51250f2e948Smrg	# Now rename the file to the real destination.
51350f2e948Smrg	$doit $mvcmd "$dsttmp" "$dst"
51450f2e948Smrg      }
51550f2e948Smrg    fi || exit 1
51650f2e948Smrg
51750f2e948Smrg    trap '' 0
51850f2e948Smrg  fi
51945bc899bSmrgdone
52045bc899bSmrg
52145bc899bSmrg# Local variables:
52245bc899bSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
52345bc899bSmrg# time-stamp-start: "scriptversion="
52445bc899bSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525b40a6198Smrg# time-stamp-time-zone: "UTC"
526b40a6198Smrg# time-stamp-end: "; # UTC"
52745bc899bSmrg# End:
528