install-sh revision 70f7c90c
1a850946eSmrg#!/bin/sh
2a850946eSmrg# install - install a program, script, or datafile
370f7c90cSmrg
470f7c90cSmrgscriptversion=2009-04-28.21; # UTC
570f7c90cSmrg
6a850946eSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a850946eSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8a850946eSmrg# following copyright and license.
9a850946eSmrg#
10a850946eSmrg# Copyright (C) 1994 X Consortium
11a850946eSmrg#
12a850946eSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a850946eSmrg# of this software and associated documentation files (the "Software"), to
14a850946eSmrg# deal in the Software without restriction, including without limitation the
15a850946eSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a850946eSmrg# sell copies of the Software, and to permit persons to whom the Software is
17a850946eSmrg# furnished to do so, subject to the following conditions:
18a850946eSmrg#
19a850946eSmrg# The above copyright notice and this permission notice shall be included in
20a850946eSmrg# all copies or substantial portions of the Software.
21a850946eSmrg#
22a850946eSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a850946eSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a850946eSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a850946eSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a850946eSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a850946eSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a850946eSmrg#
29a850946eSmrg# Except as contained in this notice, the name of the X Consortium shall not
30a850946eSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a850946eSmrg# ings in this Software without prior written authorization from the X Consor-
32a850946eSmrg# tium.
33a850946eSmrg#
34a850946eSmrg#
35a850946eSmrg# FSF changes to this file are in the public domain.
36a850946eSmrg#
37a850946eSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38a850946eSmrg# `make' implicit rules from creating a file called install from it
39a850946eSmrg# when there is no Makefile.
40a850946eSmrg#
41a850946eSmrg# This script is compatible with the BSD install script, but was written
4270f7c90cSmrg# from scratch.
43a850946eSmrg
4470f7c90cSmrgnl='
4570f7c90cSmrg'
4670f7c90cSmrgIFS=" ""	$nl"
47a850946eSmrg
48a850946eSmrg# set DOITPROG to echo to test this script
49a850946eSmrg
50a850946eSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5170f7c90cSmrgdoit=${DOITPROG-}
5270f7c90cSmrgif test -z "$doit"; then
5370f7c90cSmrg  doit_exec=exec
54a850946eSmrgelse
5570f7c90cSmrg  doit_exec=$doit
56a850946eSmrgfi
57a850946eSmrg
5870f7c90cSmrg# Put in absolute file names if you don't have them in your path;
5970f7c90cSmrg# or use environment vars.
6070f7c90cSmrg
6170f7c90cSmrgchgrpprog=${CHGRPPROG-chgrp}
6270f7c90cSmrgchmodprog=${CHMODPROG-chmod}
6370f7c90cSmrgchownprog=${CHOWNPROG-chown}
6470f7c90cSmrgcmpprog=${CMPPROG-cmp}
6570f7c90cSmrgcpprog=${CPPROG-cp}
6670f7c90cSmrgmkdirprog=${MKDIRPROG-mkdir}
6770f7c90cSmrgmvprog=${MVPROG-mv}
6870f7c90cSmrgrmprog=${RMPROG-rm}
6970f7c90cSmrgstripprog=${STRIPPROG-strip}
7070f7c90cSmrg
7170f7c90cSmrgposix_glob='?'
7270f7c90cSmrginitialize_posix_glob='
7370f7c90cSmrg  test "$posix_glob" != "?" || {
7470f7c90cSmrg    if (set -f) 2>/dev/null; then
7570f7c90cSmrg      posix_glob=
7670f7c90cSmrg    else
7770f7c90cSmrg      posix_glob=:
7870f7c90cSmrg    fi
7970f7c90cSmrg  }
8070f7c90cSmrg'
8170f7c90cSmrg
8270f7c90cSmrgposix_mkdir=
8370f7c90cSmrg
8470f7c90cSmrg# Desired mode of installed file.
8570f7c90cSmrgmode=0755
8670f7c90cSmrg
8770f7c90cSmrgchgrpcmd=
8870f7c90cSmrgchmodcmd=$chmodprog
8970f7c90cSmrgchowncmd=
9070f7c90cSmrgmvcmd=$mvprog
9170f7c90cSmrgrmcmd="$rmprog -f"
9270f7c90cSmrgstripcmd=
93a850946eSmrg
9470f7c90cSmrgsrc=
9570f7c90cSmrgdst=
9670f7c90cSmrgdir_arg=
9770f7c90cSmrgdst_arg=
98a850946eSmrg
9970f7c90cSmrgcopy_on_change=false
10070f7c90cSmrgno_target_directory=
101a850946eSmrg
10270f7c90cSmrgusage="\
10370f7c90cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10470f7c90cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10570f7c90cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10670f7c90cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107a850946eSmrg
10870f7c90cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
10970f7c90cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11070f7c90cSmrgIn the 4th, create DIRECTORIES.
111a850946eSmrg
11270f7c90cSmrgOptions:
11370f7c90cSmrg     --help     display this help and exit.
11470f7c90cSmrg     --version  display version info and exit.
115a850946eSmrg
11670f7c90cSmrg  -c            (ignored)
11770f7c90cSmrg  -C            install only if different (preserve the last data modification time)
11870f7c90cSmrg  -d            create directories instead of installing files.
11970f7c90cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
12070f7c90cSmrg  -m MODE       $chmodprog installed files to MODE.
12170f7c90cSmrg  -o USER       $chownprog installed files to USER.
12270f7c90cSmrg  -s            $stripprog installed files.
12370f7c90cSmrg  -t DIRECTORY  install into DIRECTORY.
12470f7c90cSmrg  -T            report an error if DSTFILE is a directory.
125a850946eSmrg
12670f7c90cSmrgEnvironment variables override the default commands:
12770f7c90cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12870f7c90cSmrg  RMPROG STRIPPROG
12970f7c90cSmrg"
130a850946eSmrg
13170f7c90cSmrgwhile test $# -ne 0; do
13270f7c90cSmrg  case $1 in
13370f7c90cSmrg    -c) ;;
134a850946eSmrg
13570f7c90cSmrg    -C) copy_on_change=true;;
136a850946eSmrg
13770f7c90cSmrg    -d) dir_arg=true;;
138a850946eSmrg
13970f7c90cSmrg    -g) chgrpcmd="$chgrpprog $2"
14070f7c90cSmrg	shift;;
141a850946eSmrg
14270f7c90cSmrg    --help) echo "$usage"; exit $?;;
143a850946eSmrg
14470f7c90cSmrg    -m) mode=$2
14570f7c90cSmrg	case $mode in
14670f7c90cSmrg	  *' '* | *'	'* | *'
14770f7c90cSmrg'*	  | *'*'* | *'?'* | *'['*)
14870f7c90cSmrg	    echo "$0: invalid mode: $mode" >&2
14970f7c90cSmrg	    exit 1;;
15070f7c90cSmrg	esac
15170f7c90cSmrg	shift;;
152a850946eSmrg
15370f7c90cSmrg    -o) chowncmd="$chownprog $2"
15470f7c90cSmrg	shift;;
155a850946eSmrg
15670f7c90cSmrg    -s) stripcmd=$stripprog;;
157a850946eSmrg
15870f7c90cSmrg    -t) dst_arg=$2
15970f7c90cSmrg	shift;;
160a850946eSmrg
16170f7c90cSmrg    -T) no_target_directory=true;;
162a850946eSmrg
16370f7c90cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164a850946eSmrg
16570f7c90cSmrg    --)	shift
16670f7c90cSmrg	break;;
167a850946eSmrg
16870f7c90cSmrg    -*)	echo "$0: invalid option: $1" >&2
16970f7c90cSmrg	exit 1;;
170a850946eSmrg
17170f7c90cSmrg    *)  break;;
17270f7c90cSmrg  esac
17370f7c90cSmrg  shift
17470f7c90cSmrgdone
175a850946eSmrg
17670f7c90cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17770f7c90cSmrg  # When -d is used, all remaining arguments are directories to create.
17870f7c90cSmrg  # When -t is used, the destination is already specified.
17970f7c90cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18070f7c90cSmrg  for arg
18170f7c90cSmrg  do
18270f7c90cSmrg    if test -n "$dst_arg"; then
18370f7c90cSmrg      # $@ is not empty: it contains at least $arg.
18470f7c90cSmrg      set fnord "$@" "$dst_arg"
18570f7c90cSmrg      shift # fnord
18670f7c90cSmrg    fi
18770f7c90cSmrg    shift # arg
18870f7c90cSmrg    dst_arg=$arg
18970f7c90cSmrg  done
19070f7c90cSmrgfi
191a850946eSmrg
19270f7c90cSmrgif test $# -eq 0; then
19370f7c90cSmrg  if test -z "$dir_arg"; then
19470f7c90cSmrg    echo "$0: no input file specified." >&2
19570f7c90cSmrg    exit 1
19670f7c90cSmrg  fi
19770f7c90cSmrg  # It's OK to call `install-sh -d' without argument.
19870f7c90cSmrg  # This can happen when creating conditional directories.
19970f7c90cSmrg  exit 0
20070f7c90cSmrgfi
201a850946eSmrg
20270f7c90cSmrgif test -z "$dir_arg"; then
20370f7c90cSmrg  trap '(exit $?); exit' 1 2 13 15
20470f7c90cSmrg
20570f7c90cSmrg  # Set umask so as not to create temps with too-generous modes.
20670f7c90cSmrg  # However, 'strip' requires both read and write access to temps.
20770f7c90cSmrg  case $mode in
20870f7c90cSmrg    # Optimize common cases.
20970f7c90cSmrg    *644) cp_umask=133;;
21070f7c90cSmrg    *755) cp_umask=22;;
21170f7c90cSmrg
21270f7c90cSmrg    *[0-7])
21370f7c90cSmrg      if test -z "$stripcmd"; then
21470f7c90cSmrg	u_plus_rw=
21570f7c90cSmrg      else
21670f7c90cSmrg	u_plus_rw='% 200'
21770f7c90cSmrg      fi
21870f7c90cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21970f7c90cSmrg    *)
22070f7c90cSmrg      if test -z "$stripcmd"; then
22170f7c90cSmrg	u_plus_rw=
22270f7c90cSmrg      else
22370f7c90cSmrg	u_plus_rw=,u+rw
22470f7c90cSmrg      fi
22570f7c90cSmrg      cp_umask=$mode$u_plus_rw;;
22670f7c90cSmrg  esac
22770f7c90cSmrgfi
228a850946eSmrg
22970f7c90cSmrgfor src
23070f7c90cSmrgdo
23170f7c90cSmrg  # Protect names starting with `-'.
23270f7c90cSmrg  case $src in
23370f7c90cSmrg    -*) src=./$src;;
23470f7c90cSmrg  esac
23570f7c90cSmrg
23670f7c90cSmrg  if test -n "$dir_arg"; then
23770f7c90cSmrg    dst=$src
23870f7c90cSmrg    dstdir=$dst
23970f7c90cSmrg    test -d "$dstdir"
24070f7c90cSmrg    dstdir_status=$?
24170f7c90cSmrg  else
24270f7c90cSmrg
24370f7c90cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24470f7c90cSmrg    # might cause directories to be created, which would be especially bad
24570f7c90cSmrg    # if $src (and thus $dsttmp) contains '*'.
24670f7c90cSmrg    if test ! -f "$src" && test ! -d "$src"; then
24770f7c90cSmrg      echo "$0: $src does not exist." >&2
24870f7c90cSmrg      exit 1
24970f7c90cSmrg    fi
25070f7c90cSmrg
25170f7c90cSmrg    if test -z "$dst_arg"; then
25270f7c90cSmrg      echo "$0: no destination specified." >&2
25370f7c90cSmrg      exit 1
25470f7c90cSmrg    fi
25570f7c90cSmrg
25670f7c90cSmrg    dst=$dst_arg
25770f7c90cSmrg    # Protect names starting with `-'.
25870f7c90cSmrg    case $dst in
25970f7c90cSmrg      -*) dst=./$dst;;
26070f7c90cSmrg    esac
261a850946eSmrg
26270f7c90cSmrg    # If destination is a directory, append the input filename; won't work
26370f7c90cSmrg    # if double slashes aren't ignored.
26470f7c90cSmrg    if test -d "$dst"; then
26570f7c90cSmrg      if test -n "$no_target_directory"; then
26670f7c90cSmrg	echo "$0: $dst_arg: Is a directory" >&2
26770f7c90cSmrg	exit 1
26870f7c90cSmrg      fi
26970f7c90cSmrg      dstdir=$dst
27070f7c90cSmrg      dst=$dstdir/`basename "$src"`
27170f7c90cSmrg      dstdir_status=0
27270f7c90cSmrg    else
27370f7c90cSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27470f7c90cSmrg      dstdir=`
27570f7c90cSmrg	(dirname "$dst") 2>/dev/null ||
27670f7c90cSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27770f7c90cSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
27870f7c90cSmrg	     X"$dst" : 'X\(//\)$' \| \
27970f7c90cSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28070f7c90cSmrg	echo X"$dst" |
28170f7c90cSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28270f7c90cSmrg		   s//\1/
28370f7c90cSmrg		   q
28470f7c90cSmrg		 }
28570f7c90cSmrg		 /^X\(\/\/\)[^/].*/{
28670f7c90cSmrg		   s//\1/
28770f7c90cSmrg		   q
28870f7c90cSmrg		 }
28970f7c90cSmrg		 /^X\(\/\/\)$/{
29070f7c90cSmrg		   s//\1/
29170f7c90cSmrg		   q
29270f7c90cSmrg		 }
29370f7c90cSmrg		 /^X\(\/\).*/{
29470f7c90cSmrg		   s//\1/
29570f7c90cSmrg		   q
29670f7c90cSmrg		 }
29770f7c90cSmrg		 s/.*/./; q'
29870f7c90cSmrg      `
29970f7c90cSmrg
30070f7c90cSmrg      test -d "$dstdir"
30170f7c90cSmrg      dstdir_status=$?
30270f7c90cSmrg    fi
30370f7c90cSmrg  fi
30470f7c90cSmrg
30570f7c90cSmrg  obsolete_mkdir_used=false
30670f7c90cSmrg
30770f7c90cSmrg  if test $dstdir_status != 0; then
30870f7c90cSmrg    case $posix_mkdir in
30970f7c90cSmrg      '')
31070f7c90cSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
31170f7c90cSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
31270f7c90cSmrg	umask=`umask`
31370f7c90cSmrg	case $stripcmd.$umask in
31470f7c90cSmrg	  # Optimize common cases.
31570f7c90cSmrg	  *[2367][2367]) mkdir_umask=$umask;;
31670f7c90cSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31770f7c90cSmrg
31870f7c90cSmrg	  *[0-7])
31970f7c90cSmrg	    mkdir_umask=`expr $umask + 22 \
32070f7c90cSmrg	      - $umask % 100 % 40 + $umask % 20 \
32170f7c90cSmrg	      - $umask % 10 % 4 + $umask % 2
32270f7c90cSmrg	    `;;
32370f7c90cSmrg	  *) mkdir_umask=$umask,go-w;;
32470f7c90cSmrg	esac
32570f7c90cSmrg
32670f7c90cSmrg	# With -d, create the new directory with the user-specified mode.
32770f7c90cSmrg	# Otherwise, rely on $mkdir_umask.
32870f7c90cSmrg	if test -n "$dir_arg"; then
32970f7c90cSmrg	  mkdir_mode=-m$mode
330a850946eSmrg	else
33170f7c90cSmrg	  mkdir_mode=
332a850946eSmrg	fi
333a850946eSmrg
33470f7c90cSmrg	posix_mkdir=false
33570f7c90cSmrg	case $umask in
33670f7c90cSmrg	  *[123567][0-7][0-7])
33770f7c90cSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33870f7c90cSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33970f7c90cSmrg	    ;;
34070f7c90cSmrg	  *)
34170f7c90cSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34270f7c90cSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34370f7c90cSmrg
34470f7c90cSmrg	    if (umask $mkdir_umask &&
34570f7c90cSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34670f7c90cSmrg	    then
34770f7c90cSmrg	      if test -z "$dir_arg" || {
34870f7c90cSmrg		   # Check for POSIX incompatibilities with -m.
34970f7c90cSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35070f7c90cSmrg		   # other-writeable bit of parent directory when it shouldn't.
35170f7c90cSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35270f7c90cSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35370f7c90cSmrg		   case $ls_ld_tmpdir in
35470f7c90cSmrg		     d????-?r-*) different_mode=700;;
35570f7c90cSmrg		     d????-?--*) different_mode=755;;
35670f7c90cSmrg		     *) false;;
35770f7c90cSmrg		   esac &&
35870f7c90cSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35970f7c90cSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36070f7c90cSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36170f7c90cSmrg		   }
36270f7c90cSmrg		 }
36370f7c90cSmrg	      then posix_mkdir=:
36470f7c90cSmrg	      fi
36570f7c90cSmrg	      rmdir "$tmpdir/d" "$tmpdir"
36670f7c90cSmrg	    else
36770f7c90cSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
36870f7c90cSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36970f7c90cSmrg	    fi
37070f7c90cSmrg	    trap '' 0;;
37170f7c90cSmrg	esac;;
37270f7c90cSmrg    esac
373a850946eSmrg
37470f7c90cSmrg    if
37570f7c90cSmrg      $posix_mkdir && (
37670f7c90cSmrg	umask $mkdir_umask &&
37770f7c90cSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37870f7c90cSmrg      )
37970f7c90cSmrg    then :
38070f7c90cSmrg    else
38170f7c90cSmrg
38270f7c90cSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38370f7c90cSmrg      # or it failed possibly due to a race condition.  Create the
38470f7c90cSmrg      # directory the slow way, step by step, checking for races as we go.
38570f7c90cSmrg
38670f7c90cSmrg      case $dstdir in
38770f7c90cSmrg	/*) prefix='/';;
38870f7c90cSmrg	-*) prefix='./';;
38970f7c90cSmrg	*)  prefix='';;
39070f7c90cSmrg      esac
39170f7c90cSmrg
39270f7c90cSmrg      eval "$initialize_posix_glob"
39370f7c90cSmrg
39470f7c90cSmrg      oIFS=$IFS
39570f7c90cSmrg      IFS=/
39670f7c90cSmrg      $posix_glob set -f
39770f7c90cSmrg      set fnord $dstdir
39870f7c90cSmrg      shift
39970f7c90cSmrg      $posix_glob set +f
40070f7c90cSmrg      IFS=$oIFS
40170f7c90cSmrg
40270f7c90cSmrg      prefixes=
40370f7c90cSmrg
40470f7c90cSmrg      for d
40570f7c90cSmrg      do
40670f7c90cSmrg	test -z "$d" && continue
40770f7c90cSmrg
40870f7c90cSmrg	prefix=$prefix$d
40970f7c90cSmrg	if test -d "$prefix"; then
41070f7c90cSmrg	  prefixes=
41170f7c90cSmrg	else
41270f7c90cSmrg	  if $posix_mkdir; then
41370f7c90cSmrg	    (umask=$mkdir_umask &&
41470f7c90cSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41570f7c90cSmrg	    # Don't fail if two instances are running concurrently.
41670f7c90cSmrg	    test -d "$prefix" || exit 1
41770f7c90cSmrg	  else
41870f7c90cSmrg	    case $prefix in
41970f7c90cSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42070f7c90cSmrg	      *) qprefix=$prefix;;
42170f7c90cSmrg	    esac
42270f7c90cSmrg	    prefixes="$prefixes '$qprefix'"
42370f7c90cSmrg	  fi
42470f7c90cSmrg	fi
42570f7c90cSmrg	prefix=$prefix/
42670f7c90cSmrg      done
42770f7c90cSmrg
42870f7c90cSmrg      if test -n "$prefixes"; then
42970f7c90cSmrg	# Don't fail if two instances are running concurrently.
43070f7c90cSmrg	(umask $mkdir_umask &&
43170f7c90cSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43270f7c90cSmrg	  test -d "$dstdir" || exit 1
43370f7c90cSmrg	obsolete_mkdir_used=true
43470f7c90cSmrg      fi
43570f7c90cSmrg    fi
43670f7c90cSmrg  fi
43770f7c90cSmrg
43870f7c90cSmrg  if test -n "$dir_arg"; then
43970f7c90cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44070f7c90cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44170f7c90cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44270f7c90cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44370f7c90cSmrg  else
44470f7c90cSmrg
44570f7c90cSmrg    # Make a couple of temp file names in the proper directory.
44670f7c90cSmrg    dsttmp=$dstdir/_inst.$$_
44770f7c90cSmrg    rmtmp=$dstdir/_rm.$$_
44870f7c90cSmrg
44970f7c90cSmrg    # Trap to clean up those temp files at exit.
45070f7c90cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45170f7c90cSmrg
45270f7c90cSmrg    # Copy the file name to the temp name.
45370f7c90cSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45470f7c90cSmrg
45570f7c90cSmrg    # and set any options; do chmod last to preserve setuid bits.
45670f7c90cSmrg    #
45770f7c90cSmrg    # If any of these fail, we abort the whole thing.  If we want to
45870f7c90cSmrg    # ignore errors from any of these, just make sure not to ignore
45970f7c90cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46070f7c90cSmrg    #
46170f7c90cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46270f7c90cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46370f7c90cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46470f7c90cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46570f7c90cSmrg
46670f7c90cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
46770f7c90cSmrg    if $copy_on_change &&
46870f7c90cSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46970f7c90cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47070f7c90cSmrg
47170f7c90cSmrg       eval "$initialize_posix_glob" &&
47270f7c90cSmrg       $posix_glob set -f &&
47370f7c90cSmrg       set X $old && old=:$2:$4:$5:$6 &&
47470f7c90cSmrg       set X $new && new=:$2:$4:$5:$6 &&
47570f7c90cSmrg       $posix_glob set +f &&
47670f7c90cSmrg
47770f7c90cSmrg       test "$old" = "$new" &&
47870f7c90cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47970f7c90cSmrg    then
48070f7c90cSmrg      rm -f "$dsttmp"
48170f7c90cSmrg    else
48270f7c90cSmrg      # Rename the file to the real destination.
48370f7c90cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48470f7c90cSmrg
48570f7c90cSmrg      # The rename failed, perhaps because mv can't rename something else
48670f7c90cSmrg      # to itself, or perhaps because mv is so ancient that it does not
48770f7c90cSmrg      # support -f.
48870f7c90cSmrg      {
48970f7c90cSmrg	# Now remove or move aside any old file at destination location.
49070f7c90cSmrg	# We try this two ways since rm can't unlink itself on some
49170f7c90cSmrg	# systems and the destination file might be busy for other
49270f7c90cSmrg	# reasons.  In this case, the final cleanup might fail but the new
49370f7c90cSmrg	# file should still install successfully.
49470f7c90cSmrg	{
49570f7c90cSmrg	  test ! -f "$dst" ||
49670f7c90cSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49770f7c90cSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49870f7c90cSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49970f7c90cSmrg	  } ||
50070f7c90cSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
50170f7c90cSmrg	    (exit 1); exit 1
50270f7c90cSmrg	  }
50370f7c90cSmrg	} &&
50470f7c90cSmrg
50570f7c90cSmrg	# Now rename the file to the real destination.
50670f7c90cSmrg	$doit $mvcmd "$dsttmp" "$dst"
50770f7c90cSmrg      }
50870f7c90cSmrg    fi || exit 1
50970f7c90cSmrg
51070f7c90cSmrg    trap '' 0
51170f7c90cSmrg  fi
51270f7c90cSmrgdone
513a850946eSmrg
51470f7c90cSmrg# Local variables:
51570f7c90cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51670f7c90cSmrg# time-stamp-start: "scriptversion="
51770f7c90cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51870f7c90cSmrg# time-stamp-time-zone: "UTC"
51970f7c90cSmrg# time-stamp-end: "; # UTC"
52070f7c90cSmrg# End:
521