1cacd992dSmrg#!/bin/sh
2cacd992dSmrg# install - install a program, script, or datafile
314ddf674Smrg
46758a055Smrgscriptversion=2011-01-19.21; # UTC
514ddf674Smrg
6cacd992dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7cacd992dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8cacd992dSmrg# following copyright and license.
9cacd992dSmrg#
10cacd992dSmrg# Copyright (C) 1994 X Consortium
11cacd992dSmrg#
12cacd992dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13cacd992dSmrg# of this software and associated documentation files (the "Software"), to
14cacd992dSmrg# deal in the Software without restriction, including without limitation the
15cacd992dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16cacd992dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17cacd992dSmrg# furnished to do so, subject to the following conditions:
18cacd992dSmrg#
19cacd992dSmrg# The above copyright notice and this permission notice shall be included in
20cacd992dSmrg# all copies or substantial portions of the Software.
21cacd992dSmrg#
22cacd992dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23cacd992dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24cacd992dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25cacd992dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26cacd992dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27cacd992dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28cacd992dSmrg#
29cacd992dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30cacd992dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31cacd992dSmrg# ings in this Software without prior written authorization from the X Consor-
32cacd992dSmrg# tium.
33cacd992dSmrg#
34cacd992dSmrg#
35cacd992dSmrg# FSF changes to this file are in the public domain.
36cacd992dSmrg#
37cacd992dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38cacd992dSmrg# `make' implicit rules from creating a file called install from it
39cacd992dSmrg# when there is no Makefile.
40cacd992dSmrg#
41cacd992dSmrg# This script is compatible with the BSD install script, but was written
4214ddf674Smrg# from scratch.
43cacd992dSmrg
4414ddf674Smrgnl='
4514ddf674Smrg'
4614ddf674SmrgIFS=" ""	$nl"
47cacd992dSmrg
48cacd992dSmrg# set DOITPROG to echo to test this script
49cacd992dSmrg
50cacd992dSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5114ddf674Smrgdoit=${DOITPROG-}
5214ddf674Smrgif test -z "$doit"; then
5314ddf674Smrg  doit_exec=exec
54cacd992dSmrgelse
5514ddf674Smrg  doit_exec=$doit
56cacd992dSmrgfi
57cacd992dSmrg
5814ddf674Smrg# Put in absolute file names if you don't have them in your path;
5914ddf674Smrg# or use environment vars.
6014ddf674Smrg
6114ddf674Smrgchgrpprog=${CHGRPPROG-chgrp}
6214ddf674Smrgchmodprog=${CHMODPROG-chmod}
6314ddf674Smrgchownprog=${CHOWNPROG-chown}
6414ddf674Smrgcmpprog=${CMPPROG-cmp}
6514ddf674Smrgcpprog=${CPPROG-cp}
6614ddf674Smrgmkdirprog=${MKDIRPROG-mkdir}
6714ddf674Smrgmvprog=${MVPROG-mv}
6814ddf674Smrgrmprog=${RMPROG-rm}
6914ddf674Smrgstripprog=${STRIPPROG-strip}
7014ddf674Smrg
7114ddf674Smrgposix_glob='?'
7214ddf674Smrginitialize_posix_glob='
7314ddf674Smrg  test "$posix_glob" != "?" || {
7414ddf674Smrg    if (set -f) 2>/dev/null; then
7514ddf674Smrg      posix_glob=
7614ddf674Smrg    else
7714ddf674Smrg      posix_glob=:
7814ddf674Smrg    fi
7914ddf674Smrg  }
8014ddf674Smrg'
8114ddf674Smrg
8214ddf674Smrgposix_mkdir=
8314ddf674Smrg
8414ddf674Smrg# Desired mode of installed file.
8514ddf674Smrgmode=0755
8614ddf674Smrg
8714ddf674Smrgchgrpcmd=
8814ddf674Smrgchmodcmd=$chmodprog
8914ddf674Smrgchowncmd=
9014ddf674Smrgmvcmd=$mvprog
9114ddf674Smrgrmcmd="$rmprog -f"
9214ddf674Smrgstripcmd=
93cacd992dSmrg
9414ddf674Smrgsrc=
9514ddf674Smrgdst=
9614ddf674Smrgdir_arg=
9714ddf674Smrgdst_arg=
98cacd992dSmrg
9914ddf674Smrgcopy_on_change=false
10014ddf674Smrgno_target_directory=
101cacd992dSmrg
10214ddf674Smrgusage="\
10314ddf674SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10414ddf674Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10514ddf674Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10614ddf674Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107cacd992dSmrg
10814ddf674SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10914ddf674SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11014ddf674SmrgIn the 4th, create DIRECTORIES.
111cacd992dSmrg
11214ddf674SmrgOptions:
11314ddf674Smrg     --help     display this help and exit.
11414ddf674Smrg     --version  display version info and exit.
115cacd992dSmrg
11614ddf674Smrg  -c            (ignored)
11714ddf674Smrg  -C            install only if different (preserve the last data modification time)
11814ddf674Smrg  -d            create directories instead of installing files.
11914ddf674Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12014ddf674Smrg  -m MODE       $chmodprog installed files to MODE.
12114ddf674Smrg  -o USER       $chownprog installed files to USER.
12214ddf674Smrg  -s            $stripprog installed files.
12314ddf674Smrg  -t DIRECTORY  install into DIRECTORY.
12414ddf674Smrg  -T            report an error if DSTFILE is a directory.
125cacd992dSmrg
12614ddf674SmrgEnvironment variables override the default commands:
12714ddf674Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12814ddf674Smrg  RMPROG STRIPPROG
12914ddf674Smrg"
130cacd992dSmrg
13114ddf674Smrgwhile test $# -ne 0; do
13214ddf674Smrg  case $1 in
13314ddf674Smrg    -c) ;;
134cacd992dSmrg
13514ddf674Smrg    -C) copy_on_change=true;;
136cacd992dSmrg
13714ddf674Smrg    -d) dir_arg=true;;
138cacd992dSmrg
13914ddf674Smrg    -g) chgrpcmd="$chgrpprog $2"
14014ddf674Smrg	shift;;
141cacd992dSmrg
14214ddf674Smrg    --help) echo "$usage"; exit $?;;
143cacd992dSmrg
14414ddf674Smrg    -m) mode=$2
14514ddf674Smrg	case $mode in
14614ddf674Smrg	  *' '* | *'	'* | *'
14714ddf674Smrg'*	  | *'*'* | *'?'* | *'['*)
14814ddf674Smrg	    echo "$0: invalid mode: $mode" >&2
14914ddf674Smrg	    exit 1;;
15014ddf674Smrg	esac
15114ddf674Smrg	shift;;
152cacd992dSmrg
15314ddf674Smrg    -o) chowncmd="$chownprog $2"
15414ddf674Smrg	shift;;
155cacd992dSmrg
15614ddf674Smrg    -s) stripcmd=$stripprog;;
157cacd992dSmrg
15814ddf674Smrg    -t) dst_arg=$2
1596758a055Smrg	# Protect names problematic for `test' and other utilities.
1606758a055Smrg	case $dst_arg in
1616758a055Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1626758a055Smrg	esac
16314ddf674Smrg	shift;;
164cacd992dSmrg
16514ddf674Smrg    -T) no_target_directory=true;;
166cacd992dSmrg
16714ddf674Smrg    --version) echo "$0 $scriptversion"; exit $?;;
168cacd992dSmrg
16914ddf674Smrg    --)	shift
17014ddf674Smrg	break;;
171cacd992dSmrg
17214ddf674Smrg    -*)	echo "$0: invalid option: $1" >&2
17314ddf674Smrg	exit 1;;
174cacd992dSmrg
17514ddf674Smrg    *)  break;;
17614ddf674Smrg  esac
17714ddf674Smrg  shift
17814ddf674Smrgdone
179cacd992dSmrg
18014ddf674Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
18114ddf674Smrg  # When -d is used, all remaining arguments are directories to create.
18214ddf674Smrg  # When -t is used, the destination is already specified.
18314ddf674Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18414ddf674Smrg  for arg
18514ddf674Smrg  do
18614ddf674Smrg    if test -n "$dst_arg"; then
18714ddf674Smrg      # $@ is not empty: it contains at least $arg.
18814ddf674Smrg      set fnord "$@" "$dst_arg"
18914ddf674Smrg      shift # fnord
19014ddf674Smrg    fi
19114ddf674Smrg    shift # arg
19214ddf674Smrg    dst_arg=$arg
1936758a055Smrg    # Protect names problematic for `test' and other utilities.
1946758a055Smrg    case $dst_arg in
1956758a055Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1966758a055Smrg    esac
19714ddf674Smrg  done
19814ddf674Smrgfi
199cacd992dSmrg
20014ddf674Smrgif test $# -eq 0; then
20114ddf674Smrg  if test -z "$dir_arg"; then
20214ddf674Smrg    echo "$0: no input file specified." >&2
20314ddf674Smrg    exit 1
20414ddf674Smrg  fi
20514ddf674Smrg  # It's OK to call `install-sh -d' without argument.
20614ddf674Smrg  # This can happen when creating conditional directories.
20714ddf674Smrg  exit 0
20814ddf674Smrgfi
209cacd992dSmrg
21014ddf674Smrgif test -z "$dir_arg"; then
2116758a055Smrg  do_exit='(exit $ret); exit $ret'
2126758a055Smrg  trap "ret=129; $do_exit" 1
2136758a055Smrg  trap "ret=130; $do_exit" 2
2146758a055Smrg  trap "ret=141; $do_exit" 13
2156758a055Smrg  trap "ret=143; $do_exit" 15
21614ddf674Smrg
21714ddf674Smrg  # Set umask so as not to create temps with too-generous modes.
21814ddf674Smrg  # However, 'strip' requires both read and write access to temps.
21914ddf674Smrg  case $mode in
22014ddf674Smrg    # Optimize common cases.
22114ddf674Smrg    *644) cp_umask=133;;
22214ddf674Smrg    *755) cp_umask=22;;
22314ddf674Smrg
22414ddf674Smrg    *[0-7])
22514ddf674Smrg      if test -z "$stripcmd"; then
22614ddf674Smrg	u_plus_rw=
22714ddf674Smrg      else
22814ddf674Smrg	u_plus_rw='% 200'
22914ddf674Smrg      fi
23014ddf674Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23114ddf674Smrg    *)
23214ddf674Smrg      if test -z "$stripcmd"; then
23314ddf674Smrg	u_plus_rw=
23414ddf674Smrg      else
23514ddf674Smrg	u_plus_rw=,u+rw
23614ddf674Smrg      fi
23714ddf674Smrg      cp_umask=$mode$u_plus_rw;;
23814ddf674Smrg  esac
23914ddf674Smrgfi
240cacd992dSmrg
24114ddf674Smrgfor src
24214ddf674Smrgdo
2436758a055Smrg  # Protect names problematic for `test' and other utilities.
24414ddf674Smrg  case $src in
2456758a055Smrg    -* | [=\(\)!]) src=./$src;;
24614ddf674Smrg  esac
24714ddf674Smrg
24814ddf674Smrg  if test -n "$dir_arg"; then
24914ddf674Smrg    dst=$src
25014ddf674Smrg    dstdir=$dst
25114ddf674Smrg    test -d "$dstdir"
25214ddf674Smrg    dstdir_status=$?
25314ddf674Smrg  else
25414ddf674Smrg
25514ddf674Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
25614ddf674Smrg    # might cause directories to be created, which would be especially bad
25714ddf674Smrg    # if $src (and thus $dsttmp) contains '*'.
25814ddf674Smrg    if test ! -f "$src" && test ! -d "$src"; then
25914ddf674Smrg      echo "$0: $src does not exist." >&2
26014ddf674Smrg      exit 1
26114ddf674Smrg    fi
26214ddf674Smrg
26314ddf674Smrg    if test -z "$dst_arg"; then
26414ddf674Smrg      echo "$0: no destination specified." >&2
26514ddf674Smrg      exit 1
26614ddf674Smrg    fi
26714ddf674Smrg    dst=$dst_arg
268cacd992dSmrg
26914ddf674Smrg    # If destination is a directory, append the input filename; won't work
27014ddf674Smrg    # if double slashes aren't ignored.
27114ddf674Smrg    if test -d "$dst"; then
27214ddf674Smrg      if test -n "$no_target_directory"; then
27314ddf674Smrg	echo "$0: $dst_arg: Is a directory" >&2
27414ddf674Smrg	exit 1
27514ddf674Smrg      fi
27614ddf674Smrg      dstdir=$dst
27714ddf674Smrg      dst=$dstdir/`basename "$src"`
27814ddf674Smrg      dstdir_status=0
27914ddf674Smrg    else
28014ddf674Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
28114ddf674Smrg      dstdir=`
28214ddf674Smrg	(dirname "$dst") 2>/dev/null ||
28314ddf674Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
28414ddf674Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
28514ddf674Smrg	     X"$dst" : 'X\(//\)$' \| \
28614ddf674Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28714ddf674Smrg	echo X"$dst" |
28814ddf674Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28914ddf674Smrg		   s//\1/
29014ddf674Smrg		   q
29114ddf674Smrg		 }
29214ddf674Smrg		 /^X\(\/\/\)[^/].*/{
29314ddf674Smrg		   s//\1/
29414ddf674Smrg		   q
29514ddf674Smrg		 }
29614ddf674Smrg		 /^X\(\/\/\)$/{
29714ddf674Smrg		   s//\1/
29814ddf674Smrg		   q
29914ddf674Smrg		 }
30014ddf674Smrg		 /^X\(\/\).*/{
30114ddf674Smrg		   s//\1/
30214ddf674Smrg		   q
30314ddf674Smrg		 }
30414ddf674Smrg		 s/.*/./; q'
30514ddf674Smrg      `
30614ddf674Smrg
30714ddf674Smrg      test -d "$dstdir"
30814ddf674Smrg      dstdir_status=$?
30914ddf674Smrg    fi
31014ddf674Smrg  fi
31114ddf674Smrg
31214ddf674Smrg  obsolete_mkdir_used=false
31314ddf674Smrg
31414ddf674Smrg  if test $dstdir_status != 0; then
31514ddf674Smrg    case $posix_mkdir in
31614ddf674Smrg      '')
31714ddf674Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31814ddf674Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31914ddf674Smrg	umask=`umask`
32014ddf674Smrg	case $stripcmd.$umask in
32114ddf674Smrg	  # Optimize common cases.
32214ddf674Smrg	  *[2367][2367]) mkdir_umask=$umask;;
32314ddf674Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
32414ddf674Smrg
32514ddf674Smrg	  *[0-7])
32614ddf674Smrg	    mkdir_umask=`expr $umask + 22 \
32714ddf674Smrg	      - $umask % 100 % 40 + $umask % 20 \
32814ddf674Smrg	      - $umask % 10 % 4 + $umask % 2
32914ddf674Smrg	    `;;
33014ddf674Smrg	  *) mkdir_umask=$umask,go-w;;
33114ddf674Smrg	esac
33214ddf674Smrg
33314ddf674Smrg	# With -d, create the new directory with the user-specified mode.
33414ddf674Smrg	# Otherwise, rely on $mkdir_umask.
33514ddf674Smrg	if test -n "$dir_arg"; then
33614ddf674Smrg	  mkdir_mode=-m$mode
337cacd992dSmrg	else
33814ddf674Smrg	  mkdir_mode=
339cacd992dSmrg	fi
340cacd992dSmrg
34114ddf674Smrg	posix_mkdir=false
34214ddf674Smrg	case $umask in
34314ddf674Smrg	  *[123567][0-7][0-7])
34414ddf674Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
34514ddf674Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
34614ddf674Smrg	    ;;
34714ddf674Smrg	  *)
34814ddf674Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34914ddf674Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
35014ddf674Smrg
35114ddf674Smrg	    if (umask $mkdir_umask &&
35214ddf674Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
35314ddf674Smrg	    then
35414ddf674Smrg	      if test -z "$dir_arg" || {
35514ddf674Smrg		   # Check for POSIX incompatibilities with -m.
35614ddf674Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35714ddf674Smrg		   # other-writeable bit of parent directory when it shouldn't.
35814ddf674Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35914ddf674Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
36014ddf674Smrg		   case $ls_ld_tmpdir in
36114ddf674Smrg		     d????-?r-*) different_mode=700;;
36214ddf674Smrg		     d????-?--*) different_mode=755;;
36314ddf674Smrg		     *) false;;
36414ddf674Smrg		   esac &&
36514ddf674Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
36614ddf674Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36714ddf674Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36814ddf674Smrg		   }
36914ddf674Smrg		 }
37014ddf674Smrg	      then posix_mkdir=:
37114ddf674Smrg	      fi
37214ddf674Smrg	      rmdir "$tmpdir/d" "$tmpdir"
37314ddf674Smrg	    else
37414ddf674Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
37514ddf674Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
37614ddf674Smrg	    fi
37714ddf674Smrg	    trap '' 0;;
37814ddf674Smrg	esac;;
37914ddf674Smrg    esac
380cacd992dSmrg
38114ddf674Smrg    if
38214ddf674Smrg      $posix_mkdir && (
38314ddf674Smrg	umask $mkdir_umask &&
38414ddf674Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
38514ddf674Smrg      )
38614ddf674Smrg    then :
38714ddf674Smrg    else
38814ddf674Smrg
38914ddf674Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
39014ddf674Smrg      # or it failed possibly due to a race condition.  Create the
39114ddf674Smrg      # directory the slow way, step by step, checking for races as we go.
39214ddf674Smrg
39314ddf674Smrg      case $dstdir in
39414ddf674Smrg	/*) prefix='/';;
3956758a055Smrg	[-=\(\)!]*) prefix='./';;
39614ddf674Smrg	*)  prefix='';;
39714ddf674Smrg      esac
39814ddf674Smrg
39914ddf674Smrg      eval "$initialize_posix_glob"
40014ddf674Smrg
40114ddf674Smrg      oIFS=$IFS
40214ddf674Smrg      IFS=/
40314ddf674Smrg      $posix_glob set -f
40414ddf674Smrg      set fnord $dstdir
40514ddf674Smrg      shift
40614ddf674Smrg      $posix_glob set +f
40714ddf674Smrg      IFS=$oIFS
40814ddf674Smrg
40914ddf674Smrg      prefixes=
41014ddf674Smrg
41114ddf674Smrg      for d
41214ddf674Smrg      do
4136758a055Smrg	test X"$d" = X && continue
41414ddf674Smrg
41514ddf674Smrg	prefix=$prefix$d
41614ddf674Smrg	if test -d "$prefix"; then
41714ddf674Smrg	  prefixes=
41814ddf674Smrg	else
41914ddf674Smrg	  if $posix_mkdir; then
42014ddf674Smrg	    (umask=$mkdir_umask &&
42114ddf674Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
42214ddf674Smrg	    # Don't fail if two instances are running concurrently.
42314ddf674Smrg	    test -d "$prefix" || exit 1
42414ddf674Smrg	  else
42514ddf674Smrg	    case $prefix in
42614ddf674Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42714ddf674Smrg	      *) qprefix=$prefix;;
42814ddf674Smrg	    esac
42914ddf674Smrg	    prefixes="$prefixes '$qprefix'"
43014ddf674Smrg	  fi
43114ddf674Smrg	fi
43214ddf674Smrg	prefix=$prefix/
43314ddf674Smrg      done
43414ddf674Smrg
43514ddf674Smrg      if test -n "$prefixes"; then
43614ddf674Smrg	# Don't fail if two instances are running concurrently.
43714ddf674Smrg	(umask $mkdir_umask &&
43814ddf674Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43914ddf674Smrg	  test -d "$dstdir" || exit 1
44014ddf674Smrg	obsolete_mkdir_used=true
44114ddf674Smrg      fi
44214ddf674Smrg    fi
44314ddf674Smrg  fi
44414ddf674Smrg
44514ddf674Smrg  if test -n "$dir_arg"; then
44614ddf674Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44714ddf674Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44814ddf674Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44914ddf674Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
45014ddf674Smrg  else
45114ddf674Smrg
45214ddf674Smrg    # Make a couple of temp file names in the proper directory.
45314ddf674Smrg    dsttmp=$dstdir/_inst.$$_
45414ddf674Smrg    rmtmp=$dstdir/_rm.$$_
45514ddf674Smrg
45614ddf674Smrg    # Trap to clean up those temp files at exit.
45714ddf674Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45814ddf674Smrg
45914ddf674Smrg    # Copy the file name to the temp name.
46014ddf674Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
46114ddf674Smrg
46214ddf674Smrg    # and set any options; do chmod last to preserve setuid bits.
46314ddf674Smrg    #
46414ddf674Smrg    # If any of these fail, we abort the whole thing.  If we want to
46514ddf674Smrg    # ignore errors from any of these, just make sure not to ignore
46614ddf674Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46714ddf674Smrg    #
46814ddf674Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46914ddf674Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
47014ddf674Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
47114ddf674Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
47214ddf674Smrg
47314ddf674Smrg    # If -C, don't bother to copy if it wouldn't change the file.
47414ddf674Smrg    if $copy_on_change &&
47514ddf674Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
47614ddf674Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47714ddf674Smrg
47814ddf674Smrg       eval "$initialize_posix_glob" &&
47914ddf674Smrg       $posix_glob set -f &&
48014ddf674Smrg       set X $old && old=:$2:$4:$5:$6 &&
48114ddf674Smrg       set X $new && new=:$2:$4:$5:$6 &&
48214ddf674Smrg       $posix_glob set +f &&
48314ddf674Smrg
48414ddf674Smrg       test "$old" = "$new" &&
48514ddf674Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
48614ddf674Smrg    then
48714ddf674Smrg      rm -f "$dsttmp"
48814ddf674Smrg    else
48914ddf674Smrg      # Rename the file to the real destination.
49014ddf674Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
49114ddf674Smrg
49214ddf674Smrg      # The rename failed, perhaps because mv can't rename something else
49314ddf674Smrg      # to itself, or perhaps because mv is so ancient that it does not
49414ddf674Smrg      # support -f.
49514ddf674Smrg      {
49614ddf674Smrg	# Now remove or move aside any old file at destination location.
49714ddf674Smrg	# We try this two ways since rm can't unlink itself on some
49814ddf674Smrg	# systems and the destination file might be busy for other
49914ddf674Smrg	# reasons.  In this case, the final cleanup might fail but the new
50014ddf674Smrg	# file should still install successfully.
50114ddf674Smrg	{
50214ddf674Smrg	  test ! -f "$dst" ||
50314ddf674Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
50414ddf674Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
50514ddf674Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
50614ddf674Smrg	  } ||
50714ddf674Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50814ddf674Smrg	    (exit 1); exit 1
50914ddf674Smrg	  }
51014ddf674Smrg	} &&
51114ddf674Smrg
51214ddf674Smrg	# Now rename the file to the real destination.
51314ddf674Smrg	$doit $mvcmd "$dsttmp" "$dst"
51414ddf674Smrg      }
51514ddf674Smrg    fi || exit 1
51614ddf674Smrg
51714ddf674Smrg    trap '' 0
51814ddf674Smrg  fi
51914ddf674Smrgdone
520cacd992dSmrg
52114ddf674Smrg# Local variables:
52214ddf674Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
52314ddf674Smrg# time-stamp-start: "scriptversion="
52414ddf674Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
52514ddf674Smrg# time-stamp-time-zone: "UTC"
52614ddf674Smrg# time-stamp-end: "; # UTC"
52714ddf674Smrg# End:
528