install-sh revision 20915927
18440cca4Smrg#!/bin/sh
28440cca4Smrg# install - install a program, script, or datafile
3d1c293acSmrg
420915927Smrgscriptversion=2011-01-19.21; # UTC
5d1c293acSmrg
68440cca4Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
78440cca4Smrg# later released in X11R6 (xc/config/util/install.sh) with the
88440cca4Smrg# following copyright and license.
98440cca4Smrg#
108440cca4Smrg# Copyright (C) 1994 X Consortium
118440cca4Smrg#
128440cca4Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
138440cca4Smrg# of this software and associated documentation files (the "Software"), to
148440cca4Smrg# deal in the Software without restriction, including without limitation the
158440cca4Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
168440cca4Smrg# sell copies of the Software, and to permit persons to whom the Software is
178440cca4Smrg# furnished to do so, subject to the following conditions:
188440cca4Smrg#
198440cca4Smrg# The above copyright notice and this permission notice shall be included in
208440cca4Smrg# all copies or substantial portions of the Software.
218440cca4Smrg#
228440cca4Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
238440cca4Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
248440cca4Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
258440cca4Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
268440cca4Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
278440cca4Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
288440cca4Smrg#
298440cca4Smrg# Except as contained in this notice, the name of the X Consortium shall not
308440cca4Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
318440cca4Smrg# ings in this Software without prior written authorization from the X Consor-
328440cca4Smrg# tium.
338440cca4Smrg#
348440cca4Smrg#
358440cca4Smrg# FSF changes to this file are in the public domain.
368440cca4Smrg#
378440cca4Smrg# Calling this script install-sh is preferred over install.sh, to prevent
388440cca4Smrg# `make' implicit rules from creating a file called install from it
398440cca4Smrg# when there is no Makefile.
408440cca4Smrg#
418440cca4Smrg# This script is compatible with the BSD install script, but was written
42d1c293acSmrg# from scratch.
438440cca4Smrg
44d1c293acSmrgnl='
45d1c293acSmrg'
46d1c293acSmrgIFS=" ""	$nl"
478440cca4Smrg
488440cca4Smrg# set DOITPROG to echo to test this script
498440cca4Smrg
508440cca4Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51d1c293acSmrgdoit=${DOITPROG-}
52d1c293acSmrgif test -z "$doit"; then
53d1c293acSmrg  doit_exec=exec
548440cca4Smrgelse
55d1c293acSmrg  doit_exec=$doit
568440cca4Smrgfi
578440cca4Smrg
58d1c293acSmrg# Put in absolute file names if you don't have them in your path;
59d1c293acSmrg# or use environment vars.
60d1c293acSmrg
61d1c293acSmrgchgrpprog=${CHGRPPROG-chgrp}
62d1c293acSmrgchmodprog=${CHMODPROG-chmod}
63d1c293acSmrgchownprog=${CHOWNPROG-chown}
64d1c293acSmrgcmpprog=${CMPPROG-cmp}
65d1c293acSmrgcpprog=${CPPROG-cp}
66d1c293acSmrgmkdirprog=${MKDIRPROG-mkdir}
67d1c293acSmrgmvprog=${MVPROG-mv}
68d1c293acSmrgrmprog=${RMPROG-rm}
69d1c293acSmrgstripprog=${STRIPPROG-strip}
70d1c293acSmrg
71d1c293acSmrgposix_glob='?'
72d1c293acSmrginitialize_posix_glob='
73d1c293acSmrg  test "$posix_glob" != "?" || {
74d1c293acSmrg    if (set -f) 2>/dev/null; then
75d1c293acSmrg      posix_glob=
76d1c293acSmrg    else
77d1c293acSmrg      posix_glob=:
78d1c293acSmrg    fi
79d1c293acSmrg  }
80d1c293acSmrg'
81d1c293acSmrg
82d1c293acSmrgposix_mkdir=
83d1c293acSmrg
84d1c293acSmrg# Desired mode of installed file.
85d1c293acSmrgmode=0755
86d1c293acSmrg
87d1c293acSmrgchgrpcmd=
88d1c293acSmrgchmodcmd=$chmodprog
89d1c293acSmrgchowncmd=
90d1c293acSmrgmvcmd=$mvprog
91d1c293acSmrgrmcmd="$rmprog -f"
92d1c293acSmrgstripcmd=
938440cca4Smrg
94d1c293acSmrgsrc=
95d1c293acSmrgdst=
96d1c293acSmrgdir_arg=
97d1c293acSmrgdst_arg=
988440cca4Smrg
99d1c293acSmrgcopy_on_change=false
100d1c293acSmrgno_target_directory=
1018440cca4Smrg
102d1c293acSmrgusage="\
103d1c293acSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104d1c293acSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105d1c293acSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106d1c293acSmrg   or: $0 [OPTION]... -d DIRECTORIES...
1078440cca4Smrg
108d1c293acSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109d1c293acSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110d1c293acSmrgIn the 4th, create DIRECTORIES.
1118440cca4Smrg
112d1c293acSmrgOptions:
113d1c293acSmrg     --help     display this help and exit.
114d1c293acSmrg     --version  display version info and exit.
1158440cca4Smrg
116d1c293acSmrg  -c            (ignored)
117d1c293acSmrg  -C            install only if different (preserve the last data modification time)
118d1c293acSmrg  -d            create directories instead of installing files.
119d1c293acSmrg  -g GROUP      $chgrpprog installed files to GROUP.
120d1c293acSmrg  -m MODE       $chmodprog installed files to MODE.
121d1c293acSmrg  -o USER       $chownprog installed files to USER.
122d1c293acSmrg  -s            $stripprog installed files.
123d1c293acSmrg  -t DIRECTORY  install into DIRECTORY.
124d1c293acSmrg  -T            report an error if DSTFILE is a directory.
1258440cca4Smrg
126d1c293acSmrgEnvironment variables override the default commands:
127d1c293acSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128d1c293acSmrg  RMPROG STRIPPROG
129d1c293acSmrg"
1308440cca4Smrg
131d1c293acSmrgwhile test $# -ne 0; do
132d1c293acSmrg  case $1 in
133d1c293acSmrg    -c) ;;
1348440cca4Smrg
135d1c293acSmrg    -C) copy_on_change=true;;
1368440cca4Smrg
137d1c293acSmrg    -d) dir_arg=true;;
1388440cca4Smrg
139d1c293acSmrg    -g) chgrpcmd="$chgrpprog $2"
140d1c293acSmrg	shift;;
1418440cca4Smrg
142d1c293acSmrg    --help) echo "$usage"; exit $?;;
1438440cca4Smrg
144d1c293acSmrg    -m) mode=$2
145d1c293acSmrg	case $mode in
146d1c293acSmrg	  *' '* | *'	'* | *'
147d1c293acSmrg'*	  | *'*'* | *'?'* | *'['*)
148d1c293acSmrg	    echo "$0: invalid mode: $mode" >&2
149d1c293acSmrg	    exit 1;;
150d1c293acSmrg	esac
151d1c293acSmrg	shift;;
1528440cca4Smrg
153d1c293acSmrg    -o) chowncmd="$chownprog $2"
154d1c293acSmrg	shift;;
1558440cca4Smrg
156d1c293acSmrg    -s) stripcmd=$stripprog;;
1578440cca4Smrg
158d1c293acSmrg    -t) dst_arg=$2
15920915927Smrg	# Protect names problematic for `test' and other utilities.
16020915927Smrg	case $dst_arg in
16120915927Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
16220915927Smrg	esac
163d1c293acSmrg	shift;;
1648440cca4Smrg
165d1c293acSmrg    -T) no_target_directory=true;;
1668440cca4Smrg
167d1c293acSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1688440cca4Smrg
169d1c293acSmrg    --)	shift
170d1c293acSmrg	break;;
1718440cca4Smrg
172d1c293acSmrg    -*)	echo "$0: invalid option: $1" >&2
173d1c293acSmrg	exit 1;;
1748440cca4Smrg
175d1c293acSmrg    *)  break;;
176d1c293acSmrg  esac
177d1c293acSmrg  shift
178d1c293acSmrgdone
1798440cca4Smrg
180d1c293acSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181d1c293acSmrg  # When -d is used, all remaining arguments are directories to create.
182d1c293acSmrg  # When -t is used, the destination is already specified.
183d1c293acSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184d1c293acSmrg  for arg
185d1c293acSmrg  do
186d1c293acSmrg    if test -n "$dst_arg"; then
187d1c293acSmrg      # $@ is not empty: it contains at least $arg.
188d1c293acSmrg      set fnord "$@" "$dst_arg"
189d1c293acSmrg      shift # fnord
190d1c293acSmrg    fi
191d1c293acSmrg    shift # arg
192d1c293acSmrg    dst_arg=$arg
19320915927Smrg    # Protect names problematic for `test' and other utilities.
19420915927Smrg    case $dst_arg in
19520915927Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19620915927Smrg    esac
197d1c293acSmrg  done
198d1c293acSmrgfi
1998440cca4Smrg
200d1c293acSmrgif test $# -eq 0; then
201d1c293acSmrg  if test -z "$dir_arg"; then
202d1c293acSmrg    echo "$0: no input file specified." >&2
203d1c293acSmrg    exit 1
204d1c293acSmrg  fi
205d1c293acSmrg  # It's OK to call `install-sh -d' without argument.
206d1c293acSmrg  # This can happen when creating conditional directories.
207d1c293acSmrg  exit 0
208d1c293acSmrgfi
2098440cca4Smrg
210d1c293acSmrgif test -z "$dir_arg"; then
21120915927Smrg  do_exit='(exit $ret); exit $ret'
21220915927Smrg  trap "ret=129; $do_exit" 1
21320915927Smrg  trap "ret=130; $do_exit" 2
21420915927Smrg  trap "ret=141; $do_exit" 13
21520915927Smrg  trap "ret=143; $do_exit" 15
216d1c293acSmrg
217d1c293acSmrg  # Set umask so as not to create temps with too-generous modes.
218d1c293acSmrg  # However, 'strip' requires both read and write access to temps.
219d1c293acSmrg  case $mode in
220d1c293acSmrg    # Optimize common cases.
221d1c293acSmrg    *644) cp_umask=133;;
222d1c293acSmrg    *755) cp_umask=22;;
223d1c293acSmrg
224d1c293acSmrg    *[0-7])
225d1c293acSmrg      if test -z "$stripcmd"; then
226d1c293acSmrg	u_plus_rw=
227d1c293acSmrg      else
228d1c293acSmrg	u_plus_rw='% 200'
229d1c293acSmrg      fi
230d1c293acSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231d1c293acSmrg    *)
232d1c293acSmrg      if test -z "$stripcmd"; then
233d1c293acSmrg	u_plus_rw=
234d1c293acSmrg      else
235d1c293acSmrg	u_plus_rw=,u+rw
236d1c293acSmrg      fi
237d1c293acSmrg      cp_umask=$mode$u_plus_rw;;
238d1c293acSmrg  esac
239d1c293acSmrgfi
2408440cca4Smrg
241d1c293acSmrgfor src
242d1c293acSmrgdo
24320915927Smrg  # Protect names problematic for `test' and other utilities.
244d1c293acSmrg  case $src in
24520915927Smrg    -* | [=\(\)!]) src=./$src;;
246d1c293acSmrg  esac
247d1c293acSmrg
248d1c293acSmrg  if test -n "$dir_arg"; then
249d1c293acSmrg    dst=$src
250d1c293acSmrg    dstdir=$dst
251d1c293acSmrg    test -d "$dstdir"
252d1c293acSmrg    dstdir_status=$?
253d1c293acSmrg  else
254d1c293acSmrg
255d1c293acSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256d1c293acSmrg    # might cause directories to be created, which would be especially bad
257d1c293acSmrg    # if $src (and thus $dsttmp) contains '*'.
258d1c293acSmrg    if test ! -f "$src" && test ! -d "$src"; then
259d1c293acSmrg      echo "$0: $src does not exist." >&2
260d1c293acSmrg      exit 1
261d1c293acSmrg    fi
262d1c293acSmrg
263d1c293acSmrg    if test -z "$dst_arg"; then
264d1c293acSmrg      echo "$0: no destination specified." >&2
265d1c293acSmrg      exit 1
266d1c293acSmrg    fi
267d1c293acSmrg    dst=$dst_arg
2688440cca4Smrg
269d1c293acSmrg    # If destination is a directory, append the input filename; won't work
270d1c293acSmrg    # if double slashes aren't ignored.
271d1c293acSmrg    if test -d "$dst"; then
272d1c293acSmrg      if test -n "$no_target_directory"; then
273d1c293acSmrg	echo "$0: $dst_arg: Is a directory" >&2
274d1c293acSmrg	exit 1
275d1c293acSmrg      fi
276d1c293acSmrg      dstdir=$dst
277d1c293acSmrg      dst=$dstdir/`basename "$src"`
278d1c293acSmrg      dstdir_status=0
279d1c293acSmrg    else
280d1c293acSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281d1c293acSmrg      dstdir=`
282d1c293acSmrg	(dirname "$dst") 2>/dev/null ||
283d1c293acSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284d1c293acSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
285d1c293acSmrg	     X"$dst" : 'X\(//\)$' \| \
286d1c293acSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287d1c293acSmrg	echo X"$dst" |
288d1c293acSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289d1c293acSmrg		   s//\1/
290d1c293acSmrg		   q
291d1c293acSmrg		 }
292d1c293acSmrg		 /^X\(\/\/\)[^/].*/{
293d1c293acSmrg		   s//\1/
294d1c293acSmrg		   q
295d1c293acSmrg		 }
296d1c293acSmrg		 /^X\(\/\/\)$/{
297d1c293acSmrg		   s//\1/
298d1c293acSmrg		   q
299d1c293acSmrg		 }
300d1c293acSmrg		 /^X\(\/\).*/{
301d1c293acSmrg		   s//\1/
302d1c293acSmrg		   q
303d1c293acSmrg		 }
304d1c293acSmrg		 s/.*/./; q'
305d1c293acSmrg      `
306d1c293acSmrg
307d1c293acSmrg      test -d "$dstdir"
308d1c293acSmrg      dstdir_status=$?
309d1c293acSmrg    fi
310d1c293acSmrg  fi
311d1c293acSmrg
312d1c293acSmrg  obsolete_mkdir_used=false
313d1c293acSmrg
314d1c293acSmrg  if test $dstdir_status != 0; then
315d1c293acSmrg    case $posix_mkdir in
316d1c293acSmrg      '')
317d1c293acSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
318d1c293acSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
319d1c293acSmrg	umask=`umask`
320d1c293acSmrg	case $stripcmd.$umask in
321d1c293acSmrg	  # Optimize common cases.
322d1c293acSmrg	  *[2367][2367]) mkdir_umask=$umask;;
323d1c293acSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324d1c293acSmrg
325d1c293acSmrg	  *[0-7])
326d1c293acSmrg	    mkdir_umask=`expr $umask + 22 \
327d1c293acSmrg	      - $umask % 100 % 40 + $umask % 20 \
328d1c293acSmrg	      - $umask % 10 % 4 + $umask % 2
329d1c293acSmrg	    `;;
330d1c293acSmrg	  *) mkdir_umask=$umask,go-w;;
331d1c293acSmrg	esac
332d1c293acSmrg
333d1c293acSmrg	# With -d, create the new directory with the user-specified mode.
334d1c293acSmrg	# Otherwise, rely on $mkdir_umask.
335d1c293acSmrg	if test -n "$dir_arg"; then
336d1c293acSmrg	  mkdir_mode=-m$mode
3378440cca4Smrg	else
338d1c293acSmrg	  mkdir_mode=
3398440cca4Smrg	fi
3408440cca4Smrg
341d1c293acSmrg	posix_mkdir=false
342d1c293acSmrg	case $umask in
343d1c293acSmrg	  *[123567][0-7][0-7])
344d1c293acSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345d1c293acSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346d1c293acSmrg	    ;;
347d1c293acSmrg	  *)
348d1c293acSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349d1c293acSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350d1c293acSmrg
351d1c293acSmrg	    if (umask $mkdir_umask &&
352d1c293acSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353d1c293acSmrg	    then
354d1c293acSmrg	      if test -z "$dir_arg" || {
355d1c293acSmrg		   # Check for POSIX incompatibilities with -m.
356d1c293acSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357d1c293acSmrg		   # other-writeable bit of parent directory when it shouldn't.
358d1c293acSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359d1c293acSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360d1c293acSmrg		   case $ls_ld_tmpdir in
361d1c293acSmrg		     d????-?r-*) different_mode=700;;
362d1c293acSmrg		     d????-?--*) different_mode=755;;
363d1c293acSmrg		     *) false;;
364d1c293acSmrg		   esac &&
365d1c293acSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366d1c293acSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367d1c293acSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368d1c293acSmrg		   }
369d1c293acSmrg		 }
370d1c293acSmrg	      then posix_mkdir=:
371d1c293acSmrg	      fi
372d1c293acSmrg	      rmdir "$tmpdir/d" "$tmpdir"
373d1c293acSmrg	    else
374d1c293acSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
375d1c293acSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376d1c293acSmrg	    fi
377d1c293acSmrg	    trap '' 0;;
378d1c293acSmrg	esac;;
379d1c293acSmrg    esac
3808440cca4Smrg
381d1c293acSmrg    if
382d1c293acSmrg      $posix_mkdir && (
383d1c293acSmrg	umask $mkdir_umask &&
384d1c293acSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385d1c293acSmrg      )
386d1c293acSmrg    then :
387d1c293acSmrg    else
388d1c293acSmrg
389d1c293acSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390d1c293acSmrg      # or it failed possibly due to a race condition.  Create the
391d1c293acSmrg      # directory the slow way, step by step, checking for races as we go.
392d1c293acSmrg
393d1c293acSmrg      case $dstdir in
394d1c293acSmrg	/*) prefix='/';;
39520915927Smrg	[-=\(\)!]*) prefix='./';;
396d1c293acSmrg	*)  prefix='';;
397d1c293acSmrg      esac
398d1c293acSmrg
399d1c293acSmrg      eval "$initialize_posix_glob"
400d1c293acSmrg
401d1c293acSmrg      oIFS=$IFS
402d1c293acSmrg      IFS=/
403d1c293acSmrg      $posix_glob set -f
404d1c293acSmrg      set fnord $dstdir
405d1c293acSmrg      shift
406d1c293acSmrg      $posix_glob set +f
407d1c293acSmrg      IFS=$oIFS
408d1c293acSmrg
409d1c293acSmrg      prefixes=
410d1c293acSmrg
411d1c293acSmrg      for d
412d1c293acSmrg      do
41320915927Smrg	test X"$d" = X && continue
414d1c293acSmrg
415d1c293acSmrg	prefix=$prefix$d
416d1c293acSmrg	if test -d "$prefix"; then
417d1c293acSmrg	  prefixes=
418d1c293acSmrg	else
419d1c293acSmrg	  if $posix_mkdir; then
420d1c293acSmrg	    (umask=$mkdir_umask &&
421d1c293acSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422d1c293acSmrg	    # Don't fail if two instances are running concurrently.
423d1c293acSmrg	    test -d "$prefix" || exit 1
424d1c293acSmrg	  else
425d1c293acSmrg	    case $prefix in
426d1c293acSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427d1c293acSmrg	      *) qprefix=$prefix;;
428d1c293acSmrg	    esac
429d1c293acSmrg	    prefixes="$prefixes '$qprefix'"
430d1c293acSmrg	  fi
431d1c293acSmrg	fi
432d1c293acSmrg	prefix=$prefix/
433d1c293acSmrg      done
434d1c293acSmrg
435d1c293acSmrg      if test -n "$prefixes"; then
436d1c293acSmrg	# Don't fail if two instances are running concurrently.
437d1c293acSmrg	(umask $mkdir_umask &&
438d1c293acSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439d1c293acSmrg	  test -d "$dstdir" || exit 1
440d1c293acSmrg	obsolete_mkdir_used=true
441d1c293acSmrg      fi
442d1c293acSmrg    fi
443d1c293acSmrg  fi
444d1c293acSmrg
445d1c293acSmrg  if test -n "$dir_arg"; then
446d1c293acSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447d1c293acSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448d1c293acSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449d1c293acSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450d1c293acSmrg  else
451d1c293acSmrg
452d1c293acSmrg    # Make a couple of temp file names in the proper directory.
453d1c293acSmrg    dsttmp=$dstdir/_inst.$$_
454d1c293acSmrg    rmtmp=$dstdir/_rm.$$_
455d1c293acSmrg
456d1c293acSmrg    # Trap to clean up those temp files at exit.
457d1c293acSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458d1c293acSmrg
459d1c293acSmrg    # Copy the file name to the temp name.
460d1c293acSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461d1c293acSmrg
462d1c293acSmrg    # and set any options; do chmod last to preserve setuid bits.
463d1c293acSmrg    #
464d1c293acSmrg    # If any of these fail, we abort the whole thing.  If we want to
465d1c293acSmrg    # ignore errors from any of these, just make sure not to ignore
466d1c293acSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467d1c293acSmrg    #
468d1c293acSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469d1c293acSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470d1c293acSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471d1c293acSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472d1c293acSmrg
473d1c293acSmrg    # If -C, don't bother to copy if it wouldn't change the file.
474d1c293acSmrg    if $copy_on_change &&
475d1c293acSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476d1c293acSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477d1c293acSmrg
478d1c293acSmrg       eval "$initialize_posix_glob" &&
479d1c293acSmrg       $posix_glob set -f &&
480d1c293acSmrg       set X $old && old=:$2:$4:$5:$6 &&
481d1c293acSmrg       set X $new && new=:$2:$4:$5:$6 &&
482d1c293acSmrg       $posix_glob set +f &&
483d1c293acSmrg
484d1c293acSmrg       test "$old" = "$new" &&
485d1c293acSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486d1c293acSmrg    then
487d1c293acSmrg      rm -f "$dsttmp"
488d1c293acSmrg    else
489d1c293acSmrg      # Rename the file to the real destination.
490d1c293acSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491d1c293acSmrg
492d1c293acSmrg      # The rename failed, perhaps because mv can't rename something else
493d1c293acSmrg      # to itself, or perhaps because mv is so ancient that it does not
494d1c293acSmrg      # support -f.
495d1c293acSmrg      {
496d1c293acSmrg	# Now remove or move aside any old file at destination location.
497d1c293acSmrg	# We try this two ways since rm can't unlink itself on some
498d1c293acSmrg	# systems and the destination file might be busy for other
499d1c293acSmrg	# reasons.  In this case, the final cleanup might fail but the new
500d1c293acSmrg	# file should still install successfully.
501d1c293acSmrg	{
502d1c293acSmrg	  test ! -f "$dst" ||
503d1c293acSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504d1c293acSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505d1c293acSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506d1c293acSmrg	  } ||
507d1c293acSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
508d1c293acSmrg	    (exit 1); exit 1
509d1c293acSmrg	  }
510d1c293acSmrg	} &&
511d1c293acSmrg
512d1c293acSmrg	# Now rename the file to the real destination.
513d1c293acSmrg	$doit $mvcmd "$dsttmp" "$dst"
514d1c293acSmrg      }
515d1c293acSmrg    fi || exit 1
516d1c293acSmrg
517d1c293acSmrg    trap '' 0
518d1c293acSmrg  fi
519d1c293acSmrgdone
5208440cca4Smrg
521d1c293acSmrg# Local variables:
522d1c293acSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523d1c293acSmrg# time-stamp-start: "scriptversion="
524d1c293acSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525d1c293acSmrg# time-stamp-time-zone: "UTC"
526d1c293acSmrg# time-stamp-end: "; # UTC"
527d1c293acSmrg# End:
528