install-sh revision dd77ae96
172b4363aSmrg#!/bin/sh
272b4363aSmrg# install - install a program, script, or datafile
372b4363aSmrg
4dd77ae96Smrgscriptversion=2009-04-28.21; # UTC
572b4363aSmrg
672b4363aSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
772b4363aSmrg# later released in X11R6 (xc/config/util/install.sh) with the
872b4363aSmrg# following copyright and license.
972b4363aSmrg#
1072b4363aSmrg# Copyright (C) 1994 X Consortium
1172b4363aSmrg#
1272b4363aSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1372b4363aSmrg# of this software and associated documentation files (the "Software"), to
1472b4363aSmrg# deal in the Software without restriction, including without limitation the
1572b4363aSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1672b4363aSmrg# sell copies of the Software, and to permit persons to whom the Software is
1772b4363aSmrg# furnished to do so, subject to the following conditions:
1872b4363aSmrg#
1972b4363aSmrg# The above copyright notice and this permission notice shall be included in
2072b4363aSmrg# all copies or substantial portions of the Software.
2172b4363aSmrg#
2272b4363aSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2372b4363aSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2472b4363aSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2572b4363aSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2672b4363aSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2772b4363aSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2872b4363aSmrg#
2972b4363aSmrg# Except as contained in this notice, the name of the X Consortium shall not
3072b4363aSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3172b4363aSmrg# ings in this Software without prior written authorization from the X Consor-
3272b4363aSmrg# tium.
3372b4363aSmrg#
3472b4363aSmrg#
3572b4363aSmrg# FSF changes to this file are in the public domain.
3672b4363aSmrg#
3772b4363aSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3872b4363aSmrg# `make' implicit rules from creating a file called install from it
3972b4363aSmrg# when there is no Makefile.
4072b4363aSmrg#
4172b4363aSmrg# This script is compatible with the BSD install script, but was written
4272b4363aSmrg# from scratch.
4372b4363aSmrg
4472b4363aSmrgnl='
4572b4363aSmrg'
4672b4363aSmrgIFS=" ""	$nl"
4772b4363aSmrg
4872b4363aSmrg# set DOITPROG to echo to test this script
4972b4363aSmrg
5072b4363aSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51dd77ae96Smrgdoit=${DOITPROG-}
5272b4363aSmrgif test -z "$doit"; then
5372b4363aSmrg  doit_exec=exec
5472b4363aSmrgelse
5572b4363aSmrg  doit_exec=$doit
5672b4363aSmrgfi
5772b4363aSmrg
5872b4363aSmrg# Put in absolute file names if you don't have them in your path;
5972b4363aSmrg# or use environment vars.
6072b4363aSmrg
61dd77ae96Smrgchgrpprog=${CHGRPPROG-chgrp}
62dd77ae96Smrgchmodprog=${CHMODPROG-chmod}
63dd77ae96Smrgchownprog=${CHOWNPROG-chown}
64dd77ae96Smrgcmpprog=${CMPPROG-cmp}
65dd77ae96Smrgcpprog=${CPPROG-cp}
66dd77ae96Smrgmkdirprog=${MKDIRPROG-mkdir}
67dd77ae96Smrgmvprog=${MVPROG-mv}
68dd77ae96Smrgrmprog=${RMPROG-rm}
69dd77ae96Smrgstripprog=${STRIPPROG-strip}
70dd77ae96Smrg
71dd77ae96Smrgposix_glob='?'
72dd77ae96Smrginitialize_posix_glob='
73dd77ae96Smrg  test "$posix_glob" != "?" || {
74dd77ae96Smrg    if (set -f) 2>/dev/null; then
75dd77ae96Smrg      posix_glob=
76dd77ae96Smrg    else
77dd77ae96Smrg      posix_glob=:
78dd77ae96Smrg    fi
79dd77ae96Smrg  }
80dd77ae96Smrg'
8172b4363aSmrg
8272b4363aSmrgposix_mkdir=
8372b4363aSmrg
8472b4363aSmrg# Desired mode of installed file.
8572b4363aSmrgmode=0755
8672b4363aSmrg
87dd77ae96Smrgchgrpcmd=
8872b4363aSmrgchmodcmd=$chmodprog
8972b4363aSmrgchowncmd=
90dd77ae96Smrgmvcmd=$mvprog
9172b4363aSmrgrmcmd="$rmprog -f"
92dd77ae96Smrgstripcmd=
93dd77ae96Smrg
9472b4363aSmrgsrc=
9572b4363aSmrgdst=
9672b4363aSmrgdir_arg=
97dd77ae96Smrgdst_arg=
98dd77ae96Smrg
99dd77ae96Smrgcopy_on_change=false
10072b4363aSmrgno_target_directory=
10172b4363aSmrg
102dd77ae96Smrgusage="\
103dd77ae96SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10472b4363aSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10572b4363aSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10672b4363aSmrg   or: $0 [OPTION]... -d DIRECTORIES...
10772b4363aSmrg
10872b4363aSmrgIn the 1st form, copy SRCFILE to DSTFILE.
10972b4363aSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11072b4363aSmrgIn the 4th, create DIRECTORIES.
11172b4363aSmrg
11272b4363aSmrgOptions:
113dd77ae96Smrg     --help     display this help and exit.
114dd77ae96Smrg     --version  display version info and exit.
115dd77ae96Smrg
116dd77ae96Smrg  -c            (ignored)
117dd77ae96Smrg  -C            install only if different (preserve the last data modification time)
118dd77ae96Smrg  -d            create directories instead of installing files.
119dd77ae96Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120dd77ae96Smrg  -m MODE       $chmodprog installed files to MODE.
121dd77ae96Smrg  -o USER       $chownprog installed files to USER.
122dd77ae96Smrg  -s            $stripprog installed files.
123dd77ae96Smrg  -t DIRECTORY  install into DIRECTORY.
124dd77ae96Smrg  -T            report an error if DSTFILE is a directory.
12572b4363aSmrg
12672b4363aSmrgEnvironment variables override the default commands:
127dd77ae96Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128dd77ae96Smrg  RMPROG STRIPPROG
12972b4363aSmrg"
13072b4363aSmrg
13172b4363aSmrgwhile test $# -ne 0; do
13272b4363aSmrg  case $1 in
133dd77ae96Smrg    -c) ;;
134dd77ae96Smrg
135dd77ae96Smrg    -C) copy_on_change=true;;
13672b4363aSmrg
137dd77ae96Smrg    -d) dir_arg=true;;
13872b4363aSmrg
13972b4363aSmrg    -g) chgrpcmd="$chgrpprog $2"
140dd77ae96Smrg	shift;;
14172b4363aSmrg
14272b4363aSmrg    --help) echo "$usage"; exit $?;;
14372b4363aSmrg
14472b4363aSmrg    -m) mode=$2
14572b4363aSmrg	case $mode in
14672b4363aSmrg	  *' '* | *'	'* | *'
14772b4363aSmrg'*	  | *'*'* | *'?'* | *'['*)
14872b4363aSmrg	    echo "$0: invalid mode: $mode" >&2
14972b4363aSmrg	    exit 1;;
15072b4363aSmrg	esac
151dd77ae96Smrg	shift;;
15272b4363aSmrg
15372b4363aSmrg    -o) chowncmd="$chownprog $2"
154dd77ae96Smrg	shift;;
15572b4363aSmrg
156dd77ae96Smrg    -s) stripcmd=$stripprog;;
15772b4363aSmrg
158dd77ae96Smrg    -t) dst_arg=$2
159dd77ae96Smrg	shift;;
16072b4363aSmrg
161dd77ae96Smrg    -T) no_target_directory=true;;
16272b4363aSmrg
16372b4363aSmrg    --version) echo "$0 $scriptversion"; exit $?;;
16472b4363aSmrg
16572b4363aSmrg    --)	shift
16672b4363aSmrg	break;;
16772b4363aSmrg
16872b4363aSmrg    -*)	echo "$0: invalid option: $1" >&2
16972b4363aSmrg	exit 1;;
17072b4363aSmrg
17172b4363aSmrg    *)  break;;
17272b4363aSmrg  esac
173dd77ae96Smrg  shift
17472b4363aSmrgdone
17572b4363aSmrg
176dd77ae96Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17772b4363aSmrg  # When -d is used, all remaining arguments are directories to create.
17872b4363aSmrg  # When -t is used, the destination is already specified.
17972b4363aSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18072b4363aSmrg  for arg
18172b4363aSmrg  do
182dd77ae96Smrg    if test -n "$dst_arg"; then
18372b4363aSmrg      # $@ is not empty: it contains at least $arg.
184dd77ae96Smrg      set fnord "$@" "$dst_arg"
18572b4363aSmrg      shift # fnord
18672b4363aSmrg    fi
18772b4363aSmrg    shift # arg
188dd77ae96Smrg    dst_arg=$arg
18972b4363aSmrg  done
19072b4363aSmrgfi
19172b4363aSmrg
19272b4363aSmrgif test $# -eq 0; then
19372b4363aSmrg  if test -z "$dir_arg"; then
19472b4363aSmrg    echo "$0: no input file specified." >&2
19572b4363aSmrg    exit 1
19672b4363aSmrg  fi
19772b4363aSmrg  # It's OK to call `install-sh -d' without argument.
19872b4363aSmrg  # This can happen when creating conditional directories.
19972b4363aSmrg  exit 0
20072b4363aSmrgfi
20172b4363aSmrg
20272b4363aSmrgif test -z "$dir_arg"; then
20372b4363aSmrg  trap '(exit $?); exit' 1 2 13 15
20472b4363aSmrg
20572b4363aSmrg  # Set umask so as not to create temps with too-generous modes.
20672b4363aSmrg  # However, 'strip' requires both read and write access to temps.
20772b4363aSmrg  case $mode in
20872b4363aSmrg    # Optimize common cases.
20972b4363aSmrg    *644) cp_umask=133;;
21072b4363aSmrg    *755) cp_umask=22;;
21172b4363aSmrg
21272b4363aSmrg    *[0-7])
21372b4363aSmrg      if test -z "$stripcmd"; then
21472b4363aSmrg	u_plus_rw=
21572b4363aSmrg      else
21672b4363aSmrg	u_plus_rw='% 200'
21772b4363aSmrg      fi
21872b4363aSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21972b4363aSmrg    *)
22072b4363aSmrg      if test -z "$stripcmd"; then
22172b4363aSmrg	u_plus_rw=
22272b4363aSmrg      else
22372b4363aSmrg	u_plus_rw=,u+rw
22472b4363aSmrg      fi
22572b4363aSmrg      cp_umask=$mode$u_plus_rw;;
22672b4363aSmrg  esac
22772b4363aSmrgfi
22872b4363aSmrg
22972b4363aSmrgfor src
23072b4363aSmrgdo
23172b4363aSmrg  # Protect names starting with `-'.
23272b4363aSmrg  case $src in
233dd77ae96Smrg    -*) src=./$src;;
23472b4363aSmrg  esac
23572b4363aSmrg
23672b4363aSmrg  if test -n "$dir_arg"; then
23772b4363aSmrg    dst=$src
23872b4363aSmrg    dstdir=$dst
23972b4363aSmrg    test -d "$dstdir"
24072b4363aSmrg    dstdir_status=$?
24172b4363aSmrg  else
24272b4363aSmrg
24372b4363aSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24472b4363aSmrg    # might cause directories to be created, which would be especially bad
24572b4363aSmrg    # if $src (and thus $dsttmp) contains '*'.
24672b4363aSmrg    if test ! -f "$src" && test ! -d "$src"; then
24772b4363aSmrg      echo "$0: $src does not exist." >&2
24872b4363aSmrg      exit 1
24972b4363aSmrg    fi
25072b4363aSmrg
251dd77ae96Smrg    if test -z "$dst_arg"; then
25272b4363aSmrg      echo "$0: no destination specified." >&2
25372b4363aSmrg      exit 1
25472b4363aSmrg    fi
25572b4363aSmrg
256dd77ae96Smrg    dst=$dst_arg
25772b4363aSmrg    # Protect names starting with `-'.
25872b4363aSmrg    case $dst in
259dd77ae96Smrg      -*) dst=./$dst;;
26072b4363aSmrg    esac
26172b4363aSmrg
26272b4363aSmrg    # If destination is a directory, append the input filename; won't work
26372b4363aSmrg    # if double slashes aren't ignored.
26472b4363aSmrg    if test -d "$dst"; then
26572b4363aSmrg      if test -n "$no_target_directory"; then
266dd77ae96Smrg	echo "$0: $dst_arg: Is a directory" >&2
26772b4363aSmrg	exit 1
26872b4363aSmrg      fi
26972b4363aSmrg      dstdir=$dst
27072b4363aSmrg      dst=$dstdir/`basename "$src"`
27172b4363aSmrg      dstdir_status=0
27272b4363aSmrg    else
27372b4363aSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27472b4363aSmrg      dstdir=`
27572b4363aSmrg	(dirname "$dst") 2>/dev/null ||
27672b4363aSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27772b4363aSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
27872b4363aSmrg	     X"$dst" : 'X\(//\)$' \| \
27972b4363aSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28072b4363aSmrg	echo X"$dst" |
28172b4363aSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28272b4363aSmrg		   s//\1/
28372b4363aSmrg		   q
28472b4363aSmrg		 }
28572b4363aSmrg		 /^X\(\/\/\)[^/].*/{
28672b4363aSmrg		   s//\1/
28772b4363aSmrg		   q
28872b4363aSmrg		 }
28972b4363aSmrg		 /^X\(\/\/\)$/{
29072b4363aSmrg		   s//\1/
29172b4363aSmrg		   q
29272b4363aSmrg		 }
29372b4363aSmrg		 /^X\(\/\).*/{
29472b4363aSmrg		   s//\1/
29572b4363aSmrg		   q
29672b4363aSmrg		 }
29772b4363aSmrg		 s/.*/./; q'
29872b4363aSmrg      `
29972b4363aSmrg
30072b4363aSmrg      test -d "$dstdir"
30172b4363aSmrg      dstdir_status=$?
30272b4363aSmrg    fi
30372b4363aSmrg  fi
30472b4363aSmrg
30572b4363aSmrg  obsolete_mkdir_used=false
30672b4363aSmrg
30772b4363aSmrg  if test $dstdir_status != 0; then
30872b4363aSmrg    case $posix_mkdir in
30972b4363aSmrg      '')
31072b4363aSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
31172b4363aSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
31272b4363aSmrg	umask=`umask`
31372b4363aSmrg	case $stripcmd.$umask in
31472b4363aSmrg	  # Optimize common cases.
31572b4363aSmrg	  *[2367][2367]) mkdir_umask=$umask;;
31672b4363aSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31772b4363aSmrg
31872b4363aSmrg	  *[0-7])
31972b4363aSmrg	    mkdir_umask=`expr $umask + 22 \
32072b4363aSmrg	      - $umask % 100 % 40 + $umask % 20 \
32172b4363aSmrg	      - $umask % 10 % 4 + $umask % 2
32272b4363aSmrg	    `;;
32372b4363aSmrg	  *) mkdir_umask=$umask,go-w;;
32472b4363aSmrg	esac
32572b4363aSmrg
32672b4363aSmrg	# With -d, create the new directory with the user-specified mode.
32772b4363aSmrg	# Otherwise, rely on $mkdir_umask.
32872b4363aSmrg	if test -n "$dir_arg"; then
32972b4363aSmrg	  mkdir_mode=-m$mode
33072b4363aSmrg	else
33172b4363aSmrg	  mkdir_mode=
33272b4363aSmrg	fi
33372b4363aSmrg
33472b4363aSmrg	posix_mkdir=false
33572b4363aSmrg	case $umask in
33672b4363aSmrg	  *[123567][0-7][0-7])
33772b4363aSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33872b4363aSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33972b4363aSmrg	    ;;
34072b4363aSmrg	  *)
34172b4363aSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34272b4363aSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34372b4363aSmrg
34472b4363aSmrg	    if (umask $mkdir_umask &&
34572b4363aSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34672b4363aSmrg	    then
34772b4363aSmrg	      if test -z "$dir_arg" || {
34872b4363aSmrg		   # Check for POSIX incompatibilities with -m.
34972b4363aSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35072b4363aSmrg		   # other-writeable bit of parent directory when it shouldn't.
35172b4363aSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35272b4363aSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35372b4363aSmrg		   case $ls_ld_tmpdir in
35472b4363aSmrg		     d????-?r-*) different_mode=700;;
35572b4363aSmrg		     d????-?--*) different_mode=755;;
35672b4363aSmrg		     *) false;;
35772b4363aSmrg		   esac &&
35872b4363aSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35972b4363aSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36072b4363aSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36172b4363aSmrg		   }
36272b4363aSmrg		 }
36372b4363aSmrg	      then posix_mkdir=:
36472b4363aSmrg	      fi
36572b4363aSmrg	      rmdir "$tmpdir/d" "$tmpdir"
36672b4363aSmrg	    else
36772b4363aSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
36872b4363aSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36972b4363aSmrg	    fi
37072b4363aSmrg	    trap '' 0;;
37172b4363aSmrg	esac;;
37272b4363aSmrg    esac
37372b4363aSmrg
37472b4363aSmrg    if
37572b4363aSmrg      $posix_mkdir && (
37672b4363aSmrg	umask $mkdir_umask &&
37772b4363aSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37872b4363aSmrg      )
37972b4363aSmrg    then :
38072b4363aSmrg    else
38172b4363aSmrg
38272b4363aSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38372b4363aSmrg      # or it failed possibly due to a race condition.  Create the
38472b4363aSmrg      # directory the slow way, step by step, checking for races as we go.
38572b4363aSmrg
38672b4363aSmrg      case $dstdir in
387dd77ae96Smrg	/*) prefix='/';;
388dd77ae96Smrg	-*) prefix='./';;
389dd77ae96Smrg	*)  prefix='';;
39072b4363aSmrg      esac
39172b4363aSmrg
392dd77ae96Smrg      eval "$initialize_posix_glob"
39372b4363aSmrg
39472b4363aSmrg      oIFS=$IFS
39572b4363aSmrg      IFS=/
396dd77ae96Smrg      $posix_glob set -f
39772b4363aSmrg      set fnord $dstdir
39872b4363aSmrg      shift
399dd77ae96Smrg      $posix_glob set +f
40072b4363aSmrg      IFS=$oIFS
40172b4363aSmrg
40272b4363aSmrg      prefixes=
40372b4363aSmrg
40472b4363aSmrg      for d
40572b4363aSmrg      do
40672b4363aSmrg	test -z "$d" && continue
40772b4363aSmrg
40872b4363aSmrg	prefix=$prefix$d
40972b4363aSmrg	if test -d "$prefix"; then
41072b4363aSmrg	  prefixes=
41172b4363aSmrg	else
41272b4363aSmrg	  if $posix_mkdir; then
41372b4363aSmrg	    (umask=$mkdir_umask &&
41472b4363aSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41572b4363aSmrg	    # Don't fail if two instances are running concurrently.
41672b4363aSmrg	    test -d "$prefix" || exit 1
41772b4363aSmrg	  else
41872b4363aSmrg	    case $prefix in
41972b4363aSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42072b4363aSmrg	      *) qprefix=$prefix;;
42172b4363aSmrg	    esac
42272b4363aSmrg	    prefixes="$prefixes '$qprefix'"
42372b4363aSmrg	  fi
42472b4363aSmrg	fi
42572b4363aSmrg	prefix=$prefix/
42672b4363aSmrg      done
42772b4363aSmrg
42872b4363aSmrg      if test -n "$prefixes"; then
42972b4363aSmrg	# Don't fail if two instances are running concurrently.
43072b4363aSmrg	(umask $mkdir_umask &&
43172b4363aSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43272b4363aSmrg	  test -d "$dstdir" || exit 1
43372b4363aSmrg	obsolete_mkdir_used=true
43472b4363aSmrg      fi
43572b4363aSmrg    fi
43672b4363aSmrg  fi
43772b4363aSmrg
43872b4363aSmrg  if test -n "$dir_arg"; then
43972b4363aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44072b4363aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44172b4363aSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44272b4363aSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44372b4363aSmrg  else
44472b4363aSmrg
44572b4363aSmrg    # Make a couple of temp file names in the proper directory.
44672b4363aSmrg    dsttmp=$dstdir/_inst.$$_
44772b4363aSmrg    rmtmp=$dstdir/_rm.$$_
44872b4363aSmrg
44972b4363aSmrg    # Trap to clean up those temp files at exit.
45072b4363aSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45172b4363aSmrg
45272b4363aSmrg    # Copy the file name to the temp name.
45372b4363aSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45472b4363aSmrg
45572b4363aSmrg    # and set any options; do chmod last to preserve setuid bits.
45672b4363aSmrg    #
45772b4363aSmrg    # If any of these fail, we abort the whole thing.  If we want to
45872b4363aSmrg    # ignore errors from any of these, just make sure not to ignore
45972b4363aSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46072b4363aSmrg    #
461dd77ae96Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462dd77ae96Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463dd77ae96Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464dd77ae96Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465dd77ae96Smrg
466dd77ae96Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467dd77ae96Smrg    if $copy_on_change &&
468dd77ae96Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469dd77ae96Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470dd77ae96Smrg
471dd77ae96Smrg       eval "$initialize_posix_glob" &&
472dd77ae96Smrg       $posix_glob set -f &&
473dd77ae96Smrg       set X $old && old=:$2:$4:$5:$6 &&
474dd77ae96Smrg       set X $new && new=:$2:$4:$5:$6 &&
475dd77ae96Smrg       $posix_glob set +f &&
476dd77ae96Smrg
477dd77ae96Smrg       test "$old" = "$new" &&
478dd77ae96Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479dd77ae96Smrg    then
480dd77ae96Smrg      rm -f "$dsttmp"
481dd77ae96Smrg    else
482dd77ae96Smrg      # Rename the file to the real destination.
483dd77ae96Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484dd77ae96Smrg
485dd77ae96Smrg      # The rename failed, perhaps because mv can't rename something else
486dd77ae96Smrg      # to itself, or perhaps because mv is so ancient that it does not
487dd77ae96Smrg      # support -f.
488dd77ae96Smrg      {
489dd77ae96Smrg	# Now remove or move aside any old file at destination location.
490dd77ae96Smrg	# We try this two ways since rm can't unlink itself on some
491dd77ae96Smrg	# systems and the destination file might be busy for other
492dd77ae96Smrg	# reasons.  In this case, the final cleanup might fail but the new
493dd77ae96Smrg	# file should still install successfully.
494dd77ae96Smrg	{
495dd77ae96Smrg	  test ! -f "$dst" ||
496dd77ae96Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497dd77ae96Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498dd77ae96Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499dd77ae96Smrg	  } ||
500dd77ae96Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
501dd77ae96Smrg	    (exit 1); exit 1
502dd77ae96Smrg	  }
503dd77ae96Smrg	} &&
504dd77ae96Smrg
505dd77ae96Smrg	# Now rename the file to the real destination.
506dd77ae96Smrg	$doit $mvcmd "$dsttmp" "$dst"
507dd77ae96Smrg      }
508dd77ae96Smrg    fi || exit 1
50972b4363aSmrg
51072b4363aSmrg    trap '' 0
51172b4363aSmrg  fi
51272b4363aSmrgdone
51372b4363aSmrg
51472b4363aSmrg# Local variables:
51572b4363aSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51672b4363aSmrg# time-stamp-start: "scriptversion="
51772b4363aSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518dd77ae96Smrg# time-stamp-time-zone: "UTC"
519dd77ae96Smrg# time-stamp-end: "; # UTC"
52072b4363aSmrg# End:
521