17706df26Smrg#!/bin/sh
27706df26Smrg# install - install a program, script, or datafile
37706df26Smrg
4b92e825aSmrgscriptversion=2011-01-19.21; # UTC
57706df26Smrg
67706df26Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77706df26Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87706df26Smrg# following copyright and license.
97706df26Smrg#
107706df26Smrg# Copyright (C) 1994 X Consortium
117706df26Smrg#
127706df26Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137706df26Smrg# of this software and associated documentation files (the "Software"), to
147706df26Smrg# deal in the Software without restriction, including without limitation the
157706df26Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167706df26Smrg# sell copies of the Software, and to permit persons to whom the Software is
177706df26Smrg# furnished to do so, subject to the following conditions:
187706df26Smrg#
197706df26Smrg# The above copyright notice and this permission notice shall be included in
207706df26Smrg# all copies or substantial portions of the Software.
217706df26Smrg#
227706df26Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237706df26Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247706df26Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257706df26Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267706df26Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277706df26Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287706df26Smrg#
297706df26Smrg# Except as contained in this notice, the name of the X Consortium shall not
307706df26Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317706df26Smrg# ings in this Software without prior written authorization from the X Consor-
327706df26Smrg# tium.
337706df26Smrg#
347706df26Smrg#
357706df26Smrg# FSF changes to this file are in the public domain.
367706df26Smrg#
377706df26Smrg# Calling this script install-sh is preferred over install.sh, to prevent
387706df26Smrg# `make' implicit rules from creating a file called install from it
397706df26Smrg# when there is no Makefile.
407706df26Smrg#
417706df26Smrg# This script is compatible with the BSD install script, but was written
42047944f3Smrg# from scratch.
43047944f3Smrg
44047944f3Smrgnl='
45047944f3Smrg'
46047944f3SmrgIFS=" ""	$nl"
477706df26Smrg
487706df26Smrg# set DOITPROG to echo to test this script
497706df26Smrg
507706df26Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51047944f3Smrgdoit=${DOITPROG-}
52047944f3Smrgif test -z "$doit"; then
53047944f3Smrg  doit_exec=exec
54047944f3Smrgelse
55047944f3Smrg  doit_exec=$doit
56047944f3Smrgfi
577706df26Smrg
58047944f3Smrg# Put in absolute file names if you don't have them in your path;
59047944f3Smrg# or use environment vars.
60047944f3Smrg
61047944f3Smrgchgrpprog=${CHGRPPROG-chgrp}
62047944f3Smrgchmodprog=${CHMODPROG-chmod}
63047944f3Smrgchownprog=${CHOWNPROG-chown}
64047944f3Smrgcmpprog=${CMPPROG-cmp}
65047944f3Smrgcpprog=${CPPROG-cp}
66047944f3Smrgmkdirprog=${MKDIRPROG-mkdir}
67047944f3Smrgmvprog=${MVPROG-mv}
68047944f3Smrgrmprog=${RMPROG-rm}
69047944f3Smrgstripprog=${STRIPPROG-strip}
70047944f3Smrg
71047944f3Smrgposix_glob='?'
72047944f3Smrginitialize_posix_glob='
73047944f3Smrg  test "$posix_glob" != "?" || {
74047944f3Smrg    if (set -f) 2>/dev/null; then
75047944f3Smrg      posix_glob=
76047944f3Smrg    else
77047944f3Smrg      posix_glob=:
78047944f3Smrg    fi
79047944f3Smrg  }
80047944f3Smrg'
817706df26Smrg
82047944f3Smrgposix_mkdir=
83047944f3Smrg
84047944f3Smrg# Desired mode of installed file.
85047944f3Smrgmode=0755
867706df26Smrg
877706df26Smrgchgrpcmd=
88047944f3Smrgchmodcmd=$chmodprog
89047944f3Smrgchowncmd=
90047944f3Smrgmvcmd=$mvprog
917706df26Smrgrmcmd="$rmprog -f"
92047944f3Smrgstripcmd=
93047944f3Smrg
947706df26Smrgsrc=
957706df26Smrgdst=
967706df26Smrgdir_arg=
97047944f3Smrgdst_arg=
98047944f3Smrg
99047944f3Smrgcopy_on_change=false
1007706df26Smrgno_target_directory=
1017706df26Smrg
102047944f3Smrgusage="\
103047944f3SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1047706df26Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1057706df26Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1067706df26Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1077706df26Smrg
1087706df26SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1097706df26SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1107706df26SmrgIn the 4th, create DIRECTORIES.
1117706df26Smrg
1127706df26SmrgOptions:
113047944f3Smrg     --help     display this help and exit.
114047944f3Smrg     --version  display version info and exit.
115047944f3Smrg
116047944f3Smrg  -c            (ignored)
117047944f3Smrg  -C            install only if different (preserve the last data modification time)
118047944f3Smrg  -d            create directories instead of installing files.
119047944f3Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120047944f3Smrg  -m MODE       $chmodprog installed files to MODE.
121047944f3Smrg  -o USER       $chownprog installed files to USER.
122047944f3Smrg  -s            $stripprog installed files.
123047944f3Smrg  -t DIRECTORY  install into DIRECTORY.
124047944f3Smrg  -T            report an error if DSTFILE is a directory.
1257706df26Smrg
1267706df26SmrgEnvironment variables override the default commands:
127047944f3Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128047944f3Smrg  RMPROG STRIPPROG
1297706df26Smrg"
1307706df26Smrg
131047944f3Smrgwhile test $# -ne 0; do
1327706df26Smrg  case $1 in
133047944f3Smrg    -c) ;;
134047944f3Smrg
135047944f3Smrg    -C) copy_on_change=true;;
1367706df26Smrg
137047944f3Smrg    -d) dir_arg=true;;
1387706df26Smrg
1397706df26Smrg    -g) chgrpcmd="$chgrpprog $2"
140047944f3Smrg	shift;;
1417706df26Smrg
1427706df26Smrg    --help) echo "$usage"; exit $?;;
1437706df26Smrg
144047944f3Smrg    -m) mode=$2
145047944f3Smrg	case $mode in
146047944f3Smrg	  *' '* | *'	'* | *'
147047944f3Smrg'*	  | *'*'* | *'?'* | *'['*)
148047944f3Smrg	    echo "$0: invalid mode: $mode" >&2
149047944f3Smrg	    exit 1;;
150047944f3Smrg	esac
151047944f3Smrg	shift;;
1527706df26Smrg
1537706df26Smrg    -o) chowncmd="$chownprog $2"
154047944f3Smrg	shift;;
1557706df26Smrg
156047944f3Smrg    -s) stripcmd=$stripprog;;
1577706df26Smrg
158047944f3Smrg    -t) dst_arg=$2
159b92e825aSmrg	# Protect names problematic for `test' and other utilities.
160b92e825aSmrg	case $dst_arg in
161b92e825aSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162b92e825aSmrg	esac
163047944f3Smrg	shift;;
1647706df26Smrg
165047944f3Smrg    -T) no_target_directory=true;;
1667706df26Smrg
1677706df26Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1687706df26Smrg
169047944f3Smrg    --)	shift
1707706df26Smrg	break;;
171047944f3Smrg
172047944f3Smrg    -*)	echo "$0: invalid option: $1" >&2
173047944f3Smrg	exit 1;;
174047944f3Smrg
175047944f3Smrg    *)  break;;
1767706df26Smrg  esac
177047944f3Smrg  shift
1787706df26Smrgdone
1797706df26Smrg
180047944f3Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181047944f3Smrg  # When -d is used, all remaining arguments are directories to create.
182047944f3Smrg  # When -t is used, the destination is already specified.
183047944f3Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184047944f3Smrg  for arg
185047944f3Smrg  do
186047944f3Smrg    if test -n "$dst_arg"; then
187047944f3Smrg      # $@ is not empty: it contains at least $arg.
188047944f3Smrg      set fnord "$@" "$dst_arg"
189047944f3Smrg      shift # fnord
190047944f3Smrg    fi
191047944f3Smrg    shift # arg
192047944f3Smrg    dst_arg=$arg
193b92e825aSmrg    # Protect names problematic for `test' and other utilities.
194b92e825aSmrg    case $dst_arg in
195b92e825aSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196b92e825aSmrg    esac
197047944f3Smrg  done
198047944f3Smrgfi
199047944f3Smrg
200047944f3Smrgif test $# -eq 0; then
2017706df26Smrg  if test -z "$dir_arg"; then
2027706df26Smrg    echo "$0: no input file specified." >&2
2037706df26Smrg    exit 1
2047706df26Smrg  fi
2057706df26Smrg  # It's OK to call `install-sh -d' without argument.
2067706df26Smrg  # This can happen when creating conditional directories.
2077706df26Smrg  exit 0
2087706df26Smrgfi
2097706df26Smrg
210047944f3Smrgif test -z "$dir_arg"; then
211b92e825aSmrg  do_exit='(exit $ret); exit $ret'
212b92e825aSmrg  trap "ret=129; $do_exit" 1
213b92e825aSmrg  trap "ret=130; $do_exit" 2
214b92e825aSmrg  trap "ret=141; $do_exit" 13
215b92e825aSmrg  trap "ret=143; $do_exit" 15
216047944f3Smrg
217047944f3Smrg  # Set umask so as not to create temps with too-generous modes.
218047944f3Smrg  # However, 'strip' requires both read and write access to temps.
219047944f3Smrg  case $mode in
220047944f3Smrg    # Optimize common cases.
221047944f3Smrg    *644) cp_umask=133;;
222047944f3Smrg    *755) cp_umask=22;;
223047944f3Smrg
224047944f3Smrg    *[0-7])
225047944f3Smrg      if test -z "$stripcmd"; then
226047944f3Smrg	u_plus_rw=
227047944f3Smrg      else
228047944f3Smrg	u_plus_rw='% 200'
229047944f3Smrg      fi
230047944f3Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231047944f3Smrg    *)
232047944f3Smrg      if test -z "$stripcmd"; then
233047944f3Smrg	u_plus_rw=
234047944f3Smrg      else
235047944f3Smrg	u_plus_rw=,u+rw
236047944f3Smrg      fi
237047944f3Smrg      cp_umask=$mode$u_plus_rw;;
238047944f3Smrg  esac
239047944f3Smrgfi
240047944f3Smrg
2417706df26Smrgfor src
2427706df26Smrgdo
243b92e825aSmrg  # Protect names problematic for `test' and other utilities.
2447706df26Smrg  case $src in
245b92e825aSmrg    -* | [=\(\)!]) src=./$src;;
2467706df26Smrg  esac
2477706df26Smrg
2487706df26Smrg  if test -n "$dir_arg"; then
2497706df26Smrg    dst=$src
250047944f3Smrg    dstdir=$dst
251047944f3Smrg    test -d "$dstdir"
252047944f3Smrg    dstdir_status=$?
2537706df26Smrg  else
254047944f3Smrg
2557706df26Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2567706df26Smrg    # might cause directories to be created, which would be especially bad
2577706df26Smrg    # if $src (and thus $dsttmp) contains '*'.
2587706df26Smrg    if test ! -f "$src" && test ! -d "$src"; then
2597706df26Smrg      echo "$0: $src does not exist." >&2
2607706df26Smrg      exit 1
2617706df26Smrg    fi
2627706df26Smrg
263047944f3Smrg    if test -z "$dst_arg"; then
2647706df26Smrg      echo "$0: no destination specified." >&2
2657706df26Smrg      exit 1
2667706df26Smrg    fi
267047944f3Smrg    dst=$dst_arg
2687706df26Smrg
2697706df26Smrg    # If destination is a directory, append the input filename; won't work
2707706df26Smrg    # if double slashes aren't ignored.
2717706df26Smrg    if test -d "$dst"; then
2727706df26Smrg      if test -n "$no_target_directory"; then
273047944f3Smrg	echo "$0: $dst_arg: Is a directory" >&2
2747706df26Smrg	exit 1
2757706df26Smrg      fi
276047944f3Smrg      dstdir=$dst
277047944f3Smrg      dst=$dstdir/`basename "$src"`
278047944f3Smrg      dstdir_status=0
279047944f3Smrg    else
280047944f3Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281047944f3Smrg      dstdir=`
282047944f3Smrg	(dirname "$dst") 2>/dev/null ||
283047944f3Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284047944f3Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
285047944f3Smrg	     X"$dst" : 'X\(//\)$' \| \
286047944f3Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287047944f3Smrg	echo X"$dst" |
288047944f3Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289047944f3Smrg		   s//\1/
290047944f3Smrg		   q
291047944f3Smrg		 }
292047944f3Smrg		 /^X\(\/\/\)[^/].*/{
293047944f3Smrg		   s//\1/
294047944f3Smrg		   q
295047944f3Smrg		 }
296047944f3Smrg		 /^X\(\/\/\)$/{
297047944f3Smrg		   s//\1/
298047944f3Smrg		   q
299047944f3Smrg		 }
300047944f3Smrg		 /^X\(\/\).*/{
301047944f3Smrg		   s//\1/
302047944f3Smrg		   q
303047944f3Smrg		 }
304047944f3Smrg		 s/.*/./; q'
305047944f3Smrg      `
306047944f3Smrg
307047944f3Smrg      test -d "$dstdir"
308047944f3Smrg      dstdir_status=$?
3097706df26Smrg    fi
3107706df26Smrg  fi
3117706df26Smrg
312047944f3Smrg  obsolete_mkdir_used=false
313047944f3Smrg
314047944f3Smrg  if test $dstdir_status != 0; then
315047944f3Smrg    case $posix_mkdir in
316047944f3Smrg      '')
317047944f3Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
318047944f3Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
319047944f3Smrg	umask=`umask`
320047944f3Smrg	case $stripcmd.$umask in
321047944f3Smrg	  # Optimize common cases.
322047944f3Smrg	  *[2367][2367]) mkdir_umask=$umask;;
323047944f3Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324047944f3Smrg
325047944f3Smrg	  *[0-7])
326047944f3Smrg	    mkdir_umask=`expr $umask + 22 \
327047944f3Smrg	      - $umask % 100 % 40 + $umask % 20 \
328047944f3Smrg	      - $umask % 10 % 4 + $umask % 2
329047944f3Smrg	    `;;
330047944f3Smrg	  *) mkdir_umask=$umask,go-w;;
331047944f3Smrg	esac
332047944f3Smrg
333047944f3Smrg	# With -d, create the new directory with the user-specified mode.
334047944f3Smrg	# Otherwise, rely on $mkdir_umask.
335047944f3Smrg	if test -n "$dir_arg"; then
336047944f3Smrg	  mkdir_mode=-m$mode
337047944f3Smrg	else
338047944f3Smrg	  mkdir_mode=
339047944f3Smrg	fi
340047944f3Smrg
341047944f3Smrg	posix_mkdir=false
342047944f3Smrg	case $umask in
343047944f3Smrg	  *[123567][0-7][0-7])
344047944f3Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345047944f3Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346047944f3Smrg	    ;;
347047944f3Smrg	  *)
348047944f3Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349047944f3Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350047944f3Smrg
351047944f3Smrg	    if (umask $mkdir_umask &&
352047944f3Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353047944f3Smrg	    then
354047944f3Smrg	      if test -z "$dir_arg" || {
355047944f3Smrg		   # Check for POSIX incompatibilities with -m.
356047944f3Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357047944f3Smrg		   # other-writeable bit of parent directory when it shouldn't.
358047944f3Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359047944f3Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360047944f3Smrg		   case $ls_ld_tmpdir in
361047944f3Smrg		     d????-?r-*) different_mode=700;;
362047944f3Smrg		     d????-?--*) different_mode=755;;
363047944f3Smrg		     *) false;;
364047944f3Smrg		   esac &&
365047944f3Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366047944f3Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367047944f3Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368047944f3Smrg		   }
369047944f3Smrg		 }
370047944f3Smrg	      then posix_mkdir=:
371047944f3Smrg	      fi
372047944f3Smrg	      rmdir "$tmpdir/d" "$tmpdir"
373047944f3Smrg	    else
374047944f3Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
375047944f3Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376047944f3Smrg	    fi
377047944f3Smrg	    trap '' 0;;
378047944f3Smrg	esac;;
379047944f3Smrg    esac
3807706df26Smrg
381047944f3Smrg    if
382047944f3Smrg      $posix_mkdir && (
383047944f3Smrg	umask $mkdir_umask &&
384047944f3Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385047944f3Smrg      )
386047944f3Smrg    then :
387047944f3Smrg    else
3887706df26Smrg
389047944f3Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390047944f3Smrg      # or it failed possibly due to a race condition.  Create the
391047944f3Smrg      # directory the slow way, step by step, checking for races as we go.
3927706df26Smrg
393047944f3Smrg      case $dstdir in
394047944f3Smrg	/*) prefix='/';;
395b92e825aSmrg	[-=\(\)!]*) prefix='./';;
396047944f3Smrg	*)  prefix='';;
397047944f3Smrg      esac
3987706df26Smrg
399047944f3Smrg      eval "$initialize_posix_glob"
4007706df26Smrg
401047944f3Smrg      oIFS=$IFS
402047944f3Smrg      IFS=/
403047944f3Smrg      $posix_glob set -f
404047944f3Smrg      set fnord $dstdir
4057706df26Smrg      shift
406047944f3Smrg      $posix_glob set +f
407047944f3Smrg      IFS=$oIFS
408047944f3Smrg
409047944f3Smrg      prefixes=
410047944f3Smrg
411047944f3Smrg      for d
412047944f3Smrg      do
413b92e825aSmrg	test X"$d" = X && continue
414047944f3Smrg
415047944f3Smrg	prefix=$prefix$d
416047944f3Smrg	if test -d "$prefix"; then
417047944f3Smrg	  prefixes=
418047944f3Smrg	else
419047944f3Smrg	  if $posix_mkdir; then
420047944f3Smrg	    (umask=$mkdir_umask &&
421047944f3Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422047944f3Smrg	    # Don't fail if two instances are running concurrently.
423047944f3Smrg	    test -d "$prefix" || exit 1
424047944f3Smrg	  else
425047944f3Smrg	    case $prefix in
426047944f3Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427047944f3Smrg	      *) qprefix=$prefix;;
428047944f3Smrg	    esac
429047944f3Smrg	    prefixes="$prefixes '$qprefix'"
430047944f3Smrg	  fi
431047944f3Smrg	fi
432047944f3Smrg	prefix=$prefix/
433047944f3Smrg      done
434047944f3Smrg
435047944f3Smrg      if test -n "$prefixes"; then
436047944f3Smrg	# Don't fail if two instances are running concurrently.
437047944f3Smrg	(umask $mkdir_umask &&
438047944f3Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439047944f3Smrg	  test -d "$dstdir" || exit 1
440047944f3Smrg	obsolete_mkdir_used=true
4417706df26Smrg      fi
442047944f3Smrg    fi
4437706df26Smrg  fi
4447706df26Smrg
4457706df26Smrg  if test -n "$dir_arg"; then
446047944f3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447047944f3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448047944f3Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449047944f3Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4507706df26Smrg  else
4517706df26Smrg
4527706df26Smrg    # Make a couple of temp file names in the proper directory.
4537706df26Smrg    dsttmp=$dstdir/_inst.$$_
4547706df26Smrg    rmtmp=$dstdir/_rm.$$_
4557706df26Smrg
4567706df26Smrg    # Trap to clean up those temp files at exit.
4577706df26Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4587706df26Smrg
4597706df26Smrg    # Copy the file name to the temp name.
460047944f3Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4617706df26Smrg
4627706df26Smrg    # and set any options; do chmod last to preserve setuid bits.
4637706df26Smrg    #
4647706df26Smrg    # If any of these fail, we abort the whole thing.  If we want to
4657706df26Smrg    # ignore errors from any of these, just make sure not to ignore
4667706df26Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4677706df26Smrg    #
468047944f3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469047944f3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470047944f3Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471047944f3Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472047944f3Smrg
473047944f3Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474047944f3Smrg    if $copy_on_change &&
475047944f3Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476047944f3Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477047944f3Smrg
478047944f3Smrg       eval "$initialize_posix_glob" &&
479047944f3Smrg       $posix_glob set -f &&
480047944f3Smrg       set X $old && old=:$2:$4:$5:$6 &&
481047944f3Smrg       set X $new && new=:$2:$4:$5:$6 &&
482047944f3Smrg       $posix_glob set +f &&
483047944f3Smrg
484047944f3Smrg       test "$old" = "$new" &&
485047944f3Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486047944f3Smrg    then
487047944f3Smrg      rm -f "$dsttmp"
488047944f3Smrg    else
489047944f3Smrg      # Rename the file to the real destination.
490047944f3Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491047944f3Smrg
492047944f3Smrg      # The rename failed, perhaps because mv can't rename something else
493047944f3Smrg      # to itself, or perhaps because mv is so ancient that it does not
494047944f3Smrg      # support -f.
495047944f3Smrg      {
496047944f3Smrg	# Now remove or move aside any old file at destination location.
497047944f3Smrg	# We try this two ways since rm can't unlink itself on some
498047944f3Smrg	# systems and the destination file might be busy for other
499047944f3Smrg	# reasons.  In this case, the final cleanup might fail but the new
500047944f3Smrg	# file should still install successfully.
501047944f3Smrg	{
502047944f3Smrg	  test ! -f "$dst" ||
503047944f3Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504047944f3Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505047944f3Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506047944f3Smrg	  } ||
507047944f3Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
508047944f3Smrg	    (exit 1); exit 1
509047944f3Smrg	  }
510047944f3Smrg	} &&
511047944f3Smrg
512047944f3Smrg	# Now rename the file to the real destination.
513047944f3Smrg	$doit $mvcmd "$dsttmp" "$dst"
514047944f3Smrg      }
515047944f3Smrg    fi || exit 1
516047944f3Smrg
517047944f3Smrg    trap '' 0
518047944f3Smrg  fi
5197706df26Smrgdone
5207706df26Smrg
5217706df26Smrg# Local variables:
5227706df26Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5237706df26Smrg# time-stamp-start: "scriptversion="
5247706df26Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5257ae0a258Smrg# time-stamp-time-zone: "UTC"
5267ae0a258Smrg# time-stamp-end: "; # UTC"
5277706df26Smrg# End:
528