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