install-sh revision b020570b
123a0898aSmrg#!/bin/sh
223a0898aSmrg# install - install a program, script, or datafile
323a0898aSmrg
4b020570bSmrgscriptversion=2011-11-20.07; # UTC
523a0898aSmrg
623a0898aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
723a0898aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
823a0898aSmrg# following copyright and license.
923a0898aSmrg#
1023a0898aSmrg# Copyright (C) 1994 X Consortium
1123a0898aSmrg#
1223a0898aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1323a0898aSmrg# of this software and associated documentation files (the "Software"), to
1423a0898aSmrg# deal in the Software without restriction, including without limitation the
1523a0898aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1623a0898aSmrg# sell copies of the Software, and to permit persons to whom the Software is
1723a0898aSmrg# furnished to do so, subject to the following conditions:
1823a0898aSmrg#
1923a0898aSmrg# The above copyright notice and this permission notice shall be included in
2023a0898aSmrg# all copies or substantial portions of the Software.
2123a0898aSmrg#
2223a0898aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2323a0898aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2423a0898aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2523a0898aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2623a0898aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2723a0898aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2823a0898aSmrg#
2923a0898aSmrg# Except as contained in this notice, the name of the X Consortium shall not
3023a0898aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3123a0898aSmrg# ings in this Software without prior written authorization from the X Consor-
3223a0898aSmrg# tium.
3323a0898aSmrg#
3423a0898aSmrg#
3523a0898aSmrg# FSF changes to this file are in the public domain.
3623a0898aSmrg#
3723a0898aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38b020570bSmrg# 'make' implicit rules from creating a file called install from it
3923a0898aSmrg# when there is no Makefile.
4023a0898aSmrg#
4123a0898aSmrg# This script is compatible with the BSD install script, but was written
4223a0898aSmrg# from scratch.
4323a0898aSmrg
4423a0898aSmrgnl='
4523a0898aSmrg'
4623a0898aSmrgIFS=" ""	$nl"
4723a0898aSmrg
4823a0898aSmrg# set DOITPROG to echo to test this script
4923a0898aSmrg
5023a0898aSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5123a0898aSmrgdoit=${DOITPROG-}
5223a0898aSmrgif test -z "$doit"; then
5323a0898aSmrg  doit_exec=exec
5423a0898aSmrgelse
5523a0898aSmrg  doit_exec=$doit
5623a0898aSmrgfi
5723a0898aSmrg
5823a0898aSmrg# Put in absolute file names if you don't have them in your path;
5923a0898aSmrg# or use environment vars.
6023a0898aSmrg
6123a0898aSmrgchgrpprog=${CHGRPPROG-chgrp}
6223a0898aSmrgchmodprog=${CHMODPROG-chmod}
6323a0898aSmrgchownprog=${CHOWNPROG-chown}
6423a0898aSmrgcmpprog=${CMPPROG-cmp}
6523a0898aSmrgcpprog=${CPPROG-cp}
6623a0898aSmrgmkdirprog=${MKDIRPROG-mkdir}
6723a0898aSmrgmvprog=${MVPROG-mv}
6823a0898aSmrgrmprog=${RMPROG-rm}
6923a0898aSmrgstripprog=${STRIPPROG-strip}
7023a0898aSmrg
7123a0898aSmrgposix_glob='?'
7223a0898aSmrginitialize_posix_glob='
7323a0898aSmrg  test "$posix_glob" != "?" || {
7423a0898aSmrg    if (set -f) 2>/dev/null; then
7523a0898aSmrg      posix_glob=
7623a0898aSmrg    else
7723a0898aSmrg      posix_glob=:
7823a0898aSmrg    fi
7923a0898aSmrg  }
8023a0898aSmrg'
8123a0898aSmrg
8223a0898aSmrgposix_mkdir=
8323a0898aSmrg
8423a0898aSmrg# Desired mode of installed file.
8523a0898aSmrgmode=0755
8623a0898aSmrg
8723a0898aSmrgchgrpcmd=
8823a0898aSmrgchmodcmd=$chmodprog
8923a0898aSmrgchowncmd=
9023a0898aSmrgmvcmd=$mvprog
9123a0898aSmrgrmcmd="$rmprog -f"
9223a0898aSmrgstripcmd=
9323a0898aSmrg
9423a0898aSmrgsrc=
9523a0898aSmrgdst=
9623a0898aSmrgdir_arg=
9723a0898aSmrgdst_arg=
9823a0898aSmrg
9923a0898aSmrgcopy_on_change=false
10023a0898aSmrgno_target_directory=
10123a0898aSmrg
10223a0898aSmrgusage="\
10323a0898aSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10423a0898aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10523a0898aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10623a0898aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
10723a0898aSmrg
10823a0898aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
10923a0898aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11023a0898aSmrgIn the 4th, create DIRECTORIES.
11123a0898aSmrg
11223a0898aSmrgOptions:
11323a0898aSmrg     --help     display this help and exit.
11423a0898aSmrg     --version  display version info and exit.
11523a0898aSmrg
11623a0898aSmrg  -c            (ignored)
11723a0898aSmrg  -C            install only if different (preserve the last data modification time)
11823a0898aSmrg  -d            create directories instead of installing files.
11923a0898aSmrg  -g GROUP      $chgrpprog installed files to GROUP.
12023a0898aSmrg  -m MODE       $chmodprog installed files to MODE.
12123a0898aSmrg  -o USER       $chownprog installed files to USER.
12223a0898aSmrg  -s            $stripprog installed files.
12323a0898aSmrg  -t DIRECTORY  install into DIRECTORY.
12423a0898aSmrg  -T            report an error if DSTFILE is a directory.
12523a0898aSmrg
12623a0898aSmrgEnvironment variables override the default commands:
12723a0898aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12823a0898aSmrg  RMPROG STRIPPROG
12923a0898aSmrg"
13023a0898aSmrg
13123a0898aSmrgwhile test $# -ne 0; do
13223a0898aSmrg  case $1 in
13323a0898aSmrg    -c) ;;
13423a0898aSmrg
13523a0898aSmrg    -C) copy_on_change=true;;
13623a0898aSmrg
13723a0898aSmrg    -d) dir_arg=true;;
13823a0898aSmrg
13923a0898aSmrg    -g) chgrpcmd="$chgrpprog $2"
14023a0898aSmrg	shift;;
14123a0898aSmrg
14223a0898aSmrg    --help) echo "$usage"; exit $?;;
14323a0898aSmrg
14423a0898aSmrg    -m) mode=$2
14523a0898aSmrg	case $mode in
14623a0898aSmrg	  *' '* | *'	'* | *'
14723a0898aSmrg'*	  | *'*'* | *'?'* | *'['*)
14823a0898aSmrg	    echo "$0: invalid mode: $mode" >&2
14923a0898aSmrg	    exit 1;;
15023a0898aSmrg	esac
15123a0898aSmrg	shift;;
15223a0898aSmrg
15323a0898aSmrg    -o) chowncmd="$chownprog $2"
15423a0898aSmrg	shift;;
15523a0898aSmrg
15623a0898aSmrg    -s) stripcmd=$stripprog;;
15723a0898aSmrg
15823a0898aSmrg    -t) dst_arg=$2
159b020570bSmrg	# Protect names problematic for 'test' and other utilities.
160b020570bSmrg	case $dst_arg in
161b020570bSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162b020570bSmrg	esac
16323a0898aSmrg	shift;;
16423a0898aSmrg
16523a0898aSmrg    -T) no_target_directory=true;;
16623a0898aSmrg
16723a0898aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
16823a0898aSmrg
16923a0898aSmrg    --)	shift
17023a0898aSmrg	break;;
17123a0898aSmrg
17223a0898aSmrg    -*)	echo "$0: invalid option: $1" >&2
17323a0898aSmrg	exit 1;;
17423a0898aSmrg
17523a0898aSmrg    *)  break;;
17623a0898aSmrg  esac
17723a0898aSmrg  shift
17823a0898aSmrgdone
17923a0898aSmrg
18023a0898aSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
18123a0898aSmrg  # When -d is used, all remaining arguments are directories to create.
18223a0898aSmrg  # When -t is used, the destination is already specified.
18323a0898aSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18423a0898aSmrg  for arg
18523a0898aSmrg  do
18623a0898aSmrg    if test -n "$dst_arg"; then
18723a0898aSmrg      # $@ is not empty: it contains at least $arg.
18823a0898aSmrg      set fnord "$@" "$dst_arg"
18923a0898aSmrg      shift # fnord
19023a0898aSmrg    fi
19123a0898aSmrg    shift # arg
19223a0898aSmrg    dst_arg=$arg
193b020570bSmrg    # Protect names problematic for 'test' and other utilities.
194b020570bSmrg    case $dst_arg in
195b020570bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196b020570bSmrg    esac
19723a0898aSmrg  done
19823a0898aSmrgfi
19923a0898aSmrg
20023a0898aSmrgif test $# -eq 0; then
20123a0898aSmrg  if test -z "$dir_arg"; then
20223a0898aSmrg    echo "$0: no input file specified." >&2
20323a0898aSmrg    exit 1
20423a0898aSmrg  fi
205b020570bSmrg  # It's OK to call 'install-sh -d' without argument.
20623a0898aSmrg  # This can happen when creating conditional directories.
20723a0898aSmrg  exit 0
20823a0898aSmrgfi
20923a0898aSmrg
21023a0898aSmrgif test -z "$dir_arg"; then
211b020570bSmrg  do_exit='(exit $ret); exit $ret'
212b020570bSmrg  trap "ret=129; $do_exit" 1
213b020570bSmrg  trap "ret=130; $do_exit" 2
214b020570bSmrg  trap "ret=141; $do_exit" 13
215b020570bSmrg  trap "ret=143; $do_exit" 15
21623a0898aSmrg
21723a0898aSmrg  # Set umask so as not to create temps with too-generous modes.
21823a0898aSmrg  # However, 'strip' requires both read and write access to temps.
21923a0898aSmrg  case $mode in
22023a0898aSmrg    # Optimize common cases.
22123a0898aSmrg    *644) cp_umask=133;;
22223a0898aSmrg    *755) cp_umask=22;;
22323a0898aSmrg
22423a0898aSmrg    *[0-7])
22523a0898aSmrg      if test -z "$stripcmd"; then
22623a0898aSmrg	u_plus_rw=
22723a0898aSmrg      else
22823a0898aSmrg	u_plus_rw='% 200'
22923a0898aSmrg      fi
23023a0898aSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23123a0898aSmrg    *)
23223a0898aSmrg      if test -z "$stripcmd"; then
23323a0898aSmrg	u_plus_rw=
23423a0898aSmrg      else
23523a0898aSmrg	u_plus_rw=,u+rw
23623a0898aSmrg      fi
23723a0898aSmrg      cp_umask=$mode$u_plus_rw;;
23823a0898aSmrg  esac
23923a0898aSmrgfi
24023a0898aSmrg
24123a0898aSmrgfor src
24223a0898aSmrgdo
243b020570bSmrg  # Protect names problematic for 'test' and other utilities.
24423a0898aSmrg  case $src in
245b020570bSmrg    -* | [=\(\)!]) src=./$src;;
24623a0898aSmrg  esac
24723a0898aSmrg
24823a0898aSmrg  if test -n "$dir_arg"; then
24923a0898aSmrg    dst=$src
25023a0898aSmrg    dstdir=$dst
25123a0898aSmrg    test -d "$dstdir"
25223a0898aSmrg    dstdir_status=$?
25323a0898aSmrg  else
25423a0898aSmrg
25523a0898aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
25623a0898aSmrg    # might cause directories to be created, which would be especially bad
25723a0898aSmrg    # if $src (and thus $dsttmp) contains '*'.
25823a0898aSmrg    if test ! -f "$src" && test ! -d "$src"; then
25923a0898aSmrg      echo "$0: $src does not exist." >&2
26023a0898aSmrg      exit 1
26123a0898aSmrg    fi
26223a0898aSmrg
26323a0898aSmrg    if test -z "$dst_arg"; then
26423a0898aSmrg      echo "$0: no destination specified." >&2
26523a0898aSmrg      exit 1
26623a0898aSmrg    fi
26723a0898aSmrg    dst=$dst_arg
26823a0898aSmrg
26923a0898aSmrg    # If destination is a directory, append the input filename; won't work
27023a0898aSmrg    # if double slashes aren't ignored.
27123a0898aSmrg    if test -d "$dst"; then
27223a0898aSmrg      if test -n "$no_target_directory"; then
27323a0898aSmrg	echo "$0: $dst_arg: Is a directory" >&2
27423a0898aSmrg	exit 1
27523a0898aSmrg      fi
27623a0898aSmrg      dstdir=$dst
27723a0898aSmrg      dst=$dstdir/`basename "$src"`
27823a0898aSmrg      dstdir_status=0
27923a0898aSmrg    else
28023a0898aSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
28123a0898aSmrg      dstdir=`
28223a0898aSmrg	(dirname "$dst") 2>/dev/null ||
28323a0898aSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
28423a0898aSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
28523a0898aSmrg	     X"$dst" : 'X\(//\)$' \| \
28623a0898aSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28723a0898aSmrg	echo X"$dst" |
28823a0898aSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28923a0898aSmrg		   s//\1/
29023a0898aSmrg		   q
29123a0898aSmrg		 }
29223a0898aSmrg		 /^X\(\/\/\)[^/].*/{
29323a0898aSmrg		   s//\1/
29423a0898aSmrg		   q
29523a0898aSmrg		 }
29623a0898aSmrg		 /^X\(\/\/\)$/{
29723a0898aSmrg		   s//\1/
29823a0898aSmrg		   q
29923a0898aSmrg		 }
30023a0898aSmrg		 /^X\(\/\).*/{
30123a0898aSmrg		   s//\1/
30223a0898aSmrg		   q
30323a0898aSmrg		 }
30423a0898aSmrg		 s/.*/./; q'
30523a0898aSmrg      `
30623a0898aSmrg
30723a0898aSmrg      test -d "$dstdir"
30823a0898aSmrg      dstdir_status=$?
30923a0898aSmrg    fi
31023a0898aSmrg  fi
31123a0898aSmrg
31223a0898aSmrg  obsolete_mkdir_used=false
31323a0898aSmrg
31423a0898aSmrg  if test $dstdir_status != 0; then
31523a0898aSmrg    case $posix_mkdir in
31623a0898aSmrg      '')
31723a0898aSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
31823a0898aSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
31923a0898aSmrg	umask=`umask`
32023a0898aSmrg	case $stripcmd.$umask in
32123a0898aSmrg	  # Optimize common cases.
32223a0898aSmrg	  *[2367][2367]) mkdir_umask=$umask;;
32323a0898aSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
32423a0898aSmrg
32523a0898aSmrg	  *[0-7])
32623a0898aSmrg	    mkdir_umask=`expr $umask + 22 \
32723a0898aSmrg	      - $umask % 100 % 40 + $umask % 20 \
32823a0898aSmrg	      - $umask % 10 % 4 + $umask % 2
32923a0898aSmrg	    `;;
33023a0898aSmrg	  *) mkdir_umask=$umask,go-w;;
33123a0898aSmrg	esac
33223a0898aSmrg
33323a0898aSmrg	# With -d, create the new directory with the user-specified mode.
33423a0898aSmrg	# Otherwise, rely on $mkdir_umask.
33523a0898aSmrg	if test -n "$dir_arg"; then
33623a0898aSmrg	  mkdir_mode=-m$mode
33723a0898aSmrg	else
33823a0898aSmrg	  mkdir_mode=
33923a0898aSmrg	fi
34023a0898aSmrg
34123a0898aSmrg	posix_mkdir=false
34223a0898aSmrg	case $umask in
34323a0898aSmrg	  *[123567][0-7][0-7])
34423a0898aSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
34523a0898aSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
34623a0898aSmrg	    ;;
34723a0898aSmrg	  *)
34823a0898aSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34923a0898aSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
35023a0898aSmrg
35123a0898aSmrg	    if (umask $mkdir_umask &&
35223a0898aSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
35323a0898aSmrg	    then
35423a0898aSmrg	      if test -z "$dir_arg" || {
35523a0898aSmrg		   # Check for POSIX incompatibilities with -m.
35623a0898aSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357b020570bSmrg		   # other-writable bit of parent directory when it shouldn't.
35823a0898aSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35923a0898aSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
36023a0898aSmrg		   case $ls_ld_tmpdir in
36123a0898aSmrg		     d????-?r-*) different_mode=700;;
36223a0898aSmrg		     d????-?--*) different_mode=755;;
36323a0898aSmrg		     *) false;;
36423a0898aSmrg		   esac &&
36523a0898aSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
36623a0898aSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36723a0898aSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36823a0898aSmrg		   }
36923a0898aSmrg		 }
37023a0898aSmrg	      then posix_mkdir=:
37123a0898aSmrg	      fi
37223a0898aSmrg	      rmdir "$tmpdir/d" "$tmpdir"
37323a0898aSmrg	    else
37423a0898aSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
37523a0898aSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
37623a0898aSmrg	    fi
37723a0898aSmrg	    trap '' 0;;
37823a0898aSmrg	esac;;
37923a0898aSmrg    esac
38023a0898aSmrg
38123a0898aSmrg    if
38223a0898aSmrg      $posix_mkdir && (
38323a0898aSmrg	umask $mkdir_umask &&
38423a0898aSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38523a0898aSmrg      )
38623a0898aSmrg    then :
38723a0898aSmrg    else
38823a0898aSmrg
38923a0898aSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
39023a0898aSmrg      # or it failed possibly due to a race condition.  Create the
39123a0898aSmrg      # directory the slow way, step by step, checking for races as we go.
39223a0898aSmrg
39323a0898aSmrg      case $dstdir in
39423a0898aSmrg	/*) prefix='/';;
395b020570bSmrg	[-=\(\)!]*) prefix='./';;
39623a0898aSmrg	*)  prefix='';;
39723a0898aSmrg      esac
39823a0898aSmrg
39923a0898aSmrg      eval "$initialize_posix_glob"
40023a0898aSmrg
40123a0898aSmrg      oIFS=$IFS
40223a0898aSmrg      IFS=/
40323a0898aSmrg      $posix_glob set -f
40423a0898aSmrg      set fnord $dstdir
40523a0898aSmrg      shift
40623a0898aSmrg      $posix_glob set +f
40723a0898aSmrg      IFS=$oIFS
40823a0898aSmrg
40923a0898aSmrg      prefixes=
41023a0898aSmrg
41123a0898aSmrg      for d
41223a0898aSmrg      do
413b020570bSmrg	test X"$d" = X && continue
41423a0898aSmrg
41523a0898aSmrg	prefix=$prefix$d
41623a0898aSmrg	if test -d "$prefix"; then
41723a0898aSmrg	  prefixes=
41823a0898aSmrg	else
41923a0898aSmrg	  if $posix_mkdir; then
42023a0898aSmrg	    (umask=$mkdir_umask &&
42123a0898aSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42223a0898aSmrg	    # Don't fail if two instances are running concurrently.
42323a0898aSmrg	    test -d "$prefix" || exit 1
42423a0898aSmrg	  else
42523a0898aSmrg	    case $prefix in
42623a0898aSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42723a0898aSmrg	      *) qprefix=$prefix;;
42823a0898aSmrg	    esac
42923a0898aSmrg	    prefixes="$prefixes '$qprefix'"
43023a0898aSmrg	  fi
43123a0898aSmrg	fi
43223a0898aSmrg	prefix=$prefix/
43323a0898aSmrg      done
43423a0898aSmrg
43523a0898aSmrg      if test -n "$prefixes"; then
43623a0898aSmrg	# Don't fail if two instances are running concurrently.
43723a0898aSmrg	(umask $mkdir_umask &&
43823a0898aSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43923a0898aSmrg	  test -d "$dstdir" || exit 1
44023a0898aSmrg	obsolete_mkdir_used=true
44123a0898aSmrg      fi
44223a0898aSmrg    fi
44323a0898aSmrg  fi
44423a0898aSmrg
44523a0898aSmrg  if test -n "$dir_arg"; then
44623a0898aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44723a0898aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44823a0898aSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44923a0898aSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
45023a0898aSmrg  else
45123a0898aSmrg
45223a0898aSmrg    # Make a couple of temp file names in the proper directory.
45323a0898aSmrg    dsttmp=$dstdir/_inst.$$_
45423a0898aSmrg    rmtmp=$dstdir/_rm.$$_
45523a0898aSmrg
45623a0898aSmrg    # Trap to clean up those temp files at exit.
45723a0898aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45823a0898aSmrg
45923a0898aSmrg    # Copy the file name to the temp name.
46023a0898aSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
46123a0898aSmrg
46223a0898aSmrg    # and set any options; do chmod last to preserve setuid bits.
46323a0898aSmrg    #
46423a0898aSmrg    # If any of these fail, we abort the whole thing.  If we want to
46523a0898aSmrg    # ignore errors from any of these, just make sure not to ignore
46623a0898aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46723a0898aSmrg    #
46823a0898aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46923a0898aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
47023a0898aSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
47123a0898aSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
47223a0898aSmrg
47323a0898aSmrg    # If -C, don't bother to copy if it wouldn't change the file.
47423a0898aSmrg    if $copy_on_change &&
47523a0898aSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
47623a0898aSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47723a0898aSmrg
47823a0898aSmrg       eval "$initialize_posix_glob" &&
47923a0898aSmrg       $posix_glob set -f &&
48023a0898aSmrg       set X $old && old=:$2:$4:$5:$6 &&
48123a0898aSmrg       set X $new && new=:$2:$4:$5:$6 &&
48223a0898aSmrg       $posix_glob set +f &&
48323a0898aSmrg
48423a0898aSmrg       test "$old" = "$new" &&
48523a0898aSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
48623a0898aSmrg    then
48723a0898aSmrg      rm -f "$dsttmp"
48823a0898aSmrg    else
48923a0898aSmrg      # Rename the file to the real destination.
49023a0898aSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
49123a0898aSmrg
49223a0898aSmrg      # The rename failed, perhaps because mv can't rename something else
49323a0898aSmrg      # to itself, or perhaps because mv is so ancient that it does not
49423a0898aSmrg      # support -f.
49523a0898aSmrg      {
49623a0898aSmrg	# Now remove or move aside any old file at destination location.
49723a0898aSmrg	# We try this two ways since rm can't unlink itself on some
49823a0898aSmrg	# systems and the destination file might be busy for other
49923a0898aSmrg	# reasons.  In this case, the final cleanup might fail but the new
50023a0898aSmrg	# file should still install successfully.
50123a0898aSmrg	{
50223a0898aSmrg	  test ! -f "$dst" ||
50323a0898aSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
50423a0898aSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
50523a0898aSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
50623a0898aSmrg	  } ||
50723a0898aSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
50823a0898aSmrg	    (exit 1); exit 1
50923a0898aSmrg	  }
51023a0898aSmrg	} &&
51123a0898aSmrg
51223a0898aSmrg	# Now rename the file to the real destination.
51323a0898aSmrg	$doit $mvcmd "$dsttmp" "$dst"
51423a0898aSmrg      }
51523a0898aSmrg    fi || exit 1
51623a0898aSmrg
51723a0898aSmrg    trap '' 0
51823a0898aSmrg  fi
51923a0898aSmrgdone
52023a0898aSmrg
52123a0898aSmrg# Local variables:
52223a0898aSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
52323a0898aSmrg# time-stamp-start: "scriptversion="
52423a0898aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
52541c30155Smrg# time-stamp-time-zone: "UTC"
52641c30155Smrg# time-stamp-end: "; # UTC"
52723a0898aSmrg# End:
528