install-sh revision c27c18e8
1c43cc173Smrg#!/bin/sh
2c43cc173Smrg# install - install a program, script, or datafile
3c43cc173Smrg
4c27c18e8Smrgscriptversion=2009-04-28.21; # UTC
5c43cc173Smrg
6c43cc173Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c43cc173Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8c43cc173Smrg# following copyright and license.
9c43cc173Smrg#
10c43cc173Smrg# Copyright (C) 1994 X Consortium
11c43cc173Smrg#
12c43cc173Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c43cc173Smrg# of this software and associated documentation files (the "Software"), to
14c43cc173Smrg# deal in the Software without restriction, including without limitation the
15c43cc173Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c43cc173Smrg# sell copies of the Software, and to permit persons to whom the Software is
17c43cc173Smrg# furnished to do so, subject to the following conditions:
18c43cc173Smrg#
19c43cc173Smrg# The above copyright notice and this permission notice shall be included in
20c43cc173Smrg# all copies or substantial portions of the Software.
21c43cc173Smrg#
22c43cc173Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c43cc173Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c43cc173Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c43cc173Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c43cc173Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c43cc173Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c43cc173Smrg#
29c43cc173Smrg# Except as contained in this notice, the name of the X Consortium shall not
30c43cc173Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c43cc173Smrg# ings in this Software without prior written authorization from the X Consor-
32c43cc173Smrg# tium.
33c43cc173Smrg#
34c43cc173Smrg#
35c43cc173Smrg# FSF changes to this file are in the public domain.
36c43cc173Smrg#
37c43cc173Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38c43cc173Smrg# `make' implicit rules from creating a file called install from it
39c43cc173Smrg# when there is no Makefile.
40c43cc173Smrg#
41c43cc173Smrg# This script is compatible with the BSD install script, but was written
42c43cc173Smrg# from scratch.
43c43cc173Smrg
44c43cc173Smrgnl='
45c43cc173Smrg'
46c43cc173SmrgIFS=" ""	$nl"
47c43cc173Smrg
48c43cc173Smrg# set DOITPROG to echo to test this script
49c43cc173Smrg
50c43cc173Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51c27c18e8Smrgdoit=${DOITPROG-}
52c43cc173Smrgif test -z "$doit"; then
53c43cc173Smrg  doit_exec=exec
54c43cc173Smrgelse
55c43cc173Smrg  doit_exec=$doit
56c43cc173Smrgfi
57c43cc173Smrg
58c43cc173Smrg# Put in absolute file names if you don't have them in your path;
59c43cc173Smrg# or use environment vars.
60c43cc173Smrg
61c27c18e8Smrgchgrpprog=${CHGRPPROG-chgrp}
62c27c18e8Smrgchmodprog=${CHMODPROG-chmod}
63c27c18e8Smrgchownprog=${CHOWNPROG-chown}
64c27c18e8Smrgcmpprog=${CMPPROG-cmp}
65c27c18e8Smrgcpprog=${CPPROG-cp}
66c27c18e8Smrgmkdirprog=${MKDIRPROG-mkdir}
67c27c18e8Smrgmvprog=${MVPROG-mv}
68c27c18e8Smrgrmprog=${RMPROG-rm}
69c27c18e8Smrgstripprog=${STRIPPROG-strip}
70c27c18e8Smrg
71c27c18e8Smrgposix_glob='?'
72c27c18e8Smrginitialize_posix_glob='
73c27c18e8Smrg  test "$posix_glob" != "?" || {
74c27c18e8Smrg    if (set -f) 2>/dev/null; then
75c27c18e8Smrg      posix_glob=
76c27c18e8Smrg    else
77c27c18e8Smrg      posix_glob=:
78c27c18e8Smrg    fi
79c27c18e8Smrg  }
80c27c18e8Smrg'
81c43cc173Smrg
82c43cc173Smrgposix_mkdir=
83c43cc173Smrg
84c43cc173Smrg# Desired mode of installed file.
85c43cc173Smrgmode=0755
86c43cc173Smrg
87c27c18e8Smrgchgrpcmd=
88c43cc173Smrgchmodcmd=$chmodprog
89c43cc173Smrgchowncmd=
90c27c18e8Smrgmvcmd=$mvprog
91c43cc173Smrgrmcmd="$rmprog -f"
92c27c18e8Smrgstripcmd=
93c27c18e8Smrg
94c43cc173Smrgsrc=
95c43cc173Smrgdst=
96c43cc173Smrgdir_arg=
97c27c18e8Smrgdst_arg=
98c27c18e8Smrg
99c27c18e8Smrgcopy_on_change=false
100c43cc173Smrgno_target_directory=
101c43cc173Smrg
102c27c18e8Smrgusage="\
103c27c18e8SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104c43cc173Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105c43cc173Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106c43cc173Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107c43cc173Smrg
108c43cc173SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109c43cc173SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110c43cc173SmrgIn the 4th, create DIRECTORIES.
111c43cc173Smrg
112c43cc173SmrgOptions:
113c27c18e8Smrg     --help     display this help and exit.
114c27c18e8Smrg     --version  display version info and exit.
115c27c18e8Smrg
116c27c18e8Smrg  -c            (ignored)
117c27c18e8Smrg  -C            install only if different (preserve the last data modification time)
118c27c18e8Smrg  -d            create directories instead of installing files.
119c27c18e8Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120c27c18e8Smrg  -m MODE       $chmodprog installed files to MODE.
121c27c18e8Smrg  -o USER       $chownprog installed files to USER.
122c27c18e8Smrg  -s            $stripprog installed files.
123c27c18e8Smrg  -t DIRECTORY  install into DIRECTORY.
124c27c18e8Smrg  -T            report an error if DSTFILE is a directory.
125c43cc173Smrg
126c43cc173SmrgEnvironment variables override the default commands:
127c27c18e8Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128c27c18e8Smrg  RMPROG STRIPPROG
129c43cc173Smrg"
130c43cc173Smrg
131c43cc173Smrgwhile test $# -ne 0; do
132c43cc173Smrg  case $1 in
133c27c18e8Smrg    -c) ;;
134c27c18e8Smrg
135c27c18e8Smrg    -C) copy_on_change=true;;
136c43cc173Smrg
137c27c18e8Smrg    -d) dir_arg=true;;
138c43cc173Smrg
139c43cc173Smrg    -g) chgrpcmd="$chgrpprog $2"
140c27c18e8Smrg	shift;;
141c43cc173Smrg
142c43cc173Smrg    --help) echo "$usage"; exit $?;;
143c43cc173Smrg
144c43cc173Smrg    -m) mode=$2
145c43cc173Smrg	case $mode in
146c43cc173Smrg	  *' '* | *'	'* | *'
147c43cc173Smrg'*	  | *'*'* | *'?'* | *'['*)
148c43cc173Smrg	    echo "$0: invalid mode: $mode" >&2
149c43cc173Smrg	    exit 1;;
150c43cc173Smrg	esac
151c27c18e8Smrg	shift;;
152c43cc173Smrg
153c43cc173Smrg    -o) chowncmd="$chownprog $2"
154c27c18e8Smrg	shift;;
155c43cc173Smrg
156c27c18e8Smrg    -s) stripcmd=$stripprog;;
157c43cc173Smrg
158c27c18e8Smrg    -t) dst_arg=$2
159c27c18e8Smrg	shift;;
160c43cc173Smrg
161c27c18e8Smrg    -T) no_target_directory=true;;
162c43cc173Smrg
163c43cc173Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164c43cc173Smrg
165c43cc173Smrg    --)	shift
166c43cc173Smrg	break;;
167c43cc173Smrg
168c43cc173Smrg    -*)	echo "$0: invalid option: $1" >&2
169c43cc173Smrg	exit 1;;
170c43cc173Smrg
171c43cc173Smrg    *)  break;;
172c43cc173Smrg  esac
173c27c18e8Smrg  shift
174c43cc173Smrgdone
175c43cc173Smrg
176c27c18e8Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177c43cc173Smrg  # When -d is used, all remaining arguments are directories to create.
178c43cc173Smrg  # When -t is used, the destination is already specified.
179c43cc173Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180c43cc173Smrg  for arg
181c43cc173Smrg  do
182c27c18e8Smrg    if test -n "$dst_arg"; then
183c43cc173Smrg      # $@ is not empty: it contains at least $arg.
184c27c18e8Smrg      set fnord "$@" "$dst_arg"
185c43cc173Smrg      shift # fnord
186c43cc173Smrg    fi
187c43cc173Smrg    shift # arg
188c27c18e8Smrg    dst_arg=$arg
189c43cc173Smrg  done
190c43cc173Smrgfi
191c43cc173Smrg
192c43cc173Smrgif test $# -eq 0; then
193c43cc173Smrg  if test -z "$dir_arg"; then
194c43cc173Smrg    echo "$0: no input file specified." >&2
195c43cc173Smrg    exit 1
196c43cc173Smrg  fi
197c43cc173Smrg  # It's OK to call `install-sh -d' without argument.
198c43cc173Smrg  # This can happen when creating conditional directories.
199c43cc173Smrg  exit 0
200c43cc173Smrgfi
201c43cc173Smrg
202c43cc173Smrgif test -z "$dir_arg"; then
203c43cc173Smrg  trap '(exit $?); exit' 1 2 13 15
204c43cc173Smrg
205c43cc173Smrg  # Set umask so as not to create temps with too-generous modes.
206c43cc173Smrg  # However, 'strip' requires both read and write access to temps.
207c43cc173Smrg  case $mode in
208c43cc173Smrg    # Optimize common cases.
209c43cc173Smrg    *644) cp_umask=133;;
210c43cc173Smrg    *755) cp_umask=22;;
211c43cc173Smrg
212c43cc173Smrg    *[0-7])
213c43cc173Smrg      if test -z "$stripcmd"; then
214c43cc173Smrg	u_plus_rw=
215c43cc173Smrg      else
216c43cc173Smrg	u_plus_rw='% 200'
217c43cc173Smrg      fi
218c43cc173Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219c43cc173Smrg    *)
220c43cc173Smrg      if test -z "$stripcmd"; then
221c43cc173Smrg	u_plus_rw=
222c43cc173Smrg      else
223c43cc173Smrg	u_plus_rw=,u+rw
224c43cc173Smrg      fi
225c43cc173Smrg      cp_umask=$mode$u_plus_rw;;
226c43cc173Smrg  esac
227c43cc173Smrgfi
228c43cc173Smrg
229c43cc173Smrgfor src
230c43cc173Smrgdo
231c43cc173Smrg  # Protect names starting with `-'.
232c43cc173Smrg  case $src in
233c27c18e8Smrg    -*) src=./$src;;
234c43cc173Smrg  esac
235c43cc173Smrg
236c43cc173Smrg  if test -n "$dir_arg"; then
237c43cc173Smrg    dst=$src
238c43cc173Smrg    dstdir=$dst
239c43cc173Smrg    test -d "$dstdir"
240c43cc173Smrg    dstdir_status=$?
241c43cc173Smrg  else
242c43cc173Smrg
243c43cc173Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244c43cc173Smrg    # might cause directories to be created, which would be especially bad
245c43cc173Smrg    # if $src (and thus $dsttmp) contains '*'.
246c43cc173Smrg    if test ! -f "$src" && test ! -d "$src"; then
247c43cc173Smrg      echo "$0: $src does not exist." >&2
248c43cc173Smrg      exit 1
249c43cc173Smrg    fi
250c43cc173Smrg
251c27c18e8Smrg    if test -z "$dst_arg"; then
252c43cc173Smrg      echo "$0: no destination specified." >&2
253c43cc173Smrg      exit 1
254c43cc173Smrg    fi
255c43cc173Smrg
256c27c18e8Smrg    dst=$dst_arg
257c43cc173Smrg    # Protect names starting with `-'.
258c43cc173Smrg    case $dst in
259c27c18e8Smrg      -*) dst=./$dst;;
260c43cc173Smrg    esac
261c43cc173Smrg
262c43cc173Smrg    # If destination is a directory, append the input filename; won't work
263c43cc173Smrg    # if double slashes aren't ignored.
264c43cc173Smrg    if test -d "$dst"; then
265c43cc173Smrg      if test -n "$no_target_directory"; then
266c27c18e8Smrg	echo "$0: $dst_arg: Is a directory" >&2
267c43cc173Smrg	exit 1
268c43cc173Smrg      fi
269c43cc173Smrg      dstdir=$dst
270c43cc173Smrg      dst=$dstdir/`basename "$src"`
271c43cc173Smrg      dstdir_status=0
272c43cc173Smrg    else
273c43cc173Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274c43cc173Smrg      dstdir=`
275c43cc173Smrg	(dirname "$dst") 2>/dev/null ||
276c43cc173Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277c43cc173Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
278c43cc173Smrg	     X"$dst" : 'X\(//\)$' \| \
279c43cc173Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280c43cc173Smrg	echo X"$dst" |
281c43cc173Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282c43cc173Smrg		   s//\1/
283c43cc173Smrg		   q
284c43cc173Smrg		 }
285c43cc173Smrg		 /^X\(\/\/\)[^/].*/{
286c43cc173Smrg		   s//\1/
287c43cc173Smrg		   q
288c43cc173Smrg		 }
289c43cc173Smrg		 /^X\(\/\/\)$/{
290c43cc173Smrg		   s//\1/
291c43cc173Smrg		   q
292c43cc173Smrg		 }
293c43cc173Smrg		 /^X\(\/\).*/{
294c43cc173Smrg		   s//\1/
295c43cc173Smrg		   q
296c43cc173Smrg		 }
297c43cc173Smrg		 s/.*/./; q'
298c43cc173Smrg      `
299c43cc173Smrg
300c43cc173Smrg      test -d "$dstdir"
301c43cc173Smrg      dstdir_status=$?
302c43cc173Smrg    fi
303c43cc173Smrg  fi
304c43cc173Smrg
305c43cc173Smrg  obsolete_mkdir_used=false
306c43cc173Smrg
307c43cc173Smrg  if test $dstdir_status != 0; then
308c43cc173Smrg    case $posix_mkdir in
309c43cc173Smrg      '')
310c43cc173Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
311c43cc173Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
312c43cc173Smrg	umask=`umask`
313c43cc173Smrg	case $stripcmd.$umask in
314c43cc173Smrg	  # Optimize common cases.
315c43cc173Smrg	  *[2367][2367]) mkdir_umask=$umask;;
316c43cc173Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317c43cc173Smrg
318c43cc173Smrg	  *[0-7])
319c43cc173Smrg	    mkdir_umask=`expr $umask + 22 \
320c43cc173Smrg	      - $umask % 100 % 40 + $umask % 20 \
321c43cc173Smrg	      - $umask % 10 % 4 + $umask % 2
322c43cc173Smrg	    `;;
323c43cc173Smrg	  *) mkdir_umask=$umask,go-w;;
324c43cc173Smrg	esac
325c43cc173Smrg
326c43cc173Smrg	# With -d, create the new directory with the user-specified mode.
327c43cc173Smrg	# Otherwise, rely on $mkdir_umask.
328c43cc173Smrg	if test -n "$dir_arg"; then
329c43cc173Smrg	  mkdir_mode=-m$mode
330c43cc173Smrg	else
331c43cc173Smrg	  mkdir_mode=
332c43cc173Smrg	fi
333c43cc173Smrg
334c43cc173Smrg	posix_mkdir=false
335c43cc173Smrg	case $umask in
336c43cc173Smrg	  *[123567][0-7][0-7])
337c43cc173Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338c43cc173Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339c43cc173Smrg	    ;;
340c43cc173Smrg	  *)
341c43cc173Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342c43cc173Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343c43cc173Smrg
344c43cc173Smrg	    if (umask $mkdir_umask &&
345c43cc173Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346c43cc173Smrg	    then
347c43cc173Smrg	      if test -z "$dir_arg" || {
348c43cc173Smrg		   # Check for POSIX incompatibilities with -m.
349c43cc173Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350c43cc173Smrg		   # other-writeable bit of parent directory when it shouldn't.
351c43cc173Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352c43cc173Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353c43cc173Smrg		   case $ls_ld_tmpdir in
354c43cc173Smrg		     d????-?r-*) different_mode=700;;
355c43cc173Smrg		     d????-?--*) different_mode=755;;
356c43cc173Smrg		     *) false;;
357c43cc173Smrg		   esac &&
358c43cc173Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359c43cc173Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360c43cc173Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361c43cc173Smrg		   }
362c43cc173Smrg		 }
363c43cc173Smrg	      then posix_mkdir=:
364c43cc173Smrg	      fi
365c43cc173Smrg	      rmdir "$tmpdir/d" "$tmpdir"
366c43cc173Smrg	    else
367c43cc173Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
368c43cc173Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369c43cc173Smrg	    fi
370c43cc173Smrg	    trap '' 0;;
371c43cc173Smrg	esac;;
372c43cc173Smrg    esac
373c43cc173Smrg
374c43cc173Smrg    if
375c43cc173Smrg      $posix_mkdir && (
376c43cc173Smrg	umask $mkdir_umask &&
377c43cc173Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378c43cc173Smrg      )
379c43cc173Smrg    then :
380c43cc173Smrg    else
381c43cc173Smrg
382c43cc173Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383c43cc173Smrg      # or it failed possibly due to a race condition.  Create the
384c43cc173Smrg      # directory the slow way, step by step, checking for races as we go.
385c43cc173Smrg
386c43cc173Smrg      case $dstdir in
387c27c18e8Smrg	/*) prefix='/';;
388c27c18e8Smrg	-*) prefix='./';;
389c27c18e8Smrg	*)  prefix='';;
390c43cc173Smrg      esac
391c43cc173Smrg
392c27c18e8Smrg      eval "$initialize_posix_glob"
393c43cc173Smrg
394c43cc173Smrg      oIFS=$IFS
395c43cc173Smrg      IFS=/
396c27c18e8Smrg      $posix_glob set -f
397c43cc173Smrg      set fnord $dstdir
398c43cc173Smrg      shift
399c27c18e8Smrg      $posix_glob set +f
400c43cc173Smrg      IFS=$oIFS
401c43cc173Smrg
402c43cc173Smrg      prefixes=
403c43cc173Smrg
404c43cc173Smrg      for d
405c43cc173Smrg      do
406c43cc173Smrg	test -z "$d" && continue
407c43cc173Smrg
408c43cc173Smrg	prefix=$prefix$d
409c43cc173Smrg	if test -d "$prefix"; then
410c43cc173Smrg	  prefixes=
411c43cc173Smrg	else
412c43cc173Smrg	  if $posix_mkdir; then
413c43cc173Smrg	    (umask=$mkdir_umask &&
414c43cc173Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415c43cc173Smrg	    # Don't fail if two instances are running concurrently.
416c43cc173Smrg	    test -d "$prefix" || exit 1
417c43cc173Smrg	  else
418c43cc173Smrg	    case $prefix in
419c43cc173Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420c43cc173Smrg	      *) qprefix=$prefix;;
421c43cc173Smrg	    esac
422c43cc173Smrg	    prefixes="$prefixes '$qprefix'"
423c43cc173Smrg	  fi
424c43cc173Smrg	fi
425c43cc173Smrg	prefix=$prefix/
426c43cc173Smrg      done
427c43cc173Smrg
428c43cc173Smrg      if test -n "$prefixes"; then
429c43cc173Smrg	# Don't fail if two instances are running concurrently.
430c43cc173Smrg	(umask $mkdir_umask &&
431c43cc173Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432c43cc173Smrg	  test -d "$dstdir" || exit 1
433c43cc173Smrg	obsolete_mkdir_used=true
434c43cc173Smrg      fi
435c43cc173Smrg    fi
436c43cc173Smrg  fi
437c43cc173Smrg
438c43cc173Smrg  if test -n "$dir_arg"; then
439c43cc173Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440c43cc173Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441c43cc173Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442c43cc173Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443c43cc173Smrg  else
444c43cc173Smrg
445c43cc173Smrg    # Make a couple of temp file names in the proper directory.
446c43cc173Smrg    dsttmp=$dstdir/_inst.$$_
447c43cc173Smrg    rmtmp=$dstdir/_rm.$$_
448c43cc173Smrg
449c43cc173Smrg    # Trap to clean up those temp files at exit.
450c43cc173Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451c43cc173Smrg
452c43cc173Smrg    # Copy the file name to the temp name.
453c43cc173Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454c43cc173Smrg
455c43cc173Smrg    # and set any options; do chmod last to preserve setuid bits.
456c43cc173Smrg    #
457c43cc173Smrg    # If any of these fail, we abort the whole thing.  If we want to
458c43cc173Smrg    # ignore errors from any of these, just make sure not to ignore
459c43cc173Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460c43cc173Smrg    #
461c27c18e8Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462c27c18e8Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463c27c18e8Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464c27c18e8Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465c27c18e8Smrg
466c27c18e8Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467c27c18e8Smrg    if $copy_on_change &&
468c27c18e8Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469c27c18e8Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470c27c18e8Smrg
471c27c18e8Smrg       eval "$initialize_posix_glob" &&
472c27c18e8Smrg       $posix_glob set -f &&
473c27c18e8Smrg       set X $old && old=:$2:$4:$5:$6 &&
474c27c18e8Smrg       set X $new && new=:$2:$4:$5:$6 &&
475c27c18e8Smrg       $posix_glob set +f &&
476c27c18e8Smrg
477c27c18e8Smrg       test "$old" = "$new" &&
478c27c18e8Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479c27c18e8Smrg    then
480c27c18e8Smrg      rm -f "$dsttmp"
481c27c18e8Smrg    else
482c27c18e8Smrg      # Rename the file to the real destination.
483c27c18e8Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484c27c18e8Smrg
485c27c18e8Smrg      # The rename failed, perhaps because mv can't rename something else
486c27c18e8Smrg      # to itself, or perhaps because mv is so ancient that it does not
487c27c18e8Smrg      # support -f.
488c27c18e8Smrg      {
489c27c18e8Smrg	# Now remove or move aside any old file at destination location.
490c27c18e8Smrg	# We try this two ways since rm can't unlink itself on some
491c27c18e8Smrg	# systems and the destination file might be busy for other
492c27c18e8Smrg	# reasons.  In this case, the final cleanup might fail but the new
493c27c18e8Smrg	# file should still install successfully.
494c27c18e8Smrg	{
495c27c18e8Smrg	  test ! -f "$dst" ||
496c27c18e8Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497c27c18e8Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498c27c18e8Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499c27c18e8Smrg	  } ||
500c27c18e8Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
501c27c18e8Smrg	    (exit 1); exit 1
502c27c18e8Smrg	  }
503c27c18e8Smrg	} &&
504c27c18e8Smrg
505c27c18e8Smrg	# Now rename the file to the real destination.
506c27c18e8Smrg	$doit $mvcmd "$dsttmp" "$dst"
507c27c18e8Smrg      }
508c27c18e8Smrg    fi || exit 1
509c43cc173Smrg
510c43cc173Smrg    trap '' 0
511c43cc173Smrg  fi
512c43cc173Smrgdone
513c43cc173Smrg
514c43cc173Smrg# Local variables:
515c43cc173Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516c43cc173Smrg# time-stamp-start: "scriptversion="
517c43cc173Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518c27c18e8Smrg# time-stamp-time-zone: "UTC"
519c27c18e8Smrg# time-stamp-end: "; # UTC"
520c43cc173Smrg# End:
521