install-sh revision 42d69509
14456fccdSmrg#!/bin/sh
24456fccdSmrg# install - install a program, script, or datafile
34456fccdSmrg
442d69509Smrgscriptversion=2009-04-28.21; # UTC
54456fccdSmrg
64456fccdSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
74456fccdSmrg# later released in X11R6 (xc/config/util/install.sh) with the
84456fccdSmrg# following copyright and license.
94456fccdSmrg#
104456fccdSmrg# Copyright (C) 1994 X Consortium
114456fccdSmrg#
124456fccdSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
134456fccdSmrg# of this software and associated documentation files (the "Software"), to
144456fccdSmrg# deal in the Software without restriction, including without limitation the
154456fccdSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164456fccdSmrg# sell copies of the Software, and to permit persons to whom the Software is
174456fccdSmrg# furnished to do so, subject to the following conditions:
184456fccdSmrg#
194456fccdSmrg# The above copyright notice and this permission notice shall be included in
204456fccdSmrg# all copies or substantial portions of the Software.
214456fccdSmrg#
224456fccdSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234456fccdSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244456fccdSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254456fccdSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264456fccdSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274456fccdSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284456fccdSmrg#
294456fccdSmrg# Except as contained in this notice, the name of the X Consortium shall not
304456fccdSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
314456fccdSmrg# ings in this Software without prior written authorization from the X Consor-
324456fccdSmrg# tium.
334456fccdSmrg#
344456fccdSmrg#
354456fccdSmrg# FSF changes to this file are in the public domain.
364456fccdSmrg#
374456fccdSmrg# Calling this script install-sh is preferred over install.sh, to prevent
384456fccdSmrg# `make' implicit rules from creating a file called install from it
394456fccdSmrg# when there is no Makefile.
404456fccdSmrg#
414456fccdSmrg# This script is compatible with the BSD install script, but was written
4242d69509Smrg# from scratch.
4342d69509Smrg
4442d69509Smrgnl='
4542d69509Smrg'
4642d69509SmrgIFS=" ""	$nl"
474456fccdSmrg
484456fccdSmrg# set DOITPROG to echo to test this script
494456fccdSmrg
504456fccdSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5142d69509Smrgdoit=${DOITPROG-}
5242d69509Smrgif test -z "$doit"; then
5342d69509Smrg  doit_exec=exec
5442d69509Smrgelse
5542d69509Smrg  doit_exec=$doit
5642d69509Smrgfi
574456fccdSmrg
5842d69509Smrg# Put in absolute file names if you don't have them in your path;
5942d69509Smrg# or use environment vars.
6042d69509Smrg
6142d69509Smrgchgrpprog=${CHGRPPROG-chgrp}
6242d69509Smrgchmodprog=${CHMODPROG-chmod}
6342d69509Smrgchownprog=${CHOWNPROG-chown}
6442d69509Smrgcmpprog=${CMPPROG-cmp}
6542d69509Smrgcpprog=${CPPROG-cp}
6642d69509Smrgmkdirprog=${MKDIRPROG-mkdir}
6742d69509Smrgmvprog=${MVPROG-mv}
6842d69509Smrgrmprog=${RMPROG-rm}
6942d69509Smrgstripprog=${STRIPPROG-strip}
7042d69509Smrg
7142d69509Smrgposix_glob='?'
7242d69509Smrginitialize_posix_glob='
7342d69509Smrg  test "$posix_glob" != "?" || {
7442d69509Smrg    if (set -f) 2>/dev/null; then
7542d69509Smrg      posix_glob=
7642d69509Smrg    else
7742d69509Smrg      posix_glob=:
7842d69509Smrg    fi
7942d69509Smrg  }
8042d69509Smrg'
814456fccdSmrg
8242d69509Smrgposix_mkdir=
8342d69509Smrg
8442d69509Smrg# Desired mode of installed file.
8542d69509Smrgmode=0755
864456fccdSmrg
874456fccdSmrgchgrpcmd=
8842d69509Smrgchmodcmd=$chmodprog
8942d69509Smrgchowncmd=
9042d69509Smrgmvcmd=$mvprog
914456fccdSmrgrmcmd="$rmprog -f"
9242d69509Smrgstripcmd=
9342d69509Smrg
944456fccdSmrgsrc=
954456fccdSmrgdst=
964456fccdSmrgdir_arg=
9742d69509Smrgdst_arg=
9842d69509Smrg
9942d69509Smrgcopy_on_change=false
1004456fccdSmrgno_target_directory=
1014456fccdSmrg
10242d69509Smrgusage="\
10342d69509SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1044456fccdSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1054456fccdSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1064456fccdSmrg   or: $0 [OPTION]... -d DIRECTORIES...
1074456fccdSmrg
1084456fccdSmrgIn the 1st form, copy SRCFILE to DSTFILE.
1094456fccdSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1104456fccdSmrgIn the 4th, create DIRECTORIES.
1114456fccdSmrg
1124456fccdSmrgOptions:
11342d69509Smrg     --help     display this help and exit.
11442d69509Smrg     --version  display version info and exit.
11542d69509Smrg
11642d69509Smrg  -c            (ignored)
11742d69509Smrg  -C            install only if different (preserve the last data modification time)
11842d69509Smrg  -d            create directories instead of installing files.
11942d69509Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12042d69509Smrg  -m MODE       $chmodprog installed files to MODE.
12142d69509Smrg  -o USER       $chownprog installed files to USER.
12242d69509Smrg  -s            $stripprog installed files.
12342d69509Smrg  -t DIRECTORY  install into DIRECTORY.
12442d69509Smrg  -T            report an error if DSTFILE is a directory.
1254456fccdSmrg
1264456fccdSmrgEnvironment variables override the default commands:
12742d69509Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12842d69509Smrg  RMPROG STRIPPROG
1294456fccdSmrg"
1304456fccdSmrg
13142d69509Smrgwhile test $# -ne 0; do
1324456fccdSmrg  case $1 in
13342d69509Smrg    -c) ;;
13442d69509Smrg
13542d69509Smrg    -C) copy_on_change=true;;
1364456fccdSmrg
13742d69509Smrg    -d) dir_arg=true;;
1384456fccdSmrg
1394456fccdSmrg    -g) chgrpcmd="$chgrpprog $2"
14042d69509Smrg	shift;;
1414456fccdSmrg
1424456fccdSmrg    --help) echo "$usage"; exit $?;;
1434456fccdSmrg
14442d69509Smrg    -m) mode=$2
14542d69509Smrg	case $mode in
14642d69509Smrg	  *' '* | *'	'* | *'
14742d69509Smrg'*	  | *'*'* | *'?'* | *'['*)
14842d69509Smrg	    echo "$0: invalid mode: $mode" >&2
14942d69509Smrg	    exit 1;;
15042d69509Smrg	esac
15142d69509Smrg	shift;;
1524456fccdSmrg
1534456fccdSmrg    -o) chowncmd="$chownprog $2"
15442d69509Smrg	shift;;
1554456fccdSmrg
15642d69509Smrg    -s) stripcmd=$stripprog;;
1574456fccdSmrg
15842d69509Smrg    -t) dst_arg=$2
15942d69509Smrg	shift;;
1604456fccdSmrg
16142d69509Smrg    -T) no_target_directory=true;;
1624456fccdSmrg
1634456fccdSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1644456fccdSmrg
16542d69509Smrg    --)	shift
1664456fccdSmrg	break;;
16742d69509Smrg
16842d69509Smrg    -*)	echo "$0: invalid option: $1" >&2
16942d69509Smrg	exit 1;;
17042d69509Smrg
17142d69509Smrg    *)  break;;
1724456fccdSmrg  esac
17342d69509Smrg  shift
1744456fccdSmrgdone
1754456fccdSmrg
17642d69509Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17742d69509Smrg  # When -d is used, all remaining arguments are directories to create.
17842d69509Smrg  # When -t is used, the destination is already specified.
17942d69509Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18042d69509Smrg  for arg
18142d69509Smrg  do
18242d69509Smrg    if test -n "$dst_arg"; then
18342d69509Smrg      # $@ is not empty: it contains at least $arg.
18442d69509Smrg      set fnord "$@" "$dst_arg"
18542d69509Smrg      shift # fnord
18642d69509Smrg    fi
18742d69509Smrg    shift # arg
18842d69509Smrg    dst_arg=$arg
18942d69509Smrg  done
19042d69509Smrgfi
19142d69509Smrg
19242d69509Smrgif test $# -eq 0; then
1934456fccdSmrg  if test -z "$dir_arg"; then
1944456fccdSmrg    echo "$0: no input file specified." >&2
1954456fccdSmrg    exit 1
1964456fccdSmrg  fi
1974456fccdSmrg  # It's OK to call `install-sh -d' without argument.
1984456fccdSmrg  # This can happen when creating conditional directories.
1994456fccdSmrg  exit 0
2004456fccdSmrgfi
2014456fccdSmrg
20242d69509Smrgif test -z "$dir_arg"; then
20342d69509Smrg  trap '(exit $?); exit' 1 2 13 15
20442d69509Smrg
20542d69509Smrg  # Set umask so as not to create temps with too-generous modes.
20642d69509Smrg  # However, 'strip' requires both read and write access to temps.
20742d69509Smrg  case $mode in
20842d69509Smrg    # Optimize common cases.
20942d69509Smrg    *644) cp_umask=133;;
21042d69509Smrg    *755) cp_umask=22;;
21142d69509Smrg
21242d69509Smrg    *[0-7])
21342d69509Smrg      if test -z "$stripcmd"; then
21442d69509Smrg	u_plus_rw=
21542d69509Smrg      else
21642d69509Smrg	u_plus_rw='% 200'
21742d69509Smrg      fi
21842d69509Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21942d69509Smrg    *)
22042d69509Smrg      if test -z "$stripcmd"; then
22142d69509Smrg	u_plus_rw=
22242d69509Smrg      else
22342d69509Smrg	u_plus_rw=,u+rw
22442d69509Smrg      fi
22542d69509Smrg      cp_umask=$mode$u_plus_rw;;
22642d69509Smrg  esac
22742d69509Smrgfi
22842d69509Smrg
2294456fccdSmrgfor src
2304456fccdSmrgdo
2314456fccdSmrg  # Protect names starting with `-'.
2324456fccdSmrg  case $src in
23342d69509Smrg    -*) src=./$src;;
2344456fccdSmrg  esac
2354456fccdSmrg
2364456fccdSmrg  if test -n "$dir_arg"; then
2374456fccdSmrg    dst=$src
23842d69509Smrg    dstdir=$dst
23942d69509Smrg    test -d "$dstdir"
24042d69509Smrg    dstdir_status=$?
2414456fccdSmrg  else
24242d69509Smrg
2434456fccdSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2444456fccdSmrg    # might cause directories to be created, which would be especially bad
2454456fccdSmrg    # if $src (and thus $dsttmp) contains '*'.
2464456fccdSmrg    if test ! -f "$src" && test ! -d "$src"; then
2474456fccdSmrg      echo "$0: $src does not exist." >&2
2484456fccdSmrg      exit 1
2494456fccdSmrg    fi
2504456fccdSmrg
25142d69509Smrg    if test -z "$dst_arg"; then
2524456fccdSmrg      echo "$0: no destination specified." >&2
2534456fccdSmrg      exit 1
2544456fccdSmrg    fi
2554456fccdSmrg
25642d69509Smrg    dst=$dst_arg
2574456fccdSmrg    # Protect names starting with `-'.
2584456fccdSmrg    case $dst in
25942d69509Smrg      -*) dst=./$dst;;
2604456fccdSmrg    esac
2614456fccdSmrg
2624456fccdSmrg    # If destination is a directory, append the input filename; won't work
2634456fccdSmrg    # if double slashes aren't ignored.
2644456fccdSmrg    if test -d "$dst"; then
2654456fccdSmrg      if test -n "$no_target_directory"; then
26642d69509Smrg	echo "$0: $dst_arg: Is a directory" >&2
2674456fccdSmrg	exit 1
2684456fccdSmrg      fi
26942d69509Smrg      dstdir=$dst
27042d69509Smrg      dst=$dstdir/`basename "$src"`
27142d69509Smrg      dstdir_status=0
27242d69509Smrg    else
27342d69509Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27442d69509Smrg      dstdir=`
27542d69509Smrg	(dirname "$dst") 2>/dev/null ||
27642d69509Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27742d69509Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27842d69509Smrg	     X"$dst" : 'X\(//\)$' \| \
27942d69509Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28042d69509Smrg	echo X"$dst" |
28142d69509Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28242d69509Smrg		   s//\1/
28342d69509Smrg		   q
28442d69509Smrg		 }
28542d69509Smrg		 /^X\(\/\/\)[^/].*/{
28642d69509Smrg		   s//\1/
28742d69509Smrg		   q
28842d69509Smrg		 }
28942d69509Smrg		 /^X\(\/\/\)$/{
29042d69509Smrg		   s//\1/
29142d69509Smrg		   q
29242d69509Smrg		 }
29342d69509Smrg		 /^X\(\/\).*/{
29442d69509Smrg		   s//\1/
29542d69509Smrg		   q
29642d69509Smrg		 }
29742d69509Smrg		 s/.*/./; q'
29842d69509Smrg      `
29942d69509Smrg
30042d69509Smrg      test -d "$dstdir"
30142d69509Smrg      dstdir_status=$?
3024456fccdSmrg    fi
3034456fccdSmrg  fi
3044456fccdSmrg
30542d69509Smrg  obsolete_mkdir_used=false
30642d69509Smrg
30742d69509Smrg  if test $dstdir_status != 0; then
30842d69509Smrg    case $posix_mkdir in
30942d69509Smrg      '')
31042d69509Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31142d69509Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31242d69509Smrg	umask=`umask`
31342d69509Smrg	case $stripcmd.$umask in
31442d69509Smrg	  # Optimize common cases.
31542d69509Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31642d69509Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31742d69509Smrg
31842d69509Smrg	  *[0-7])
31942d69509Smrg	    mkdir_umask=`expr $umask + 22 \
32042d69509Smrg	      - $umask % 100 % 40 + $umask % 20 \
32142d69509Smrg	      - $umask % 10 % 4 + $umask % 2
32242d69509Smrg	    `;;
32342d69509Smrg	  *) mkdir_umask=$umask,go-w;;
32442d69509Smrg	esac
32542d69509Smrg
32642d69509Smrg	# With -d, create the new directory with the user-specified mode.
32742d69509Smrg	# Otherwise, rely on $mkdir_umask.
32842d69509Smrg	if test -n "$dir_arg"; then
32942d69509Smrg	  mkdir_mode=-m$mode
33042d69509Smrg	else
33142d69509Smrg	  mkdir_mode=
33242d69509Smrg	fi
33342d69509Smrg
33442d69509Smrg	posix_mkdir=false
33542d69509Smrg	case $umask in
33642d69509Smrg	  *[123567][0-7][0-7])
33742d69509Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33842d69509Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33942d69509Smrg	    ;;
34042d69509Smrg	  *)
34142d69509Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34242d69509Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34342d69509Smrg
34442d69509Smrg	    if (umask $mkdir_umask &&
34542d69509Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34642d69509Smrg	    then
34742d69509Smrg	      if test -z "$dir_arg" || {
34842d69509Smrg		   # Check for POSIX incompatibilities with -m.
34942d69509Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35042d69509Smrg		   # other-writeable bit of parent directory when it shouldn't.
35142d69509Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35242d69509Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35342d69509Smrg		   case $ls_ld_tmpdir in
35442d69509Smrg		     d????-?r-*) different_mode=700;;
35542d69509Smrg		     d????-?--*) different_mode=755;;
35642d69509Smrg		     *) false;;
35742d69509Smrg		   esac &&
35842d69509Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35942d69509Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36042d69509Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36142d69509Smrg		   }
36242d69509Smrg		 }
36342d69509Smrg	      then posix_mkdir=:
36442d69509Smrg	      fi
36542d69509Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36642d69509Smrg	    else
36742d69509Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36842d69509Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36942d69509Smrg	    fi
37042d69509Smrg	    trap '' 0;;
37142d69509Smrg	esac;;
37242d69509Smrg    esac
3734456fccdSmrg
37442d69509Smrg    if
37542d69509Smrg      $posix_mkdir && (
37642d69509Smrg	umask $mkdir_umask &&
37742d69509Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37842d69509Smrg      )
37942d69509Smrg    then :
38042d69509Smrg    else
3814456fccdSmrg
38242d69509Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38342d69509Smrg      # or it failed possibly due to a race condition.  Create the
38442d69509Smrg      # directory the slow way, step by step, checking for races as we go.
3854456fccdSmrg
38642d69509Smrg      case $dstdir in
38742d69509Smrg	/*) prefix='/';;
38842d69509Smrg	-*) prefix='./';;
38942d69509Smrg	*)  prefix='';;
39042d69509Smrg      esac
3914456fccdSmrg
39242d69509Smrg      eval "$initialize_posix_glob"
3934456fccdSmrg
39442d69509Smrg      oIFS=$IFS
39542d69509Smrg      IFS=/
39642d69509Smrg      $posix_glob set -f
39742d69509Smrg      set fnord $dstdir
3984456fccdSmrg      shift
39942d69509Smrg      $posix_glob set +f
40042d69509Smrg      IFS=$oIFS
40142d69509Smrg
40242d69509Smrg      prefixes=
40342d69509Smrg
40442d69509Smrg      for d
40542d69509Smrg      do
40642d69509Smrg	test -z "$d" && continue
40742d69509Smrg
40842d69509Smrg	prefix=$prefix$d
40942d69509Smrg	if test -d "$prefix"; then
41042d69509Smrg	  prefixes=
41142d69509Smrg	else
41242d69509Smrg	  if $posix_mkdir; then
41342d69509Smrg	    (umask=$mkdir_umask &&
41442d69509Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41542d69509Smrg	    # Don't fail if two instances are running concurrently.
41642d69509Smrg	    test -d "$prefix" || exit 1
41742d69509Smrg	  else
41842d69509Smrg	    case $prefix in
41942d69509Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42042d69509Smrg	      *) qprefix=$prefix;;
42142d69509Smrg	    esac
42242d69509Smrg	    prefixes="$prefixes '$qprefix'"
42342d69509Smrg	  fi
42442d69509Smrg	fi
42542d69509Smrg	prefix=$prefix/
42642d69509Smrg      done
42742d69509Smrg
42842d69509Smrg      if test -n "$prefixes"; then
42942d69509Smrg	# Don't fail if two instances are running concurrently.
43042d69509Smrg	(umask $mkdir_umask &&
43142d69509Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43242d69509Smrg	  test -d "$dstdir" || exit 1
43342d69509Smrg	obsolete_mkdir_used=true
4344456fccdSmrg      fi
43542d69509Smrg    fi
4364456fccdSmrg  fi
4374456fccdSmrg
4384456fccdSmrg  if test -n "$dir_arg"; then
43942d69509Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44042d69509Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44142d69509Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44242d69509Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4434456fccdSmrg  else
4444456fccdSmrg
4454456fccdSmrg    # Make a couple of temp file names in the proper directory.
4464456fccdSmrg    dsttmp=$dstdir/_inst.$$_
4474456fccdSmrg    rmtmp=$dstdir/_rm.$$_
4484456fccdSmrg
4494456fccdSmrg    # Trap to clean up those temp files at exit.
4504456fccdSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4514456fccdSmrg
4524456fccdSmrg    # Copy the file name to the temp name.
45342d69509Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4544456fccdSmrg
4554456fccdSmrg    # and set any options; do chmod last to preserve setuid bits.
4564456fccdSmrg    #
4574456fccdSmrg    # If any of these fail, we abort the whole thing.  If we want to
4584456fccdSmrg    # ignore errors from any of these, just make sure not to ignore
4594456fccdSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4604456fccdSmrg    #
46142d69509Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46242d69509Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46342d69509Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46442d69509Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46542d69509Smrg
46642d69509Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46742d69509Smrg    if $copy_on_change &&
46842d69509Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46942d69509Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47042d69509Smrg
47142d69509Smrg       eval "$initialize_posix_glob" &&
47242d69509Smrg       $posix_glob set -f &&
47342d69509Smrg       set X $old && old=:$2:$4:$5:$6 &&
47442d69509Smrg       set X $new && new=:$2:$4:$5:$6 &&
47542d69509Smrg       $posix_glob set +f &&
47642d69509Smrg
47742d69509Smrg       test "$old" = "$new" &&
47842d69509Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47942d69509Smrg    then
48042d69509Smrg      rm -f "$dsttmp"
48142d69509Smrg    else
48242d69509Smrg      # Rename the file to the real destination.
48342d69509Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48442d69509Smrg
48542d69509Smrg      # The rename failed, perhaps because mv can't rename something else
48642d69509Smrg      # to itself, or perhaps because mv is so ancient that it does not
48742d69509Smrg      # support -f.
48842d69509Smrg      {
48942d69509Smrg	# Now remove or move aside any old file at destination location.
49042d69509Smrg	# We try this two ways since rm can't unlink itself on some
49142d69509Smrg	# systems and the destination file might be busy for other
49242d69509Smrg	# reasons.  In this case, the final cleanup might fail but the new
49342d69509Smrg	# file should still install successfully.
49442d69509Smrg	{
49542d69509Smrg	  test ! -f "$dst" ||
49642d69509Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49742d69509Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49842d69509Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49942d69509Smrg	  } ||
50042d69509Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50142d69509Smrg	    (exit 1); exit 1
50242d69509Smrg	  }
50342d69509Smrg	} &&
50442d69509Smrg
50542d69509Smrg	# Now rename the file to the real destination.
50642d69509Smrg	$doit $mvcmd "$dsttmp" "$dst"
50742d69509Smrg      }
50842d69509Smrg    fi || exit 1
50942d69509Smrg
51042d69509Smrg    trap '' 0
51142d69509Smrg  fi
5124456fccdSmrgdone
5134456fccdSmrg
5144456fccdSmrg# Local variables:
5154456fccdSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5164456fccdSmrg# time-stamp-start: "scriptversion="
5174456fccdSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51842d69509Smrg# time-stamp-time-zone: "UTC"
51942d69509Smrg# time-stamp-end: "; # UTC"
5204456fccdSmrg# End:
521