1d983712dSmrg#!/bin/sh
2d983712dSmrg# install - install a program, script, or datafile
3d983712dSmrg
44b9470b1Smrgscriptversion=2011-01-19.21; # UTC
5d983712dSmrg
6d983712dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7d983712dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8d983712dSmrg# following copyright and license.
9d983712dSmrg#
10d983712dSmrg# Copyright (C) 1994 X Consortium
11d983712dSmrg#
12d983712dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13d983712dSmrg# of this software and associated documentation files (the "Software"), to
14d983712dSmrg# deal in the Software without restriction, including without limitation the
15d983712dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16d983712dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17d983712dSmrg# furnished to do so, subject to the following conditions:
18d983712dSmrg#
19d983712dSmrg# The above copyright notice and this permission notice shall be included in
20d983712dSmrg# all copies or substantial portions of the Software.
21d983712dSmrg#
22d983712dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23d983712dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24d983712dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25d983712dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26d983712dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27d983712dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28d983712dSmrg#
29d983712dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30d983712dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31d983712dSmrg# ings in this Software without prior written authorization from the X Consor-
32d983712dSmrg# tium.
33d983712dSmrg#
34d983712dSmrg#
35d983712dSmrg# FSF changes to this file are in the public domain.
36d983712dSmrg#
37d983712dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38d983712dSmrg# `make' implicit rules from creating a file called install from it
39d983712dSmrg# when there is no Makefile.
40d983712dSmrg#
41d983712dSmrg# This script is compatible with the BSD install script, but was written
42962c3257Smrg# from scratch.
43962c3257Smrg
44962c3257Smrgnl='
45962c3257Smrg'
46962c3257SmrgIFS=" ""	$nl"
47d983712dSmrg
48d983712dSmrg# set DOITPROG to echo to test this script
49d983712dSmrg
50d983712dSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51962c3257Smrgdoit=${DOITPROG-}
52962c3257Smrgif test -z "$doit"; then
53962c3257Smrg  doit_exec=exec
54962c3257Smrgelse
55962c3257Smrg  doit_exec=$doit
56962c3257Smrgfi
57d983712dSmrg
58962c3257Smrg# Put in absolute file names if you don't have them in your path;
59962c3257Smrg# or use environment vars.
60962c3257Smrg
61962c3257Smrgchgrpprog=${CHGRPPROG-chgrp}
62962c3257Smrgchmodprog=${CHMODPROG-chmod}
63962c3257Smrgchownprog=${CHOWNPROG-chown}
64962c3257Smrgcmpprog=${CMPPROG-cmp}
65962c3257Smrgcpprog=${CPPROG-cp}
66962c3257Smrgmkdirprog=${MKDIRPROG-mkdir}
67962c3257Smrgmvprog=${MVPROG-mv}
68962c3257Smrgrmprog=${RMPROG-rm}
69962c3257Smrgstripprog=${STRIPPROG-strip}
70962c3257Smrg
71962c3257Smrgposix_glob='?'
72962c3257Smrginitialize_posix_glob='
73962c3257Smrg  test "$posix_glob" != "?" || {
74962c3257Smrg    if (set -f) 2>/dev/null; then
75962c3257Smrg      posix_glob=
76962c3257Smrg    else
77962c3257Smrg      posix_glob=:
78962c3257Smrg    fi
79962c3257Smrg  }
80962c3257Smrg'
81d983712dSmrg
82962c3257Smrgposix_mkdir=
83962c3257Smrg
84962c3257Smrg# Desired mode of installed file.
85962c3257Smrgmode=0755
86d983712dSmrg
87d983712dSmrgchgrpcmd=
88962c3257Smrgchmodcmd=$chmodprog
89962c3257Smrgchowncmd=
90962c3257Smrgmvcmd=$mvprog
91d983712dSmrgrmcmd="$rmprog -f"
92962c3257Smrgstripcmd=
93962c3257Smrg
94d983712dSmrgsrc=
95d983712dSmrgdst=
96d983712dSmrgdir_arg=
97962c3257Smrgdst_arg=
98962c3257Smrg
99962c3257Smrgcopy_on_change=false
100d983712dSmrgno_target_directory=
101d983712dSmrg
102962c3257Smrgusage="\
103962c3257SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104d983712dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105d983712dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106d983712dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107d983712dSmrg
108d983712dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109d983712dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110d983712dSmrgIn the 4th, create DIRECTORIES.
111d983712dSmrg
112d983712dSmrgOptions:
113962c3257Smrg     --help     display this help and exit.
114962c3257Smrg     --version  display version info and exit.
115962c3257Smrg
116962c3257Smrg  -c            (ignored)
117962c3257Smrg  -C            install only if different (preserve the last data modification time)
118962c3257Smrg  -d            create directories instead of installing files.
119962c3257Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120962c3257Smrg  -m MODE       $chmodprog installed files to MODE.
121962c3257Smrg  -o USER       $chownprog installed files to USER.
122962c3257Smrg  -s            $stripprog installed files.
123962c3257Smrg  -t DIRECTORY  install into DIRECTORY.
124962c3257Smrg  -T            report an error if DSTFILE is a directory.
125d983712dSmrg
126d983712dSmrgEnvironment variables override the default commands:
127962c3257Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128962c3257Smrg  RMPROG STRIPPROG
129d983712dSmrg"
130d983712dSmrg
131962c3257Smrgwhile test $# -ne 0; do
132d983712dSmrg  case $1 in
133962c3257Smrg    -c) ;;
134962c3257Smrg
135962c3257Smrg    -C) copy_on_change=true;;
136d983712dSmrg
137962c3257Smrg    -d) dir_arg=true;;
138d983712dSmrg
139d983712dSmrg    -g) chgrpcmd="$chgrpprog $2"
140962c3257Smrg	shift;;
141d983712dSmrg
142d983712dSmrg    --help) echo "$usage"; exit $?;;
143d983712dSmrg
144962c3257Smrg    -m) mode=$2
145962c3257Smrg	case $mode in
146962c3257Smrg	  *' '* | *'	'* | *'
147962c3257Smrg'*	  | *'*'* | *'?'* | *'['*)
148962c3257Smrg	    echo "$0: invalid mode: $mode" >&2
149962c3257Smrg	    exit 1;;
150962c3257Smrg	esac
151962c3257Smrg	shift;;
152d983712dSmrg
153d983712dSmrg    -o) chowncmd="$chownprog $2"
154962c3257Smrg	shift;;
155d983712dSmrg
156962c3257Smrg    -s) stripcmd=$stripprog;;
157d983712dSmrg
158962c3257Smrg    -t) dst_arg=$2
1594b9470b1Smrg	# Protect names problematic for `test' and other utilities.
1604b9470b1Smrg	case $dst_arg in
1614b9470b1Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1624b9470b1Smrg	esac
163962c3257Smrg	shift;;
164d983712dSmrg
165962c3257Smrg    -T) no_target_directory=true;;
166d983712dSmrg
167d983712dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
168d983712dSmrg
169962c3257Smrg    --)	shift
170d983712dSmrg	break;;
171962c3257Smrg
172962c3257Smrg    -*)	echo "$0: invalid option: $1" >&2
173962c3257Smrg	exit 1;;
174962c3257Smrg
175962c3257Smrg    *)  break;;
176d983712dSmrg  esac
177962c3257Smrg  shift
178d983712dSmrgdone
179d983712dSmrg
180962c3257Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181962c3257Smrg  # When -d is used, all remaining arguments are directories to create.
182962c3257Smrg  # When -t is used, the destination is already specified.
183962c3257Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184962c3257Smrg  for arg
185962c3257Smrg  do
186962c3257Smrg    if test -n "$dst_arg"; then
187962c3257Smrg      # $@ is not empty: it contains at least $arg.
188962c3257Smrg      set fnord "$@" "$dst_arg"
189962c3257Smrg      shift # fnord
190962c3257Smrg    fi
191962c3257Smrg    shift # arg
192962c3257Smrg    dst_arg=$arg
1934b9470b1Smrg    # Protect names problematic for `test' and other utilities.
1944b9470b1Smrg    case $dst_arg in
1954b9470b1Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1964b9470b1Smrg    esac
197962c3257Smrg  done
198962c3257Smrgfi
199962c3257Smrg
200962c3257Smrgif test $# -eq 0; then
201d983712dSmrg  if test -z "$dir_arg"; then
202d983712dSmrg    echo "$0: no input file specified." >&2
203d983712dSmrg    exit 1
204d983712dSmrg  fi
205d983712dSmrg  # It's OK to call `install-sh -d' without argument.
206d983712dSmrg  # This can happen when creating conditional directories.
207d983712dSmrg  exit 0
208d983712dSmrgfi
209d983712dSmrg
210962c3257Smrgif test -z "$dir_arg"; then
2114b9470b1Smrg  do_exit='(exit $ret); exit $ret'
2124b9470b1Smrg  trap "ret=129; $do_exit" 1
2134b9470b1Smrg  trap "ret=130; $do_exit" 2
2144b9470b1Smrg  trap "ret=141; $do_exit" 13
2154b9470b1Smrg  trap "ret=143; $do_exit" 15
216962c3257Smrg
217962c3257Smrg  # Set umask so as not to create temps with too-generous modes.
218962c3257Smrg  # However, 'strip' requires both read and write access to temps.
219962c3257Smrg  case $mode in
220962c3257Smrg    # Optimize common cases.
221962c3257Smrg    *644) cp_umask=133;;
222962c3257Smrg    *755) cp_umask=22;;
223962c3257Smrg
224962c3257Smrg    *[0-7])
225962c3257Smrg      if test -z "$stripcmd"; then
226962c3257Smrg	u_plus_rw=
227962c3257Smrg      else
228962c3257Smrg	u_plus_rw='% 200'
229962c3257Smrg      fi
230962c3257Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231962c3257Smrg    *)
232962c3257Smrg      if test -z "$stripcmd"; then
233962c3257Smrg	u_plus_rw=
234962c3257Smrg      else
235962c3257Smrg	u_plus_rw=,u+rw
236962c3257Smrg      fi
237962c3257Smrg      cp_umask=$mode$u_plus_rw;;
238962c3257Smrg  esac
239962c3257Smrgfi
240962c3257Smrg
241d983712dSmrgfor src
242d983712dSmrgdo
2434b9470b1Smrg  # Protect names problematic for `test' and other utilities.
244d983712dSmrg  case $src in
2454b9470b1Smrg    -* | [=\(\)!]) src=./$src;;
246d983712dSmrg  esac
247d983712dSmrg
248d983712dSmrg  if test -n "$dir_arg"; then
249d983712dSmrg    dst=$src
250962c3257Smrg    dstdir=$dst
251962c3257Smrg    test -d "$dstdir"
252962c3257Smrg    dstdir_status=$?
253d983712dSmrg  else
254962c3257Smrg
255d983712dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256d983712dSmrg    # might cause directories to be created, which would be especially bad
257d983712dSmrg    # if $src (and thus $dsttmp) contains '*'.
258d983712dSmrg    if test ! -f "$src" && test ! -d "$src"; then
259d983712dSmrg      echo "$0: $src does not exist." >&2
260d983712dSmrg      exit 1
261d983712dSmrg    fi
262d983712dSmrg
263962c3257Smrg    if test -z "$dst_arg"; then
264d983712dSmrg      echo "$0: no destination specified." >&2
265d983712dSmrg      exit 1
266d983712dSmrg    fi
267962c3257Smrg    dst=$dst_arg
268d983712dSmrg
269d983712dSmrg    # If destination is a directory, append the input filename; won't work
270d983712dSmrg    # if double slashes aren't ignored.
271d983712dSmrg    if test -d "$dst"; then
272d983712dSmrg      if test -n "$no_target_directory"; then
273962c3257Smrg	echo "$0: $dst_arg: Is a directory" >&2
274d983712dSmrg	exit 1
275d983712dSmrg      fi
276962c3257Smrg      dstdir=$dst
277962c3257Smrg      dst=$dstdir/`basename "$src"`
278962c3257Smrg      dstdir_status=0
279962c3257Smrg    else
280962c3257Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281962c3257Smrg      dstdir=`
282962c3257Smrg	(dirname "$dst") 2>/dev/null ||
283962c3257Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284962c3257Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
285962c3257Smrg	     X"$dst" : 'X\(//\)$' \| \
286962c3257Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287962c3257Smrg	echo X"$dst" |
288962c3257Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289962c3257Smrg		   s//\1/
290962c3257Smrg		   q
291962c3257Smrg		 }
292962c3257Smrg		 /^X\(\/\/\)[^/].*/{
293962c3257Smrg		   s//\1/
294962c3257Smrg		   q
295962c3257Smrg		 }
296962c3257Smrg		 /^X\(\/\/\)$/{
297962c3257Smrg		   s//\1/
298962c3257Smrg		   q
299962c3257Smrg		 }
300962c3257Smrg		 /^X\(\/\).*/{
301962c3257Smrg		   s//\1/
302962c3257Smrg		   q
303962c3257Smrg		 }
304962c3257Smrg		 s/.*/./; q'
305962c3257Smrg      `
306962c3257Smrg
307962c3257Smrg      test -d "$dstdir"
308962c3257Smrg      dstdir_status=$?
309d983712dSmrg    fi
310d983712dSmrg  fi
311d983712dSmrg
312962c3257Smrg  obsolete_mkdir_used=false
313962c3257Smrg
314962c3257Smrg  if test $dstdir_status != 0; then
315962c3257Smrg    case $posix_mkdir in
316962c3257Smrg      '')
317962c3257Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
318962c3257Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
319962c3257Smrg	umask=`umask`
320962c3257Smrg	case $stripcmd.$umask in
321962c3257Smrg	  # Optimize common cases.
322962c3257Smrg	  *[2367][2367]) mkdir_umask=$umask;;
323962c3257Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324962c3257Smrg
325962c3257Smrg	  *[0-7])
326962c3257Smrg	    mkdir_umask=`expr $umask + 22 \
327962c3257Smrg	      - $umask % 100 % 40 + $umask % 20 \
328962c3257Smrg	      - $umask % 10 % 4 + $umask % 2
329962c3257Smrg	    `;;
330962c3257Smrg	  *) mkdir_umask=$umask,go-w;;
331962c3257Smrg	esac
332962c3257Smrg
333962c3257Smrg	# With -d, create the new directory with the user-specified mode.
334962c3257Smrg	# Otherwise, rely on $mkdir_umask.
335962c3257Smrg	if test -n "$dir_arg"; then
336962c3257Smrg	  mkdir_mode=-m$mode
337962c3257Smrg	else
338962c3257Smrg	  mkdir_mode=
339962c3257Smrg	fi
340962c3257Smrg
341962c3257Smrg	posix_mkdir=false
342962c3257Smrg	case $umask in
343962c3257Smrg	  *[123567][0-7][0-7])
344962c3257Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345962c3257Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346962c3257Smrg	    ;;
347962c3257Smrg	  *)
348962c3257Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349962c3257Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350962c3257Smrg
351962c3257Smrg	    if (umask $mkdir_umask &&
352962c3257Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353962c3257Smrg	    then
354962c3257Smrg	      if test -z "$dir_arg" || {
355962c3257Smrg		   # Check for POSIX incompatibilities with -m.
356962c3257Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357962c3257Smrg		   # other-writeable bit of parent directory when it shouldn't.
358962c3257Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359962c3257Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360962c3257Smrg		   case $ls_ld_tmpdir in
361962c3257Smrg		     d????-?r-*) different_mode=700;;
362962c3257Smrg		     d????-?--*) different_mode=755;;
363962c3257Smrg		     *) false;;
364962c3257Smrg		   esac &&
365962c3257Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366962c3257Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367962c3257Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368962c3257Smrg		   }
369962c3257Smrg		 }
370962c3257Smrg	      then posix_mkdir=:
371962c3257Smrg	      fi
372962c3257Smrg	      rmdir "$tmpdir/d" "$tmpdir"
373962c3257Smrg	    else
374962c3257Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
375962c3257Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376962c3257Smrg	    fi
377962c3257Smrg	    trap '' 0;;
378962c3257Smrg	esac;;
379962c3257Smrg    esac
380d983712dSmrg
381962c3257Smrg    if
382962c3257Smrg      $posix_mkdir && (
383962c3257Smrg	umask $mkdir_umask &&
384962c3257Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385962c3257Smrg      )
386962c3257Smrg    then :
387962c3257Smrg    else
388d983712dSmrg
389962c3257Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390962c3257Smrg      # or it failed possibly due to a race condition.  Create the
391962c3257Smrg      # directory the slow way, step by step, checking for races as we go.
392d983712dSmrg
393962c3257Smrg      case $dstdir in
394962c3257Smrg	/*) prefix='/';;
3954b9470b1Smrg	[-=\(\)!]*) prefix='./';;
396962c3257Smrg	*)  prefix='';;
397962c3257Smrg      esac
398d983712dSmrg
399962c3257Smrg      eval "$initialize_posix_glob"
400d983712dSmrg
401962c3257Smrg      oIFS=$IFS
402962c3257Smrg      IFS=/
403962c3257Smrg      $posix_glob set -f
404962c3257Smrg      set fnord $dstdir
405d983712dSmrg      shift
406962c3257Smrg      $posix_glob set +f
407962c3257Smrg      IFS=$oIFS
408962c3257Smrg
409962c3257Smrg      prefixes=
410962c3257Smrg
411962c3257Smrg      for d
412962c3257Smrg      do
4134b9470b1Smrg	test X"$d" = X && continue
414962c3257Smrg
415962c3257Smrg	prefix=$prefix$d
416962c3257Smrg	if test -d "$prefix"; then
417962c3257Smrg	  prefixes=
418962c3257Smrg	else
419962c3257Smrg	  if $posix_mkdir; then
420962c3257Smrg	    (umask=$mkdir_umask &&
421962c3257Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422962c3257Smrg	    # Don't fail if two instances are running concurrently.
423962c3257Smrg	    test -d "$prefix" || exit 1
424962c3257Smrg	  else
425962c3257Smrg	    case $prefix in
426962c3257Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427962c3257Smrg	      *) qprefix=$prefix;;
428962c3257Smrg	    esac
429962c3257Smrg	    prefixes="$prefixes '$qprefix'"
430962c3257Smrg	  fi
431962c3257Smrg	fi
432962c3257Smrg	prefix=$prefix/
433962c3257Smrg      done
434962c3257Smrg
435962c3257Smrg      if test -n "$prefixes"; then
436962c3257Smrg	# Don't fail if two instances are running concurrently.
437962c3257Smrg	(umask $mkdir_umask &&
438962c3257Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439962c3257Smrg	  test -d "$dstdir" || exit 1
440962c3257Smrg	obsolete_mkdir_used=true
441d983712dSmrg      fi
442962c3257Smrg    fi
443d983712dSmrg  fi
444d983712dSmrg
445d983712dSmrg  if test -n "$dir_arg"; then
446962c3257Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447962c3257Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448962c3257Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449962c3257Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450d983712dSmrg  else
451d983712dSmrg
452d983712dSmrg    # Make a couple of temp file names in the proper directory.
453d983712dSmrg    dsttmp=$dstdir/_inst.$$_
454d983712dSmrg    rmtmp=$dstdir/_rm.$$_
455d983712dSmrg
456d983712dSmrg    # Trap to clean up those temp files at exit.
457d983712dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458d983712dSmrg
459d983712dSmrg    # Copy the file name to the temp name.
460962c3257Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461d983712dSmrg
462d983712dSmrg    # and set any options; do chmod last to preserve setuid bits.
463d983712dSmrg    #
464d983712dSmrg    # If any of these fail, we abort the whole thing.  If we want to
465d983712dSmrg    # ignore errors from any of these, just make sure not to ignore
466d983712dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467d983712dSmrg    #
468962c3257Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469962c3257Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470962c3257Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471962c3257Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472962c3257Smrg
473962c3257Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474962c3257Smrg    if $copy_on_change &&
475962c3257Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476962c3257Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477962c3257Smrg
478962c3257Smrg       eval "$initialize_posix_glob" &&
479962c3257Smrg       $posix_glob set -f &&
480962c3257Smrg       set X $old && old=:$2:$4:$5:$6 &&
481962c3257Smrg       set X $new && new=:$2:$4:$5:$6 &&
482962c3257Smrg       $posix_glob set +f &&
483962c3257Smrg
484962c3257Smrg       test "$old" = "$new" &&
485962c3257Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486962c3257Smrg    then
487962c3257Smrg      rm -f "$dsttmp"
488962c3257Smrg    else
489962c3257Smrg      # Rename the file to the real destination.
490962c3257Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491962c3257Smrg
492962c3257Smrg      # The rename failed, perhaps because mv can't rename something else
493962c3257Smrg      # to itself, or perhaps because mv is so ancient that it does not
494962c3257Smrg      # support -f.
495962c3257Smrg      {
496962c3257Smrg	# Now remove or move aside any old file at destination location.
497962c3257Smrg	# We try this two ways since rm can't unlink itself on some
498962c3257Smrg	# systems and the destination file might be busy for other
499962c3257Smrg	# reasons.  In this case, the final cleanup might fail but the new
500962c3257Smrg	# file should still install successfully.
501962c3257Smrg	{
502962c3257Smrg	  test ! -f "$dst" ||
503962c3257Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504962c3257Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505962c3257Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506962c3257Smrg	  } ||
507962c3257Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
508962c3257Smrg	    (exit 1); exit 1
509962c3257Smrg	  }
510962c3257Smrg	} &&
511962c3257Smrg
512962c3257Smrg	# Now rename the file to the real destination.
513962c3257Smrg	$doit $mvcmd "$dsttmp" "$dst"
514962c3257Smrg      }
515962c3257Smrg    fi || exit 1
516962c3257Smrg
517962c3257Smrg    trap '' 0
518962c3257Smrg  fi
519d983712dSmrgdone
520d983712dSmrg
521d983712dSmrg# Local variables:
522d983712dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523d983712dSmrg# time-stamp-start: "scriptversion="
524d983712dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5254b9470b1Smrg# time-stamp-time-zone: "UTC"
5264b9470b1Smrg# time-stamp-end: "; # UTC"
527d983712dSmrg# End:
528