1566738d7Smrg#!/bin/sh
2566738d7Smrg# install - install a program, script, or datafile
350718ea6Smrg
450718ea6Smrgscriptversion=2009-04-28.21; # UTC
550718ea6Smrg
6566738d7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7566738d7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8566738d7Smrg# following copyright and license.
9566738d7Smrg#
10566738d7Smrg# Copyright (C) 1994 X Consortium
11566738d7Smrg#
12566738d7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13566738d7Smrg# of this software and associated documentation files (the "Software"), to
14566738d7Smrg# deal in the Software without restriction, including without limitation the
15566738d7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16566738d7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17566738d7Smrg# furnished to do so, subject to the following conditions:
18566738d7Smrg#
19566738d7Smrg# The above copyright notice and this permission notice shall be included in
20566738d7Smrg# all copies or substantial portions of the Software.
21566738d7Smrg#
22566738d7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23566738d7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24566738d7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25566738d7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26566738d7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27566738d7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28566738d7Smrg#
29566738d7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30566738d7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31566738d7Smrg# ings in this Software without prior written authorization from the X Consor-
32566738d7Smrg# tium.
33566738d7Smrg#
34566738d7Smrg#
35566738d7Smrg# FSF changes to this file are in the public domain.
36566738d7Smrg#
37566738d7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38566738d7Smrg# `make' implicit rules from creating a file called install from it
39566738d7Smrg# when there is no Makefile.
40566738d7Smrg#
41566738d7Smrg# This script is compatible with the BSD install script, but was written
4250718ea6Smrg# from scratch.
43566738d7Smrg
4450718ea6Smrgnl='
4550718ea6Smrg'
4650718ea6SmrgIFS=" ""	$nl"
47566738d7Smrg
48566738d7Smrg# set DOITPROG to echo to test this script
49566738d7Smrg
50566738d7Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5150718ea6Smrgdoit=${DOITPROG-}
5250718ea6Smrgif test -z "$doit"; then
5350718ea6Smrg  doit_exec=exec
54566738d7Smrgelse
5550718ea6Smrg  doit_exec=$doit
56566738d7Smrgfi
57566738d7Smrg
5850718ea6Smrg# Put in absolute file names if you don't have them in your path;
5950718ea6Smrg# or use environment vars.
6050718ea6Smrg
6150718ea6Smrgchgrpprog=${CHGRPPROG-chgrp}
6250718ea6Smrgchmodprog=${CHMODPROG-chmod}
6350718ea6Smrgchownprog=${CHOWNPROG-chown}
6450718ea6Smrgcmpprog=${CMPPROG-cmp}
6550718ea6Smrgcpprog=${CPPROG-cp}
6650718ea6Smrgmkdirprog=${MKDIRPROG-mkdir}
6750718ea6Smrgmvprog=${MVPROG-mv}
6850718ea6Smrgrmprog=${RMPROG-rm}
6950718ea6Smrgstripprog=${STRIPPROG-strip}
7050718ea6Smrg
7150718ea6Smrgposix_glob='?'
7250718ea6Smrginitialize_posix_glob='
7350718ea6Smrg  test "$posix_glob" != "?" || {
7450718ea6Smrg    if (set -f) 2>/dev/null; then
7550718ea6Smrg      posix_glob=
7650718ea6Smrg    else
7750718ea6Smrg      posix_glob=:
7850718ea6Smrg    fi
7950718ea6Smrg  }
8050718ea6Smrg'
8150718ea6Smrg
8250718ea6Smrgposix_mkdir=
8350718ea6Smrg
8450718ea6Smrg# Desired mode of installed file.
8550718ea6Smrgmode=0755
8650718ea6Smrg
8750718ea6Smrgchgrpcmd=
8850718ea6Smrgchmodcmd=$chmodprog
8950718ea6Smrgchowncmd=
9050718ea6Smrgmvcmd=$mvprog
9150718ea6Smrgrmcmd="$rmprog -f"
9250718ea6Smrgstripcmd=
93566738d7Smrg
9450718ea6Smrgsrc=
9550718ea6Smrgdst=
9650718ea6Smrgdir_arg=
9750718ea6Smrgdst_arg=
98566738d7Smrg
9950718ea6Smrgcopy_on_change=false
10050718ea6Smrgno_target_directory=
101566738d7Smrg
10250718ea6Smrgusage="\
10350718ea6SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10450718ea6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10550718ea6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10650718ea6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107566738d7Smrg
10850718ea6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10950718ea6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11050718ea6SmrgIn the 4th, create DIRECTORIES.
111566738d7Smrg
11250718ea6SmrgOptions:
11350718ea6Smrg     --help     display this help and exit.
11450718ea6Smrg     --version  display version info and exit.
115566738d7Smrg
11650718ea6Smrg  -c            (ignored)
11750718ea6Smrg  -C            install only if different (preserve the last data modification time)
11850718ea6Smrg  -d            create directories instead of installing files.
11950718ea6Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12050718ea6Smrg  -m MODE       $chmodprog installed files to MODE.
12150718ea6Smrg  -o USER       $chownprog installed files to USER.
12250718ea6Smrg  -s            $stripprog installed files.
12350718ea6Smrg  -t DIRECTORY  install into DIRECTORY.
12450718ea6Smrg  -T            report an error if DSTFILE is a directory.
125566738d7Smrg
12650718ea6SmrgEnvironment variables override the default commands:
12750718ea6Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12850718ea6Smrg  RMPROG STRIPPROG
12950718ea6Smrg"
130566738d7Smrg
13150718ea6Smrgwhile test $# -ne 0; do
13250718ea6Smrg  case $1 in
13350718ea6Smrg    -c) ;;
134566738d7Smrg
13550718ea6Smrg    -C) copy_on_change=true;;
136566738d7Smrg
13750718ea6Smrg    -d) dir_arg=true;;
138566738d7Smrg
13950718ea6Smrg    -g) chgrpcmd="$chgrpprog $2"
14050718ea6Smrg	shift;;
141566738d7Smrg
14250718ea6Smrg    --help) echo "$usage"; exit $?;;
143566738d7Smrg
14450718ea6Smrg    -m) mode=$2
14550718ea6Smrg	case $mode in
14650718ea6Smrg	  *' '* | *'	'* | *'
14750718ea6Smrg'*	  | *'*'* | *'?'* | *'['*)
14850718ea6Smrg	    echo "$0: invalid mode: $mode" >&2
14950718ea6Smrg	    exit 1;;
15050718ea6Smrg	esac
15150718ea6Smrg	shift;;
152566738d7Smrg
15350718ea6Smrg    -o) chowncmd="$chownprog $2"
15450718ea6Smrg	shift;;
155566738d7Smrg
15650718ea6Smrg    -s) stripcmd=$stripprog;;
157566738d7Smrg
15850718ea6Smrg    -t) dst_arg=$2
15950718ea6Smrg	shift;;
160566738d7Smrg
16150718ea6Smrg    -T) no_target_directory=true;;
162566738d7Smrg
16350718ea6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164566738d7Smrg
16550718ea6Smrg    --)	shift
16650718ea6Smrg	break;;
167566738d7Smrg
16850718ea6Smrg    -*)	echo "$0: invalid option: $1" >&2
16950718ea6Smrg	exit 1;;
170566738d7Smrg
17150718ea6Smrg    *)  break;;
17250718ea6Smrg  esac
17350718ea6Smrg  shift
17450718ea6Smrgdone
175566738d7Smrg
17650718ea6Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17750718ea6Smrg  # When -d is used, all remaining arguments are directories to create.
17850718ea6Smrg  # When -t is used, the destination is already specified.
17950718ea6Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18050718ea6Smrg  for arg
18150718ea6Smrg  do
18250718ea6Smrg    if test -n "$dst_arg"; then
18350718ea6Smrg      # $@ is not empty: it contains at least $arg.
18450718ea6Smrg      set fnord "$@" "$dst_arg"
18550718ea6Smrg      shift # fnord
18650718ea6Smrg    fi
18750718ea6Smrg    shift # arg
18850718ea6Smrg    dst_arg=$arg
18950718ea6Smrg  done
19050718ea6Smrgfi
191566738d7Smrg
19250718ea6Smrgif test $# -eq 0; then
19350718ea6Smrg  if test -z "$dir_arg"; then
19450718ea6Smrg    echo "$0: no input file specified." >&2
19550718ea6Smrg    exit 1
19650718ea6Smrg  fi
19750718ea6Smrg  # It's OK to call `install-sh -d' without argument.
19850718ea6Smrg  # This can happen when creating conditional directories.
19950718ea6Smrg  exit 0
20050718ea6Smrgfi
201566738d7Smrg
20250718ea6Smrgif test -z "$dir_arg"; then
20350718ea6Smrg  trap '(exit $?); exit' 1 2 13 15
20450718ea6Smrg
20550718ea6Smrg  # Set umask so as not to create temps with too-generous modes.
20650718ea6Smrg  # However, 'strip' requires both read and write access to temps.
20750718ea6Smrg  case $mode in
20850718ea6Smrg    # Optimize common cases.
20950718ea6Smrg    *644) cp_umask=133;;
21050718ea6Smrg    *755) cp_umask=22;;
21150718ea6Smrg
21250718ea6Smrg    *[0-7])
21350718ea6Smrg      if test -z "$stripcmd"; then
21450718ea6Smrg	u_plus_rw=
21550718ea6Smrg      else
21650718ea6Smrg	u_plus_rw='% 200'
21750718ea6Smrg      fi
21850718ea6Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21950718ea6Smrg    *)
22050718ea6Smrg      if test -z "$stripcmd"; then
22150718ea6Smrg	u_plus_rw=
22250718ea6Smrg      else
22350718ea6Smrg	u_plus_rw=,u+rw
22450718ea6Smrg      fi
22550718ea6Smrg      cp_umask=$mode$u_plus_rw;;
22650718ea6Smrg  esac
22750718ea6Smrgfi
228566738d7Smrg
22950718ea6Smrgfor src
23050718ea6Smrgdo
23150718ea6Smrg  # Protect names starting with `-'.
23250718ea6Smrg  case $src in
23350718ea6Smrg    -*) src=./$src;;
23450718ea6Smrg  esac
23550718ea6Smrg
23650718ea6Smrg  if test -n "$dir_arg"; then
23750718ea6Smrg    dst=$src
23850718ea6Smrg    dstdir=$dst
23950718ea6Smrg    test -d "$dstdir"
24050718ea6Smrg    dstdir_status=$?
24150718ea6Smrg  else
24250718ea6Smrg
24350718ea6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24450718ea6Smrg    # might cause directories to be created, which would be especially bad
24550718ea6Smrg    # if $src (and thus $dsttmp) contains '*'.
24650718ea6Smrg    if test ! -f "$src" && test ! -d "$src"; then
24750718ea6Smrg      echo "$0: $src does not exist." >&2
24850718ea6Smrg      exit 1
24950718ea6Smrg    fi
25050718ea6Smrg
25150718ea6Smrg    if test -z "$dst_arg"; then
25250718ea6Smrg      echo "$0: no destination specified." >&2
25350718ea6Smrg      exit 1
25450718ea6Smrg    fi
25550718ea6Smrg
25650718ea6Smrg    dst=$dst_arg
25750718ea6Smrg    # Protect names starting with `-'.
25850718ea6Smrg    case $dst in
25950718ea6Smrg      -*) dst=./$dst;;
26050718ea6Smrg    esac
261566738d7Smrg
26250718ea6Smrg    # If destination is a directory, append the input filename; won't work
26350718ea6Smrg    # if double slashes aren't ignored.
26450718ea6Smrg    if test -d "$dst"; then
26550718ea6Smrg      if test -n "$no_target_directory"; then
26650718ea6Smrg	echo "$0: $dst_arg: Is a directory" >&2
26750718ea6Smrg	exit 1
26850718ea6Smrg      fi
26950718ea6Smrg      dstdir=$dst
27050718ea6Smrg      dst=$dstdir/`basename "$src"`
27150718ea6Smrg      dstdir_status=0
27250718ea6Smrg    else
27350718ea6Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27450718ea6Smrg      dstdir=`
27550718ea6Smrg	(dirname "$dst") 2>/dev/null ||
27650718ea6Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27750718ea6Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27850718ea6Smrg	     X"$dst" : 'X\(//\)$' \| \
27950718ea6Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28050718ea6Smrg	echo X"$dst" |
28150718ea6Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28250718ea6Smrg		   s//\1/
28350718ea6Smrg		   q
28450718ea6Smrg		 }
28550718ea6Smrg		 /^X\(\/\/\)[^/].*/{
28650718ea6Smrg		   s//\1/
28750718ea6Smrg		   q
28850718ea6Smrg		 }
28950718ea6Smrg		 /^X\(\/\/\)$/{
29050718ea6Smrg		   s//\1/
29150718ea6Smrg		   q
29250718ea6Smrg		 }
29350718ea6Smrg		 /^X\(\/\).*/{
29450718ea6Smrg		   s//\1/
29550718ea6Smrg		   q
29650718ea6Smrg		 }
29750718ea6Smrg		 s/.*/./; q'
29850718ea6Smrg      `
29950718ea6Smrg
30050718ea6Smrg      test -d "$dstdir"
30150718ea6Smrg      dstdir_status=$?
30250718ea6Smrg    fi
30350718ea6Smrg  fi
30450718ea6Smrg
30550718ea6Smrg  obsolete_mkdir_used=false
30650718ea6Smrg
30750718ea6Smrg  if test $dstdir_status != 0; then
30850718ea6Smrg    case $posix_mkdir in
30950718ea6Smrg      '')
31050718ea6Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31150718ea6Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31250718ea6Smrg	umask=`umask`
31350718ea6Smrg	case $stripcmd.$umask in
31450718ea6Smrg	  # Optimize common cases.
31550718ea6Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31650718ea6Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31750718ea6Smrg
31850718ea6Smrg	  *[0-7])
31950718ea6Smrg	    mkdir_umask=`expr $umask + 22 \
32050718ea6Smrg	      - $umask % 100 % 40 + $umask % 20 \
32150718ea6Smrg	      - $umask % 10 % 4 + $umask % 2
32250718ea6Smrg	    `;;
32350718ea6Smrg	  *) mkdir_umask=$umask,go-w;;
32450718ea6Smrg	esac
32550718ea6Smrg
32650718ea6Smrg	# With -d, create the new directory with the user-specified mode.
32750718ea6Smrg	# Otherwise, rely on $mkdir_umask.
32850718ea6Smrg	if test -n "$dir_arg"; then
32950718ea6Smrg	  mkdir_mode=-m$mode
330566738d7Smrg	else
33150718ea6Smrg	  mkdir_mode=
332566738d7Smrg	fi
333566738d7Smrg
33450718ea6Smrg	posix_mkdir=false
33550718ea6Smrg	case $umask in
33650718ea6Smrg	  *[123567][0-7][0-7])
33750718ea6Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33850718ea6Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33950718ea6Smrg	    ;;
34050718ea6Smrg	  *)
34150718ea6Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34250718ea6Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34350718ea6Smrg
34450718ea6Smrg	    if (umask $mkdir_umask &&
34550718ea6Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34650718ea6Smrg	    then
34750718ea6Smrg	      if test -z "$dir_arg" || {
34850718ea6Smrg		   # Check for POSIX incompatibilities with -m.
34950718ea6Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35050718ea6Smrg		   # other-writeable bit of parent directory when it shouldn't.
35150718ea6Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35250718ea6Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35350718ea6Smrg		   case $ls_ld_tmpdir in
35450718ea6Smrg		     d????-?r-*) different_mode=700;;
35550718ea6Smrg		     d????-?--*) different_mode=755;;
35650718ea6Smrg		     *) false;;
35750718ea6Smrg		   esac &&
35850718ea6Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35950718ea6Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36050718ea6Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36150718ea6Smrg		   }
36250718ea6Smrg		 }
36350718ea6Smrg	      then posix_mkdir=:
36450718ea6Smrg	      fi
36550718ea6Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36650718ea6Smrg	    else
36750718ea6Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36850718ea6Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36950718ea6Smrg	    fi
37050718ea6Smrg	    trap '' 0;;
37150718ea6Smrg	esac;;
37250718ea6Smrg    esac
373566738d7Smrg
37450718ea6Smrg    if
37550718ea6Smrg      $posix_mkdir && (
37650718ea6Smrg	umask $mkdir_umask &&
37750718ea6Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37850718ea6Smrg      )
37950718ea6Smrg    then :
38050718ea6Smrg    else
38150718ea6Smrg
38250718ea6Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38350718ea6Smrg      # or it failed possibly due to a race condition.  Create the
38450718ea6Smrg      # directory the slow way, step by step, checking for races as we go.
38550718ea6Smrg
38650718ea6Smrg      case $dstdir in
38750718ea6Smrg	/*) prefix='/';;
38850718ea6Smrg	-*) prefix='./';;
38950718ea6Smrg	*)  prefix='';;
39050718ea6Smrg      esac
39150718ea6Smrg
39250718ea6Smrg      eval "$initialize_posix_glob"
39350718ea6Smrg
39450718ea6Smrg      oIFS=$IFS
39550718ea6Smrg      IFS=/
39650718ea6Smrg      $posix_glob set -f
39750718ea6Smrg      set fnord $dstdir
39850718ea6Smrg      shift
39950718ea6Smrg      $posix_glob set +f
40050718ea6Smrg      IFS=$oIFS
40150718ea6Smrg
40250718ea6Smrg      prefixes=
40350718ea6Smrg
40450718ea6Smrg      for d
40550718ea6Smrg      do
40650718ea6Smrg	test -z "$d" && continue
40750718ea6Smrg
40850718ea6Smrg	prefix=$prefix$d
40950718ea6Smrg	if test -d "$prefix"; then
41050718ea6Smrg	  prefixes=
41150718ea6Smrg	else
41250718ea6Smrg	  if $posix_mkdir; then
41350718ea6Smrg	    (umask=$mkdir_umask &&
41450718ea6Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41550718ea6Smrg	    # Don't fail if two instances are running concurrently.
41650718ea6Smrg	    test -d "$prefix" || exit 1
41750718ea6Smrg	  else
41850718ea6Smrg	    case $prefix in
41950718ea6Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42050718ea6Smrg	      *) qprefix=$prefix;;
42150718ea6Smrg	    esac
42250718ea6Smrg	    prefixes="$prefixes '$qprefix'"
42350718ea6Smrg	  fi
42450718ea6Smrg	fi
42550718ea6Smrg	prefix=$prefix/
42650718ea6Smrg      done
42750718ea6Smrg
42850718ea6Smrg      if test -n "$prefixes"; then
42950718ea6Smrg	# Don't fail if two instances are running concurrently.
43050718ea6Smrg	(umask $mkdir_umask &&
43150718ea6Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43250718ea6Smrg	  test -d "$dstdir" || exit 1
43350718ea6Smrg	obsolete_mkdir_used=true
43450718ea6Smrg      fi
43550718ea6Smrg    fi
43650718ea6Smrg  fi
43750718ea6Smrg
43850718ea6Smrg  if test -n "$dir_arg"; then
43950718ea6Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44050718ea6Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44150718ea6Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44250718ea6Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44350718ea6Smrg  else
44450718ea6Smrg
44550718ea6Smrg    # Make a couple of temp file names in the proper directory.
44650718ea6Smrg    dsttmp=$dstdir/_inst.$$_
44750718ea6Smrg    rmtmp=$dstdir/_rm.$$_
44850718ea6Smrg
44950718ea6Smrg    # Trap to clean up those temp files at exit.
45050718ea6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45150718ea6Smrg
45250718ea6Smrg    # Copy the file name to the temp name.
45350718ea6Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45450718ea6Smrg
45550718ea6Smrg    # and set any options; do chmod last to preserve setuid bits.
45650718ea6Smrg    #
45750718ea6Smrg    # If any of these fail, we abort the whole thing.  If we want to
45850718ea6Smrg    # ignore errors from any of these, just make sure not to ignore
45950718ea6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46050718ea6Smrg    #
46150718ea6Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46250718ea6Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46350718ea6Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46450718ea6Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46550718ea6Smrg
46650718ea6Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46750718ea6Smrg    if $copy_on_change &&
46850718ea6Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46950718ea6Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47050718ea6Smrg
47150718ea6Smrg       eval "$initialize_posix_glob" &&
47250718ea6Smrg       $posix_glob set -f &&
47350718ea6Smrg       set X $old && old=:$2:$4:$5:$6 &&
47450718ea6Smrg       set X $new && new=:$2:$4:$5:$6 &&
47550718ea6Smrg       $posix_glob set +f &&
47650718ea6Smrg
47750718ea6Smrg       test "$old" = "$new" &&
47850718ea6Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47950718ea6Smrg    then
48050718ea6Smrg      rm -f "$dsttmp"
48150718ea6Smrg    else
48250718ea6Smrg      # Rename the file to the real destination.
48350718ea6Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48450718ea6Smrg
48550718ea6Smrg      # The rename failed, perhaps because mv can't rename something else
48650718ea6Smrg      # to itself, or perhaps because mv is so ancient that it does not
48750718ea6Smrg      # support -f.
48850718ea6Smrg      {
48950718ea6Smrg	# Now remove or move aside any old file at destination location.
49050718ea6Smrg	# We try this two ways since rm can't unlink itself on some
49150718ea6Smrg	# systems and the destination file might be busy for other
49250718ea6Smrg	# reasons.  In this case, the final cleanup might fail but the new
49350718ea6Smrg	# file should still install successfully.
49450718ea6Smrg	{
49550718ea6Smrg	  test ! -f "$dst" ||
49650718ea6Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49750718ea6Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49850718ea6Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49950718ea6Smrg	  } ||
50050718ea6Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50150718ea6Smrg	    (exit 1); exit 1
50250718ea6Smrg	  }
50350718ea6Smrg	} &&
50450718ea6Smrg
50550718ea6Smrg	# Now rename the file to the real destination.
50650718ea6Smrg	$doit $mvcmd "$dsttmp" "$dst"
50750718ea6Smrg      }
50850718ea6Smrg    fi || exit 1
50950718ea6Smrg
51050718ea6Smrg    trap '' 0
51150718ea6Smrg  fi
51250718ea6Smrgdone
513566738d7Smrg
51450718ea6Smrg# Local variables:
51550718ea6Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51650718ea6Smrg# time-stamp-start: "scriptversion="
51750718ea6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51850718ea6Smrg# time-stamp-time-zone: "UTC"
51950718ea6Smrg# time-stamp-end: "; # UTC"
52050718ea6Smrg# End:
521