install-sh revision 2ae83157
1444c061aSmrg#!/bin/sh
2444c061aSmrg# install - install a program, script, or datafile
3444c061aSmrg
42ae83157Smrgscriptversion=2009-04-28.21; # UTC
5444c061aSmrg
6444c061aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7444c061aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8444c061aSmrg# following copyright and license.
9444c061aSmrg#
10444c061aSmrg# Copyright (C) 1994 X Consortium
11444c061aSmrg#
12444c061aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13444c061aSmrg# of this software and associated documentation files (the "Software"), to
14444c061aSmrg# deal in the Software without restriction, including without limitation the
15444c061aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16444c061aSmrg# sell copies of the Software, and to permit persons to whom the Software is
17444c061aSmrg# furnished to do so, subject to the following conditions:
18444c061aSmrg#
19444c061aSmrg# The above copyright notice and this permission notice shall be included in
20444c061aSmrg# all copies or substantial portions of the Software.
21444c061aSmrg#
22444c061aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23444c061aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24444c061aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25444c061aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26444c061aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27444c061aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28444c061aSmrg#
29444c061aSmrg# Except as contained in this notice, the name of the X Consortium shall not
30444c061aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31444c061aSmrg# ings in this Software without prior written authorization from the X Consor-
32444c061aSmrg# tium.
33444c061aSmrg#
34444c061aSmrg#
35444c061aSmrg# FSF changes to this file are in the public domain.
36444c061aSmrg#
37444c061aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38444c061aSmrg# `make' implicit rules from creating a file called install from it
39444c061aSmrg# when there is no Makefile.
40444c061aSmrg#
41444c061aSmrg# This script is compatible with the BSD install script, but was written
422265a131Smrg# from scratch.
432265a131Smrg
442265a131Smrgnl='
452265a131Smrg'
462265a131SmrgIFS=" ""	$nl"
47444c061aSmrg
48444c061aSmrg# set DOITPROG to echo to test this script
49444c061aSmrg
50444c061aSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
512265a131Smrgdoit=${DOITPROG-}
522265a131Smrgif test -z "$doit"; then
532265a131Smrg  doit_exec=exec
542265a131Smrgelse
552265a131Smrg  doit_exec=$doit
562265a131Smrgfi
57444c061aSmrg
582265a131Smrg# Put in absolute file names if you don't have them in your path;
592265a131Smrg# or use environment vars.
602265a131Smrg
612265a131Smrgchgrpprog=${CHGRPPROG-chgrp}
622265a131Smrgchmodprog=${CHMODPROG-chmod}
632265a131Smrgchownprog=${CHOWNPROG-chown}
642265a131Smrgcmpprog=${CMPPROG-cmp}
652265a131Smrgcpprog=${CPPROG-cp}
662265a131Smrgmkdirprog=${MKDIRPROG-mkdir}
672265a131Smrgmvprog=${MVPROG-mv}
682265a131Smrgrmprog=${RMPROG-rm}
692265a131Smrgstripprog=${STRIPPROG-strip}
702265a131Smrg
712265a131Smrgposix_glob='?'
722265a131Smrginitialize_posix_glob='
732265a131Smrg  test "$posix_glob" != "?" || {
742265a131Smrg    if (set -f) 2>/dev/null; then
752265a131Smrg      posix_glob=
762265a131Smrg    else
772265a131Smrg      posix_glob=:
782265a131Smrg    fi
792265a131Smrg  }
802265a131Smrg'
81444c061aSmrg
822265a131Smrgposix_mkdir=
832265a131Smrg
842265a131Smrg# Desired mode of installed file.
852265a131Smrgmode=0755
86444c061aSmrg
87444c061aSmrgchgrpcmd=
882265a131Smrgchmodcmd=$chmodprog
892265a131Smrgchowncmd=
902265a131Smrgmvcmd=$mvprog
91444c061aSmrgrmcmd="$rmprog -f"
922265a131Smrgstripcmd=
932265a131Smrg
94444c061aSmrgsrc=
95444c061aSmrgdst=
96444c061aSmrgdir_arg=
972265a131Smrgdst_arg=
982265a131Smrg
992265a131Smrgcopy_on_change=false
100444c061aSmrgno_target_directory=
101444c061aSmrg
1022265a131Smrgusage="\
1032265a131SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104444c061aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105444c061aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106444c061aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107444c061aSmrg
108444c061aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109444c061aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110444c061aSmrgIn the 4th, create DIRECTORIES.
111444c061aSmrg
112444c061aSmrgOptions:
1132265a131Smrg     --help     display this help and exit.
1142265a131Smrg     --version  display version info and exit.
1152265a131Smrg
1162265a131Smrg  -c            (ignored)
1172265a131Smrg  -C            install only if different (preserve the last data modification time)
1182265a131Smrg  -d            create directories instead of installing files.
1192265a131Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1202265a131Smrg  -m MODE       $chmodprog installed files to MODE.
1212265a131Smrg  -o USER       $chownprog installed files to USER.
1222265a131Smrg  -s            $stripprog installed files.
1232265a131Smrg  -t DIRECTORY  install into DIRECTORY.
1242265a131Smrg  -T            report an error if DSTFILE is a directory.
125444c061aSmrg
126444c061aSmrgEnvironment variables override the default commands:
1272265a131Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1282265a131Smrg  RMPROG STRIPPROG
129444c061aSmrg"
130444c061aSmrg
1312265a131Smrgwhile test $# -ne 0; do
132444c061aSmrg  case $1 in
1332265a131Smrg    -c) ;;
1342265a131Smrg
1352265a131Smrg    -C) copy_on_change=true;;
136444c061aSmrg
1372265a131Smrg    -d) dir_arg=true;;
138444c061aSmrg
139444c061aSmrg    -g) chgrpcmd="$chgrpprog $2"
1402265a131Smrg	shift;;
141444c061aSmrg
142444c061aSmrg    --help) echo "$usage"; exit $?;;
143444c061aSmrg
1442265a131Smrg    -m) mode=$2
1452265a131Smrg	case $mode in
1462265a131Smrg	  *' '* | *'	'* | *'
1472265a131Smrg'*	  | *'*'* | *'?'* | *'['*)
1482265a131Smrg	    echo "$0: invalid mode: $mode" >&2
1492265a131Smrg	    exit 1;;
1502265a131Smrg	esac
1512265a131Smrg	shift;;
152444c061aSmrg
153444c061aSmrg    -o) chowncmd="$chownprog $2"
1542265a131Smrg	shift;;
155444c061aSmrg
1562265a131Smrg    -s) stripcmd=$stripprog;;
157444c061aSmrg
1582265a131Smrg    -t) dst_arg=$2
1592265a131Smrg	shift;;
160444c061aSmrg
1612265a131Smrg    -T) no_target_directory=true;;
162444c061aSmrg
163444c061aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164444c061aSmrg
1652265a131Smrg    --)	shift
166444c061aSmrg	break;;
1672265a131Smrg
1682265a131Smrg    -*)	echo "$0: invalid option: $1" >&2
1692265a131Smrg	exit 1;;
1702265a131Smrg
1712265a131Smrg    *)  break;;
172444c061aSmrg  esac
1732265a131Smrg  shift
174444c061aSmrgdone
175444c061aSmrg
1762265a131Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1772265a131Smrg  # When -d is used, all remaining arguments are directories to create.
1782265a131Smrg  # When -t is used, the destination is already specified.
1792265a131Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1802265a131Smrg  for arg
1812265a131Smrg  do
1822265a131Smrg    if test -n "$dst_arg"; then
1832265a131Smrg      # $@ is not empty: it contains at least $arg.
1842265a131Smrg      set fnord "$@" "$dst_arg"
1852265a131Smrg      shift # fnord
1862265a131Smrg    fi
1872265a131Smrg    shift # arg
1882265a131Smrg    dst_arg=$arg
1892265a131Smrg  done
1902265a131Smrgfi
1912265a131Smrg
1922265a131Smrgif test $# -eq 0; then
193444c061aSmrg  if test -z "$dir_arg"; then
194444c061aSmrg    echo "$0: no input file specified." >&2
195444c061aSmrg    exit 1
196444c061aSmrg  fi
197444c061aSmrg  # It's OK to call `install-sh -d' without argument.
198444c061aSmrg  # This can happen when creating conditional directories.
199444c061aSmrg  exit 0
200444c061aSmrgfi
201444c061aSmrg
2022265a131Smrgif test -z "$dir_arg"; then
2032265a131Smrg  trap '(exit $?); exit' 1 2 13 15
2042265a131Smrg
2052265a131Smrg  # Set umask so as not to create temps with too-generous modes.
2062265a131Smrg  # However, 'strip' requires both read and write access to temps.
2072265a131Smrg  case $mode in
2082265a131Smrg    # Optimize common cases.
2092265a131Smrg    *644) cp_umask=133;;
2102265a131Smrg    *755) cp_umask=22;;
2112265a131Smrg
2122265a131Smrg    *[0-7])
2132265a131Smrg      if test -z "$stripcmd"; then
2142265a131Smrg	u_plus_rw=
2152265a131Smrg      else
2162265a131Smrg	u_plus_rw='% 200'
2172265a131Smrg      fi
2182265a131Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2192265a131Smrg    *)
2202265a131Smrg      if test -z "$stripcmd"; then
2212265a131Smrg	u_plus_rw=
2222265a131Smrg      else
2232265a131Smrg	u_plus_rw=,u+rw
2242265a131Smrg      fi
2252265a131Smrg      cp_umask=$mode$u_plus_rw;;
2262265a131Smrg  esac
2272265a131Smrgfi
2282265a131Smrg
229444c061aSmrgfor src
230444c061aSmrgdo
231444c061aSmrg  # Protect names starting with `-'.
232444c061aSmrg  case $src in
2332265a131Smrg    -*) src=./$src;;
234444c061aSmrg  esac
235444c061aSmrg
236444c061aSmrg  if test -n "$dir_arg"; then
237444c061aSmrg    dst=$src
2382265a131Smrg    dstdir=$dst
2392265a131Smrg    test -d "$dstdir"
2402265a131Smrg    dstdir_status=$?
241444c061aSmrg  else
2422265a131Smrg
243444c061aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244444c061aSmrg    # might cause directories to be created, which would be especially bad
245444c061aSmrg    # if $src (and thus $dsttmp) contains '*'.
246444c061aSmrg    if test ! -f "$src" && test ! -d "$src"; then
247444c061aSmrg      echo "$0: $src does not exist." >&2
248444c061aSmrg      exit 1
249444c061aSmrg    fi
250444c061aSmrg
2512265a131Smrg    if test -z "$dst_arg"; then
252444c061aSmrg      echo "$0: no destination specified." >&2
253444c061aSmrg      exit 1
254444c061aSmrg    fi
255444c061aSmrg
2562265a131Smrg    dst=$dst_arg
257444c061aSmrg    # Protect names starting with `-'.
258444c061aSmrg    case $dst in
2592265a131Smrg      -*) dst=./$dst;;
260444c061aSmrg    esac
261444c061aSmrg
262444c061aSmrg    # If destination is a directory, append the input filename; won't work
263444c061aSmrg    # if double slashes aren't ignored.
264444c061aSmrg    if test -d "$dst"; then
265444c061aSmrg      if test -n "$no_target_directory"; then
2662265a131Smrg	echo "$0: $dst_arg: Is a directory" >&2
267444c061aSmrg	exit 1
268444c061aSmrg      fi
2692265a131Smrg      dstdir=$dst
2702265a131Smrg      dst=$dstdir/`basename "$src"`
2712265a131Smrg      dstdir_status=0
2722265a131Smrg    else
2732265a131Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2742265a131Smrg      dstdir=`
2752265a131Smrg	(dirname "$dst") 2>/dev/null ||
2762265a131Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2772265a131Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2782265a131Smrg	     X"$dst" : 'X\(//\)$' \| \
2792265a131Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2802265a131Smrg	echo X"$dst" |
2812265a131Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2822265a131Smrg		   s//\1/
2832265a131Smrg		   q
2842265a131Smrg		 }
2852265a131Smrg		 /^X\(\/\/\)[^/].*/{
2862265a131Smrg		   s//\1/
2872265a131Smrg		   q
2882265a131Smrg		 }
2892265a131Smrg		 /^X\(\/\/\)$/{
2902265a131Smrg		   s//\1/
2912265a131Smrg		   q
2922265a131Smrg		 }
2932265a131Smrg		 /^X\(\/\).*/{
2942265a131Smrg		   s//\1/
2952265a131Smrg		   q
2962265a131Smrg		 }
2972265a131Smrg		 s/.*/./; q'
2982265a131Smrg      `
2992265a131Smrg
3002265a131Smrg      test -d "$dstdir"
3012265a131Smrg      dstdir_status=$?
302444c061aSmrg    fi
303444c061aSmrg  fi
304444c061aSmrg
3052265a131Smrg  obsolete_mkdir_used=false
3062265a131Smrg
3072265a131Smrg  if test $dstdir_status != 0; then
3082265a131Smrg    case $posix_mkdir in
3092265a131Smrg      '')
3102265a131Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3112265a131Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3122265a131Smrg	umask=`umask`
3132265a131Smrg	case $stripcmd.$umask in
3142265a131Smrg	  # Optimize common cases.
3152265a131Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3162265a131Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3172265a131Smrg
3182265a131Smrg	  *[0-7])
3192265a131Smrg	    mkdir_umask=`expr $umask + 22 \
3202265a131Smrg	      - $umask % 100 % 40 + $umask % 20 \
3212265a131Smrg	      - $umask % 10 % 4 + $umask % 2
3222265a131Smrg	    `;;
3232265a131Smrg	  *) mkdir_umask=$umask,go-w;;
3242265a131Smrg	esac
3252265a131Smrg
3262265a131Smrg	# With -d, create the new directory with the user-specified mode.
3272265a131Smrg	# Otherwise, rely on $mkdir_umask.
3282265a131Smrg	if test -n "$dir_arg"; then
3292265a131Smrg	  mkdir_mode=-m$mode
3302265a131Smrg	else
3312265a131Smrg	  mkdir_mode=
3322265a131Smrg	fi
3332265a131Smrg
3342265a131Smrg	posix_mkdir=false
3352265a131Smrg	case $umask in
3362265a131Smrg	  *[123567][0-7][0-7])
3372265a131Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3382265a131Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3392265a131Smrg	    ;;
3402265a131Smrg	  *)
3412265a131Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3422265a131Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3432265a131Smrg
3442265a131Smrg	    if (umask $mkdir_umask &&
3452265a131Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3462265a131Smrg	    then
3472265a131Smrg	      if test -z "$dir_arg" || {
3482265a131Smrg		   # Check for POSIX incompatibilities with -m.
3492265a131Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3502265a131Smrg		   # other-writeable bit of parent directory when it shouldn't.
3512265a131Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3522265a131Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3532265a131Smrg		   case $ls_ld_tmpdir in
3542265a131Smrg		     d????-?r-*) different_mode=700;;
3552265a131Smrg		     d????-?--*) different_mode=755;;
3562265a131Smrg		     *) false;;
3572265a131Smrg		   esac &&
3582265a131Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3592265a131Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3602265a131Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3612265a131Smrg		   }
3622265a131Smrg		 }
3632265a131Smrg	      then posix_mkdir=:
3642265a131Smrg	      fi
3652265a131Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3662265a131Smrg	    else
3672265a131Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3682265a131Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3692265a131Smrg	    fi
3702265a131Smrg	    trap '' 0;;
3712265a131Smrg	esac;;
3722265a131Smrg    esac
373444c061aSmrg
3742265a131Smrg    if
3752265a131Smrg      $posix_mkdir && (
3762265a131Smrg	umask $mkdir_umask &&
3772265a131Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3782265a131Smrg      )
3792265a131Smrg    then :
3802265a131Smrg    else
381444c061aSmrg
3822265a131Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3832265a131Smrg      # or it failed possibly due to a race condition.  Create the
3842265a131Smrg      # directory the slow way, step by step, checking for races as we go.
385444c061aSmrg
3862265a131Smrg      case $dstdir in
3872265a131Smrg	/*) prefix='/';;
3882265a131Smrg	-*) prefix='./';;
3892265a131Smrg	*)  prefix='';;
3902265a131Smrg      esac
391444c061aSmrg
3922265a131Smrg      eval "$initialize_posix_glob"
393444c061aSmrg
3942265a131Smrg      oIFS=$IFS
3952265a131Smrg      IFS=/
3962265a131Smrg      $posix_glob set -f
3972265a131Smrg      set fnord $dstdir
398444c061aSmrg      shift
3992265a131Smrg      $posix_glob set +f
4002265a131Smrg      IFS=$oIFS
4012265a131Smrg
4022265a131Smrg      prefixes=
4032265a131Smrg
4042265a131Smrg      for d
4052265a131Smrg      do
4062265a131Smrg	test -z "$d" && continue
4072265a131Smrg
4082265a131Smrg	prefix=$prefix$d
4092265a131Smrg	if test -d "$prefix"; then
4102265a131Smrg	  prefixes=
4112265a131Smrg	else
4122265a131Smrg	  if $posix_mkdir; then
4132265a131Smrg	    (umask=$mkdir_umask &&
4142265a131Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4152265a131Smrg	    # Don't fail if two instances are running concurrently.
4162265a131Smrg	    test -d "$prefix" || exit 1
4172265a131Smrg	  else
4182265a131Smrg	    case $prefix in
4192265a131Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4202265a131Smrg	      *) qprefix=$prefix;;
4212265a131Smrg	    esac
4222265a131Smrg	    prefixes="$prefixes '$qprefix'"
4232265a131Smrg	  fi
4242265a131Smrg	fi
4252265a131Smrg	prefix=$prefix/
4262265a131Smrg      done
4272265a131Smrg
4282265a131Smrg      if test -n "$prefixes"; then
4292265a131Smrg	# Don't fail if two instances are running concurrently.
4302265a131Smrg	(umask $mkdir_umask &&
4312265a131Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4322265a131Smrg	  test -d "$dstdir" || exit 1
4332265a131Smrg	obsolete_mkdir_used=true
434444c061aSmrg      fi
4352265a131Smrg    fi
436444c061aSmrg  fi
437444c061aSmrg
438444c061aSmrg  if test -n "$dir_arg"; then
4392265a131Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4402265a131Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4412265a131Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4422265a131Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443444c061aSmrg  else
444444c061aSmrg
445444c061aSmrg    # Make a couple of temp file names in the proper directory.
446444c061aSmrg    dsttmp=$dstdir/_inst.$$_
447444c061aSmrg    rmtmp=$dstdir/_rm.$$_
448444c061aSmrg
449444c061aSmrg    # Trap to clean up those temp files at exit.
450444c061aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451444c061aSmrg
452444c061aSmrg    # Copy the file name to the temp name.
4532265a131Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454444c061aSmrg
455444c061aSmrg    # and set any options; do chmod last to preserve setuid bits.
456444c061aSmrg    #
457444c061aSmrg    # If any of these fail, we abort the whole thing.  If we want to
458444c061aSmrg    # ignore errors from any of these, just make sure not to ignore
459444c061aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460444c061aSmrg    #
4612265a131Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4622265a131Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4632265a131Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4642265a131Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4652265a131Smrg
4662265a131Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4672265a131Smrg    if $copy_on_change &&
4682265a131Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4692265a131Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4702265a131Smrg
4712265a131Smrg       eval "$initialize_posix_glob" &&
4722265a131Smrg       $posix_glob set -f &&
4732265a131Smrg       set X $old && old=:$2:$4:$5:$6 &&
4742265a131Smrg       set X $new && new=:$2:$4:$5:$6 &&
4752265a131Smrg       $posix_glob set +f &&
4762265a131Smrg
4772265a131Smrg       test "$old" = "$new" &&
4782265a131Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4792265a131Smrg    then
4802265a131Smrg      rm -f "$dsttmp"
4812265a131Smrg    else
4822265a131Smrg      # Rename the file to the real destination.
4832265a131Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4842265a131Smrg
4852265a131Smrg      # The rename failed, perhaps because mv can't rename something else
4862265a131Smrg      # to itself, or perhaps because mv is so ancient that it does not
4872265a131Smrg      # support -f.
4882265a131Smrg      {
4892265a131Smrg	# Now remove or move aside any old file at destination location.
4902265a131Smrg	# We try this two ways since rm can't unlink itself on some
4912265a131Smrg	# systems and the destination file might be busy for other
4922265a131Smrg	# reasons.  In this case, the final cleanup might fail but the new
4932265a131Smrg	# file should still install successfully.
4942265a131Smrg	{
4952265a131Smrg	  test ! -f "$dst" ||
4962265a131Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4972265a131Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4982265a131Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4992265a131Smrg	  } ||
5002265a131Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5012265a131Smrg	    (exit 1); exit 1
5022265a131Smrg	  }
5032265a131Smrg	} &&
5042265a131Smrg
5052265a131Smrg	# Now rename the file to the real destination.
5062265a131Smrg	$doit $mvcmd "$dsttmp" "$dst"
5072265a131Smrg      }
5082265a131Smrg    fi || exit 1
5092265a131Smrg
5102265a131Smrg    trap '' 0
5112265a131Smrg  fi
512444c061aSmrgdone
513444c061aSmrg
514444c061aSmrg# Local variables:
515444c061aSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516444c061aSmrg# time-stamp-start: "scriptversion="
517444c061aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5182ae83157Smrg# time-stamp-time-zone: "UTC"
5192ae83157Smrg# time-stamp-end: "; # UTC"
520444c061aSmrg# End:
521