1209ff23fSmrg#!/bin/sh
2209ff23fSmrg# install - install a program, script, or datafile
3209ff23fSmrg
443df4709Smrgscriptversion=2009-04-28.21; # UTC
5209ff23fSmrg
6209ff23fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7209ff23fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8209ff23fSmrg# following copyright and license.
9209ff23fSmrg#
10209ff23fSmrg# Copyright (C) 1994 X Consortium
11209ff23fSmrg#
12209ff23fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13209ff23fSmrg# of this software and associated documentation files (the "Software"), to
14209ff23fSmrg# deal in the Software without restriction, including without limitation the
15209ff23fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16209ff23fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17209ff23fSmrg# furnished to do so, subject to the following conditions:
18209ff23fSmrg#
19209ff23fSmrg# The above copyright notice and this permission notice shall be included in
20209ff23fSmrg# all copies or substantial portions of the Software.
21209ff23fSmrg#
22209ff23fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23209ff23fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24209ff23fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25209ff23fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26209ff23fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27209ff23fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28209ff23fSmrg#
29209ff23fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30209ff23fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31209ff23fSmrg# ings in this Software without prior written authorization from the X Consor-
32209ff23fSmrg# tium.
33209ff23fSmrg#
34209ff23fSmrg#
35209ff23fSmrg# FSF changes to this file are in the public domain.
36209ff23fSmrg#
37209ff23fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3843df4709Smrg# `make' implicit rules from creating a file called install from it
39209ff23fSmrg# when there is no Makefile.
40209ff23fSmrg#
41209ff23fSmrg# This script is compatible with the BSD install script, but was written
42209ff23fSmrg# from scratch.
43209ff23fSmrg
44209ff23fSmrgnl='
45209ff23fSmrg'
4643df4709SmrgIFS=" ""	$nl"
47209ff23fSmrg
4843df4709Smrg# set DOITPROG to echo to test this script
49209ff23fSmrg
5043df4709Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51c503f109Smrgdoit=${DOITPROG-}
5243df4709Smrgif test -z "$doit"; then
5343df4709Smrg  doit_exec=exec
5443df4709Smrgelse
5543df4709Smrg  doit_exec=$doit
5643df4709Smrgfi
57209ff23fSmrg
58209ff23fSmrg# Put in absolute file names if you don't have them in your path;
59209ff23fSmrg# or use environment vars.
60209ff23fSmrg
61c503f109Smrgchgrpprog=${CHGRPPROG-chgrp}
62c503f109Smrgchmodprog=${CHMODPROG-chmod}
63c503f109Smrgchownprog=${CHOWNPROG-chown}
64c503f109Smrgcmpprog=${CMPPROG-cmp}
65c503f109Smrgcpprog=${CPPROG-cp}
66c503f109Smrgmkdirprog=${MKDIRPROG-mkdir}
67c503f109Smrgmvprog=${MVPROG-mv}
68c503f109Smrgrmprog=${RMPROG-rm}
69c503f109Smrgstripprog=${STRIPPROG-strip}
70c503f109Smrg
7143df4709Smrgposix_glob='?'
7243df4709Smrginitialize_posix_glob='
7343df4709Smrg  test "$posix_glob" != "?" || {
7443df4709Smrg    if (set -f) 2>/dev/null; then
7543df4709Smrg      posix_glob=
7643df4709Smrg    else
7743df4709Smrg      posix_glob=:
7843df4709Smrg    fi
7943df4709Smrg  }
8043df4709Smrg'
8143df4709Smrg
82209ff23fSmrgposix_mkdir=
83209ff23fSmrg
84209ff23fSmrg# Desired mode of installed file.
85209ff23fSmrgmode=0755
86209ff23fSmrg
87c503f109Smrgchgrpcmd=
88209ff23fSmrgchmodcmd=$chmodprog
89209ff23fSmrgchowncmd=
90c503f109Smrgmvcmd=$mvprog
91209ff23fSmrgrmcmd="$rmprog -f"
92c503f109Smrgstripcmd=
93c503f109Smrg
94209ff23fSmrgsrc=
95209ff23fSmrgdst=
96209ff23fSmrgdir_arg=
97c503f109Smrgdst_arg=
98c503f109Smrg
99c503f109Smrgcopy_on_change=false
10043df4709Smrgno_target_directory=
101209ff23fSmrg
102c503f109Smrgusage="\
103c503f109SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104209ff23fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105209ff23fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106209ff23fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107209ff23fSmrg
108209ff23fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109209ff23fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110209ff23fSmrgIn the 4th, create DIRECTORIES.
111209ff23fSmrg
112209ff23fSmrgOptions:
113c503f109Smrg     --help     display this help and exit.
114c503f109Smrg     --version  display version info and exit.
115c503f109Smrg
116c503f109Smrg  -c            (ignored)
117c503f109Smrg  -C            install only if different (preserve the last data modification time)
118c503f109Smrg  -d            create directories instead of installing files.
119c503f109Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120c503f109Smrg  -m MODE       $chmodprog installed files to MODE.
121c503f109Smrg  -o USER       $chownprog installed files to USER.
122c503f109Smrg  -s            $stripprog installed files.
123c503f109Smrg  -t DIRECTORY  install into DIRECTORY.
124c503f109Smrg  -T            report an error if DSTFILE is a directory.
125209ff23fSmrg
126209ff23fSmrgEnvironment variables override the default commands:
127c503f109Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128c503f109Smrg  RMPROG STRIPPROG
129209ff23fSmrg"
130209ff23fSmrg
131209ff23fSmrgwhile test $# -ne 0; do
132209ff23fSmrg  case $1 in
133c503f109Smrg    -c) ;;
134c503f109Smrg
135c503f109Smrg    -C) copy_on_change=true;;
136209ff23fSmrg
137c503f109Smrg    -d) dir_arg=true;;
138209ff23fSmrg
139209ff23fSmrg    -g) chgrpcmd="$chgrpprog $2"
14043df4709Smrg	shift;;
141209ff23fSmrg
142209ff23fSmrg    --help) echo "$usage"; exit $?;;
143209ff23fSmrg
144209ff23fSmrg    -m) mode=$2
14543df4709Smrg	case $mode in
14643df4709Smrg	  *' '* | *'	'* | *'
14743df4709Smrg'*	  | *'*'* | *'?'* | *'['*)
14843df4709Smrg	    echo "$0: invalid mode: $mode" >&2
14943df4709Smrg	    exit 1;;
15043df4709Smrg	esac
15143df4709Smrg	shift;;
152209ff23fSmrg
153209ff23fSmrg    -o) chowncmd="$chownprog $2"
15443df4709Smrg	shift;;
155209ff23fSmrg
156c503f109Smrg    -s) stripcmd=$stripprog;;
157209ff23fSmrg
15843df4709Smrg    -t) dst_arg=$2
15943df4709Smrg	shift;;
160209ff23fSmrg
16143df4709Smrg    -T) no_target_directory=true;;
162209ff23fSmrg
163209ff23fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164209ff23fSmrg
16543df4709Smrg    --)	shift
16643df4709Smrg	break;;
167209ff23fSmrg
16843df4709Smrg    -*)	echo "$0: invalid option: $1" >&2
16943df4709Smrg	exit 1;;
170209ff23fSmrg
171209ff23fSmrg    *)  break;;
172209ff23fSmrg  esac
173c503f109Smrg  shift
174209ff23fSmrgdone
175209ff23fSmrg
176c503f109Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177209ff23fSmrg  # When -d is used, all remaining arguments are directories to create.
178209ff23fSmrg  # When -t is used, the destination is already specified.
179209ff23fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180209ff23fSmrg  for arg
181209ff23fSmrg  do
182c503f109Smrg    if test -n "$dst_arg"; then
183209ff23fSmrg      # $@ is not empty: it contains at least $arg.
184c503f109Smrg      set fnord "$@" "$dst_arg"
185209ff23fSmrg      shift # fnord
186209ff23fSmrg    fi
187209ff23fSmrg    shift # arg
188c503f109Smrg    dst_arg=$arg
189209ff23fSmrg  done
190209ff23fSmrgfi
191209ff23fSmrg
192209ff23fSmrgif test $# -eq 0; then
193209ff23fSmrg  if test -z "$dir_arg"; then
194209ff23fSmrg    echo "$0: no input file specified." >&2
195209ff23fSmrg    exit 1
196209ff23fSmrg  fi
19743df4709Smrg  # It's OK to call `install-sh -d' without argument.
198209ff23fSmrg  # This can happen when creating conditional directories.
199209ff23fSmrg  exit 0
200209ff23fSmrgfi
201209ff23fSmrg
202209ff23fSmrgif test -z "$dir_arg"; then
20343df4709Smrg  trap '(exit $?); exit' 1 2 13 15
204209ff23fSmrg
205209ff23fSmrg  # Set umask so as not to create temps with too-generous modes.
206209ff23fSmrg  # However, 'strip' requires both read and write access to temps.
207209ff23fSmrg  case $mode in
208209ff23fSmrg    # Optimize common cases.
209209ff23fSmrg    *644) cp_umask=133;;
210209ff23fSmrg    *755) cp_umask=22;;
211209ff23fSmrg
212209ff23fSmrg    *[0-7])
213209ff23fSmrg      if test -z "$stripcmd"; then
21443df4709Smrg	u_plus_rw=
215209ff23fSmrg      else
21643df4709Smrg	u_plus_rw='% 200'
217209ff23fSmrg      fi
218209ff23fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219209ff23fSmrg    *)
220209ff23fSmrg      if test -z "$stripcmd"; then
22143df4709Smrg	u_plus_rw=
222209ff23fSmrg      else
22343df4709Smrg	u_plus_rw=,u+rw
224209ff23fSmrg      fi
225209ff23fSmrg      cp_umask=$mode$u_plus_rw;;
226209ff23fSmrg  esac
227209ff23fSmrgfi
228209ff23fSmrg
229209ff23fSmrgfor src
230209ff23fSmrgdo
23143df4709Smrg  # Protect names starting with `-'.
232209ff23fSmrg  case $src in
23343df4709Smrg    -*) src=./$src;;
234209ff23fSmrg  esac
235209ff23fSmrg
236209ff23fSmrg  if test -n "$dir_arg"; then
237209ff23fSmrg    dst=$src
238209ff23fSmrg    dstdir=$dst
239209ff23fSmrg    test -d "$dstdir"
240209ff23fSmrg    dstdir_status=$?
241209ff23fSmrg  else
242209ff23fSmrg
243209ff23fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244209ff23fSmrg    # might cause directories to be created, which would be especially bad
245209ff23fSmrg    # if $src (and thus $dsttmp) contains '*'.
246209ff23fSmrg    if test ! -f "$src" && test ! -d "$src"; then
247209ff23fSmrg      echo "$0: $src does not exist." >&2
248209ff23fSmrg      exit 1
249209ff23fSmrg    fi
250209ff23fSmrg
251c503f109Smrg    if test -z "$dst_arg"; then
252209ff23fSmrg      echo "$0: no destination specified." >&2
253209ff23fSmrg      exit 1
254209ff23fSmrg    fi
25543df4709Smrg
256c503f109Smrg    dst=$dst_arg
25743df4709Smrg    # Protect names starting with `-'.
25843df4709Smrg    case $dst in
25943df4709Smrg      -*) dst=./$dst;;
26043df4709Smrg    esac
261209ff23fSmrg
262209ff23fSmrg    # If destination is a directory, append the input filename; won't work
263209ff23fSmrg    # if double slashes aren't ignored.
264209ff23fSmrg    if test -d "$dst"; then
26543df4709Smrg      if test -n "$no_target_directory"; then
26643df4709Smrg	echo "$0: $dst_arg: Is a directory" >&2
26743df4709Smrg	exit 1
268209ff23fSmrg      fi
269209ff23fSmrg      dstdir=$dst
270209ff23fSmrg      dst=$dstdir/`basename "$src"`
271209ff23fSmrg      dstdir_status=0
272209ff23fSmrg    else
27343df4709Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27443df4709Smrg      dstdir=`
27543df4709Smrg	(dirname "$dst") 2>/dev/null ||
27643df4709Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27743df4709Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27843df4709Smrg	     X"$dst" : 'X\(//\)$' \| \
27943df4709Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28043df4709Smrg	echo X"$dst" |
28143df4709Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28243df4709Smrg		   s//\1/
28343df4709Smrg		   q
28443df4709Smrg		 }
28543df4709Smrg		 /^X\(\/\/\)[^/].*/{
28643df4709Smrg		   s//\1/
28743df4709Smrg		   q
28843df4709Smrg		 }
28943df4709Smrg		 /^X\(\/\/\)$/{
29043df4709Smrg		   s//\1/
29143df4709Smrg		   q
29243df4709Smrg		 }
29343df4709Smrg		 /^X\(\/\).*/{
29443df4709Smrg		   s//\1/
29543df4709Smrg		   q
29643df4709Smrg		 }
29743df4709Smrg		 s/.*/./; q'
29843df4709Smrg      `
29943df4709Smrg
300209ff23fSmrg      test -d "$dstdir"
301209ff23fSmrg      dstdir_status=$?
302209ff23fSmrg    fi
303209ff23fSmrg  fi
304209ff23fSmrg
305209ff23fSmrg  obsolete_mkdir_used=false
306209ff23fSmrg
307209ff23fSmrg  if test $dstdir_status != 0; then
308209ff23fSmrg    case $posix_mkdir in
309209ff23fSmrg      '')
31043df4709Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31143df4709Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31243df4709Smrg	umask=`umask`
31343df4709Smrg	case $stripcmd.$umask in
31443df4709Smrg	  # Optimize common cases.
31543df4709Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31643df4709Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31743df4709Smrg
31843df4709Smrg	  *[0-7])
31943df4709Smrg	    mkdir_umask=`expr $umask + 22 \
32043df4709Smrg	      - $umask % 100 % 40 + $umask % 20 \
32143df4709Smrg	      - $umask % 10 % 4 + $umask % 2
32243df4709Smrg	    `;;
32343df4709Smrg	  *) mkdir_umask=$umask,go-w;;
32443df4709Smrg	esac
32543df4709Smrg
32643df4709Smrg	# With -d, create the new directory with the user-specified mode.
32743df4709Smrg	# Otherwise, rely on $mkdir_umask.
32843df4709Smrg	if test -n "$dir_arg"; then
32943df4709Smrg	  mkdir_mode=-m$mode
33043df4709Smrg	else
33143df4709Smrg	  mkdir_mode=
33243df4709Smrg	fi
33343df4709Smrg
33443df4709Smrg	posix_mkdir=false
33543df4709Smrg	case $umask in
33643df4709Smrg	  *[123567][0-7][0-7])
33743df4709Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33843df4709Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33943df4709Smrg	    ;;
34043df4709Smrg	  *)
34143df4709Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34243df4709Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34343df4709Smrg
34443df4709Smrg	    if (umask $mkdir_umask &&
34543df4709Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34643df4709Smrg	    then
34743df4709Smrg	      if test -z "$dir_arg" || {
34843df4709Smrg		   # Check for POSIX incompatibilities with -m.
34943df4709Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35043df4709Smrg		   # other-writeable bit of parent directory when it shouldn't.
35143df4709Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35243df4709Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35343df4709Smrg		   case $ls_ld_tmpdir in
35443df4709Smrg		     d????-?r-*) different_mode=700;;
35543df4709Smrg		     d????-?--*) different_mode=755;;
35643df4709Smrg		     *) false;;
35743df4709Smrg		   esac &&
35843df4709Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35943df4709Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36043df4709Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36143df4709Smrg		   }
36243df4709Smrg		 }
36343df4709Smrg	      then posix_mkdir=:
36443df4709Smrg	      fi
36543df4709Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36643df4709Smrg	    else
36743df4709Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36843df4709Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36943df4709Smrg	    fi
37043df4709Smrg	    trap '' 0;;
37143df4709Smrg	esac;;
372209ff23fSmrg    esac
373209ff23fSmrg
374209ff23fSmrg    if
375209ff23fSmrg      $posix_mkdir && (
37643df4709Smrg	umask $mkdir_umask &&
37743df4709Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378209ff23fSmrg      )
379209ff23fSmrg    then :
380209ff23fSmrg    else
381209ff23fSmrg
382209ff23fSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383209ff23fSmrg      # or it failed possibly due to a race condition.  Create the
384209ff23fSmrg      # directory the slow way, step by step, checking for races as we go.
385209ff23fSmrg
386209ff23fSmrg      case $dstdir in
38743df4709Smrg	/*) prefix='/';;
38843df4709Smrg	-*) prefix='./';;
38943df4709Smrg	*)  prefix='';;
390209ff23fSmrg      esac
391209ff23fSmrg
39243df4709Smrg      eval "$initialize_posix_glob"
39343df4709Smrg
394209ff23fSmrg      oIFS=$IFS
395209ff23fSmrg      IFS=/
39643df4709Smrg      $posix_glob set -f
397209ff23fSmrg      set fnord $dstdir
398209ff23fSmrg      shift
39943df4709Smrg      $posix_glob set +f
400209ff23fSmrg      IFS=$oIFS
401209ff23fSmrg
402209ff23fSmrg      prefixes=
403209ff23fSmrg
404209ff23fSmrg      for d
405209ff23fSmrg      do
40643df4709Smrg	test -z "$d" && continue
40743df4709Smrg
40843df4709Smrg	prefix=$prefix$d
40943df4709Smrg	if test -d "$prefix"; then
41043df4709Smrg	  prefixes=
41143df4709Smrg	else
41243df4709Smrg	  if $posix_mkdir; then
41343df4709Smrg	    (umask=$mkdir_umask &&
41443df4709Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41543df4709Smrg	    # Don't fail if two instances are running concurrently.
41643df4709Smrg	    test -d "$prefix" || exit 1
41743df4709Smrg	  else
41843df4709Smrg	    case $prefix in
41943df4709Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42043df4709Smrg	      *) qprefix=$prefix;;
42143df4709Smrg	    esac
42243df4709Smrg	    prefixes="$prefixes '$qprefix'"
42343df4709Smrg	  fi
42443df4709Smrg	fi
42543df4709Smrg	prefix=$prefix/
426209ff23fSmrg      done
427209ff23fSmrg
428209ff23fSmrg      if test -n "$prefixes"; then
42943df4709Smrg	# Don't fail if two instances are running concurrently.
43043df4709Smrg	(umask $mkdir_umask &&
43143df4709Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43243df4709Smrg	  test -d "$dstdir" || exit 1
43343df4709Smrg	obsolete_mkdir_used=true
434209ff23fSmrg      fi
435209ff23fSmrg    fi
436209ff23fSmrg  fi
437209ff23fSmrg
438209ff23fSmrg  if test -n "$dir_arg"; then
439209ff23fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440209ff23fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441209ff23fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442209ff23fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443209ff23fSmrg  else
444209ff23fSmrg
445209ff23fSmrg    # Make a couple of temp file names in the proper directory.
446209ff23fSmrg    dsttmp=$dstdir/_inst.$$_
447209ff23fSmrg    rmtmp=$dstdir/_rm.$$_
448209ff23fSmrg
449209ff23fSmrg    # Trap to clean up those temp files at exit.
450209ff23fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451209ff23fSmrg
452209ff23fSmrg    # Copy the file name to the temp name.
453209ff23fSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454209ff23fSmrg
455209ff23fSmrg    # and set any options; do chmod last to preserve setuid bits.
456209ff23fSmrg    #
457209ff23fSmrg    # If any of these fail, we abort the whole thing.  If we want to
458209ff23fSmrg    # ignore errors from any of these, just make sure not to ignore
459209ff23fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460209ff23fSmrg    #
461c503f109Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462c503f109Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463c503f109Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464c503f109Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465c503f109Smrg
466c503f109Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467c503f109Smrg    if $copy_on_change &&
46843df4709Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46943df4709Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47043df4709Smrg
47143df4709Smrg       eval "$initialize_posix_glob" &&
47243df4709Smrg       $posix_glob set -f &&
473c503f109Smrg       set X $old && old=:$2:$4:$5:$6 &&
474c503f109Smrg       set X $new && new=:$2:$4:$5:$6 &&
47543df4709Smrg       $posix_glob set +f &&
47643df4709Smrg
477c503f109Smrg       test "$old" = "$new" &&
478c503f109Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479c503f109Smrg    then
480c503f109Smrg      rm -f "$dsttmp"
481c503f109Smrg    else
482c503f109Smrg      # Rename the file to the real destination.
483c503f109Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484c503f109Smrg
485c503f109Smrg      # The rename failed, perhaps because mv can't rename something else
486c503f109Smrg      # to itself, or perhaps because mv is so ancient that it does not
487c503f109Smrg      # support -f.
488c503f109Smrg      {
48943df4709Smrg	# Now remove or move aside any old file at destination location.
49043df4709Smrg	# We try this two ways since rm can't unlink itself on some
49143df4709Smrg	# systems and the destination file might be busy for other
49243df4709Smrg	# reasons.  In this case, the final cleanup might fail but the new
49343df4709Smrg	# file should still install successfully.
49443df4709Smrg	{
49543df4709Smrg	  test ! -f "$dst" ||
49643df4709Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49743df4709Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49843df4709Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49943df4709Smrg	  } ||
50043df4709Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50143df4709Smrg	    (exit 1); exit 1
50243df4709Smrg	  }
50343df4709Smrg	} &&
50443df4709Smrg
50543df4709Smrg	# Now rename the file to the real destination.
50643df4709Smrg	$doit $mvcmd "$dsttmp" "$dst"
507c503f109Smrg      }
508c503f109Smrg    fi || exit 1
509209ff23fSmrg
510209ff23fSmrg    trap '' 0
511209ff23fSmrg  fi
512209ff23fSmrgdone
513209ff23fSmrg
514209ff23fSmrg# Local variables:
515209ff23fSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516209ff23fSmrg# time-stamp-start: "scriptversion="
517209ff23fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5182f39173dSmrg# time-stamp-time-zone: "UTC"
5192f39173dSmrg# time-stamp-end: "; # UTC"
520209ff23fSmrg# End:
521