install-sh revision a9274bcd
166fe65f6Smrg#!/bin/sh
266fe65f6Smrg# install - install a program, script, or datafile
366fe65f6Smrg
4a9274bcdSmrgscriptversion=2009-04-28.21; # UTC
566fe65f6Smrg
666fe65f6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
766fe65f6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
866fe65f6Smrg# following copyright and license.
966fe65f6Smrg#
1066fe65f6Smrg# Copyright (C) 1994 X Consortium
1166fe65f6Smrg#
1266fe65f6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1366fe65f6Smrg# of this software and associated documentation files (the "Software"), to
1466fe65f6Smrg# deal in the Software without restriction, including without limitation the
1566fe65f6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1666fe65f6Smrg# sell copies of the Software, and to permit persons to whom the Software is
1766fe65f6Smrg# furnished to do so, subject to the following conditions:
1866fe65f6Smrg#
1966fe65f6Smrg# The above copyright notice and this permission notice shall be included in
2066fe65f6Smrg# all copies or substantial portions of the Software.
2166fe65f6Smrg#
2266fe65f6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2366fe65f6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2466fe65f6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2566fe65f6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2666fe65f6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2766fe65f6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2866fe65f6Smrg#
2966fe65f6Smrg# Except as contained in this notice, the name of the X Consortium shall not
3066fe65f6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3166fe65f6Smrg# ings in this Software without prior written authorization from the X Consor-
3266fe65f6Smrg# tium.
3366fe65f6Smrg#
3466fe65f6Smrg#
3566fe65f6Smrg# FSF changes to this file are in the public domain.
3666fe65f6Smrg#
3766fe65f6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3866fe65f6Smrg# `make' implicit rules from creating a file called install from it
3966fe65f6Smrg# when there is no Makefile.
4066fe65f6Smrg#
4166fe65f6Smrg# This script is compatible with the BSD install script, but was written
4289d09728Smrg# from scratch.
4389d09728Smrg
4489d09728Smrgnl='
4589d09728Smrg'
4689d09728SmrgIFS=" ""	$nl"
4766fe65f6Smrg
4866fe65f6Smrg# set DOITPROG to echo to test this script
4966fe65f6Smrg
5066fe65f6Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5189d09728Smrgdoit=${DOITPROG-}
5289d09728Smrgif test -z "$doit"; then
5389d09728Smrg  doit_exec=exec
5489d09728Smrgelse
5589d09728Smrg  doit_exec=$doit
5689d09728Smrgfi
5766fe65f6Smrg
5889d09728Smrg# Put in absolute file names if you don't have them in your path;
5989d09728Smrg# or use environment vars.
6089d09728Smrg
6189d09728Smrgchgrpprog=${CHGRPPROG-chgrp}
6289d09728Smrgchmodprog=${CHMODPROG-chmod}
6389d09728Smrgchownprog=${CHOWNPROG-chown}
6489d09728Smrgcmpprog=${CMPPROG-cmp}
6589d09728Smrgcpprog=${CPPROG-cp}
6689d09728Smrgmkdirprog=${MKDIRPROG-mkdir}
6789d09728Smrgmvprog=${MVPROG-mv}
6889d09728Smrgrmprog=${RMPROG-rm}
6989d09728Smrgstripprog=${STRIPPROG-strip}
7089d09728Smrg
7189d09728Smrgposix_glob='?'
7289d09728Smrginitialize_posix_glob='
7389d09728Smrg  test "$posix_glob" != "?" || {
7489d09728Smrg    if (set -f) 2>/dev/null; then
7589d09728Smrg      posix_glob=
7689d09728Smrg    else
7789d09728Smrg      posix_glob=:
7889d09728Smrg    fi
7989d09728Smrg  }
8089d09728Smrg'
8166fe65f6Smrg
8289d09728Smrgposix_mkdir=
8389d09728Smrg
8489d09728Smrg# Desired mode of installed file.
8589d09728Smrgmode=0755
8666fe65f6Smrg
8766fe65f6Smrgchgrpcmd=
8889d09728Smrgchmodcmd=$chmodprog
8989d09728Smrgchowncmd=
9089d09728Smrgmvcmd=$mvprog
9166fe65f6Smrgrmcmd="$rmprog -f"
9289d09728Smrgstripcmd=
9389d09728Smrg
9466fe65f6Smrgsrc=
9566fe65f6Smrgdst=
9666fe65f6Smrgdir_arg=
9789d09728Smrgdst_arg=
9889d09728Smrg
9989d09728Smrgcopy_on_change=false
10066fe65f6Smrgno_target_directory=
10166fe65f6Smrg
10289d09728Smrgusage="\
10389d09728SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10466fe65f6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10566fe65f6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10666fe65f6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
10766fe65f6Smrg
10866fe65f6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10966fe65f6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11066fe65f6SmrgIn the 4th, create DIRECTORIES.
11166fe65f6Smrg
11266fe65f6SmrgOptions:
11389d09728Smrg     --help     display this help and exit.
11489d09728Smrg     --version  display version info and exit.
11589d09728Smrg
11689d09728Smrg  -c            (ignored)
11789d09728Smrg  -C            install only if different (preserve the last data modification time)
11889d09728Smrg  -d            create directories instead of installing files.
11989d09728Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12089d09728Smrg  -m MODE       $chmodprog installed files to MODE.
12189d09728Smrg  -o USER       $chownprog installed files to USER.
12289d09728Smrg  -s            $stripprog installed files.
12389d09728Smrg  -t DIRECTORY  install into DIRECTORY.
12489d09728Smrg  -T            report an error if DSTFILE is a directory.
12566fe65f6Smrg
12666fe65f6SmrgEnvironment variables override the default commands:
12789d09728Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12889d09728Smrg  RMPROG STRIPPROG
12966fe65f6Smrg"
13066fe65f6Smrg
13189d09728Smrgwhile test $# -ne 0; do
13266fe65f6Smrg  case $1 in
13389d09728Smrg    -c) ;;
13489d09728Smrg
13589d09728Smrg    -C) copy_on_change=true;;
13666fe65f6Smrg
13789d09728Smrg    -d) dir_arg=true;;
13866fe65f6Smrg
13966fe65f6Smrg    -g) chgrpcmd="$chgrpprog $2"
14089d09728Smrg	shift;;
14166fe65f6Smrg
14266fe65f6Smrg    --help) echo "$usage"; exit $?;;
14366fe65f6Smrg
14489d09728Smrg    -m) mode=$2
14589d09728Smrg	case $mode in
14689d09728Smrg	  *' '* | *'	'* | *'
14789d09728Smrg'*	  | *'*'* | *'?'* | *'['*)
14889d09728Smrg	    echo "$0: invalid mode: $mode" >&2
14989d09728Smrg	    exit 1;;
15089d09728Smrg	esac
15189d09728Smrg	shift;;
15266fe65f6Smrg
15366fe65f6Smrg    -o) chowncmd="$chownprog $2"
15489d09728Smrg	shift;;
15566fe65f6Smrg
15689d09728Smrg    -s) stripcmd=$stripprog;;
15766fe65f6Smrg
15889d09728Smrg    -t) dst_arg=$2
15989d09728Smrg	shift;;
16066fe65f6Smrg
16189d09728Smrg    -T) no_target_directory=true;;
16266fe65f6Smrg
16366fe65f6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
16466fe65f6Smrg
16589d09728Smrg    --)	shift
16666fe65f6Smrg	break;;
16789d09728Smrg
16889d09728Smrg    -*)	echo "$0: invalid option: $1" >&2
16989d09728Smrg	exit 1;;
17089d09728Smrg
17189d09728Smrg    *)  break;;
17266fe65f6Smrg  esac
17389d09728Smrg  shift
17466fe65f6Smrgdone
17566fe65f6Smrg
17689d09728Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17789d09728Smrg  # When -d is used, all remaining arguments are directories to create.
17889d09728Smrg  # When -t is used, the destination is already specified.
17989d09728Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18089d09728Smrg  for arg
18189d09728Smrg  do
18289d09728Smrg    if test -n "$dst_arg"; then
18389d09728Smrg      # $@ is not empty: it contains at least $arg.
18489d09728Smrg      set fnord "$@" "$dst_arg"
18589d09728Smrg      shift # fnord
18689d09728Smrg    fi
18789d09728Smrg    shift # arg
18889d09728Smrg    dst_arg=$arg
18989d09728Smrg  done
19089d09728Smrgfi
19189d09728Smrg
19289d09728Smrgif test $# -eq 0; then
19366fe65f6Smrg  if test -z "$dir_arg"; then
19466fe65f6Smrg    echo "$0: no input file specified." >&2
19566fe65f6Smrg    exit 1
19666fe65f6Smrg  fi
19766fe65f6Smrg  # It's OK to call `install-sh -d' without argument.
19866fe65f6Smrg  # This can happen when creating conditional directories.
19966fe65f6Smrg  exit 0
20066fe65f6Smrgfi
20166fe65f6Smrg
20289d09728Smrgif test -z "$dir_arg"; then
20389d09728Smrg  trap '(exit $?); exit' 1 2 13 15
20489d09728Smrg
20589d09728Smrg  # Set umask so as not to create temps with too-generous modes.
20689d09728Smrg  # However, 'strip' requires both read and write access to temps.
20789d09728Smrg  case $mode in
20889d09728Smrg    # Optimize common cases.
20989d09728Smrg    *644) cp_umask=133;;
21089d09728Smrg    *755) cp_umask=22;;
21189d09728Smrg
21289d09728Smrg    *[0-7])
21389d09728Smrg      if test -z "$stripcmd"; then
21489d09728Smrg	u_plus_rw=
21589d09728Smrg      else
21689d09728Smrg	u_plus_rw='% 200'
21789d09728Smrg      fi
21889d09728Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21989d09728Smrg    *)
22089d09728Smrg      if test -z "$stripcmd"; then
22189d09728Smrg	u_plus_rw=
22289d09728Smrg      else
22389d09728Smrg	u_plus_rw=,u+rw
22489d09728Smrg      fi
22589d09728Smrg      cp_umask=$mode$u_plus_rw;;
22689d09728Smrg  esac
22789d09728Smrgfi
22889d09728Smrg
22966fe65f6Smrgfor src
23066fe65f6Smrgdo
23166fe65f6Smrg  # Protect names starting with `-'.
23266fe65f6Smrg  case $src in
23389d09728Smrg    -*) src=./$src;;
23466fe65f6Smrg  esac
23566fe65f6Smrg
23666fe65f6Smrg  if test -n "$dir_arg"; then
23766fe65f6Smrg    dst=$src
23889d09728Smrg    dstdir=$dst
23989d09728Smrg    test -d "$dstdir"
24089d09728Smrg    dstdir_status=$?
24166fe65f6Smrg  else
24289d09728Smrg
24366fe65f6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24466fe65f6Smrg    # might cause directories to be created, which would be especially bad
24566fe65f6Smrg    # if $src (and thus $dsttmp) contains '*'.
24666fe65f6Smrg    if test ! -f "$src" && test ! -d "$src"; then
24766fe65f6Smrg      echo "$0: $src does not exist." >&2
24866fe65f6Smrg      exit 1
24966fe65f6Smrg    fi
25066fe65f6Smrg
25189d09728Smrg    if test -z "$dst_arg"; then
25266fe65f6Smrg      echo "$0: no destination specified." >&2
25366fe65f6Smrg      exit 1
25466fe65f6Smrg    fi
25566fe65f6Smrg
25689d09728Smrg    dst=$dst_arg
25766fe65f6Smrg    # Protect names starting with `-'.
25866fe65f6Smrg    case $dst in
25989d09728Smrg      -*) dst=./$dst;;
26066fe65f6Smrg    esac
26166fe65f6Smrg
26266fe65f6Smrg    # If destination is a directory, append the input filename; won't work
26366fe65f6Smrg    # if double slashes aren't ignored.
26466fe65f6Smrg    if test -d "$dst"; then
26566fe65f6Smrg      if test -n "$no_target_directory"; then
26689d09728Smrg	echo "$0: $dst_arg: Is a directory" >&2
26766fe65f6Smrg	exit 1
26866fe65f6Smrg      fi
26989d09728Smrg      dstdir=$dst
27089d09728Smrg      dst=$dstdir/`basename "$src"`
27189d09728Smrg      dstdir_status=0
27289d09728Smrg    else
27389d09728Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27489d09728Smrg      dstdir=`
27589d09728Smrg	(dirname "$dst") 2>/dev/null ||
27689d09728Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27789d09728Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27889d09728Smrg	     X"$dst" : 'X\(//\)$' \| \
27989d09728Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28089d09728Smrg	echo X"$dst" |
28189d09728Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28289d09728Smrg		   s//\1/
28389d09728Smrg		   q
28489d09728Smrg		 }
28589d09728Smrg		 /^X\(\/\/\)[^/].*/{
28689d09728Smrg		   s//\1/
28789d09728Smrg		   q
28889d09728Smrg		 }
28989d09728Smrg		 /^X\(\/\/\)$/{
29089d09728Smrg		   s//\1/
29189d09728Smrg		   q
29289d09728Smrg		 }
29389d09728Smrg		 /^X\(\/\).*/{
29489d09728Smrg		   s//\1/
29589d09728Smrg		   q
29689d09728Smrg		 }
29789d09728Smrg		 s/.*/./; q'
29889d09728Smrg      `
29989d09728Smrg
30089d09728Smrg      test -d "$dstdir"
30189d09728Smrg      dstdir_status=$?
30266fe65f6Smrg    fi
30366fe65f6Smrg  fi
30466fe65f6Smrg
30589d09728Smrg  obsolete_mkdir_used=false
30689d09728Smrg
30789d09728Smrg  if test $dstdir_status != 0; then
30889d09728Smrg    case $posix_mkdir in
30989d09728Smrg      '')
31089d09728Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31189d09728Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31289d09728Smrg	umask=`umask`
31389d09728Smrg	case $stripcmd.$umask in
31489d09728Smrg	  # Optimize common cases.
31589d09728Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31689d09728Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31789d09728Smrg
31889d09728Smrg	  *[0-7])
31989d09728Smrg	    mkdir_umask=`expr $umask + 22 \
32089d09728Smrg	      - $umask % 100 % 40 + $umask % 20 \
32189d09728Smrg	      - $umask % 10 % 4 + $umask % 2
32289d09728Smrg	    `;;
32389d09728Smrg	  *) mkdir_umask=$umask,go-w;;
32489d09728Smrg	esac
32589d09728Smrg
32689d09728Smrg	# With -d, create the new directory with the user-specified mode.
32789d09728Smrg	# Otherwise, rely on $mkdir_umask.
32889d09728Smrg	if test -n "$dir_arg"; then
32989d09728Smrg	  mkdir_mode=-m$mode
33089d09728Smrg	else
33189d09728Smrg	  mkdir_mode=
33289d09728Smrg	fi
33389d09728Smrg
33489d09728Smrg	posix_mkdir=false
33589d09728Smrg	case $umask in
33689d09728Smrg	  *[123567][0-7][0-7])
33789d09728Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33889d09728Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33989d09728Smrg	    ;;
34089d09728Smrg	  *)
34189d09728Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34289d09728Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34389d09728Smrg
34489d09728Smrg	    if (umask $mkdir_umask &&
34589d09728Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34689d09728Smrg	    then
34789d09728Smrg	      if test -z "$dir_arg" || {
34889d09728Smrg		   # Check for POSIX incompatibilities with -m.
34989d09728Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35089d09728Smrg		   # other-writeable bit of parent directory when it shouldn't.
35189d09728Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35289d09728Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35389d09728Smrg		   case $ls_ld_tmpdir in
35489d09728Smrg		     d????-?r-*) different_mode=700;;
35589d09728Smrg		     d????-?--*) different_mode=755;;
35689d09728Smrg		     *) false;;
35789d09728Smrg		   esac &&
35889d09728Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35989d09728Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36089d09728Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36189d09728Smrg		   }
36289d09728Smrg		 }
36389d09728Smrg	      then posix_mkdir=:
36489d09728Smrg	      fi
36589d09728Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36689d09728Smrg	    else
36789d09728Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36889d09728Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36989d09728Smrg	    fi
37089d09728Smrg	    trap '' 0;;
37189d09728Smrg	esac;;
37289d09728Smrg    esac
37366fe65f6Smrg
37489d09728Smrg    if
37589d09728Smrg      $posix_mkdir && (
37689d09728Smrg	umask $mkdir_umask &&
37789d09728Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37889d09728Smrg      )
37989d09728Smrg    then :
38089d09728Smrg    else
38166fe65f6Smrg
38289d09728Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38389d09728Smrg      # or it failed possibly due to a race condition.  Create the
38489d09728Smrg      # directory the slow way, step by step, checking for races as we go.
38566fe65f6Smrg
38689d09728Smrg      case $dstdir in
38789d09728Smrg	/*) prefix='/';;
38889d09728Smrg	-*) prefix='./';;
38989d09728Smrg	*)  prefix='';;
39089d09728Smrg      esac
39166fe65f6Smrg
39289d09728Smrg      eval "$initialize_posix_glob"
39366fe65f6Smrg
39489d09728Smrg      oIFS=$IFS
39589d09728Smrg      IFS=/
39689d09728Smrg      $posix_glob set -f
39789d09728Smrg      set fnord $dstdir
39866fe65f6Smrg      shift
39989d09728Smrg      $posix_glob set +f
40089d09728Smrg      IFS=$oIFS
40189d09728Smrg
40289d09728Smrg      prefixes=
40389d09728Smrg
40489d09728Smrg      for d
40589d09728Smrg      do
40689d09728Smrg	test -z "$d" && continue
40789d09728Smrg
40889d09728Smrg	prefix=$prefix$d
40989d09728Smrg	if test -d "$prefix"; then
41089d09728Smrg	  prefixes=
41189d09728Smrg	else
41289d09728Smrg	  if $posix_mkdir; then
41389d09728Smrg	    (umask=$mkdir_umask &&
41489d09728Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41589d09728Smrg	    # Don't fail if two instances are running concurrently.
41689d09728Smrg	    test -d "$prefix" || exit 1
41789d09728Smrg	  else
41889d09728Smrg	    case $prefix in
41989d09728Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42089d09728Smrg	      *) qprefix=$prefix;;
42189d09728Smrg	    esac
42289d09728Smrg	    prefixes="$prefixes '$qprefix'"
42389d09728Smrg	  fi
42489d09728Smrg	fi
42589d09728Smrg	prefix=$prefix/
42689d09728Smrg      done
42789d09728Smrg
42889d09728Smrg      if test -n "$prefixes"; then
42989d09728Smrg	# Don't fail if two instances are running concurrently.
43089d09728Smrg	(umask $mkdir_umask &&
43189d09728Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43289d09728Smrg	  test -d "$dstdir" || exit 1
43389d09728Smrg	obsolete_mkdir_used=true
43466fe65f6Smrg      fi
43589d09728Smrg    fi
43666fe65f6Smrg  fi
43766fe65f6Smrg
43866fe65f6Smrg  if test -n "$dir_arg"; then
43989d09728Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44089d09728Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44189d09728Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44289d09728Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44366fe65f6Smrg  else
44466fe65f6Smrg
44566fe65f6Smrg    # Make a couple of temp file names in the proper directory.
44666fe65f6Smrg    dsttmp=$dstdir/_inst.$$_
44766fe65f6Smrg    rmtmp=$dstdir/_rm.$$_
44866fe65f6Smrg
44966fe65f6Smrg    # Trap to clean up those temp files at exit.
45066fe65f6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45166fe65f6Smrg
45266fe65f6Smrg    # Copy the file name to the temp name.
45389d09728Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45466fe65f6Smrg
45566fe65f6Smrg    # and set any options; do chmod last to preserve setuid bits.
45666fe65f6Smrg    #
45766fe65f6Smrg    # If any of these fail, we abort the whole thing.  If we want to
45866fe65f6Smrg    # ignore errors from any of these, just make sure not to ignore
45966fe65f6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46066fe65f6Smrg    #
46189d09728Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46289d09728Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46389d09728Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46489d09728Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46589d09728Smrg
46689d09728Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46789d09728Smrg    if $copy_on_change &&
46889d09728Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46989d09728Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47089d09728Smrg
47189d09728Smrg       eval "$initialize_posix_glob" &&
47289d09728Smrg       $posix_glob set -f &&
47389d09728Smrg       set X $old && old=:$2:$4:$5:$6 &&
47489d09728Smrg       set X $new && new=:$2:$4:$5:$6 &&
47589d09728Smrg       $posix_glob set +f &&
47689d09728Smrg
47789d09728Smrg       test "$old" = "$new" &&
47889d09728Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47989d09728Smrg    then
48089d09728Smrg      rm -f "$dsttmp"
48189d09728Smrg    else
48289d09728Smrg      # Rename the file to the real destination.
48389d09728Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48489d09728Smrg
48589d09728Smrg      # The rename failed, perhaps because mv can't rename something else
48689d09728Smrg      # to itself, or perhaps because mv is so ancient that it does not
48789d09728Smrg      # support -f.
48889d09728Smrg      {
48989d09728Smrg	# Now remove or move aside any old file at destination location.
49089d09728Smrg	# We try this two ways since rm can't unlink itself on some
49189d09728Smrg	# systems and the destination file might be busy for other
49289d09728Smrg	# reasons.  In this case, the final cleanup might fail but the new
49389d09728Smrg	# file should still install successfully.
49489d09728Smrg	{
49589d09728Smrg	  test ! -f "$dst" ||
49689d09728Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49789d09728Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49889d09728Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49989d09728Smrg	  } ||
50089d09728Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50189d09728Smrg	    (exit 1); exit 1
50289d09728Smrg	  }
50389d09728Smrg	} &&
50489d09728Smrg
50589d09728Smrg	# Now rename the file to the real destination.
50689d09728Smrg	$doit $mvcmd "$dsttmp" "$dst"
50789d09728Smrg      }
50889d09728Smrg    fi || exit 1
50989d09728Smrg
51089d09728Smrg    trap '' 0
51189d09728Smrg  fi
51266fe65f6Smrgdone
51366fe65f6Smrg
51466fe65f6Smrg# Local variables:
51566fe65f6Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51666fe65f6Smrg# time-stamp-start: "scriptversion="
51766fe65f6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518a9274bcdSmrg# time-stamp-time-zone: "UTC"
519a9274bcdSmrg# time-stamp-end: "; # UTC"
52066fe65f6Smrg# End:
521