install-sh revision eaa3dbe0
1692f60a7Smrg#!/bin/sh
2692f60a7Smrg# install - install a program, script, or datafile
3692f60a7Smrg
4eaa3dbe0Smrgscriptversion=2011-11-20.07; # UTC
5692f60a7Smrg
6692f60a7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7692f60a7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8692f60a7Smrg# following copyright and license.
9692f60a7Smrg#
10692f60a7Smrg# Copyright (C) 1994 X Consortium
11692f60a7Smrg#
12692f60a7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13692f60a7Smrg# of this software and associated documentation files (the "Software"), to
14692f60a7Smrg# deal in the Software without restriction, including without limitation the
15692f60a7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16692f60a7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17692f60a7Smrg# furnished to do so, subject to the following conditions:
18692f60a7Smrg#
19692f60a7Smrg# The above copyright notice and this permission notice shall be included in
20692f60a7Smrg# all copies or substantial portions of the Software.
21692f60a7Smrg#
22692f60a7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23692f60a7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24692f60a7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25692f60a7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26692f60a7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27692f60a7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28692f60a7Smrg#
29692f60a7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30692f60a7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31692f60a7Smrg# ings in this Software without prior written authorization from the X Consor-
32692f60a7Smrg# tium.
33692f60a7Smrg#
34692f60a7Smrg#
35692f60a7Smrg# FSF changes to this file are in the public domain.
36692f60a7Smrg#
37692f60a7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38eaa3dbe0Smrg# 'make' implicit rules from creating a file called install from it
39692f60a7Smrg# when there is no Makefile.
40692f60a7Smrg#
41692f60a7Smrg# This script is compatible with the BSD install script, but was written
4254569438Smrg# from scratch.
4354569438Smrg
4454569438Smrgnl='
4554569438Smrg'
4654569438SmrgIFS=" ""	$nl"
47692f60a7Smrg
48692f60a7Smrg# set DOITPROG to echo to test this script
49692f60a7Smrg
50692f60a7Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5154569438Smrgdoit=${DOITPROG-}
5254569438Smrgif test -z "$doit"; then
5354569438Smrg  doit_exec=exec
5454569438Smrgelse
5554569438Smrg  doit_exec=$doit
5654569438Smrgfi
57692f60a7Smrg
5854569438Smrg# Put in absolute file names if you don't have them in your path;
5954569438Smrg# or use environment vars.
6054569438Smrg
6154569438Smrgchgrpprog=${CHGRPPROG-chgrp}
6254569438Smrgchmodprog=${CHMODPROG-chmod}
6354569438Smrgchownprog=${CHOWNPROG-chown}
6454569438Smrgcmpprog=${CMPPROG-cmp}
6554569438Smrgcpprog=${CPPROG-cp}
6654569438Smrgmkdirprog=${MKDIRPROG-mkdir}
6754569438Smrgmvprog=${MVPROG-mv}
6854569438Smrgrmprog=${RMPROG-rm}
6954569438Smrgstripprog=${STRIPPROG-strip}
7054569438Smrg
7154569438Smrgposix_glob='?'
7254569438Smrginitialize_posix_glob='
7354569438Smrg  test "$posix_glob" != "?" || {
7454569438Smrg    if (set -f) 2>/dev/null; then
7554569438Smrg      posix_glob=
7654569438Smrg    else
7754569438Smrg      posix_glob=:
7854569438Smrg    fi
7954569438Smrg  }
8054569438Smrg'
81692f60a7Smrg
8254569438Smrgposix_mkdir=
8354569438Smrg
8454569438Smrg# Desired mode of installed file.
8554569438Smrgmode=0755
86692f60a7Smrg
87692f60a7Smrgchgrpcmd=
8854569438Smrgchmodcmd=$chmodprog
8954569438Smrgchowncmd=
9054569438Smrgmvcmd=$mvprog
91692f60a7Smrgrmcmd="$rmprog -f"
9254569438Smrgstripcmd=
9354569438Smrg
94692f60a7Smrgsrc=
95692f60a7Smrgdst=
96692f60a7Smrgdir_arg=
9754569438Smrgdst_arg=
9854569438Smrg
9954569438Smrgcopy_on_change=false
100692f60a7Smrgno_target_directory=
101692f60a7Smrg
10254569438Smrgusage="\
10354569438SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104692f60a7Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105692f60a7Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106692f60a7Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107692f60a7Smrg
108692f60a7SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109692f60a7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110692f60a7SmrgIn the 4th, create DIRECTORIES.
111692f60a7Smrg
112692f60a7SmrgOptions:
11354569438Smrg     --help     display this help and exit.
11454569438Smrg     --version  display version info and exit.
11554569438Smrg
11654569438Smrg  -c            (ignored)
11754569438Smrg  -C            install only if different (preserve the last data modification time)
11854569438Smrg  -d            create directories instead of installing files.
11954569438Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12054569438Smrg  -m MODE       $chmodprog installed files to MODE.
12154569438Smrg  -o USER       $chownprog installed files to USER.
12254569438Smrg  -s            $stripprog installed files.
12354569438Smrg  -t DIRECTORY  install into DIRECTORY.
12454569438Smrg  -T            report an error if DSTFILE is a directory.
125692f60a7Smrg
126692f60a7SmrgEnvironment variables override the default commands:
12754569438Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12854569438Smrg  RMPROG STRIPPROG
129692f60a7Smrg"
130692f60a7Smrg
13154569438Smrgwhile test $# -ne 0; do
132692f60a7Smrg  case $1 in
13354569438Smrg    -c) ;;
13454569438Smrg
13554569438Smrg    -C) copy_on_change=true;;
136692f60a7Smrg
13754569438Smrg    -d) dir_arg=true;;
138692f60a7Smrg
139692f60a7Smrg    -g) chgrpcmd="$chgrpprog $2"
14054569438Smrg	shift;;
141692f60a7Smrg
142692f60a7Smrg    --help) echo "$usage"; exit $?;;
143692f60a7Smrg
14454569438Smrg    -m) mode=$2
14554569438Smrg	case $mode in
14654569438Smrg	  *' '* | *'	'* | *'
14754569438Smrg'*	  | *'*'* | *'?'* | *'['*)
14854569438Smrg	    echo "$0: invalid mode: $mode" >&2
14954569438Smrg	    exit 1;;
15054569438Smrg	esac
15154569438Smrg	shift;;
152692f60a7Smrg
153692f60a7Smrg    -o) chowncmd="$chownprog $2"
15454569438Smrg	shift;;
155692f60a7Smrg
15654569438Smrg    -s) stripcmd=$stripprog;;
157692f60a7Smrg
15854569438Smrg    -t) dst_arg=$2
159eaa3dbe0Smrg	# Protect names problematic for 'test' and other utilities.
1603f6d0e1dSmrg	case $dst_arg in
1613f6d0e1dSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1623f6d0e1dSmrg	esac
16354569438Smrg	shift;;
164692f60a7Smrg
16554569438Smrg    -T) no_target_directory=true;;
166692f60a7Smrg
167692f60a7Smrg    --version) echo "$0 $scriptversion"; exit $?;;
168692f60a7Smrg
16954569438Smrg    --)	shift
170692f60a7Smrg	break;;
17154569438Smrg
17254569438Smrg    -*)	echo "$0: invalid option: $1" >&2
17354569438Smrg	exit 1;;
17454569438Smrg
17554569438Smrg    *)  break;;
176692f60a7Smrg  esac
17754569438Smrg  shift
178692f60a7Smrgdone
179692f60a7Smrg
18054569438Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
18154569438Smrg  # When -d is used, all remaining arguments are directories to create.
18254569438Smrg  # When -t is used, the destination is already specified.
18354569438Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18454569438Smrg  for arg
18554569438Smrg  do
18654569438Smrg    if test -n "$dst_arg"; then
18754569438Smrg      # $@ is not empty: it contains at least $arg.
18854569438Smrg      set fnord "$@" "$dst_arg"
18954569438Smrg      shift # fnord
19054569438Smrg    fi
19154569438Smrg    shift # arg
19254569438Smrg    dst_arg=$arg
193eaa3dbe0Smrg    # Protect names problematic for 'test' and other utilities.
1943f6d0e1dSmrg    case $dst_arg in
1953f6d0e1dSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1963f6d0e1dSmrg    esac
19754569438Smrg  done
19854569438Smrgfi
19954569438Smrg
20054569438Smrgif test $# -eq 0; then
201692f60a7Smrg  if test -z "$dir_arg"; then
202692f60a7Smrg    echo "$0: no input file specified." >&2
203692f60a7Smrg    exit 1
204692f60a7Smrg  fi
205eaa3dbe0Smrg  # It's OK to call 'install-sh -d' without argument.
206692f60a7Smrg  # This can happen when creating conditional directories.
207692f60a7Smrg  exit 0
208692f60a7Smrgfi
209692f60a7Smrg
21054569438Smrgif test -z "$dir_arg"; then
2113f6d0e1dSmrg  do_exit='(exit $ret); exit $ret'
2123f6d0e1dSmrg  trap "ret=129; $do_exit" 1
2133f6d0e1dSmrg  trap "ret=130; $do_exit" 2
2143f6d0e1dSmrg  trap "ret=141; $do_exit" 13
2153f6d0e1dSmrg  trap "ret=143; $do_exit" 15
21654569438Smrg
21754569438Smrg  # Set umask so as not to create temps with too-generous modes.
21854569438Smrg  # However, 'strip' requires both read and write access to temps.
21954569438Smrg  case $mode in
22054569438Smrg    # Optimize common cases.
22154569438Smrg    *644) cp_umask=133;;
22254569438Smrg    *755) cp_umask=22;;
22354569438Smrg
22454569438Smrg    *[0-7])
22554569438Smrg      if test -z "$stripcmd"; then
22654569438Smrg	u_plus_rw=
22754569438Smrg      else
22854569438Smrg	u_plus_rw='% 200'
22954569438Smrg      fi
23054569438Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23154569438Smrg    *)
23254569438Smrg      if test -z "$stripcmd"; then
23354569438Smrg	u_plus_rw=
23454569438Smrg      else
23554569438Smrg	u_plus_rw=,u+rw
23654569438Smrg      fi
23754569438Smrg      cp_umask=$mode$u_plus_rw;;
23854569438Smrg  esac
23954569438Smrgfi
24054569438Smrg
241692f60a7Smrgfor src
242692f60a7Smrgdo
243eaa3dbe0Smrg  # Protect names problematic for 'test' and other utilities.
244692f60a7Smrg  case $src in
2453f6d0e1dSmrg    -* | [=\(\)!]) src=./$src;;
246692f60a7Smrg  esac
247692f60a7Smrg
248692f60a7Smrg  if test -n "$dir_arg"; then
249692f60a7Smrg    dst=$src
25054569438Smrg    dstdir=$dst
25154569438Smrg    test -d "$dstdir"
25254569438Smrg    dstdir_status=$?
253692f60a7Smrg  else
25454569438Smrg
255692f60a7Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256692f60a7Smrg    # might cause directories to be created, which would be especially bad
257692f60a7Smrg    # if $src (and thus $dsttmp) contains '*'.
258692f60a7Smrg    if test ! -f "$src" && test ! -d "$src"; then
259692f60a7Smrg      echo "$0: $src does not exist." >&2
260692f60a7Smrg      exit 1
261692f60a7Smrg    fi
262692f60a7Smrg
26354569438Smrg    if test -z "$dst_arg"; then
264692f60a7Smrg      echo "$0: no destination specified." >&2
265692f60a7Smrg      exit 1
266692f60a7Smrg    fi
26754569438Smrg    dst=$dst_arg
268692f60a7Smrg
269692f60a7Smrg    # If destination is a directory, append the input filename; won't work
270692f60a7Smrg    # if double slashes aren't ignored.
271692f60a7Smrg    if test -d "$dst"; then
272692f60a7Smrg      if test -n "$no_target_directory"; then
27354569438Smrg	echo "$0: $dst_arg: Is a directory" >&2
274692f60a7Smrg	exit 1
275692f60a7Smrg      fi
27654569438Smrg      dstdir=$dst
27754569438Smrg      dst=$dstdir/`basename "$src"`
27854569438Smrg      dstdir_status=0
27954569438Smrg    else
28054569438Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
28154569438Smrg      dstdir=`
28254569438Smrg	(dirname "$dst") 2>/dev/null ||
28354569438Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
28454569438Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
28554569438Smrg	     X"$dst" : 'X\(//\)$' \| \
28654569438Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28754569438Smrg	echo X"$dst" |
28854569438Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28954569438Smrg		   s//\1/
29054569438Smrg		   q
29154569438Smrg		 }
29254569438Smrg		 /^X\(\/\/\)[^/].*/{
29354569438Smrg		   s//\1/
29454569438Smrg		   q
29554569438Smrg		 }
29654569438Smrg		 /^X\(\/\/\)$/{
29754569438Smrg		   s//\1/
29854569438Smrg		   q
29954569438Smrg		 }
30054569438Smrg		 /^X\(\/\).*/{
30154569438Smrg		   s//\1/
30254569438Smrg		   q
30354569438Smrg		 }
30454569438Smrg		 s/.*/./; q'
30554569438Smrg      `
30654569438Smrg
30754569438Smrg      test -d "$dstdir"
30854569438Smrg      dstdir_status=$?
309692f60a7Smrg    fi
310692f60a7Smrg  fi
311692f60a7Smrg
31254569438Smrg  obsolete_mkdir_used=false
31354569438Smrg
31454569438Smrg  if test $dstdir_status != 0; then
31554569438Smrg    case $posix_mkdir in
31654569438Smrg      '')
31754569438Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31854569438Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31954569438Smrg	umask=`umask`
32054569438Smrg	case $stripcmd.$umask in
32154569438Smrg	  # Optimize common cases.
32254569438Smrg	  *[2367][2367]) mkdir_umask=$umask;;
32354569438Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
32454569438Smrg
32554569438Smrg	  *[0-7])
32654569438Smrg	    mkdir_umask=`expr $umask + 22 \
32754569438Smrg	      - $umask % 100 % 40 + $umask % 20 \
32854569438Smrg	      - $umask % 10 % 4 + $umask % 2
32954569438Smrg	    `;;
33054569438Smrg	  *) mkdir_umask=$umask,go-w;;
33154569438Smrg	esac
33254569438Smrg
33354569438Smrg	# With -d, create the new directory with the user-specified mode.
33454569438Smrg	# Otherwise, rely on $mkdir_umask.
33554569438Smrg	if test -n "$dir_arg"; then
33654569438Smrg	  mkdir_mode=-m$mode
33754569438Smrg	else
33854569438Smrg	  mkdir_mode=
33954569438Smrg	fi
34054569438Smrg
34154569438Smrg	posix_mkdir=false
34254569438Smrg	case $umask in
34354569438Smrg	  *[123567][0-7][0-7])
34454569438Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
34554569438Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
34654569438Smrg	    ;;
34754569438Smrg	  *)
34854569438Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34954569438Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
35054569438Smrg
35154569438Smrg	    if (umask $mkdir_umask &&
35254569438Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
35354569438Smrg	    then
35454569438Smrg	      if test -z "$dir_arg" || {
35554569438Smrg		   # Check for POSIX incompatibilities with -m.
35654569438Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357eaa3dbe0Smrg		   # other-writable bit of parent directory when it shouldn't.
35854569438Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35954569438Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
36054569438Smrg		   case $ls_ld_tmpdir in
36154569438Smrg		     d????-?r-*) different_mode=700;;
36254569438Smrg		     d????-?--*) different_mode=755;;
36354569438Smrg		     *) false;;
36454569438Smrg		   esac &&
36554569438Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
36654569438Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36754569438Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36854569438Smrg		   }
36954569438Smrg		 }
37054569438Smrg	      then posix_mkdir=:
37154569438Smrg	      fi
37254569438Smrg	      rmdir "$tmpdir/d" "$tmpdir"
37354569438Smrg	    else
37454569438Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
37554569438Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
37654569438Smrg	    fi
37754569438Smrg	    trap '' 0;;
37854569438Smrg	esac;;
37954569438Smrg    esac
380692f60a7Smrg
38154569438Smrg    if
38254569438Smrg      $posix_mkdir && (
38354569438Smrg	umask $mkdir_umask &&
38454569438Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38554569438Smrg      )
38654569438Smrg    then :
38754569438Smrg    else
388692f60a7Smrg
38954569438Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
39054569438Smrg      # or it failed possibly due to a race condition.  Create the
39154569438Smrg      # directory the slow way, step by step, checking for races as we go.
392692f60a7Smrg
39354569438Smrg      case $dstdir in
39454569438Smrg	/*) prefix='/';;
3953f6d0e1dSmrg	[-=\(\)!]*) prefix='./';;
39654569438Smrg	*)  prefix='';;
39754569438Smrg      esac
398692f60a7Smrg
39954569438Smrg      eval "$initialize_posix_glob"
400692f60a7Smrg
40154569438Smrg      oIFS=$IFS
40254569438Smrg      IFS=/
40354569438Smrg      $posix_glob set -f
40454569438Smrg      set fnord $dstdir
405692f60a7Smrg      shift
40654569438Smrg      $posix_glob set +f
40754569438Smrg      IFS=$oIFS
40854569438Smrg
40954569438Smrg      prefixes=
41054569438Smrg
41154569438Smrg      for d
41254569438Smrg      do
4133f6d0e1dSmrg	test X"$d" = X && continue
41454569438Smrg
41554569438Smrg	prefix=$prefix$d
41654569438Smrg	if test -d "$prefix"; then
41754569438Smrg	  prefixes=
41854569438Smrg	else
41954569438Smrg	  if $posix_mkdir; then
42054569438Smrg	    (umask=$mkdir_umask &&
42154569438Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42254569438Smrg	    # Don't fail if two instances are running concurrently.
42354569438Smrg	    test -d "$prefix" || exit 1
42454569438Smrg	  else
42554569438Smrg	    case $prefix in
42654569438Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42754569438Smrg	      *) qprefix=$prefix;;
42854569438Smrg	    esac
42954569438Smrg	    prefixes="$prefixes '$qprefix'"
43054569438Smrg	  fi
43154569438Smrg	fi
43254569438Smrg	prefix=$prefix/
43354569438Smrg      done
43454569438Smrg
43554569438Smrg      if test -n "$prefixes"; then
43654569438Smrg	# Don't fail if two instances are running concurrently.
43754569438Smrg	(umask $mkdir_umask &&
43854569438Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43954569438Smrg	  test -d "$dstdir" || exit 1
44054569438Smrg	obsolete_mkdir_used=true
441692f60a7Smrg      fi
44254569438Smrg    fi
443692f60a7Smrg  fi
444692f60a7Smrg
445692f60a7Smrg  if test -n "$dir_arg"; then
44654569438Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44754569438Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44854569438Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44954569438Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450692f60a7Smrg  else
451692f60a7Smrg
452692f60a7Smrg    # Make a couple of temp file names in the proper directory.
453692f60a7Smrg    dsttmp=$dstdir/_inst.$$_
454692f60a7Smrg    rmtmp=$dstdir/_rm.$$_
455692f60a7Smrg
456692f60a7Smrg    # Trap to clean up those temp files at exit.
457692f60a7Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458692f60a7Smrg
459692f60a7Smrg    # Copy the file name to the temp name.
46054569438Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461692f60a7Smrg
462692f60a7Smrg    # and set any options; do chmod last to preserve setuid bits.
463692f60a7Smrg    #
464692f60a7Smrg    # If any of these fail, we abort the whole thing.  If we want to
465692f60a7Smrg    # ignore errors from any of these, just make sure not to ignore
466692f60a7Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467692f60a7Smrg    #
46854569438Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46954569438Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
47054569438Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
47154569438Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
47254569438Smrg
47354569438Smrg    # If -C, don't bother to copy if it wouldn't change the file.
47454569438Smrg    if $copy_on_change &&
47554569438Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
47654569438Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47754569438Smrg
47854569438Smrg       eval "$initialize_posix_glob" &&
47954569438Smrg       $posix_glob set -f &&
48054569438Smrg       set X $old && old=:$2:$4:$5:$6 &&
48154569438Smrg       set X $new && new=:$2:$4:$5:$6 &&
48254569438Smrg       $posix_glob set +f &&
48354569438Smrg
48454569438Smrg       test "$old" = "$new" &&
48554569438Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
48654569438Smrg    then
48754569438Smrg      rm -f "$dsttmp"
48854569438Smrg    else
48954569438Smrg      # Rename the file to the real destination.
49054569438Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
49154569438Smrg
49254569438Smrg      # The rename failed, perhaps because mv can't rename something else
49354569438Smrg      # to itself, or perhaps because mv is so ancient that it does not
49454569438Smrg      # support -f.
49554569438Smrg      {
49654569438Smrg	# Now remove or move aside any old file at destination location.
49754569438Smrg	# We try this two ways since rm can't unlink itself on some
49854569438Smrg	# systems and the destination file might be busy for other
49954569438Smrg	# reasons.  In this case, the final cleanup might fail but the new
50054569438Smrg	# file should still install successfully.
50154569438Smrg	{
50254569438Smrg	  test ! -f "$dst" ||
50354569438Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
50454569438Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
50554569438Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
50654569438Smrg	  } ||
50754569438Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50854569438Smrg	    (exit 1); exit 1
50954569438Smrg	  }
51054569438Smrg	} &&
51154569438Smrg
51254569438Smrg	# Now rename the file to the real destination.
51354569438Smrg	$doit $mvcmd "$dsttmp" "$dst"
51454569438Smrg      }
51554569438Smrg    fi || exit 1
51654569438Smrg
51754569438Smrg    trap '' 0
51854569438Smrg  fi
519692f60a7Smrgdone
520692f60a7Smrg
521692f60a7Smrg# Local variables:
522692f60a7Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523692f60a7Smrg# time-stamp-start: "scriptversion="
524692f60a7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5253f6d0e1dSmrg# time-stamp-time-zone: "UTC"
5263f6d0e1dSmrg# time-stamp-end: "; # UTC"
527692f60a7Smrg# End:
528