install-sh revision 35d5702b
1a6fdc6faSmrg#!/bin/sh
2a6fdc6faSmrg# install - install a program, script, or datafile
335d5702bSmrg
435d5702bSmrgscriptversion=2009-04-28.21; # UTC
535d5702bSmrg
6a6fdc6faSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a6fdc6faSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8a6fdc6faSmrg# following copyright and license.
9a6fdc6faSmrg#
10a6fdc6faSmrg# Copyright (C) 1994 X Consortium
11a6fdc6faSmrg#
12a6fdc6faSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a6fdc6faSmrg# of this software and associated documentation files (the "Software"), to
14a6fdc6faSmrg# deal in the Software without restriction, including without limitation the
15a6fdc6faSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a6fdc6faSmrg# sell copies of the Software, and to permit persons to whom the Software is
17a6fdc6faSmrg# furnished to do so, subject to the following conditions:
18a6fdc6faSmrg#
19a6fdc6faSmrg# The above copyright notice and this permission notice shall be included in
20a6fdc6faSmrg# all copies or substantial portions of the Software.
21a6fdc6faSmrg#
22a6fdc6faSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a6fdc6faSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a6fdc6faSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a6fdc6faSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a6fdc6faSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a6fdc6faSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a6fdc6faSmrg#
29a6fdc6faSmrg# Except as contained in this notice, the name of the X Consortium shall not
30a6fdc6faSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a6fdc6faSmrg# ings in this Software without prior written authorization from the X Consor-
32a6fdc6faSmrg# tium.
33a6fdc6faSmrg#
34a6fdc6faSmrg#
35a6fdc6faSmrg# FSF changes to this file are in the public domain.
36a6fdc6faSmrg#
37a6fdc6faSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38a6fdc6faSmrg# `make' implicit rules from creating a file called install from it
39a6fdc6faSmrg# when there is no Makefile.
40a6fdc6faSmrg#
41a6fdc6faSmrg# This script is compatible with the BSD install script, but was written
4235d5702bSmrg# from scratch.
43a6fdc6faSmrg
4435d5702bSmrgnl='
4535d5702bSmrg'
4635d5702bSmrgIFS=" ""	$nl"
47a6fdc6faSmrg
48a6fdc6faSmrg# set DOITPROG to echo to test this script
49a6fdc6faSmrg
50a6fdc6faSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5135d5702bSmrgdoit=${DOITPROG-}
5235d5702bSmrgif test -z "$doit"; then
5335d5702bSmrg  doit_exec=exec
54a6fdc6faSmrgelse
5535d5702bSmrg  doit_exec=$doit
56a6fdc6faSmrgfi
57a6fdc6faSmrg
5835d5702bSmrg# Put in absolute file names if you don't have them in your path;
5935d5702bSmrg# or use environment vars.
6035d5702bSmrg
6135d5702bSmrgchgrpprog=${CHGRPPROG-chgrp}
6235d5702bSmrgchmodprog=${CHMODPROG-chmod}
6335d5702bSmrgchownprog=${CHOWNPROG-chown}
6435d5702bSmrgcmpprog=${CMPPROG-cmp}
6535d5702bSmrgcpprog=${CPPROG-cp}
6635d5702bSmrgmkdirprog=${MKDIRPROG-mkdir}
6735d5702bSmrgmvprog=${MVPROG-mv}
6835d5702bSmrgrmprog=${RMPROG-rm}
6935d5702bSmrgstripprog=${STRIPPROG-strip}
7035d5702bSmrg
7135d5702bSmrgposix_glob='?'
7235d5702bSmrginitialize_posix_glob='
7335d5702bSmrg  test "$posix_glob" != "?" || {
7435d5702bSmrg    if (set -f) 2>/dev/null; then
7535d5702bSmrg      posix_glob=
7635d5702bSmrg    else
7735d5702bSmrg      posix_glob=:
7835d5702bSmrg    fi
7935d5702bSmrg  }
8035d5702bSmrg'
8135d5702bSmrg
8235d5702bSmrgposix_mkdir=
8335d5702bSmrg
8435d5702bSmrg# Desired mode of installed file.
8535d5702bSmrgmode=0755
8635d5702bSmrg
8735d5702bSmrgchgrpcmd=
8835d5702bSmrgchmodcmd=$chmodprog
8935d5702bSmrgchowncmd=
9035d5702bSmrgmvcmd=$mvprog
9135d5702bSmrgrmcmd="$rmprog -f"
9235d5702bSmrgstripcmd=
93a6fdc6faSmrg
9435d5702bSmrgsrc=
9535d5702bSmrgdst=
9635d5702bSmrgdir_arg=
9735d5702bSmrgdst_arg=
98a6fdc6faSmrg
9935d5702bSmrgcopy_on_change=false
10035d5702bSmrgno_target_directory=
101a6fdc6faSmrg
10235d5702bSmrgusage="\
10335d5702bSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10435d5702bSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10535d5702bSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10635d5702bSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107a6fdc6faSmrg
10835d5702bSmrgIn the 1st form, copy SRCFILE to DSTFILE.
10935d5702bSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11035d5702bSmrgIn the 4th, create DIRECTORIES.
111a6fdc6faSmrg
11235d5702bSmrgOptions:
11335d5702bSmrg     --help     display this help and exit.
11435d5702bSmrg     --version  display version info and exit.
115a6fdc6faSmrg
11635d5702bSmrg  -c            (ignored)
11735d5702bSmrg  -C            install only if different (preserve the last data modification time)
11835d5702bSmrg  -d            create directories instead of installing files.
11935d5702bSmrg  -g GROUP      $chgrpprog installed files to GROUP.
12035d5702bSmrg  -m MODE       $chmodprog installed files to MODE.
12135d5702bSmrg  -o USER       $chownprog installed files to USER.
12235d5702bSmrg  -s            $stripprog installed files.
12335d5702bSmrg  -t DIRECTORY  install into DIRECTORY.
12435d5702bSmrg  -T            report an error if DSTFILE is a directory.
125a6fdc6faSmrg
12635d5702bSmrgEnvironment variables override the default commands:
12735d5702bSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12835d5702bSmrg  RMPROG STRIPPROG
12935d5702bSmrg"
130a6fdc6faSmrg
13135d5702bSmrgwhile test $# -ne 0; do
13235d5702bSmrg  case $1 in
13335d5702bSmrg    -c) ;;
134a6fdc6faSmrg
13535d5702bSmrg    -C) copy_on_change=true;;
136a6fdc6faSmrg
13735d5702bSmrg    -d) dir_arg=true;;
138a6fdc6faSmrg
13935d5702bSmrg    -g) chgrpcmd="$chgrpprog $2"
14035d5702bSmrg	shift;;
141a6fdc6faSmrg
14235d5702bSmrg    --help) echo "$usage"; exit $?;;
143a6fdc6faSmrg
14435d5702bSmrg    -m) mode=$2
14535d5702bSmrg	case $mode in
14635d5702bSmrg	  *' '* | *'	'* | *'
14735d5702bSmrg'*	  | *'*'* | *'?'* | *'['*)
14835d5702bSmrg	    echo "$0: invalid mode: $mode" >&2
14935d5702bSmrg	    exit 1;;
15035d5702bSmrg	esac
15135d5702bSmrg	shift;;
152a6fdc6faSmrg
15335d5702bSmrg    -o) chowncmd="$chownprog $2"
15435d5702bSmrg	shift;;
155a6fdc6faSmrg
15635d5702bSmrg    -s) stripcmd=$stripprog;;
157a6fdc6faSmrg
15835d5702bSmrg    -t) dst_arg=$2
15935d5702bSmrg	shift;;
160a6fdc6faSmrg
16135d5702bSmrg    -T) no_target_directory=true;;
162a6fdc6faSmrg
16335d5702bSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164a6fdc6faSmrg
16535d5702bSmrg    --)	shift
16635d5702bSmrg	break;;
167a6fdc6faSmrg
16835d5702bSmrg    -*)	echo "$0: invalid option: $1" >&2
16935d5702bSmrg	exit 1;;
170a6fdc6faSmrg
17135d5702bSmrg    *)  break;;
17235d5702bSmrg  esac
17335d5702bSmrg  shift
17435d5702bSmrgdone
175a6fdc6faSmrg
17635d5702bSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17735d5702bSmrg  # When -d is used, all remaining arguments are directories to create.
17835d5702bSmrg  # When -t is used, the destination is already specified.
17935d5702bSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18035d5702bSmrg  for arg
18135d5702bSmrg  do
18235d5702bSmrg    if test -n "$dst_arg"; then
18335d5702bSmrg      # $@ is not empty: it contains at least $arg.
18435d5702bSmrg      set fnord "$@" "$dst_arg"
18535d5702bSmrg      shift # fnord
18635d5702bSmrg    fi
18735d5702bSmrg    shift # arg
18835d5702bSmrg    dst_arg=$arg
18935d5702bSmrg  done
19035d5702bSmrgfi
191a6fdc6faSmrg
19235d5702bSmrgif test $# -eq 0; then
19335d5702bSmrg  if test -z "$dir_arg"; then
19435d5702bSmrg    echo "$0: no input file specified." >&2
19535d5702bSmrg    exit 1
19635d5702bSmrg  fi
19735d5702bSmrg  # It's OK to call `install-sh -d' without argument.
19835d5702bSmrg  # This can happen when creating conditional directories.
19935d5702bSmrg  exit 0
20035d5702bSmrgfi
201a6fdc6faSmrg
20235d5702bSmrgif test -z "$dir_arg"; then
20335d5702bSmrg  trap '(exit $?); exit' 1 2 13 15
20435d5702bSmrg
20535d5702bSmrg  # Set umask so as not to create temps with too-generous modes.
20635d5702bSmrg  # However, 'strip' requires both read and write access to temps.
20735d5702bSmrg  case $mode in
20835d5702bSmrg    # Optimize common cases.
20935d5702bSmrg    *644) cp_umask=133;;
21035d5702bSmrg    *755) cp_umask=22;;
21135d5702bSmrg
21235d5702bSmrg    *[0-7])
21335d5702bSmrg      if test -z "$stripcmd"; then
21435d5702bSmrg	u_plus_rw=
21535d5702bSmrg      else
21635d5702bSmrg	u_plus_rw='% 200'
21735d5702bSmrg      fi
21835d5702bSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21935d5702bSmrg    *)
22035d5702bSmrg      if test -z "$stripcmd"; then
22135d5702bSmrg	u_plus_rw=
22235d5702bSmrg      else
22335d5702bSmrg	u_plus_rw=,u+rw
22435d5702bSmrg      fi
22535d5702bSmrg      cp_umask=$mode$u_plus_rw;;
22635d5702bSmrg  esac
22735d5702bSmrgfi
228a6fdc6faSmrg
22935d5702bSmrgfor src
23035d5702bSmrgdo
23135d5702bSmrg  # Protect names starting with `-'.
23235d5702bSmrg  case $src in
23335d5702bSmrg    -*) src=./$src;;
23435d5702bSmrg  esac
23535d5702bSmrg
23635d5702bSmrg  if test -n "$dir_arg"; then
23735d5702bSmrg    dst=$src
23835d5702bSmrg    dstdir=$dst
23935d5702bSmrg    test -d "$dstdir"
24035d5702bSmrg    dstdir_status=$?
24135d5702bSmrg  else
24235d5702bSmrg
24335d5702bSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24435d5702bSmrg    # might cause directories to be created, which would be especially bad
24535d5702bSmrg    # if $src (and thus $dsttmp) contains '*'.
24635d5702bSmrg    if test ! -f "$src" && test ! -d "$src"; then
24735d5702bSmrg      echo "$0: $src does not exist." >&2
24835d5702bSmrg      exit 1
24935d5702bSmrg    fi
25035d5702bSmrg
25135d5702bSmrg    if test -z "$dst_arg"; then
25235d5702bSmrg      echo "$0: no destination specified." >&2
25335d5702bSmrg      exit 1
25435d5702bSmrg    fi
25535d5702bSmrg
25635d5702bSmrg    dst=$dst_arg
25735d5702bSmrg    # Protect names starting with `-'.
25835d5702bSmrg    case $dst in
25935d5702bSmrg      -*) dst=./$dst;;
26035d5702bSmrg    esac
261a6fdc6faSmrg
26235d5702bSmrg    # If destination is a directory, append the input filename; won't work
26335d5702bSmrg    # if double slashes aren't ignored.
26435d5702bSmrg    if test -d "$dst"; then
26535d5702bSmrg      if test -n "$no_target_directory"; then
26635d5702bSmrg	echo "$0: $dst_arg: Is a directory" >&2
26735d5702bSmrg	exit 1
26835d5702bSmrg      fi
26935d5702bSmrg      dstdir=$dst
27035d5702bSmrg      dst=$dstdir/`basename "$src"`
27135d5702bSmrg      dstdir_status=0
27235d5702bSmrg    else
27335d5702bSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27435d5702bSmrg      dstdir=`
27535d5702bSmrg	(dirname "$dst") 2>/dev/null ||
27635d5702bSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27735d5702bSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
27835d5702bSmrg	     X"$dst" : 'X\(//\)$' \| \
27935d5702bSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28035d5702bSmrg	echo X"$dst" |
28135d5702bSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28235d5702bSmrg		   s//\1/
28335d5702bSmrg		   q
28435d5702bSmrg		 }
28535d5702bSmrg		 /^X\(\/\/\)[^/].*/{
28635d5702bSmrg		   s//\1/
28735d5702bSmrg		   q
28835d5702bSmrg		 }
28935d5702bSmrg		 /^X\(\/\/\)$/{
29035d5702bSmrg		   s//\1/
29135d5702bSmrg		   q
29235d5702bSmrg		 }
29335d5702bSmrg		 /^X\(\/\).*/{
29435d5702bSmrg		   s//\1/
29535d5702bSmrg		   q
29635d5702bSmrg		 }
29735d5702bSmrg		 s/.*/./; q'
29835d5702bSmrg      `
29935d5702bSmrg
30035d5702bSmrg      test -d "$dstdir"
30135d5702bSmrg      dstdir_status=$?
30235d5702bSmrg    fi
30335d5702bSmrg  fi
30435d5702bSmrg
30535d5702bSmrg  obsolete_mkdir_used=false
30635d5702bSmrg
30735d5702bSmrg  if test $dstdir_status != 0; then
30835d5702bSmrg    case $posix_mkdir in
30935d5702bSmrg      '')
31035d5702bSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
31135d5702bSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
31235d5702bSmrg	umask=`umask`
31335d5702bSmrg	case $stripcmd.$umask in
31435d5702bSmrg	  # Optimize common cases.
31535d5702bSmrg	  *[2367][2367]) mkdir_umask=$umask;;
31635d5702bSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31735d5702bSmrg
31835d5702bSmrg	  *[0-7])
31935d5702bSmrg	    mkdir_umask=`expr $umask + 22 \
32035d5702bSmrg	      - $umask % 100 % 40 + $umask % 20 \
32135d5702bSmrg	      - $umask % 10 % 4 + $umask % 2
32235d5702bSmrg	    `;;
32335d5702bSmrg	  *) mkdir_umask=$umask,go-w;;
32435d5702bSmrg	esac
32535d5702bSmrg
32635d5702bSmrg	# With -d, create the new directory with the user-specified mode.
32735d5702bSmrg	# Otherwise, rely on $mkdir_umask.
32835d5702bSmrg	if test -n "$dir_arg"; then
32935d5702bSmrg	  mkdir_mode=-m$mode
330a6fdc6faSmrg	else
33135d5702bSmrg	  mkdir_mode=
332a6fdc6faSmrg	fi
333a6fdc6faSmrg
33435d5702bSmrg	posix_mkdir=false
33535d5702bSmrg	case $umask in
33635d5702bSmrg	  *[123567][0-7][0-7])
33735d5702bSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33835d5702bSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33935d5702bSmrg	    ;;
34035d5702bSmrg	  *)
34135d5702bSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34235d5702bSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34335d5702bSmrg
34435d5702bSmrg	    if (umask $mkdir_umask &&
34535d5702bSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34635d5702bSmrg	    then
34735d5702bSmrg	      if test -z "$dir_arg" || {
34835d5702bSmrg		   # Check for POSIX incompatibilities with -m.
34935d5702bSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35035d5702bSmrg		   # other-writeable bit of parent directory when it shouldn't.
35135d5702bSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35235d5702bSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35335d5702bSmrg		   case $ls_ld_tmpdir in
35435d5702bSmrg		     d????-?r-*) different_mode=700;;
35535d5702bSmrg		     d????-?--*) different_mode=755;;
35635d5702bSmrg		     *) false;;
35735d5702bSmrg		   esac &&
35835d5702bSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35935d5702bSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36035d5702bSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36135d5702bSmrg		   }
36235d5702bSmrg		 }
36335d5702bSmrg	      then posix_mkdir=:
36435d5702bSmrg	      fi
36535d5702bSmrg	      rmdir "$tmpdir/d" "$tmpdir"
36635d5702bSmrg	    else
36735d5702bSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
36835d5702bSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36935d5702bSmrg	    fi
37035d5702bSmrg	    trap '' 0;;
37135d5702bSmrg	esac;;
37235d5702bSmrg    esac
373a6fdc6faSmrg
37435d5702bSmrg    if
37535d5702bSmrg      $posix_mkdir && (
37635d5702bSmrg	umask $mkdir_umask &&
37735d5702bSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37835d5702bSmrg      )
37935d5702bSmrg    then :
38035d5702bSmrg    else
38135d5702bSmrg
38235d5702bSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38335d5702bSmrg      # or it failed possibly due to a race condition.  Create the
38435d5702bSmrg      # directory the slow way, step by step, checking for races as we go.
38535d5702bSmrg
38635d5702bSmrg      case $dstdir in
38735d5702bSmrg	/*) prefix='/';;
38835d5702bSmrg	-*) prefix='./';;
38935d5702bSmrg	*)  prefix='';;
39035d5702bSmrg      esac
39135d5702bSmrg
39235d5702bSmrg      eval "$initialize_posix_glob"
39335d5702bSmrg
39435d5702bSmrg      oIFS=$IFS
39535d5702bSmrg      IFS=/
39635d5702bSmrg      $posix_glob set -f
39735d5702bSmrg      set fnord $dstdir
39835d5702bSmrg      shift
39935d5702bSmrg      $posix_glob set +f
40035d5702bSmrg      IFS=$oIFS
40135d5702bSmrg
40235d5702bSmrg      prefixes=
40335d5702bSmrg
40435d5702bSmrg      for d
40535d5702bSmrg      do
40635d5702bSmrg	test -z "$d" && continue
40735d5702bSmrg
40835d5702bSmrg	prefix=$prefix$d
40935d5702bSmrg	if test -d "$prefix"; then
41035d5702bSmrg	  prefixes=
41135d5702bSmrg	else
41235d5702bSmrg	  if $posix_mkdir; then
41335d5702bSmrg	    (umask=$mkdir_umask &&
41435d5702bSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41535d5702bSmrg	    # Don't fail if two instances are running concurrently.
41635d5702bSmrg	    test -d "$prefix" || exit 1
41735d5702bSmrg	  else
41835d5702bSmrg	    case $prefix in
41935d5702bSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42035d5702bSmrg	      *) qprefix=$prefix;;
42135d5702bSmrg	    esac
42235d5702bSmrg	    prefixes="$prefixes '$qprefix'"
42335d5702bSmrg	  fi
42435d5702bSmrg	fi
42535d5702bSmrg	prefix=$prefix/
42635d5702bSmrg      done
42735d5702bSmrg
42835d5702bSmrg      if test -n "$prefixes"; then
42935d5702bSmrg	# Don't fail if two instances are running concurrently.
43035d5702bSmrg	(umask $mkdir_umask &&
43135d5702bSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43235d5702bSmrg	  test -d "$dstdir" || exit 1
43335d5702bSmrg	obsolete_mkdir_used=true
43435d5702bSmrg      fi
43535d5702bSmrg    fi
43635d5702bSmrg  fi
43735d5702bSmrg
43835d5702bSmrg  if test -n "$dir_arg"; then
43935d5702bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44035d5702bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44135d5702bSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44235d5702bSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44335d5702bSmrg  else
44435d5702bSmrg
44535d5702bSmrg    # Make a couple of temp file names in the proper directory.
44635d5702bSmrg    dsttmp=$dstdir/_inst.$$_
44735d5702bSmrg    rmtmp=$dstdir/_rm.$$_
44835d5702bSmrg
44935d5702bSmrg    # Trap to clean up those temp files at exit.
45035d5702bSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45135d5702bSmrg
45235d5702bSmrg    # Copy the file name to the temp name.
45335d5702bSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45435d5702bSmrg
45535d5702bSmrg    # and set any options; do chmod last to preserve setuid bits.
45635d5702bSmrg    #
45735d5702bSmrg    # If any of these fail, we abort the whole thing.  If we want to
45835d5702bSmrg    # ignore errors from any of these, just make sure not to ignore
45935d5702bSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46035d5702bSmrg    #
46135d5702bSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46235d5702bSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46335d5702bSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46435d5702bSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46535d5702bSmrg
46635d5702bSmrg    # If -C, don't bother to copy if it wouldn't change the file.
46735d5702bSmrg    if $copy_on_change &&
46835d5702bSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46935d5702bSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47035d5702bSmrg
47135d5702bSmrg       eval "$initialize_posix_glob" &&
47235d5702bSmrg       $posix_glob set -f &&
47335d5702bSmrg       set X $old && old=:$2:$4:$5:$6 &&
47435d5702bSmrg       set X $new && new=:$2:$4:$5:$6 &&
47535d5702bSmrg       $posix_glob set +f &&
47635d5702bSmrg
47735d5702bSmrg       test "$old" = "$new" &&
47835d5702bSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47935d5702bSmrg    then
48035d5702bSmrg      rm -f "$dsttmp"
48135d5702bSmrg    else
48235d5702bSmrg      # Rename the file to the real destination.
48335d5702bSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48435d5702bSmrg
48535d5702bSmrg      # The rename failed, perhaps because mv can't rename something else
48635d5702bSmrg      # to itself, or perhaps because mv is so ancient that it does not
48735d5702bSmrg      # support -f.
48835d5702bSmrg      {
48935d5702bSmrg	# Now remove or move aside any old file at destination location.
49035d5702bSmrg	# We try this two ways since rm can't unlink itself on some
49135d5702bSmrg	# systems and the destination file might be busy for other
49235d5702bSmrg	# reasons.  In this case, the final cleanup might fail but the new
49335d5702bSmrg	# file should still install successfully.
49435d5702bSmrg	{
49535d5702bSmrg	  test ! -f "$dst" ||
49635d5702bSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49735d5702bSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49835d5702bSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49935d5702bSmrg	  } ||
50035d5702bSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
50135d5702bSmrg	    (exit 1); exit 1
50235d5702bSmrg	  }
50335d5702bSmrg	} &&
50435d5702bSmrg
50535d5702bSmrg	# Now rename the file to the real destination.
50635d5702bSmrg	$doit $mvcmd "$dsttmp" "$dst"
50735d5702bSmrg      }
50835d5702bSmrg    fi || exit 1
50935d5702bSmrg
51035d5702bSmrg    trap '' 0
51135d5702bSmrg  fi
51235d5702bSmrgdone
513a6fdc6faSmrg
51435d5702bSmrg# Local variables:
51535d5702bSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51635d5702bSmrg# time-stamp-start: "scriptversion="
51735d5702bSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51835d5702bSmrg# time-stamp-time-zone: "UTC"
51935d5702bSmrg# time-stamp-end: "; # UTC"
52035d5702bSmrg# End:
521