install-sh revision 50f2e948
145bc899bSmrg#!/bin/sh
245bc899bSmrg# install - install a program, script, or datafile
345bc899bSmrg
450f2e948Smrgscriptversion=2006-12-25.00
545bc899bSmrg
645bc899bSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
745bc899bSmrg# later released in X11R6 (xc/config/util/install.sh) with the
845bc899bSmrg# following copyright and license.
945bc899bSmrg#
1045bc899bSmrg# Copyright (C) 1994 X Consortium
1145bc899bSmrg#
1245bc899bSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1345bc899bSmrg# of this software and associated documentation files (the "Software"), to
1445bc899bSmrg# deal in the Software without restriction, including without limitation the
1545bc899bSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1645bc899bSmrg# sell copies of the Software, and to permit persons to whom the Software is
1745bc899bSmrg# furnished to do so, subject to the following conditions:
1845bc899bSmrg#
1945bc899bSmrg# The above copyright notice and this permission notice shall be included in
2045bc899bSmrg# all copies or substantial portions of the Software.
2145bc899bSmrg#
2245bc899bSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2345bc899bSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2445bc899bSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2545bc899bSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2645bc899bSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2745bc899bSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2845bc899bSmrg#
2945bc899bSmrg# Except as contained in this notice, the name of the X Consortium shall not
3045bc899bSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3145bc899bSmrg# ings in this Software without prior written authorization from the X Consor-
3245bc899bSmrg# tium.
3345bc899bSmrg#
3445bc899bSmrg#
3545bc899bSmrg# FSF changes to this file are in the public domain.
3645bc899bSmrg#
3745bc899bSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3845bc899bSmrg# `make' implicit rules from creating a file called install from it
3945bc899bSmrg# when there is no Makefile.
4045bc899bSmrg#
4145bc899bSmrg# This script is compatible with the BSD install script, but was written
4250f2e948Smrg# from scratch.
4350f2e948Smrg
4450f2e948Smrgnl='
4550f2e948Smrg'
4650f2e948SmrgIFS=" ""	$nl"
4745bc899bSmrg
4845bc899bSmrg# set DOITPROG to echo to test this script
4945bc899bSmrg
5045bc899bSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5150f2e948Smrgdoit=${DOITPROG-}
5250f2e948Smrgif test -z "$doit"; then
5350f2e948Smrg  doit_exec=exec
5450f2e948Smrgelse
5550f2e948Smrg  doit_exec=$doit
5650f2e948Smrgfi
5745bc899bSmrg
5850f2e948Smrg# Put in absolute file names if you don't have them in your path;
5950f2e948Smrg# or use environment vars.
6050f2e948Smrg
6150f2e948Smrgchgrpprog=${CHGRPPROG-chgrp}
6250f2e948Smrgchmodprog=${CHMODPROG-chmod}
6350f2e948Smrgchownprog=${CHOWNPROG-chown}
6450f2e948Smrgcmpprog=${CMPPROG-cmp}
6550f2e948Smrgcpprog=${CPPROG-cp}
6650f2e948Smrgmkdirprog=${MKDIRPROG-mkdir}
6750f2e948Smrgmvprog=${MVPROG-mv}
6850f2e948Smrgrmprog=${RMPROG-rm}
6950f2e948Smrgstripprog=${STRIPPROG-strip}
7050f2e948Smrg
7150f2e948Smrgposix_glob='?'
7250f2e948Smrginitialize_posix_glob='
7350f2e948Smrg  test "$posix_glob" != "?" || {
7450f2e948Smrg    if (set -f) 2>/dev/null; then
7550f2e948Smrg      posix_glob=
7650f2e948Smrg    else
7750f2e948Smrg      posix_glob=:
7850f2e948Smrg    fi
7950f2e948Smrg  }
8050f2e948Smrg'
811ac89addSmrg
8250f2e948Smrgposix_mkdir=
8350f2e948Smrg
8450f2e948Smrg# Desired mode of installed file.
8550f2e948Smrgmode=0755
8645bc899bSmrg
87226fade8Smrgchgrpcmd=
8850f2e948Smrgchmodcmd=$chmodprog
8950f2e948Smrgchowncmd=
9050f2e948Smrgmvcmd=$mvprog
91226fade8Smrgrmcmd="$rmprog -f"
9250f2e948Smrgstripcmd=
9350f2e948Smrg
9445bc899bSmrgsrc=
9545bc899bSmrgdst=
9645bc899bSmrgdir_arg=
9750f2e948Smrgdst_arg=
9850f2e948Smrg
9950f2e948Smrgcopy_on_change=false
10045bc899bSmrgno_target_directory=
10145bc899bSmrg
10250f2e948Smrgusage="\
10350f2e948SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10445bc899bSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10545bc899bSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10645bc899bSmrg   or: $0 [OPTION]... -d DIRECTORIES...
10745bc899bSmrg
10845bc899bSmrgIn the 1st form, copy SRCFILE to DSTFILE.
10945bc899bSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11045bc899bSmrgIn the 4th, create DIRECTORIES.
11145bc899bSmrg
11245bc899bSmrgOptions:
11350f2e948Smrg     --help     display this help and exit.
11450f2e948Smrg     --version  display version info and exit.
11550f2e948Smrg
11650f2e948Smrg  -c            (ignored)
11750f2e948Smrg  -C            install only if different (preserve the last data modification time)
11850f2e948Smrg  -d            create directories instead of installing files.
11950f2e948Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12050f2e948Smrg  -m MODE       $chmodprog installed files to MODE.
12150f2e948Smrg  -o USER       $chownprog installed files to USER.
12250f2e948Smrg  -s            $stripprog installed files.
12350f2e948Smrg  -t DIRECTORY  install into DIRECTORY.
12450f2e948Smrg  -T            report an error if DSTFILE is a directory.
12545bc899bSmrg
12645bc899bSmrgEnvironment variables override the default commands:
12750f2e948Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12850f2e948Smrg  RMPROG STRIPPROG
12945bc899bSmrg"
13045bc899bSmrg
13150f2e948Smrgwhile test $# -ne 0; do
13245bc899bSmrg  case $1 in
13350f2e948Smrg    -c) ;;
13450f2e948Smrg
13550f2e948Smrg    -C) copy_on_change=true;;
13645bc899bSmrg
13750f2e948Smrg    -d) dir_arg=true;;
13845bc899bSmrg
13945bc899bSmrg    -g) chgrpcmd="$chgrpprog $2"
14050f2e948Smrg	shift;;
14145bc899bSmrg
14245bc899bSmrg    --help) echo "$usage"; exit $?;;
14345bc899bSmrg
14450f2e948Smrg    -m) mode=$2
14550f2e948Smrg	case $mode in
14650f2e948Smrg	  *' '* | *'	'* | *'
14750f2e948Smrg'*	  | *'*'* | *'?'* | *'['*)
14850f2e948Smrg	    echo "$0: invalid mode: $mode" >&2
14950f2e948Smrg	    exit 1;;
15050f2e948Smrg	esac
15150f2e948Smrg	shift;;
15245bc899bSmrg
15345bc899bSmrg    -o) chowncmd="$chownprog $2"
15450f2e948Smrg	shift;;
15545bc899bSmrg
15650f2e948Smrg    -s) stripcmd=$stripprog;;
15745bc899bSmrg
15850f2e948Smrg    -t) dst_arg=$2
15950f2e948Smrg	shift;;
16045bc899bSmrg
16150f2e948Smrg    -T) no_target_directory=true;;
16245bc899bSmrg
16345bc899bSmrg    --version) echo "$0 $scriptversion"; exit $?;;
16445bc899bSmrg
16550f2e948Smrg    --)	shift
16645bc899bSmrg	break;;
16750f2e948Smrg
16850f2e948Smrg    -*)	echo "$0: invalid option: $1" >&2
16950f2e948Smrg	exit 1;;
17050f2e948Smrg
17150f2e948Smrg    *)  break;;
17245bc899bSmrg  esac
17350f2e948Smrg  shift
17445bc899bSmrgdone
17545bc899bSmrg
17650f2e948Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17750f2e948Smrg  # When -d is used, all remaining arguments are directories to create.
17850f2e948Smrg  # When -t is used, the destination is already specified.
17950f2e948Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18050f2e948Smrg  for arg
18150f2e948Smrg  do
18250f2e948Smrg    if test -n "$dst_arg"; then
18350f2e948Smrg      # $@ is not empty: it contains at least $arg.
18450f2e948Smrg      set fnord "$@" "$dst_arg"
18550f2e948Smrg      shift # fnord
18650f2e948Smrg    fi
18750f2e948Smrg    shift # arg
18850f2e948Smrg    dst_arg=$arg
18950f2e948Smrg  done
19050f2e948Smrgfi
19150f2e948Smrg
19250f2e948Smrgif test $# -eq 0; then
19345bc899bSmrg  if test -z "$dir_arg"; then
19445bc899bSmrg    echo "$0: no input file specified." >&2
19545bc899bSmrg    exit 1
19645bc899bSmrg  fi
19745bc899bSmrg  # It's OK to call `install-sh -d' without argument.
19845bc899bSmrg  # This can happen when creating conditional directories.
19945bc899bSmrg  exit 0
20045bc899bSmrgfi
20145bc899bSmrg
20250f2e948Smrgif test -z "$dir_arg"; then
20350f2e948Smrg  trap '(exit $?); exit' 1 2 13 15
20450f2e948Smrg
20550f2e948Smrg  # Set umask so as not to create temps with too-generous modes.
20650f2e948Smrg  # However, 'strip' requires both read and write access to temps.
20750f2e948Smrg  case $mode in
20850f2e948Smrg    # Optimize common cases.
20950f2e948Smrg    *644) cp_umask=133;;
21050f2e948Smrg    *755) cp_umask=22;;
21150f2e948Smrg
21250f2e948Smrg    *[0-7])
21350f2e948Smrg      if test -z "$stripcmd"; then
21450f2e948Smrg	u_plus_rw=
21550f2e948Smrg      else
21650f2e948Smrg	u_plus_rw='% 200'
21750f2e948Smrg      fi
21850f2e948Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21950f2e948Smrg    *)
22050f2e948Smrg      if test -z "$stripcmd"; then
22150f2e948Smrg	u_plus_rw=
22250f2e948Smrg      else
22350f2e948Smrg	u_plus_rw=,u+rw
22450f2e948Smrg      fi
22550f2e948Smrg      cp_umask=$mode$u_plus_rw;;
22650f2e948Smrg  esac
22750f2e948Smrgfi
22850f2e948Smrg
22945bc899bSmrgfor src
23045bc899bSmrgdo
23145bc899bSmrg  # Protect names starting with `-'.
23245bc899bSmrg  case $src in
23350f2e948Smrg    -*) src=./$src;;
23445bc899bSmrg  esac
23545bc899bSmrg
23645bc899bSmrg  if test -n "$dir_arg"; then
23745bc899bSmrg    dst=$src
23850f2e948Smrg    dstdir=$dst
23950f2e948Smrg    test -d "$dstdir"
24050f2e948Smrg    dstdir_status=$?
241226fade8Smrg  else
24250f2e948Smrg
24345bc899bSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24445bc899bSmrg    # might cause directories to be created, which would be especially bad
24545bc899bSmrg    # if $src (and thus $dsttmp) contains '*'.
24645bc899bSmrg    if test ! -f "$src" && test ! -d "$src"; then
24745bc899bSmrg      echo "$0: $src does not exist." >&2
24845bc899bSmrg      exit 1
24945bc899bSmrg    fi
25045bc899bSmrg
25150f2e948Smrg    if test -z "$dst_arg"; then
25245bc899bSmrg      echo "$0: no destination specified." >&2
25345bc899bSmrg      exit 1
25445bc899bSmrg    fi
25545bc899bSmrg
25650f2e948Smrg    dst=$dst_arg
25745bc899bSmrg    # Protect names starting with `-'.
25845bc899bSmrg    case $dst in
25950f2e948Smrg      -*) dst=./$dst;;
26045bc899bSmrg    esac
26145bc899bSmrg
26245bc899bSmrg    # If destination is a directory, append the input filename; won't work
26345bc899bSmrg    # if double slashes aren't ignored.
26445bc899bSmrg    if test -d "$dst"; then
26545bc899bSmrg      if test -n "$no_target_directory"; then
26650f2e948Smrg	echo "$0: $dst_arg: Is a directory" >&2
26745bc899bSmrg	exit 1
26845bc899bSmrg      fi
26950f2e948Smrg      dstdir=$dst
27050f2e948Smrg      dst=$dstdir/`basename "$src"`
27150f2e948Smrg      dstdir_status=0
27250f2e948Smrg    else
27350f2e948Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27450f2e948Smrg      dstdir=`
27550f2e948Smrg	(dirname "$dst") 2>/dev/null ||
27650f2e948Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27750f2e948Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27850f2e948Smrg	     X"$dst" : 'X\(//\)$' \| \
27950f2e948Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28050f2e948Smrg	echo X"$dst" |
28150f2e948Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28250f2e948Smrg		   s//\1/
28350f2e948Smrg		   q
28450f2e948Smrg		 }
28550f2e948Smrg		 /^X\(\/\/\)[^/].*/{
28650f2e948Smrg		   s//\1/
28750f2e948Smrg		   q
28850f2e948Smrg		 }
28950f2e948Smrg		 /^X\(\/\/\)$/{
29050f2e948Smrg		   s//\1/
29150f2e948Smrg		   q
29250f2e948Smrg		 }
29350f2e948Smrg		 /^X\(\/\).*/{
29450f2e948Smrg		   s//\1/
29550f2e948Smrg		   q
29650f2e948Smrg		 }
29750f2e948Smrg		 s/.*/./; q'
29850f2e948Smrg      `
29950f2e948Smrg
30050f2e948Smrg      test -d "$dstdir"
30150f2e948Smrg      dstdir_status=$?
30245bc899bSmrg    fi
30345bc899bSmrg  fi
30445bc899bSmrg
30550f2e948Smrg  obsolete_mkdir_used=false
30650f2e948Smrg
30750f2e948Smrg  if test $dstdir_status != 0; then
30850f2e948Smrg    case $posix_mkdir in
30950f2e948Smrg      '')
31050f2e948Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31150f2e948Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31250f2e948Smrg	umask=`umask`
31350f2e948Smrg	case $stripcmd.$umask in
31450f2e948Smrg	  # Optimize common cases.
31550f2e948Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31650f2e948Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31750f2e948Smrg
31850f2e948Smrg	  *[0-7])
31950f2e948Smrg	    mkdir_umask=`expr $umask + 22 \
32050f2e948Smrg	      - $umask % 100 % 40 + $umask % 20 \
32150f2e948Smrg	      - $umask % 10 % 4 + $umask % 2
32250f2e948Smrg	    `;;
32350f2e948Smrg	  *) mkdir_umask=$umask,go-w;;
32450f2e948Smrg	esac
32550f2e948Smrg
32650f2e948Smrg	# With -d, create the new directory with the user-specified mode.
32750f2e948Smrg	# Otherwise, rely on $mkdir_umask.
32850f2e948Smrg	if test -n "$dir_arg"; then
32950f2e948Smrg	  mkdir_mode=-m$mode
33050f2e948Smrg	else
33150f2e948Smrg	  mkdir_mode=
33250f2e948Smrg	fi
33350f2e948Smrg
33450f2e948Smrg	posix_mkdir=false
33550f2e948Smrg	case $umask in
33650f2e948Smrg	  *[123567][0-7][0-7])
33750f2e948Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33850f2e948Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33950f2e948Smrg	    ;;
34050f2e948Smrg	  *)
34150f2e948Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34250f2e948Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34350f2e948Smrg
34450f2e948Smrg	    if (umask $mkdir_umask &&
34550f2e948Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34650f2e948Smrg	    then
34750f2e948Smrg	      if test -z "$dir_arg" || {
34850f2e948Smrg		   # Check for POSIX incompatibilities with -m.
34950f2e948Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35050f2e948Smrg		   # other-writeable bit of parent directory when it shouldn't.
35150f2e948Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35250f2e948Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35350f2e948Smrg		   case $ls_ld_tmpdir in
35450f2e948Smrg		     d????-?r-*) different_mode=700;;
35550f2e948Smrg		     d????-?--*) different_mode=755;;
35650f2e948Smrg		     *) false;;
35750f2e948Smrg		   esac &&
35850f2e948Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35950f2e948Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36050f2e948Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36150f2e948Smrg		   }
36250f2e948Smrg		 }
36350f2e948Smrg	      then posix_mkdir=:
36450f2e948Smrg	      fi
36550f2e948Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36650f2e948Smrg	    else
36750f2e948Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36850f2e948Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36950f2e948Smrg	    fi
37050f2e948Smrg	    trap '' 0;;
37150f2e948Smrg	esac;;
37250f2e948Smrg    esac
37345bc899bSmrg
37450f2e948Smrg    if
37550f2e948Smrg      $posix_mkdir && (
37650f2e948Smrg	umask $mkdir_umask &&
37750f2e948Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37850f2e948Smrg      )
37950f2e948Smrg    then :
38050f2e948Smrg    else
38145bc899bSmrg
38250f2e948Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38350f2e948Smrg      # or it failed possibly due to a race condition.  Create the
38450f2e948Smrg      # directory the slow way, step by step, checking for races as we go.
38545bc899bSmrg
38650f2e948Smrg      case $dstdir in
38750f2e948Smrg	/*) prefix='/';;
38850f2e948Smrg	-*) prefix='./';;
38950f2e948Smrg	*)  prefix='';;
39050f2e948Smrg      esac
39145bc899bSmrg
39250f2e948Smrg      eval "$initialize_posix_glob"
39345bc899bSmrg
39450f2e948Smrg      oIFS=$IFS
39550f2e948Smrg      IFS=/
39650f2e948Smrg      $posix_glob set -f
39750f2e948Smrg      set fnord $dstdir
39845bc899bSmrg      shift
39950f2e948Smrg      $posix_glob set +f
40050f2e948Smrg      IFS=$oIFS
40150f2e948Smrg
40250f2e948Smrg      prefixes=
40350f2e948Smrg
40450f2e948Smrg      for d
40550f2e948Smrg      do
40650f2e948Smrg	test -z "$d" && continue
40750f2e948Smrg
40850f2e948Smrg	prefix=$prefix$d
40950f2e948Smrg	if test -d "$prefix"; then
41050f2e948Smrg	  prefixes=
41150f2e948Smrg	else
41250f2e948Smrg	  if $posix_mkdir; then
41350f2e948Smrg	    (umask=$mkdir_umask &&
41450f2e948Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41550f2e948Smrg	    # Don't fail if two instances are running concurrently.
41650f2e948Smrg	    test -d "$prefix" || exit 1
41750f2e948Smrg	  else
41850f2e948Smrg	    case $prefix in
41950f2e948Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42050f2e948Smrg	      *) qprefix=$prefix;;
42150f2e948Smrg	    esac
42250f2e948Smrg	    prefixes="$prefixes '$qprefix'"
42350f2e948Smrg	  fi
42450f2e948Smrg	fi
42550f2e948Smrg	prefix=$prefix/
42650f2e948Smrg      done
42750f2e948Smrg
42850f2e948Smrg      if test -n "$prefixes"; then
42950f2e948Smrg	# Don't fail if two instances are running concurrently.
43050f2e948Smrg	(umask $mkdir_umask &&
43150f2e948Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43250f2e948Smrg	  test -d "$dstdir" || exit 1
43350f2e948Smrg	obsolete_mkdir_used=true
43445bc899bSmrg      fi
43550f2e948Smrg    fi
43645bc899bSmrg  fi
43745bc899bSmrg
43845bc899bSmrg  if test -n "$dir_arg"; then
43950f2e948Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44050f2e948Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44150f2e948Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44250f2e948Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44345bc899bSmrg  else
44445bc899bSmrg
44545bc899bSmrg    # Make a couple of temp file names in the proper directory.
44645bc899bSmrg    dsttmp=$dstdir/_inst.$$_
44745bc899bSmrg    rmtmp=$dstdir/_rm.$$_
44845bc899bSmrg
44945bc899bSmrg    # Trap to clean up those temp files at exit.
45045bc899bSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45145bc899bSmrg
45245bc899bSmrg    # Copy the file name to the temp name.
45350f2e948Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45445bc899bSmrg
45545bc899bSmrg    # and set any options; do chmod last to preserve setuid bits.
45645bc899bSmrg    #
45745bc899bSmrg    # If any of these fail, we abort the whole thing.  If we want to
45845bc899bSmrg    # ignore errors from any of these, just make sure not to ignore
45945bc899bSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46045bc899bSmrg    #
46150f2e948Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46250f2e948Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46350f2e948Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46450f2e948Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46550f2e948Smrg
46650f2e948Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46750f2e948Smrg    if $copy_on_change &&
46850f2e948Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46950f2e948Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47050f2e948Smrg
47150f2e948Smrg       eval "$initialize_posix_glob" &&
47250f2e948Smrg       $posix_glob set -f &&
47350f2e948Smrg       set X $old && old=:$2:$4:$5:$6 &&
47450f2e948Smrg       set X $new && new=:$2:$4:$5:$6 &&
47550f2e948Smrg       $posix_glob set +f &&
47650f2e948Smrg
47750f2e948Smrg       test "$old" = "$new" &&
47850f2e948Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47950f2e948Smrg    then
48050f2e948Smrg      rm -f "$dsttmp"
48150f2e948Smrg    else
48250f2e948Smrg      # Rename the file to the real destination.
48350f2e948Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48450f2e948Smrg
48550f2e948Smrg      # The rename failed, perhaps because mv can't rename something else
48650f2e948Smrg      # to itself, or perhaps because mv is so ancient that it does not
48750f2e948Smrg      # support -f.
48850f2e948Smrg      {
48950f2e948Smrg	# Now remove or move aside any old file at destination location.
49050f2e948Smrg	# We try this two ways since rm can't unlink itself on some
49150f2e948Smrg	# systems and the destination file might be busy for other
49250f2e948Smrg	# reasons.  In this case, the final cleanup might fail but the new
49350f2e948Smrg	# file should still install successfully.
49450f2e948Smrg	{
49550f2e948Smrg	  test ! -f "$dst" ||
49650f2e948Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49750f2e948Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49850f2e948Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49950f2e948Smrg	  } ||
50050f2e948Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50150f2e948Smrg	    (exit 1); exit 1
50250f2e948Smrg	  }
50350f2e948Smrg	} &&
50450f2e948Smrg
50550f2e948Smrg	# Now rename the file to the real destination.
50650f2e948Smrg	$doit $mvcmd "$dsttmp" "$dst"
50750f2e948Smrg      }
50850f2e948Smrg    fi || exit 1
50950f2e948Smrg
51050f2e948Smrg    trap '' 0
51150f2e948Smrg  fi
51245bc899bSmrgdone
51345bc899bSmrg
51445bc899bSmrg# Local variables:
51545bc899bSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51645bc899bSmrg# time-stamp-start: "scriptversion="
51745bc899bSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51845bc899bSmrg# time-stamp-end: "$"
51945bc899bSmrg# End:
520