install-sh revision 14ddf674
1cacd992dSmrg#!/bin/sh
2cacd992dSmrg# install - install a program, script, or datafile
314ddf674Smrg
414ddf674Smrgscriptversion=2009-04-28.21; # UTC
514ddf674Smrg
6cacd992dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7cacd992dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8cacd992dSmrg# following copyright and license.
9cacd992dSmrg#
10cacd992dSmrg# Copyright (C) 1994 X Consortium
11cacd992dSmrg#
12cacd992dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13cacd992dSmrg# of this software and associated documentation files (the "Software"), to
14cacd992dSmrg# deal in the Software without restriction, including without limitation the
15cacd992dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16cacd992dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17cacd992dSmrg# furnished to do so, subject to the following conditions:
18cacd992dSmrg#
19cacd992dSmrg# The above copyright notice and this permission notice shall be included in
20cacd992dSmrg# all copies or substantial portions of the Software.
21cacd992dSmrg#
22cacd992dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23cacd992dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24cacd992dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25cacd992dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26cacd992dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27cacd992dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28cacd992dSmrg#
29cacd992dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30cacd992dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31cacd992dSmrg# ings in this Software without prior written authorization from the X Consor-
32cacd992dSmrg# tium.
33cacd992dSmrg#
34cacd992dSmrg#
35cacd992dSmrg# FSF changes to this file are in the public domain.
36cacd992dSmrg#
37cacd992dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38cacd992dSmrg# `make' implicit rules from creating a file called install from it
39cacd992dSmrg# when there is no Makefile.
40cacd992dSmrg#
41cacd992dSmrg# This script is compatible with the BSD install script, but was written
4214ddf674Smrg# from scratch.
43cacd992dSmrg
4414ddf674Smrgnl='
4514ddf674Smrg'
4614ddf674SmrgIFS=" ""	$nl"
47cacd992dSmrg
48cacd992dSmrg# set DOITPROG to echo to test this script
49cacd992dSmrg
50cacd992dSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5114ddf674Smrgdoit=${DOITPROG-}
5214ddf674Smrgif test -z "$doit"; then
5314ddf674Smrg  doit_exec=exec
54cacd992dSmrgelse
5514ddf674Smrg  doit_exec=$doit
56cacd992dSmrgfi
57cacd992dSmrg
5814ddf674Smrg# Put in absolute file names if you don't have them in your path;
5914ddf674Smrg# or use environment vars.
6014ddf674Smrg
6114ddf674Smrgchgrpprog=${CHGRPPROG-chgrp}
6214ddf674Smrgchmodprog=${CHMODPROG-chmod}
6314ddf674Smrgchownprog=${CHOWNPROG-chown}
6414ddf674Smrgcmpprog=${CMPPROG-cmp}
6514ddf674Smrgcpprog=${CPPROG-cp}
6614ddf674Smrgmkdirprog=${MKDIRPROG-mkdir}
6714ddf674Smrgmvprog=${MVPROG-mv}
6814ddf674Smrgrmprog=${RMPROG-rm}
6914ddf674Smrgstripprog=${STRIPPROG-strip}
7014ddf674Smrg
7114ddf674Smrgposix_glob='?'
7214ddf674Smrginitialize_posix_glob='
7314ddf674Smrg  test "$posix_glob" != "?" || {
7414ddf674Smrg    if (set -f) 2>/dev/null; then
7514ddf674Smrg      posix_glob=
7614ddf674Smrg    else
7714ddf674Smrg      posix_glob=:
7814ddf674Smrg    fi
7914ddf674Smrg  }
8014ddf674Smrg'
8114ddf674Smrg
8214ddf674Smrgposix_mkdir=
8314ddf674Smrg
8414ddf674Smrg# Desired mode of installed file.
8514ddf674Smrgmode=0755
8614ddf674Smrg
8714ddf674Smrgchgrpcmd=
8814ddf674Smrgchmodcmd=$chmodprog
8914ddf674Smrgchowncmd=
9014ddf674Smrgmvcmd=$mvprog
9114ddf674Smrgrmcmd="$rmprog -f"
9214ddf674Smrgstripcmd=
93cacd992dSmrg
9414ddf674Smrgsrc=
9514ddf674Smrgdst=
9614ddf674Smrgdir_arg=
9714ddf674Smrgdst_arg=
98cacd992dSmrg
9914ddf674Smrgcopy_on_change=false
10014ddf674Smrgno_target_directory=
101cacd992dSmrg
10214ddf674Smrgusage="\
10314ddf674SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10414ddf674Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10514ddf674Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10614ddf674Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107cacd992dSmrg
10814ddf674SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10914ddf674SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11014ddf674SmrgIn the 4th, create DIRECTORIES.
111cacd992dSmrg
11214ddf674SmrgOptions:
11314ddf674Smrg     --help     display this help and exit.
11414ddf674Smrg     --version  display version info and exit.
115cacd992dSmrg
11614ddf674Smrg  -c            (ignored)
11714ddf674Smrg  -C            install only if different (preserve the last data modification time)
11814ddf674Smrg  -d            create directories instead of installing files.
11914ddf674Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12014ddf674Smrg  -m MODE       $chmodprog installed files to MODE.
12114ddf674Smrg  -o USER       $chownprog installed files to USER.
12214ddf674Smrg  -s            $stripprog installed files.
12314ddf674Smrg  -t DIRECTORY  install into DIRECTORY.
12414ddf674Smrg  -T            report an error if DSTFILE is a directory.
125cacd992dSmrg
12614ddf674SmrgEnvironment variables override the default commands:
12714ddf674Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12814ddf674Smrg  RMPROG STRIPPROG
12914ddf674Smrg"
130cacd992dSmrg
13114ddf674Smrgwhile test $# -ne 0; do
13214ddf674Smrg  case $1 in
13314ddf674Smrg    -c) ;;
134cacd992dSmrg
13514ddf674Smrg    -C) copy_on_change=true;;
136cacd992dSmrg
13714ddf674Smrg    -d) dir_arg=true;;
138cacd992dSmrg
13914ddf674Smrg    -g) chgrpcmd="$chgrpprog $2"
14014ddf674Smrg	shift;;
141cacd992dSmrg
14214ddf674Smrg    --help) echo "$usage"; exit $?;;
143cacd992dSmrg
14414ddf674Smrg    -m) mode=$2
14514ddf674Smrg	case $mode in
14614ddf674Smrg	  *' '* | *'	'* | *'
14714ddf674Smrg'*	  | *'*'* | *'?'* | *'['*)
14814ddf674Smrg	    echo "$0: invalid mode: $mode" >&2
14914ddf674Smrg	    exit 1;;
15014ddf674Smrg	esac
15114ddf674Smrg	shift;;
152cacd992dSmrg
15314ddf674Smrg    -o) chowncmd="$chownprog $2"
15414ddf674Smrg	shift;;
155cacd992dSmrg
15614ddf674Smrg    -s) stripcmd=$stripprog;;
157cacd992dSmrg
15814ddf674Smrg    -t) dst_arg=$2
15914ddf674Smrg	shift;;
160cacd992dSmrg
16114ddf674Smrg    -T) no_target_directory=true;;
162cacd992dSmrg
16314ddf674Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164cacd992dSmrg
16514ddf674Smrg    --)	shift
16614ddf674Smrg	break;;
167cacd992dSmrg
16814ddf674Smrg    -*)	echo "$0: invalid option: $1" >&2
16914ddf674Smrg	exit 1;;
170cacd992dSmrg
17114ddf674Smrg    *)  break;;
17214ddf674Smrg  esac
17314ddf674Smrg  shift
17414ddf674Smrgdone
175cacd992dSmrg
17614ddf674Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17714ddf674Smrg  # When -d is used, all remaining arguments are directories to create.
17814ddf674Smrg  # When -t is used, the destination is already specified.
17914ddf674Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18014ddf674Smrg  for arg
18114ddf674Smrg  do
18214ddf674Smrg    if test -n "$dst_arg"; then
18314ddf674Smrg      # $@ is not empty: it contains at least $arg.
18414ddf674Smrg      set fnord "$@" "$dst_arg"
18514ddf674Smrg      shift # fnord
18614ddf674Smrg    fi
18714ddf674Smrg    shift # arg
18814ddf674Smrg    dst_arg=$arg
18914ddf674Smrg  done
19014ddf674Smrgfi
191cacd992dSmrg
19214ddf674Smrgif test $# -eq 0; then
19314ddf674Smrg  if test -z "$dir_arg"; then
19414ddf674Smrg    echo "$0: no input file specified." >&2
19514ddf674Smrg    exit 1
19614ddf674Smrg  fi
19714ddf674Smrg  # It's OK to call `install-sh -d' without argument.
19814ddf674Smrg  # This can happen when creating conditional directories.
19914ddf674Smrg  exit 0
20014ddf674Smrgfi
201cacd992dSmrg
20214ddf674Smrgif test -z "$dir_arg"; then
20314ddf674Smrg  trap '(exit $?); exit' 1 2 13 15
20414ddf674Smrg
20514ddf674Smrg  # Set umask so as not to create temps with too-generous modes.
20614ddf674Smrg  # However, 'strip' requires both read and write access to temps.
20714ddf674Smrg  case $mode in
20814ddf674Smrg    # Optimize common cases.
20914ddf674Smrg    *644) cp_umask=133;;
21014ddf674Smrg    *755) cp_umask=22;;
21114ddf674Smrg
21214ddf674Smrg    *[0-7])
21314ddf674Smrg      if test -z "$stripcmd"; then
21414ddf674Smrg	u_plus_rw=
21514ddf674Smrg      else
21614ddf674Smrg	u_plus_rw='% 200'
21714ddf674Smrg      fi
21814ddf674Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21914ddf674Smrg    *)
22014ddf674Smrg      if test -z "$stripcmd"; then
22114ddf674Smrg	u_plus_rw=
22214ddf674Smrg      else
22314ddf674Smrg	u_plus_rw=,u+rw
22414ddf674Smrg      fi
22514ddf674Smrg      cp_umask=$mode$u_plus_rw;;
22614ddf674Smrg  esac
22714ddf674Smrgfi
228cacd992dSmrg
22914ddf674Smrgfor src
23014ddf674Smrgdo
23114ddf674Smrg  # Protect names starting with `-'.
23214ddf674Smrg  case $src in
23314ddf674Smrg    -*) src=./$src;;
23414ddf674Smrg  esac
23514ddf674Smrg
23614ddf674Smrg  if test -n "$dir_arg"; then
23714ddf674Smrg    dst=$src
23814ddf674Smrg    dstdir=$dst
23914ddf674Smrg    test -d "$dstdir"
24014ddf674Smrg    dstdir_status=$?
24114ddf674Smrg  else
24214ddf674Smrg
24314ddf674Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24414ddf674Smrg    # might cause directories to be created, which would be especially bad
24514ddf674Smrg    # if $src (and thus $dsttmp) contains '*'.
24614ddf674Smrg    if test ! -f "$src" && test ! -d "$src"; then
24714ddf674Smrg      echo "$0: $src does not exist." >&2
24814ddf674Smrg      exit 1
24914ddf674Smrg    fi
25014ddf674Smrg
25114ddf674Smrg    if test -z "$dst_arg"; then
25214ddf674Smrg      echo "$0: no destination specified." >&2
25314ddf674Smrg      exit 1
25414ddf674Smrg    fi
25514ddf674Smrg
25614ddf674Smrg    dst=$dst_arg
25714ddf674Smrg    # Protect names starting with `-'.
25814ddf674Smrg    case $dst in
25914ddf674Smrg      -*) dst=./$dst;;
26014ddf674Smrg    esac
261cacd992dSmrg
26214ddf674Smrg    # If destination is a directory, append the input filename; won't work
26314ddf674Smrg    # if double slashes aren't ignored.
26414ddf674Smrg    if test -d "$dst"; then
26514ddf674Smrg      if test -n "$no_target_directory"; then
26614ddf674Smrg	echo "$0: $dst_arg: Is a directory" >&2
26714ddf674Smrg	exit 1
26814ddf674Smrg      fi
26914ddf674Smrg      dstdir=$dst
27014ddf674Smrg      dst=$dstdir/`basename "$src"`
27114ddf674Smrg      dstdir_status=0
27214ddf674Smrg    else
27314ddf674Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27414ddf674Smrg      dstdir=`
27514ddf674Smrg	(dirname "$dst") 2>/dev/null ||
27614ddf674Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27714ddf674Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27814ddf674Smrg	     X"$dst" : 'X\(//\)$' \| \
27914ddf674Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28014ddf674Smrg	echo X"$dst" |
28114ddf674Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28214ddf674Smrg		   s//\1/
28314ddf674Smrg		   q
28414ddf674Smrg		 }
28514ddf674Smrg		 /^X\(\/\/\)[^/].*/{
28614ddf674Smrg		   s//\1/
28714ddf674Smrg		   q
28814ddf674Smrg		 }
28914ddf674Smrg		 /^X\(\/\/\)$/{
29014ddf674Smrg		   s//\1/
29114ddf674Smrg		   q
29214ddf674Smrg		 }
29314ddf674Smrg		 /^X\(\/\).*/{
29414ddf674Smrg		   s//\1/
29514ddf674Smrg		   q
29614ddf674Smrg		 }
29714ddf674Smrg		 s/.*/./; q'
29814ddf674Smrg      `
29914ddf674Smrg
30014ddf674Smrg      test -d "$dstdir"
30114ddf674Smrg      dstdir_status=$?
30214ddf674Smrg    fi
30314ddf674Smrg  fi
30414ddf674Smrg
30514ddf674Smrg  obsolete_mkdir_used=false
30614ddf674Smrg
30714ddf674Smrg  if test $dstdir_status != 0; then
30814ddf674Smrg    case $posix_mkdir in
30914ddf674Smrg      '')
31014ddf674Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31114ddf674Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31214ddf674Smrg	umask=`umask`
31314ddf674Smrg	case $stripcmd.$umask in
31414ddf674Smrg	  # Optimize common cases.
31514ddf674Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31614ddf674Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31714ddf674Smrg
31814ddf674Smrg	  *[0-7])
31914ddf674Smrg	    mkdir_umask=`expr $umask + 22 \
32014ddf674Smrg	      - $umask % 100 % 40 + $umask % 20 \
32114ddf674Smrg	      - $umask % 10 % 4 + $umask % 2
32214ddf674Smrg	    `;;
32314ddf674Smrg	  *) mkdir_umask=$umask,go-w;;
32414ddf674Smrg	esac
32514ddf674Smrg
32614ddf674Smrg	# With -d, create the new directory with the user-specified mode.
32714ddf674Smrg	# Otherwise, rely on $mkdir_umask.
32814ddf674Smrg	if test -n "$dir_arg"; then
32914ddf674Smrg	  mkdir_mode=-m$mode
330cacd992dSmrg	else
33114ddf674Smrg	  mkdir_mode=
332cacd992dSmrg	fi
333cacd992dSmrg
33414ddf674Smrg	posix_mkdir=false
33514ddf674Smrg	case $umask in
33614ddf674Smrg	  *[123567][0-7][0-7])
33714ddf674Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33814ddf674Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33914ddf674Smrg	    ;;
34014ddf674Smrg	  *)
34114ddf674Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34214ddf674Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34314ddf674Smrg
34414ddf674Smrg	    if (umask $mkdir_umask &&
34514ddf674Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34614ddf674Smrg	    then
34714ddf674Smrg	      if test -z "$dir_arg" || {
34814ddf674Smrg		   # Check for POSIX incompatibilities with -m.
34914ddf674Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35014ddf674Smrg		   # other-writeable bit of parent directory when it shouldn't.
35114ddf674Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35214ddf674Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35314ddf674Smrg		   case $ls_ld_tmpdir in
35414ddf674Smrg		     d????-?r-*) different_mode=700;;
35514ddf674Smrg		     d????-?--*) different_mode=755;;
35614ddf674Smrg		     *) false;;
35714ddf674Smrg		   esac &&
35814ddf674Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35914ddf674Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36014ddf674Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36114ddf674Smrg		   }
36214ddf674Smrg		 }
36314ddf674Smrg	      then posix_mkdir=:
36414ddf674Smrg	      fi
36514ddf674Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36614ddf674Smrg	    else
36714ddf674Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36814ddf674Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36914ddf674Smrg	    fi
37014ddf674Smrg	    trap '' 0;;
37114ddf674Smrg	esac;;
37214ddf674Smrg    esac
373cacd992dSmrg
37414ddf674Smrg    if
37514ddf674Smrg      $posix_mkdir && (
37614ddf674Smrg	umask $mkdir_umask &&
37714ddf674Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37814ddf674Smrg      )
37914ddf674Smrg    then :
38014ddf674Smrg    else
38114ddf674Smrg
38214ddf674Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38314ddf674Smrg      # or it failed possibly due to a race condition.  Create the
38414ddf674Smrg      # directory the slow way, step by step, checking for races as we go.
38514ddf674Smrg
38614ddf674Smrg      case $dstdir in
38714ddf674Smrg	/*) prefix='/';;
38814ddf674Smrg	-*) prefix='./';;
38914ddf674Smrg	*)  prefix='';;
39014ddf674Smrg      esac
39114ddf674Smrg
39214ddf674Smrg      eval "$initialize_posix_glob"
39314ddf674Smrg
39414ddf674Smrg      oIFS=$IFS
39514ddf674Smrg      IFS=/
39614ddf674Smrg      $posix_glob set -f
39714ddf674Smrg      set fnord $dstdir
39814ddf674Smrg      shift
39914ddf674Smrg      $posix_glob set +f
40014ddf674Smrg      IFS=$oIFS
40114ddf674Smrg
40214ddf674Smrg      prefixes=
40314ddf674Smrg
40414ddf674Smrg      for d
40514ddf674Smrg      do
40614ddf674Smrg	test -z "$d" && continue
40714ddf674Smrg
40814ddf674Smrg	prefix=$prefix$d
40914ddf674Smrg	if test -d "$prefix"; then
41014ddf674Smrg	  prefixes=
41114ddf674Smrg	else
41214ddf674Smrg	  if $posix_mkdir; then
41314ddf674Smrg	    (umask=$mkdir_umask &&
41414ddf674Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41514ddf674Smrg	    # Don't fail if two instances are running concurrently.
41614ddf674Smrg	    test -d "$prefix" || exit 1
41714ddf674Smrg	  else
41814ddf674Smrg	    case $prefix in
41914ddf674Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42014ddf674Smrg	      *) qprefix=$prefix;;
42114ddf674Smrg	    esac
42214ddf674Smrg	    prefixes="$prefixes '$qprefix'"
42314ddf674Smrg	  fi
42414ddf674Smrg	fi
42514ddf674Smrg	prefix=$prefix/
42614ddf674Smrg      done
42714ddf674Smrg
42814ddf674Smrg      if test -n "$prefixes"; then
42914ddf674Smrg	# Don't fail if two instances are running concurrently.
43014ddf674Smrg	(umask $mkdir_umask &&
43114ddf674Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43214ddf674Smrg	  test -d "$dstdir" || exit 1
43314ddf674Smrg	obsolete_mkdir_used=true
43414ddf674Smrg      fi
43514ddf674Smrg    fi
43614ddf674Smrg  fi
43714ddf674Smrg
43814ddf674Smrg  if test -n "$dir_arg"; then
43914ddf674Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44014ddf674Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44114ddf674Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44214ddf674Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44314ddf674Smrg  else
44414ddf674Smrg
44514ddf674Smrg    # Make a couple of temp file names in the proper directory.
44614ddf674Smrg    dsttmp=$dstdir/_inst.$$_
44714ddf674Smrg    rmtmp=$dstdir/_rm.$$_
44814ddf674Smrg
44914ddf674Smrg    # Trap to clean up those temp files at exit.
45014ddf674Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45114ddf674Smrg
45214ddf674Smrg    # Copy the file name to the temp name.
45314ddf674Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45414ddf674Smrg
45514ddf674Smrg    # and set any options; do chmod last to preserve setuid bits.
45614ddf674Smrg    #
45714ddf674Smrg    # If any of these fail, we abort the whole thing.  If we want to
45814ddf674Smrg    # ignore errors from any of these, just make sure not to ignore
45914ddf674Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46014ddf674Smrg    #
46114ddf674Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46214ddf674Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46314ddf674Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46414ddf674Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46514ddf674Smrg
46614ddf674Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46714ddf674Smrg    if $copy_on_change &&
46814ddf674Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46914ddf674Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47014ddf674Smrg
47114ddf674Smrg       eval "$initialize_posix_glob" &&
47214ddf674Smrg       $posix_glob set -f &&
47314ddf674Smrg       set X $old && old=:$2:$4:$5:$6 &&
47414ddf674Smrg       set X $new && new=:$2:$4:$5:$6 &&
47514ddf674Smrg       $posix_glob set +f &&
47614ddf674Smrg
47714ddf674Smrg       test "$old" = "$new" &&
47814ddf674Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47914ddf674Smrg    then
48014ddf674Smrg      rm -f "$dsttmp"
48114ddf674Smrg    else
48214ddf674Smrg      # Rename the file to the real destination.
48314ddf674Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48414ddf674Smrg
48514ddf674Smrg      # The rename failed, perhaps because mv can't rename something else
48614ddf674Smrg      # to itself, or perhaps because mv is so ancient that it does not
48714ddf674Smrg      # support -f.
48814ddf674Smrg      {
48914ddf674Smrg	# Now remove or move aside any old file at destination location.
49014ddf674Smrg	# We try this two ways since rm can't unlink itself on some
49114ddf674Smrg	# systems and the destination file might be busy for other
49214ddf674Smrg	# reasons.  In this case, the final cleanup might fail but the new
49314ddf674Smrg	# file should still install successfully.
49414ddf674Smrg	{
49514ddf674Smrg	  test ! -f "$dst" ||
49614ddf674Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49714ddf674Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49814ddf674Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49914ddf674Smrg	  } ||
50014ddf674Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50114ddf674Smrg	    (exit 1); exit 1
50214ddf674Smrg	  }
50314ddf674Smrg	} &&
50414ddf674Smrg
50514ddf674Smrg	# Now rename the file to the real destination.
50614ddf674Smrg	$doit $mvcmd "$dsttmp" "$dst"
50714ddf674Smrg      }
50814ddf674Smrg    fi || exit 1
50914ddf674Smrg
51014ddf674Smrg    trap '' 0
51114ddf674Smrg  fi
51214ddf674Smrgdone
513cacd992dSmrg
51414ddf674Smrg# Local variables:
51514ddf674Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51614ddf674Smrg# time-stamp-start: "scriptversion="
51714ddf674Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51814ddf674Smrg# time-stamp-time-zone: "UTC"
51914ddf674Smrg# time-stamp-end: "; # UTC"
52014ddf674Smrg# End:
521