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