install-sh revision ba064abe
1191cded7Smrg#!/bin/sh
2191cded7Smrg# install - install a program, script, or datafile
3191cded7Smrg
4ba064abeSmrgscriptversion=2009-04-28.21; # UTC
5191cded7Smrg
6191cded7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7191cded7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8191cded7Smrg# following copyright and license.
9191cded7Smrg#
10191cded7Smrg# Copyright (C) 1994 X Consortium
11191cded7Smrg#
12191cded7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13191cded7Smrg# of this software and associated documentation files (the "Software"), to
14191cded7Smrg# deal in the Software without restriction, including without limitation the
15191cded7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16191cded7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17191cded7Smrg# furnished to do so, subject to the following conditions:
18191cded7Smrg#
19191cded7Smrg# The above copyright notice and this permission notice shall be included in
20191cded7Smrg# all copies or substantial portions of the Software.
21191cded7Smrg#
22191cded7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23191cded7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24191cded7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25191cded7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26191cded7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27191cded7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28191cded7Smrg#
29191cded7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30191cded7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31191cded7Smrg# ings in this Software without prior written authorization from the X Consor-
32191cded7Smrg# tium.
33191cded7Smrg#
34191cded7Smrg#
35191cded7Smrg# FSF changes to this file are in the public domain.
36191cded7Smrg#
37191cded7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38191cded7Smrg# `make' implicit rules from creating a file called install from it
39191cded7Smrg# when there is no Makefile.
40191cded7Smrg#
41191cded7Smrg# This script is compatible with the BSD install script, but was written
42b290cf36Smrg# from scratch.
43b290cf36Smrg
44b290cf36Smrgnl='
45b290cf36Smrg'
46b290cf36SmrgIFS=" ""	$nl"
47191cded7Smrg
48191cded7Smrg# set DOITPROG to echo to test this script
49191cded7Smrg
50191cded7Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51b290cf36Smrgdoit=${DOITPROG-}
52b290cf36Smrgif test -z "$doit"; then
53b290cf36Smrg  doit_exec=exec
54b290cf36Smrgelse
55b290cf36Smrg  doit_exec=$doit
56b290cf36Smrgfi
57191cded7Smrg
58b290cf36Smrg# Put in absolute file names if you don't have them in your path;
59b290cf36Smrg# or use environment vars.
60b290cf36Smrg
61b290cf36Smrgchgrpprog=${CHGRPPROG-chgrp}
62b290cf36Smrgchmodprog=${CHMODPROG-chmod}
63b290cf36Smrgchownprog=${CHOWNPROG-chown}
64b290cf36Smrgcmpprog=${CMPPROG-cmp}
65b290cf36Smrgcpprog=${CPPROG-cp}
66b290cf36Smrgmkdirprog=${MKDIRPROG-mkdir}
67b290cf36Smrgmvprog=${MVPROG-mv}
68b290cf36Smrgrmprog=${RMPROG-rm}
69b290cf36Smrgstripprog=${STRIPPROG-strip}
70b290cf36Smrg
71b290cf36Smrgposix_glob='?'
72b290cf36Smrginitialize_posix_glob='
73b290cf36Smrg  test "$posix_glob" != "?" || {
74b290cf36Smrg    if (set -f) 2>/dev/null; then
75b290cf36Smrg      posix_glob=
76b290cf36Smrg    else
77b290cf36Smrg      posix_glob=:
78b290cf36Smrg    fi
79b290cf36Smrg  }
80b290cf36Smrg'
81191cded7Smrg
82b290cf36Smrgposix_mkdir=
83b290cf36Smrg
84b290cf36Smrg# Desired mode of installed file.
85b290cf36Smrgmode=0755
86191cded7Smrg
87191cded7Smrgchgrpcmd=
88b290cf36Smrgchmodcmd=$chmodprog
89b290cf36Smrgchowncmd=
90b290cf36Smrgmvcmd=$mvprog
91191cded7Smrgrmcmd="$rmprog -f"
92b290cf36Smrgstripcmd=
93b290cf36Smrg
94191cded7Smrgsrc=
95191cded7Smrgdst=
96191cded7Smrgdir_arg=
97b290cf36Smrgdst_arg=
98b290cf36Smrg
99b290cf36Smrgcopy_on_change=false
100191cded7Smrgno_target_directory=
101191cded7Smrg
102b290cf36Smrgusage="\
103b290cf36SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104191cded7Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105191cded7Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106191cded7Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107191cded7Smrg
108191cded7SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109191cded7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110191cded7SmrgIn the 4th, create DIRECTORIES.
111191cded7Smrg
112191cded7SmrgOptions:
113b290cf36Smrg     --help     display this help and exit.
114b290cf36Smrg     --version  display version info and exit.
115b290cf36Smrg
116b290cf36Smrg  -c            (ignored)
117b290cf36Smrg  -C            install only if different (preserve the last data modification time)
118b290cf36Smrg  -d            create directories instead of installing files.
119b290cf36Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120b290cf36Smrg  -m MODE       $chmodprog installed files to MODE.
121b290cf36Smrg  -o USER       $chownprog installed files to USER.
122b290cf36Smrg  -s            $stripprog installed files.
123b290cf36Smrg  -t DIRECTORY  install into DIRECTORY.
124b290cf36Smrg  -T            report an error if DSTFILE is a directory.
125191cded7Smrg
126191cded7SmrgEnvironment variables override the default commands:
127b290cf36Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128b290cf36Smrg  RMPROG STRIPPROG
129191cded7Smrg"
130191cded7Smrg
131b290cf36Smrgwhile test $# -ne 0; do
132191cded7Smrg  case $1 in
133b290cf36Smrg    -c) ;;
134b290cf36Smrg
135b290cf36Smrg    -C) copy_on_change=true;;
136191cded7Smrg
137b290cf36Smrg    -d) dir_arg=true;;
138191cded7Smrg
139191cded7Smrg    -g) chgrpcmd="$chgrpprog $2"
140b290cf36Smrg	shift;;
141191cded7Smrg
142191cded7Smrg    --help) echo "$usage"; exit $?;;
143191cded7Smrg
144b290cf36Smrg    -m) mode=$2
145b290cf36Smrg	case $mode in
146b290cf36Smrg	  *' '* | *'	'* | *'
147b290cf36Smrg'*	  | *'*'* | *'?'* | *'['*)
148b290cf36Smrg	    echo "$0: invalid mode: $mode" >&2
149b290cf36Smrg	    exit 1;;
150b290cf36Smrg	esac
151b290cf36Smrg	shift;;
152191cded7Smrg
153191cded7Smrg    -o) chowncmd="$chownprog $2"
154b290cf36Smrg	shift;;
155191cded7Smrg
156b290cf36Smrg    -s) stripcmd=$stripprog;;
157191cded7Smrg
158b290cf36Smrg    -t) dst_arg=$2
159b290cf36Smrg	shift;;
160191cded7Smrg
161b290cf36Smrg    -T) no_target_directory=true;;
162191cded7Smrg
163191cded7Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164191cded7Smrg
165b290cf36Smrg    --)	shift
166191cded7Smrg	break;;
167b290cf36Smrg
168b290cf36Smrg    -*)	echo "$0: invalid option: $1" >&2
169b290cf36Smrg	exit 1;;
170b290cf36Smrg
171b290cf36Smrg    *)  break;;
172191cded7Smrg  esac
173b290cf36Smrg  shift
174191cded7Smrgdone
175191cded7Smrg
176b290cf36Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177b290cf36Smrg  # When -d is used, all remaining arguments are directories to create.
178b290cf36Smrg  # When -t is used, the destination is already specified.
179b290cf36Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180b290cf36Smrg  for arg
181b290cf36Smrg  do
182b290cf36Smrg    if test -n "$dst_arg"; then
183b290cf36Smrg      # $@ is not empty: it contains at least $arg.
184b290cf36Smrg      set fnord "$@" "$dst_arg"
185b290cf36Smrg      shift # fnord
186b290cf36Smrg    fi
187b290cf36Smrg    shift # arg
188b290cf36Smrg    dst_arg=$arg
189b290cf36Smrg  done
190b290cf36Smrgfi
191b290cf36Smrg
192b290cf36Smrgif test $# -eq 0; then
193191cded7Smrg  if test -z "$dir_arg"; then
194191cded7Smrg    echo "$0: no input file specified." >&2
195191cded7Smrg    exit 1
196191cded7Smrg  fi
197191cded7Smrg  # It's OK to call `install-sh -d' without argument.
198191cded7Smrg  # This can happen when creating conditional directories.
199191cded7Smrg  exit 0
200191cded7Smrgfi
201191cded7Smrg
202b290cf36Smrgif test -z "$dir_arg"; then
203b290cf36Smrg  trap '(exit $?); exit' 1 2 13 15
204b290cf36Smrg
205b290cf36Smrg  # Set umask so as not to create temps with too-generous modes.
206b290cf36Smrg  # However, 'strip' requires both read and write access to temps.
207b290cf36Smrg  case $mode in
208b290cf36Smrg    # Optimize common cases.
209b290cf36Smrg    *644) cp_umask=133;;
210b290cf36Smrg    *755) cp_umask=22;;
211b290cf36Smrg
212b290cf36Smrg    *[0-7])
213b290cf36Smrg      if test -z "$stripcmd"; then
214b290cf36Smrg	u_plus_rw=
215b290cf36Smrg      else
216b290cf36Smrg	u_plus_rw='% 200'
217b290cf36Smrg      fi
218b290cf36Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219b290cf36Smrg    *)
220b290cf36Smrg      if test -z "$stripcmd"; then
221b290cf36Smrg	u_plus_rw=
222b290cf36Smrg      else
223b290cf36Smrg	u_plus_rw=,u+rw
224b290cf36Smrg      fi
225b290cf36Smrg      cp_umask=$mode$u_plus_rw;;
226b290cf36Smrg  esac
227b290cf36Smrgfi
228b290cf36Smrg
229191cded7Smrgfor src
230191cded7Smrgdo
231191cded7Smrg  # Protect names starting with `-'.
232191cded7Smrg  case $src in
233b290cf36Smrg    -*) src=./$src;;
234191cded7Smrg  esac
235191cded7Smrg
236191cded7Smrg  if test -n "$dir_arg"; then
237191cded7Smrg    dst=$src
238b290cf36Smrg    dstdir=$dst
239b290cf36Smrg    test -d "$dstdir"
240b290cf36Smrg    dstdir_status=$?
241191cded7Smrg  else
242b290cf36Smrg
243191cded7Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244191cded7Smrg    # might cause directories to be created, which would be especially bad
245191cded7Smrg    # if $src (and thus $dsttmp) contains '*'.
246191cded7Smrg    if test ! -f "$src" && test ! -d "$src"; then
247191cded7Smrg      echo "$0: $src does not exist." >&2
248191cded7Smrg      exit 1
249191cded7Smrg    fi
250191cded7Smrg
251b290cf36Smrg    if test -z "$dst_arg"; then
252191cded7Smrg      echo "$0: no destination specified." >&2
253191cded7Smrg      exit 1
254191cded7Smrg    fi
255191cded7Smrg
256b290cf36Smrg    dst=$dst_arg
257191cded7Smrg    # Protect names starting with `-'.
258191cded7Smrg    case $dst in
259b290cf36Smrg      -*) dst=./$dst;;
260191cded7Smrg    esac
261191cded7Smrg
262191cded7Smrg    # If destination is a directory, append the input filename; won't work
263191cded7Smrg    # if double slashes aren't ignored.
264191cded7Smrg    if test -d "$dst"; then
265191cded7Smrg      if test -n "$no_target_directory"; then
266b290cf36Smrg	echo "$0: $dst_arg: Is a directory" >&2
267191cded7Smrg	exit 1
268191cded7Smrg      fi
269b290cf36Smrg      dstdir=$dst
270b290cf36Smrg      dst=$dstdir/`basename "$src"`
271b290cf36Smrg      dstdir_status=0
272b290cf36Smrg    else
273b290cf36Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274b290cf36Smrg      dstdir=`
275b290cf36Smrg	(dirname "$dst") 2>/dev/null ||
276b290cf36Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277b290cf36Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
278b290cf36Smrg	     X"$dst" : 'X\(//\)$' \| \
279b290cf36Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280b290cf36Smrg	echo X"$dst" |
281b290cf36Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282b290cf36Smrg		   s//\1/
283b290cf36Smrg		   q
284b290cf36Smrg		 }
285b290cf36Smrg		 /^X\(\/\/\)[^/].*/{
286b290cf36Smrg		   s//\1/
287b290cf36Smrg		   q
288b290cf36Smrg		 }
289b290cf36Smrg		 /^X\(\/\/\)$/{
290b290cf36Smrg		   s//\1/
291b290cf36Smrg		   q
292b290cf36Smrg		 }
293b290cf36Smrg		 /^X\(\/\).*/{
294b290cf36Smrg		   s//\1/
295b290cf36Smrg		   q
296b290cf36Smrg		 }
297b290cf36Smrg		 s/.*/./; q'
298b290cf36Smrg      `
299b290cf36Smrg
300b290cf36Smrg      test -d "$dstdir"
301b290cf36Smrg      dstdir_status=$?
302191cded7Smrg    fi
303191cded7Smrg  fi
304191cded7Smrg
305b290cf36Smrg  obsolete_mkdir_used=false
306b290cf36Smrg
307b290cf36Smrg  if test $dstdir_status != 0; then
308b290cf36Smrg    case $posix_mkdir in
309b290cf36Smrg      '')
310b290cf36Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
311b290cf36Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
312b290cf36Smrg	umask=`umask`
313b290cf36Smrg	case $stripcmd.$umask in
314b290cf36Smrg	  # Optimize common cases.
315b290cf36Smrg	  *[2367][2367]) mkdir_umask=$umask;;
316b290cf36Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317b290cf36Smrg
318b290cf36Smrg	  *[0-7])
319b290cf36Smrg	    mkdir_umask=`expr $umask + 22 \
320b290cf36Smrg	      - $umask % 100 % 40 + $umask % 20 \
321b290cf36Smrg	      - $umask % 10 % 4 + $umask % 2
322b290cf36Smrg	    `;;
323b290cf36Smrg	  *) mkdir_umask=$umask,go-w;;
324b290cf36Smrg	esac
325b290cf36Smrg
326b290cf36Smrg	# With -d, create the new directory with the user-specified mode.
327b290cf36Smrg	# Otherwise, rely on $mkdir_umask.
328b290cf36Smrg	if test -n "$dir_arg"; then
329b290cf36Smrg	  mkdir_mode=-m$mode
330b290cf36Smrg	else
331b290cf36Smrg	  mkdir_mode=
332b290cf36Smrg	fi
333b290cf36Smrg
334b290cf36Smrg	posix_mkdir=false
335b290cf36Smrg	case $umask in
336b290cf36Smrg	  *[123567][0-7][0-7])
337b290cf36Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338b290cf36Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339b290cf36Smrg	    ;;
340b290cf36Smrg	  *)
341b290cf36Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342b290cf36Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343b290cf36Smrg
344b290cf36Smrg	    if (umask $mkdir_umask &&
345b290cf36Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346b290cf36Smrg	    then
347b290cf36Smrg	      if test -z "$dir_arg" || {
348b290cf36Smrg		   # Check for POSIX incompatibilities with -m.
349b290cf36Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350b290cf36Smrg		   # other-writeable bit of parent directory when it shouldn't.
351b290cf36Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352b290cf36Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353b290cf36Smrg		   case $ls_ld_tmpdir in
354b290cf36Smrg		     d????-?r-*) different_mode=700;;
355b290cf36Smrg		     d????-?--*) different_mode=755;;
356b290cf36Smrg		     *) false;;
357b290cf36Smrg		   esac &&
358b290cf36Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359b290cf36Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360b290cf36Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361b290cf36Smrg		   }
362b290cf36Smrg		 }
363b290cf36Smrg	      then posix_mkdir=:
364b290cf36Smrg	      fi
365b290cf36Smrg	      rmdir "$tmpdir/d" "$tmpdir"
366b290cf36Smrg	    else
367b290cf36Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
368b290cf36Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369b290cf36Smrg	    fi
370b290cf36Smrg	    trap '' 0;;
371b290cf36Smrg	esac;;
372b290cf36Smrg    esac
373191cded7Smrg
374b290cf36Smrg    if
375b290cf36Smrg      $posix_mkdir && (
376b290cf36Smrg	umask $mkdir_umask &&
377b290cf36Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378b290cf36Smrg      )
379b290cf36Smrg    then :
380b290cf36Smrg    else
381191cded7Smrg
382b290cf36Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383b290cf36Smrg      # or it failed possibly due to a race condition.  Create the
384b290cf36Smrg      # directory the slow way, step by step, checking for races as we go.
385191cded7Smrg
386b290cf36Smrg      case $dstdir in
387b290cf36Smrg	/*) prefix='/';;
388b290cf36Smrg	-*) prefix='./';;
389b290cf36Smrg	*)  prefix='';;
390b290cf36Smrg      esac
391191cded7Smrg
392b290cf36Smrg      eval "$initialize_posix_glob"
393191cded7Smrg
394b290cf36Smrg      oIFS=$IFS
395b290cf36Smrg      IFS=/
396b290cf36Smrg      $posix_glob set -f
397b290cf36Smrg      set fnord $dstdir
398191cded7Smrg      shift
399b290cf36Smrg      $posix_glob set +f
400b290cf36Smrg      IFS=$oIFS
401b290cf36Smrg
402b290cf36Smrg      prefixes=
403b290cf36Smrg
404b290cf36Smrg      for d
405b290cf36Smrg      do
406b290cf36Smrg	test -z "$d" && continue
407b290cf36Smrg
408b290cf36Smrg	prefix=$prefix$d
409b290cf36Smrg	if test -d "$prefix"; then
410b290cf36Smrg	  prefixes=
411b290cf36Smrg	else
412b290cf36Smrg	  if $posix_mkdir; then
413b290cf36Smrg	    (umask=$mkdir_umask &&
414b290cf36Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415b290cf36Smrg	    # Don't fail if two instances are running concurrently.
416b290cf36Smrg	    test -d "$prefix" || exit 1
417b290cf36Smrg	  else
418b290cf36Smrg	    case $prefix in
419b290cf36Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420b290cf36Smrg	      *) qprefix=$prefix;;
421b290cf36Smrg	    esac
422b290cf36Smrg	    prefixes="$prefixes '$qprefix'"
423b290cf36Smrg	  fi
424b290cf36Smrg	fi
425b290cf36Smrg	prefix=$prefix/
426b290cf36Smrg      done
427b290cf36Smrg
428b290cf36Smrg      if test -n "$prefixes"; then
429b290cf36Smrg	# Don't fail if two instances are running concurrently.
430b290cf36Smrg	(umask $mkdir_umask &&
431b290cf36Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432b290cf36Smrg	  test -d "$dstdir" || exit 1
433b290cf36Smrg	obsolete_mkdir_used=true
434191cded7Smrg      fi
435b290cf36Smrg    fi
436191cded7Smrg  fi
437191cded7Smrg
438191cded7Smrg  if test -n "$dir_arg"; then
439b290cf36Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440b290cf36Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441b290cf36Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442b290cf36Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443191cded7Smrg  else
444191cded7Smrg
445191cded7Smrg    # Make a couple of temp file names in the proper directory.
446191cded7Smrg    dsttmp=$dstdir/_inst.$$_
447191cded7Smrg    rmtmp=$dstdir/_rm.$$_
448191cded7Smrg
449191cded7Smrg    # Trap to clean up those temp files at exit.
450191cded7Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451191cded7Smrg
452191cded7Smrg    # Copy the file name to the temp name.
453b290cf36Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454191cded7Smrg
455191cded7Smrg    # and set any options; do chmod last to preserve setuid bits.
456191cded7Smrg    #
457191cded7Smrg    # If any of these fail, we abort the whole thing.  If we want to
458191cded7Smrg    # ignore errors from any of these, just make sure not to ignore
459191cded7Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460191cded7Smrg    #
461b290cf36Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462b290cf36Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463b290cf36Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464b290cf36Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465b290cf36Smrg
466b290cf36Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467b290cf36Smrg    if $copy_on_change &&
468b290cf36Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469b290cf36Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470b290cf36Smrg
471b290cf36Smrg       eval "$initialize_posix_glob" &&
472b290cf36Smrg       $posix_glob set -f &&
473b290cf36Smrg       set X $old && old=:$2:$4:$5:$6 &&
474b290cf36Smrg       set X $new && new=:$2:$4:$5:$6 &&
475b290cf36Smrg       $posix_glob set +f &&
476b290cf36Smrg
477b290cf36Smrg       test "$old" = "$new" &&
478b290cf36Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479b290cf36Smrg    then
480b290cf36Smrg      rm -f "$dsttmp"
481b290cf36Smrg    else
482b290cf36Smrg      # Rename the file to the real destination.
483b290cf36Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484b290cf36Smrg
485b290cf36Smrg      # The rename failed, perhaps because mv can't rename something else
486b290cf36Smrg      # to itself, or perhaps because mv is so ancient that it does not
487b290cf36Smrg      # support -f.
488b290cf36Smrg      {
489b290cf36Smrg	# Now remove or move aside any old file at destination location.
490b290cf36Smrg	# We try this two ways since rm can't unlink itself on some
491b290cf36Smrg	# systems and the destination file might be busy for other
492b290cf36Smrg	# reasons.  In this case, the final cleanup might fail but the new
493b290cf36Smrg	# file should still install successfully.
494b290cf36Smrg	{
495b290cf36Smrg	  test ! -f "$dst" ||
496b290cf36Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497b290cf36Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498b290cf36Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499b290cf36Smrg	  } ||
500b290cf36Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
501b290cf36Smrg	    (exit 1); exit 1
502b290cf36Smrg	  }
503b290cf36Smrg	} &&
504b290cf36Smrg
505b290cf36Smrg	# Now rename the file to the real destination.
506b290cf36Smrg	$doit $mvcmd "$dsttmp" "$dst"
507b290cf36Smrg      }
508b290cf36Smrg    fi || exit 1
509b290cf36Smrg
510b290cf36Smrg    trap '' 0
511b290cf36Smrg  fi
512191cded7Smrgdone
513191cded7Smrg
514191cded7Smrg# Local variables:
515191cded7Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516191cded7Smrg# time-stamp-start: "scriptversion="
517191cded7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518ba064abeSmrg# time-stamp-time-zone: "UTC"
519ba064abeSmrg# time-stamp-end: "; # UTC"
520191cded7Smrg# End:
521