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