install-sh revision 11245024
141b2f0bdSmrg#!/bin/sh
241b2f0bdSmrg# install - install a program, script, or datafile
341b2f0bdSmrg
411245024Smrgscriptversion=2009-04-28.21; # UTC
541b2f0bdSmrg
641b2f0bdSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
741b2f0bdSmrg# later released in X11R6 (xc/config/util/install.sh) with the
841b2f0bdSmrg# following copyright and license.
941b2f0bdSmrg#
1041b2f0bdSmrg# Copyright (C) 1994 X Consortium
1141b2f0bdSmrg#
1241b2f0bdSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1341b2f0bdSmrg# of this software and associated documentation files (the "Software"), to
1441b2f0bdSmrg# deal in the Software without restriction, including without limitation the
1541b2f0bdSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1641b2f0bdSmrg# sell copies of the Software, and to permit persons to whom the Software is
1741b2f0bdSmrg# furnished to do so, subject to the following conditions:
1841b2f0bdSmrg#
1941b2f0bdSmrg# The above copyright notice and this permission notice shall be included in
2041b2f0bdSmrg# all copies or substantial portions of the Software.
2141b2f0bdSmrg#
2241b2f0bdSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2341b2f0bdSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2441b2f0bdSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2541b2f0bdSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2641b2f0bdSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2741b2f0bdSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2841b2f0bdSmrg#
2941b2f0bdSmrg# Except as contained in this notice, the name of the X Consortium shall not
3041b2f0bdSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3141b2f0bdSmrg# ings in this Software without prior written authorization from the X Consor-
3241b2f0bdSmrg# tium.
3341b2f0bdSmrg#
3441b2f0bdSmrg#
3541b2f0bdSmrg# FSF changes to this file are in the public domain.
3641b2f0bdSmrg#
3741b2f0bdSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3841b2f0bdSmrg# `make' implicit rules from creating a file called install from it
3941b2f0bdSmrg# when there is no Makefile.
4041b2f0bdSmrg#
4141b2f0bdSmrg# This script is compatible with the BSD install script, but was written
4211245024Smrg# from scratch.
4311245024Smrg
4411245024Smrgnl='
4511245024Smrg'
4611245024SmrgIFS=" ""	$nl"
4741b2f0bdSmrg
4841b2f0bdSmrg# set DOITPROG to echo to test this script
4941b2f0bdSmrg
5041b2f0bdSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5111245024Smrgdoit=${DOITPROG-}
5211245024Smrgif test -z "$doit"; then
5311245024Smrg  doit_exec=exec
5411245024Smrgelse
5511245024Smrg  doit_exec=$doit
5611245024Smrgfi
5741b2f0bdSmrg
5811245024Smrg# Put in absolute file names if you don't have them in your path;
5911245024Smrg# or use environment vars.
6011245024Smrg
6111245024Smrgchgrpprog=${CHGRPPROG-chgrp}
6211245024Smrgchmodprog=${CHMODPROG-chmod}
6311245024Smrgchownprog=${CHOWNPROG-chown}
6411245024Smrgcmpprog=${CMPPROG-cmp}
6511245024Smrgcpprog=${CPPROG-cp}
6611245024Smrgmkdirprog=${MKDIRPROG-mkdir}
6711245024Smrgmvprog=${MVPROG-mv}
6811245024Smrgrmprog=${RMPROG-rm}
6911245024Smrgstripprog=${STRIPPROG-strip}
7011245024Smrg
7111245024Smrgposix_glob='?'
7211245024Smrginitialize_posix_glob='
7311245024Smrg  test "$posix_glob" != "?" || {
7411245024Smrg    if (set -f) 2>/dev/null; then
7511245024Smrg      posix_glob=
7611245024Smrg    else
7711245024Smrg      posix_glob=:
7811245024Smrg    fi
7911245024Smrg  }
8011245024Smrg'
8141b2f0bdSmrg
8211245024Smrgposix_mkdir=
8311245024Smrg
8411245024Smrg# Desired mode of installed file.
8511245024Smrgmode=0755
8641b2f0bdSmrg
8741b2f0bdSmrgchgrpcmd=
8811245024Smrgchmodcmd=$chmodprog
8911245024Smrgchowncmd=
9011245024Smrgmvcmd=$mvprog
9141b2f0bdSmrgrmcmd="$rmprog -f"
9211245024Smrgstripcmd=
9311245024Smrg
9441b2f0bdSmrgsrc=
9541b2f0bdSmrgdst=
9641b2f0bdSmrgdir_arg=
9711245024Smrgdst_arg=
9811245024Smrg
9911245024Smrgcopy_on_change=false
10041b2f0bdSmrgno_target_directory=
10141b2f0bdSmrg
10211245024Smrgusage="\
10311245024SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10441b2f0bdSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10541b2f0bdSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10641b2f0bdSmrg   or: $0 [OPTION]... -d DIRECTORIES...
10741b2f0bdSmrg
10841b2f0bdSmrgIn the 1st form, copy SRCFILE to DSTFILE.
10941b2f0bdSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11041b2f0bdSmrgIn the 4th, create DIRECTORIES.
11141b2f0bdSmrg
11241b2f0bdSmrgOptions:
11311245024Smrg     --help     display this help and exit.
11411245024Smrg     --version  display version info and exit.
11511245024Smrg
11611245024Smrg  -c            (ignored)
11711245024Smrg  -C            install only if different (preserve the last data modification time)
11811245024Smrg  -d            create directories instead of installing files.
11911245024Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12011245024Smrg  -m MODE       $chmodprog installed files to MODE.
12111245024Smrg  -o USER       $chownprog installed files to USER.
12211245024Smrg  -s            $stripprog installed files.
12311245024Smrg  -t DIRECTORY  install into DIRECTORY.
12411245024Smrg  -T            report an error if DSTFILE is a directory.
12541b2f0bdSmrg
12641b2f0bdSmrgEnvironment variables override the default commands:
12711245024Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12811245024Smrg  RMPROG STRIPPROG
12941b2f0bdSmrg"
13041b2f0bdSmrg
13111245024Smrgwhile test $# -ne 0; do
13241b2f0bdSmrg  case $1 in
13311245024Smrg    -c) ;;
13411245024Smrg
13511245024Smrg    -C) copy_on_change=true;;
13641b2f0bdSmrg
13711245024Smrg    -d) dir_arg=true;;
13841b2f0bdSmrg
13941b2f0bdSmrg    -g) chgrpcmd="$chgrpprog $2"
14011245024Smrg	shift;;
14141b2f0bdSmrg
14241b2f0bdSmrg    --help) echo "$usage"; exit $?;;
14341b2f0bdSmrg
14411245024Smrg    -m) mode=$2
14511245024Smrg	case $mode in
14611245024Smrg	  *' '* | *'	'* | *'
14711245024Smrg'*	  | *'*'* | *'?'* | *'['*)
14811245024Smrg	    echo "$0: invalid mode: $mode" >&2
14911245024Smrg	    exit 1;;
15011245024Smrg	esac
15111245024Smrg	shift;;
15241b2f0bdSmrg
15341b2f0bdSmrg    -o) chowncmd="$chownprog $2"
15411245024Smrg	shift;;
15541b2f0bdSmrg
15611245024Smrg    -s) stripcmd=$stripprog;;
15741b2f0bdSmrg
15811245024Smrg    -t) dst_arg=$2
15911245024Smrg	shift;;
16041b2f0bdSmrg
16111245024Smrg    -T) no_target_directory=true;;
16241b2f0bdSmrg
16341b2f0bdSmrg    --version) echo "$0 $scriptversion"; exit $?;;
16441b2f0bdSmrg
16511245024Smrg    --)	shift
16641b2f0bdSmrg	break;;
16711245024Smrg
16811245024Smrg    -*)	echo "$0: invalid option: $1" >&2
16911245024Smrg	exit 1;;
17011245024Smrg
17111245024Smrg    *)  break;;
17241b2f0bdSmrg  esac
17311245024Smrg  shift
17441b2f0bdSmrgdone
17541b2f0bdSmrg
17611245024Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17711245024Smrg  # When -d is used, all remaining arguments are directories to create.
17811245024Smrg  # When -t is used, the destination is already specified.
17911245024Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18011245024Smrg  for arg
18111245024Smrg  do
18211245024Smrg    if test -n "$dst_arg"; then
18311245024Smrg      # $@ is not empty: it contains at least $arg.
18411245024Smrg      set fnord "$@" "$dst_arg"
18511245024Smrg      shift # fnord
18611245024Smrg    fi
18711245024Smrg    shift # arg
18811245024Smrg    dst_arg=$arg
18911245024Smrg  done
19011245024Smrgfi
19111245024Smrg
19211245024Smrgif test $# -eq 0; then
19341b2f0bdSmrg  if test -z "$dir_arg"; then
19441b2f0bdSmrg    echo "$0: no input file specified." >&2
19541b2f0bdSmrg    exit 1
19641b2f0bdSmrg  fi
19741b2f0bdSmrg  # It's OK to call `install-sh -d' without argument.
19841b2f0bdSmrg  # This can happen when creating conditional directories.
19941b2f0bdSmrg  exit 0
20041b2f0bdSmrgfi
20141b2f0bdSmrg
20211245024Smrgif test -z "$dir_arg"; then
20311245024Smrg  trap '(exit $?); exit' 1 2 13 15
20411245024Smrg
20511245024Smrg  # Set umask so as not to create temps with too-generous modes.
20611245024Smrg  # However, 'strip' requires both read and write access to temps.
20711245024Smrg  case $mode in
20811245024Smrg    # Optimize common cases.
20911245024Smrg    *644) cp_umask=133;;
21011245024Smrg    *755) cp_umask=22;;
21111245024Smrg
21211245024Smrg    *[0-7])
21311245024Smrg      if test -z "$stripcmd"; then
21411245024Smrg	u_plus_rw=
21511245024Smrg      else
21611245024Smrg	u_plus_rw='% 200'
21711245024Smrg      fi
21811245024Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21911245024Smrg    *)
22011245024Smrg      if test -z "$stripcmd"; then
22111245024Smrg	u_plus_rw=
22211245024Smrg      else
22311245024Smrg	u_plus_rw=,u+rw
22411245024Smrg      fi
22511245024Smrg      cp_umask=$mode$u_plus_rw;;
22611245024Smrg  esac
22711245024Smrgfi
22811245024Smrg
22941b2f0bdSmrgfor src
23041b2f0bdSmrgdo
23141b2f0bdSmrg  # Protect names starting with `-'.
23241b2f0bdSmrg  case $src in
23311245024Smrg    -*) src=./$src;;
23441b2f0bdSmrg  esac
23541b2f0bdSmrg
23641b2f0bdSmrg  if test -n "$dir_arg"; then
23741b2f0bdSmrg    dst=$src
23811245024Smrg    dstdir=$dst
23911245024Smrg    test -d "$dstdir"
24011245024Smrg    dstdir_status=$?
24141b2f0bdSmrg  else
24211245024Smrg
24341b2f0bdSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24441b2f0bdSmrg    # might cause directories to be created, which would be especially bad
24541b2f0bdSmrg    # if $src (and thus $dsttmp) contains '*'.
24641b2f0bdSmrg    if test ! -f "$src" && test ! -d "$src"; then
24741b2f0bdSmrg      echo "$0: $src does not exist." >&2
24841b2f0bdSmrg      exit 1
24941b2f0bdSmrg    fi
25041b2f0bdSmrg
25111245024Smrg    if test -z "$dst_arg"; then
25241b2f0bdSmrg      echo "$0: no destination specified." >&2
25341b2f0bdSmrg      exit 1
25441b2f0bdSmrg    fi
25541b2f0bdSmrg
25611245024Smrg    dst=$dst_arg
25741b2f0bdSmrg    # Protect names starting with `-'.
25841b2f0bdSmrg    case $dst in
25911245024Smrg      -*) dst=./$dst;;
26041b2f0bdSmrg    esac
26141b2f0bdSmrg
26241b2f0bdSmrg    # If destination is a directory, append the input filename; won't work
26341b2f0bdSmrg    # if double slashes aren't ignored.
26441b2f0bdSmrg    if test -d "$dst"; then
26541b2f0bdSmrg      if test -n "$no_target_directory"; then
26611245024Smrg	echo "$0: $dst_arg: Is a directory" >&2
26741b2f0bdSmrg	exit 1
26841b2f0bdSmrg      fi
26911245024Smrg      dstdir=$dst
27011245024Smrg      dst=$dstdir/`basename "$src"`
27111245024Smrg      dstdir_status=0
27211245024Smrg    else
27311245024Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27411245024Smrg      dstdir=`
27511245024Smrg	(dirname "$dst") 2>/dev/null ||
27611245024Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27711245024Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27811245024Smrg	     X"$dst" : 'X\(//\)$' \| \
27911245024Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28011245024Smrg	echo X"$dst" |
28111245024Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28211245024Smrg		   s//\1/
28311245024Smrg		   q
28411245024Smrg		 }
28511245024Smrg		 /^X\(\/\/\)[^/].*/{
28611245024Smrg		   s//\1/
28711245024Smrg		   q
28811245024Smrg		 }
28911245024Smrg		 /^X\(\/\/\)$/{
29011245024Smrg		   s//\1/
29111245024Smrg		   q
29211245024Smrg		 }
29311245024Smrg		 /^X\(\/\).*/{
29411245024Smrg		   s//\1/
29511245024Smrg		   q
29611245024Smrg		 }
29711245024Smrg		 s/.*/./; q'
29811245024Smrg      `
29911245024Smrg
30011245024Smrg      test -d "$dstdir"
30111245024Smrg      dstdir_status=$?
30241b2f0bdSmrg    fi
30341b2f0bdSmrg  fi
30441b2f0bdSmrg
30511245024Smrg  obsolete_mkdir_used=false
30611245024Smrg
30711245024Smrg  if test $dstdir_status != 0; then
30811245024Smrg    case $posix_mkdir in
30911245024Smrg      '')
31011245024Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31111245024Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31211245024Smrg	umask=`umask`
31311245024Smrg	case $stripcmd.$umask in
31411245024Smrg	  # Optimize common cases.
31511245024Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31611245024Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31711245024Smrg
31811245024Smrg	  *[0-7])
31911245024Smrg	    mkdir_umask=`expr $umask + 22 \
32011245024Smrg	      - $umask % 100 % 40 + $umask % 20 \
32111245024Smrg	      - $umask % 10 % 4 + $umask % 2
32211245024Smrg	    `;;
32311245024Smrg	  *) mkdir_umask=$umask,go-w;;
32411245024Smrg	esac
32511245024Smrg
32611245024Smrg	# With -d, create the new directory with the user-specified mode.
32711245024Smrg	# Otherwise, rely on $mkdir_umask.
32811245024Smrg	if test -n "$dir_arg"; then
32911245024Smrg	  mkdir_mode=-m$mode
33011245024Smrg	else
33111245024Smrg	  mkdir_mode=
33211245024Smrg	fi
33311245024Smrg
33411245024Smrg	posix_mkdir=false
33511245024Smrg	case $umask in
33611245024Smrg	  *[123567][0-7][0-7])
33711245024Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33811245024Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33911245024Smrg	    ;;
34011245024Smrg	  *)
34111245024Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34211245024Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34311245024Smrg
34411245024Smrg	    if (umask $mkdir_umask &&
34511245024Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34611245024Smrg	    then
34711245024Smrg	      if test -z "$dir_arg" || {
34811245024Smrg		   # Check for POSIX incompatibilities with -m.
34911245024Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35011245024Smrg		   # other-writeable bit of parent directory when it shouldn't.
35111245024Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35211245024Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35311245024Smrg		   case $ls_ld_tmpdir in
35411245024Smrg		     d????-?r-*) different_mode=700;;
35511245024Smrg		     d????-?--*) different_mode=755;;
35611245024Smrg		     *) false;;
35711245024Smrg		   esac &&
35811245024Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35911245024Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36011245024Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36111245024Smrg		   }
36211245024Smrg		 }
36311245024Smrg	      then posix_mkdir=:
36411245024Smrg	      fi
36511245024Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36611245024Smrg	    else
36711245024Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36811245024Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36911245024Smrg	    fi
37011245024Smrg	    trap '' 0;;
37111245024Smrg	esac;;
37211245024Smrg    esac
37341b2f0bdSmrg
37411245024Smrg    if
37511245024Smrg      $posix_mkdir && (
37611245024Smrg	umask $mkdir_umask &&
37711245024Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37811245024Smrg      )
37911245024Smrg    then :
38011245024Smrg    else
38141b2f0bdSmrg
38211245024Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38311245024Smrg      # or it failed possibly due to a race condition.  Create the
38411245024Smrg      # directory the slow way, step by step, checking for races as we go.
38541b2f0bdSmrg
38611245024Smrg      case $dstdir in
38711245024Smrg	/*) prefix='/';;
38811245024Smrg	-*) prefix='./';;
38911245024Smrg	*)  prefix='';;
39011245024Smrg      esac
39141b2f0bdSmrg
39211245024Smrg      eval "$initialize_posix_glob"
39341b2f0bdSmrg
39411245024Smrg      oIFS=$IFS
39511245024Smrg      IFS=/
39611245024Smrg      $posix_glob set -f
39711245024Smrg      set fnord $dstdir
39841b2f0bdSmrg      shift
39911245024Smrg      $posix_glob set +f
40011245024Smrg      IFS=$oIFS
40111245024Smrg
40211245024Smrg      prefixes=
40311245024Smrg
40411245024Smrg      for d
40511245024Smrg      do
40611245024Smrg	test -z "$d" && continue
40711245024Smrg
40811245024Smrg	prefix=$prefix$d
40911245024Smrg	if test -d "$prefix"; then
41011245024Smrg	  prefixes=
41111245024Smrg	else
41211245024Smrg	  if $posix_mkdir; then
41311245024Smrg	    (umask=$mkdir_umask &&
41411245024Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41511245024Smrg	    # Don't fail if two instances are running concurrently.
41611245024Smrg	    test -d "$prefix" || exit 1
41711245024Smrg	  else
41811245024Smrg	    case $prefix in
41911245024Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42011245024Smrg	      *) qprefix=$prefix;;
42111245024Smrg	    esac
42211245024Smrg	    prefixes="$prefixes '$qprefix'"
42311245024Smrg	  fi
42411245024Smrg	fi
42511245024Smrg	prefix=$prefix/
42611245024Smrg      done
42711245024Smrg
42811245024Smrg      if test -n "$prefixes"; then
42911245024Smrg	# Don't fail if two instances are running concurrently.
43011245024Smrg	(umask $mkdir_umask &&
43111245024Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43211245024Smrg	  test -d "$dstdir" || exit 1
43311245024Smrg	obsolete_mkdir_used=true
43441b2f0bdSmrg      fi
43511245024Smrg    fi
43641b2f0bdSmrg  fi
43741b2f0bdSmrg
43841b2f0bdSmrg  if test -n "$dir_arg"; then
43911245024Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44011245024Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44111245024Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44211245024Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44341b2f0bdSmrg  else
44441b2f0bdSmrg
44541b2f0bdSmrg    # Make a couple of temp file names in the proper directory.
44641b2f0bdSmrg    dsttmp=$dstdir/_inst.$$_
44741b2f0bdSmrg    rmtmp=$dstdir/_rm.$$_
44841b2f0bdSmrg
44941b2f0bdSmrg    # Trap to clean up those temp files at exit.
45041b2f0bdSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45141b2f0bdSmrg
45241b2f0bdSmrg    # Copy the file name to the temp name.
45311245024Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45441b2f0bdSmrg
45541b2f0bdSmrg    # and set any options; do chmod last to preserve setuid bits.
45641b2f0bdSmrg    #
45741b2f0bdSmrg    # If any of these fail, we abort the whole thing.  If we want to
45841b2f0bdSmrg    # ignore errors from any of these, just make sure not to ignore
45941b2f0bdSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46041b2f0bdSmrg    #
46111245024Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46211245024Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46311245024Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46411245024Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46511245024Smrg
46611245024Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46711245024Smrg    if $copy_on_change &&
46811245024Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46911245024Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47011245024Smrg
47111245024Smrg       eval "$initialize_posix_glob" &&
47211245024Smrg       $posix_glob set -f &&
47311245024Smrg       set X $old && old=:$2:$4:$5:$6 &&
47411245024Smrg       set X $new && new=:$2:$4:$5:$6 &&
47511245024Smrg       $posix_glob set +f &&
47611245024Smrg
47711245024Smrg       test "$old" = "$new" &&
47811245024Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47911245024Smrg    then
48011245024Smrg      rm -f "$dsttmp"
48111245024Smrg    else
48211245024Smrg      # Rename the file to the real destination.
48311245024Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48411245024Smrg
48511245024Smrg      # The rename failed, perhaps because mv can't rename something else
48611245024Smrg      # to itself, or perhaps because mv is so ancient that it does not
48711245024Smrg      # support -f.
48811245024Smrg      {
48911245024Smrg	# Now remove or move aside any old file at destination location.
49011245024Smrg	# We try this two ways since rm can't unlink itself on some
49111245024Smrg	# systems and the destination file might be busy for other
49211245024Smrg	# reasons.  In this case, the final cleanup might fail but the new
49311245024Smrg	# file should still install successfully.
49411245024Smrg	{
49511245024Smrg	  test ! -f "$dst" ||
49611245024Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49711245024Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49811245024Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49911245024Smrg	  } ||
50011245024Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50111245024Smrg	    (exit 1); exit 1
50211245024Smrg	  }
50311245024Smrg	} &&
50411245024Smrg
50511245024Smrg	# Now rename the file to the real destination.
50611245024Smrg	$doit $mvcmd "$dsttmp" "$dst"
50711245024Smrg      }
50811245024Smrg    fi || exit 1
50911245024Smrg
51011245024Smrg    trap '' 0
51111245024Smrg  fi
51241b2f0bdSmrgdone
51341b2f0bdSmrg
51441b2f0bdSmrg# Local variables:
51541b2f0bdSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51641b2f0bdSmrg# time-stamp-start: "scriptversion="
51741b2f0bdSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51811245024Smrg# time-stamp-time-zone: "UTC"
51911245024Smrg# time-stamp-end: "; # UTC"
52041b2f0bdSmrg# End:
521