install-sh revision 8f65982a
19aa228fdSmrg#!/bin/sh
29aa228fdSmrg# install - install a program, script, or datafile
39aa228fdSmrg
48f65982aSmrgscriptversion=2009-04-28.21; # UTC
59aa228fdSmrg
69aa228fdSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
79aa228fdSmrg# later released in X11R6 (xc/config/util/install.sh) with the
89aa228fdSmrg# following copyright and license.
99aa228fdSmrg#
109aa228fdSmrg# Copyright (C) 1994 X Consortium
119aa228fdSmrg#
129aa228fdSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
139aa228fdSmrg# of this software and associated documentation files (the "Software"), to
149aa228fdSmrg# deal in the Software without restriction, including without limitation the
159aa228fdSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
169aa228fdSmrg# sell copies of the Software, and to permit persons to whom the Software is
179aa228fdSmrg# furnished to do so, subject to the following conditions:
189aa228fdSmrg#
199aa228fdSmrg# The above copyright notice and this permission notice shall be included in
209aa228fdSmrg# all copies or substantial portions of the Software.
219aa228fdSmrg#
229aa228fdSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239aa228fdSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249aa228fdSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
259aa228fdSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
269aa228fdSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
279aa228fdSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
289aa228fdSmrg#
299aa228fdSmrg# Except as contained in this notice, the name of the X Consortium shall not
309aa228fdSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
319aa228fdSmrg# ings in this Software without prior written authorization from the X Consor-
329aa228fdSmrg# tium.
339aa228fdSmrg#
349aa228fdSmrg#
359aa228fdSmrg# FSF changes to this file are in the public domain.
369aa228fdSmrg#
379aa228fdSmrg# Calling this script install-sh is preferred over install.sh, to prevent
389aa228fdSmrg# `make' implicit rules from creating a file called install from it
399aa228fdSmrg# when there is no Makefile.
409aa228fdSmrg#
419aa228fdSmrg# This script is compatible with the BSD install script, but was written
429aa228fdSmrg# from scratch.
439aa228fdSmrg
449aa228fdSmrgnl='
459aa228fdSmrg'
469aa228fdSmrgIFS=" ""	$nl"
479aa228fdSmrg
489aa228fdSmrg# set DOITPROG to echo to test this script
499aa228fdSmrg
509aa228fdSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
518f65982aSmrgdoit=${DOITPROG-}
529aa228fdSmrgif test -z "$doit"; then
539aa228fdSmrg  doit_exec=exec
549aa228fdSmrgelse
559aa228fdSmrg  doit_exec=$doit
569aa228fdSmrgfi
579aa228fdSmrg
589aa228fdSmrg# Put in absolute file names if you don't have them in your path;
599aa228fdSmrg# or use environment vars.
609aa228fdSmrg
618f65982aSmrgchgrpprog=${CHGRPPROG-chgrp}
628f65982aSmrgchmodprog=${CHMODPROG-chmod}
638f65982aSmrgchownprog=${CHOWNPROG-chown}
648f65982aSmrgcmpprog=${CMPPROG-cmp}
658f65982aSmrgcpprog=${CPPROG-cp}
668f65982aSmrgmkdirprog=${MKDIRPROG-mkdir}
678f65982aSmrgmvprog=${MVPROG-mv}
688f65982aSmrgrmprog=${RMPROG-rm}
698f65982aSmrgstripprog=${STRIPPROG-strip}
708f65982aSmrg
718f65982aSmrgposix_glob='?'
728f65982aSmrginitialize_posix_glob='
738f65982aSmrg  test "$posix_glob" != "?" || {
748f65982aSmrg    if (set -f) 2>/dev/null; then
758f65982aSmrg      posix_glob=
768f65982aSmrg    else
778f65982aSmrg      posix_glob=:
788f65982aSmrg    fi
798f65982aSmrg  }
808f65982aSmrg'
819aa228fdSmrg
829aa228fdSmrgposix_mkdir=
839aa228fdSmrg
849aa228fdSmrg# Desired mode of installed file.
859aa228fdSmrgmode=0755
869aa228fdSmrg
878f65982aSmrgchgrpcmd=
889aa228fdSmrgchmodcmd=$chmodprog
899aa228fdSmrgchowncmd=
908f65982aSmrgmvcmd=$mvprog
919aa228fdSmrgrmcmd="$rmprog -f"
928f65982aSmrgstripcmd=
938f65982aSmrg
949aa228fdSmrgsrc=
959aa228fdSmrgdst=
969aa228fdSmrgdir_arg=
978f65982aSmrgdst_arg=
988f65982aSmrg
998f65982aSmrgcopy_on_change=false
1009aa228fdSmrgno_target_directory=
1019aa228fdSmrg
1028f65982aSmrgusage="\
1038f65982aSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1049aa228fdSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1059aa228fdSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1069aa228fdSmrg   or: $0 [OPTION]... -d DIRECTORIES...
1079aa228fdSmrg
1089aa228fdSmrgIn the 1st form, copy SRCFILE to DSTFILE.
1099aa228fdSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1109aa228fdSmrgIn the 4th, create DIRECTORIES.
1119aa228fdSmrg
1129aa228fdSmrgOptions:
1138f65982aSmrg     --help     display this help and exit.
1148f65982aSmrg     --version  display version info and exit.
1158f65982aSmrg
1168f65982aSmrg  -c            (ignored)
1178f65982aSmrg  -C            install only if different (preserve the last data modification time)
1188f65982aSmrg  -d            create directories instead of installing files.
1198f65982aSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1208f65982aSmrg  -m MODE       $chmodprog installed files to MODE.
1218f65982aSmrg  -o USER       $chownprog installed files to USER.
1228f65982aSmrg  -s            $stripprog installed files.
1238f65982aSmrg  -t DIRECTORY  install into DIRECTORY.
1248f65982aSmrg  -T            report an error if DSTFILE is a directory.
1259aa228fdSmrg
1269aa228fdSmrgEnvironment variables override the default commands:
1278f65982aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1288f65982aSmrg  RMPROG STRIPPROG
1299aa228fdSmrg"
1309aa228fdSmrg
1319aa228fdSmrgwhile test $# -ne 0; do
1329aa228fdSmrg  case $1 in
1338f65982aSmrg    -c) ;;
1348f65982aSmrg
1358f65982aSmrg    -C) copy_on_change=true;;
1369aa228fdSmrg
1378f65982aSmrg    -d) dir_arg=true;;
1389aa228fdSmrg
1399aa228fdSmrg    -g) chgrpcmd="$chgrpprog $2"
1408f65982aSmrg	shift;;
1419aa228fdSmrg
1429aa228fdSmrg    --help) echo "$usage"; exit $?;;
1439aa228fdSmrg
1449aa228fdSmrg    -m) mode=$2
1459aa228fdSmrg	case $mode in
1469aa228fdSmrg	  *' '* | *'	'* | *'
1479aa228fdSmrg'*	  | *'*'* | *'?'* | *'['*)
1489aa228fdSmrg	    echo "$0: invalid mode: $mode" >&2
1499aa228fdSmrg	    exit 1;;
1509aa228fdSmrg	esac
1518f65982aSmrg	shift;;
1529aa228fdSmrg
1539aa228fdSmrg    -o) chowncmd="$chownprog $2"
1548f65982aSmrg	shift;;
1559aa228fdSmrg
1568f65982aSmrg    -s) stripcmd=$stripprog;;
1579aa228fdSmrg
1588f65982aSmrg    -t) dst_arg=$2
1598f65982aSmrg	shift;;
1609aa228fdSmrg
1618f65982aSmrg    -T) no_target_directory=true;;
1629aa228fdSmrg
1639aa228fdSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1649aa228fdSmrg
1659aa228fdSmrg    --)	shift
1669aa228fdSmrg	break;;
1679aa228fdSmrg
1689aa228fdSmrg    -*)	echo "$0: invalid option: $1" >&2
1699aa228fdSmrg	exit 1;;
1709aa228fdSmrg
1719aa228fdSmrg    *)  break;;
1729aa228fdSmrg  esac
1738f65982aSmrg  shift
1749aa228fdSmrgdone
1759aa228fdSmrg
1768f65982aSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1779aa228fdSmrg  # When -d is used, all remaining arguments are directories to create.
1789aa228fdSmrg  # When -t is used, the destination is already specified.
1799aa228fdSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1809aa228fdSmrg  for arg
1819aa228fdSmrg  do
1828f65982aSmrg    if test -n "$dst_arg"; then
1839aa228fdSmrg      # $@ is not empty: it contains at least $arg.
1848f65982aSmrg      set fnord "$@" "$dst_arg"
1859aa228fdSmrg      shift # fnord
1869aa228fdSmrg    fi
1879aa228fdSmrg    shift # arg
1888f65982aSmrg    dst_arg=$arg
1899aa228fdSmrg  done
1909aa228fdSmrgfi
1919aa228fdSmrg
1929aa228fdSmrgif test $# -eq 0; then
1939aa228fdSmrg  if test -z "$dir_arg"; then
1949aa228fdSmrg    echo "$0: no input file specified." >&2
1959aa228fdSmrg    exit 1
1969aa228fdSmrg  fi
1979aa228fdSmrg  # It's OK to call `install-sh -d' without argument.
1989aa228fdSmrg  # This can happen when creating conditional directories.
1999aa228fdSmrg  exit 0
2009aa228fdSmrgfi
2019aa228fdSmrg
2029aa228fdSmrgif test -z "$dir_arg"; then
2039aa228fdSmrg  trap '(exit $?); exit' 1 2 13 15
2049aa228fdSmrg
2059aa228fdSmrg  # Set umask so as not to create temps with too-generous modes.
2069aa228fdSmrg  # However, 'strip' requires both read and write access to temps.
2079aa228fdSmrg  case $mode in
2089aa228fdSmrg    # Optimize common cases.
2099aa228fdSmrg    *644) cp_umask=133;;
2109aa228fdSmrg    *755) cp_umask=22;;
2119aa228fdSmrg
2129aa228fdSmrg    *[0-7])
2139aa228fdSmrg      if test -z "$stripcmd"; then
2149aa228fdSmrg	u_plus_rw=
2159aa228fdSmrg      else
2169aa228fdSmrg	u_plus_rw='% 200'
2179aa228fdSmrg      fi
2189aa228fdSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2199aa228fdSmrg    *)
2209aa228fdSmrg      if test -z "$stripcmd"; then
2219aa228fdSmrg	u_plus_rw=
2229aa228fdSmrg      else
2239aa228fdSmrg	u_plus_rw=,u+rw
2249aa228fdSmrg      fi
2259aa228fdSmrg      cp_umask=$mode$u_plus_rw;;
2269aa228fdSmrg  esac
2279aa228fdSmrgfi
2289aa228fdSmrg
2299aa228fdSmrgfor src
2309aa228fdSmrgdo
2319aa228fdSmrg  # Protect names starting with `-'.
2329aa228fdSmrg  case $src in
2338f65982aSmrg    -*) src=./$src;;
2349aa228fdSmrg  esac
2359aa228fdSmrg
2369aa228fdSmrg  if test -n "$dir_arg"; then
2379aa228fdSmrg    dst=$src
2389aa228fdSmrg    dstdir=$dst
2399aa228fdSmrg    test -d "$dstdir"
2409aa228fdSmrg    dstdir_status=$?
2419aa228fdSmrg  else
2429aa228fdSmrg
2439aa228fdSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2449aa228fdSmrg    # might cause directories to be created, which would be especially bad
2459aa228fdSmrg    # if $src (and thus $dsttmp) contains '*'.
2469aa228fdSmrg    if test ! -f "$src" && test ! -d "$src"; then
2479aa228fdSmrg      echo "$0: $src does not exist." >&2
2489aa228fdSmrg      exit 1
2499aa228fdSmrg    fi
2509aa228fdSmrg
2518f65982aSmrg    if test -z "$dst_arg"; then
2529aa228fdSmrg      echo "$0: no destination specified." >&2
2539aa228fdSmrg      exit 1
2549aa228fdSmrg    fi
2559aa228fdSmrg
2568f65982aSmrg    dst=$dst_arg
2579aa228fdSmrg    # Protect names starting with `-'.
2589aa228fdSmrg    case $dst in
2598f65982aSmrg      -*) dst=./$dst;;
2609aa228fdSmrg    esac
2619aa228fdSmrg
2629aa228fdSmrg    # If destination is a directory, append the input filename; won't work
2639aa228fdSmrg    # if double slashes aren't ignored.
2649aa228fdSmrg    if test -d "$dst"; then
2659aa228fdSmrg      if test -n "$no_target_directory"; then
2668f65982aSmrg	echo "$0: $dst_arg: Is a directory" >&2
2679aa228fdSmrg	exit 1
2689aa228fdSmrg      fi
2699aa228fdSmrg      dstdir=$dst
2709aa228fdSmrg      dst=$dstdir/`basename "$src"`
2719aa228fdSmrg      dstdir_status=0
2729aa228fdSmrg    else
2739aa228fdSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2749aa228fdSmrg      dstdir=`
2759aa228fdSmrg	(dirname "$dst") 2>/dev/null ||
2769aa228fdSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2779aa228fdSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
2789aa228fdSmrg	     X"$dst" : 'X\(//\)$' \| \
2799aa228fdSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2809aa228fdSmrg	echo X"$dst" |
2819aa228fdSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2829aa228fdSmrg		   s//\1/
2839aa228fdSmrg		   q
2849aa228fdSmrg		 }
2859aa228fdSmrg		 /^X\(\/\/\)[^/].*/{
2869aa228fdSmrg		   s//\1/
2879aa228fdSmrg		   q
2889aa228fdSmrg		 }
2899aa228fdSmrg		 /^X\(\/\/\)$/{
2909aa228fdSmrg		   s//\1/
2919aa228fdSmrg		   q
2929aa228fdSmrg		 }
2939aa228fdSmrg		 /^X\(\/\).*/{
2949aa228fdSmrg		   s//\1/
2959aa228fdSmrg		   q
2969aa228fdSmrg		 }
2979aa228fdSmrg		 s/.*/./; q'
2989aa228fdSmrg      `
2999aa228fdSmrg
3009aa228fdSmrg      test -d "$dstdir"
3019aa228fdSmrg      dstdir_status=$?
3029aa228fdSmrg    fi
3039aa228fdSmrg  fi
3049aa228fdSmrg
3059aa228fdSmrg  obsolete_mkdir_used=false
3069aa228fdSmrg
3079aa228fdSmrg  if test $dstdir_status != 0; then
3089aa228fdSmrg    case $posix_mkdir in
3099aa228fdSmrg      '')
3109aa228fdSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
3119aa228fdSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
3129aa228fdSmrg	umask=`umask`
3139aa228fdSmrg	case $stripcmd.$umask in
3149aa228fdSmrg	  # Optimize common cases.
3159aa228fdSmrg	  *[2367][2367]) mkdir_umask=$umask;;
3169aa228fdSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3179aa228fdSmrg
3189aa228fdSmrg	  *[0-7])
3199aa228fdSmrg	    mkdir_umask=`expr $umask + 22 \
3209aa228fdSmrg	      - $umask % 100 % 40 + $umask % 20 \
3219aa228fdSmrg	      - $umask % 10 % 4 + $umask % 2
3229aa228fdSmrg	    `;;
3239aa228fdSmrg	  *) mkdir_umask=$umask,go-w;;
3249aa228fdSmrg	esac
3259aa228fdSmrg
3269aa228fdSmrg	# With -d, create the new directory with the user-specified mode.
3279aa228fdSmrg	# Otherwise, rely on $mkdir_umask.
3289aa228fdSmrg	if test -n "$dir_arg"; then
3299aa228fdSmrg	  mkdir_mode=-m$mode
3309aa228fdSmrg	else
3319aa228fdSmrg	  mkdir_mode=
3329aa228fdSmrg	fi
3339aa228fdSmrg
3349aa228fdSmrg	posix_mkdir=false
3359aa228fdSmrg	case $umask in
3369aa228fdSmrg	  *[123567][0-7][0-7])
3379aa228fdSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3389aa228fdSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3399aa228fdSmrg	    ;;
3409aa228fdSmrg	  *)
3419aa228fdSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3429aa228fdSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3439aa228fdSmrg
3449aa228fdSmrg	    if (umask $mkdir_umask &&
3459aa228fdSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3469aa228fdSmrg	    then
3479aa228fdSmrg	      if test -z "$dir_arg" || {
3489aa228fdSmrg		   # Check for POSIX incompatibilities with -m.
3499aa228fdSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3509aa228fdSmrg		   # other-writeable bit of parent directory when it shouldn't.
3519aa228fdSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3529aa228fdSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3539aa228fdSmrg		   case $ls_ld_tmpdir in
3549aa228fdSmrg		     d????-?r-*) different_mode=700;;
3559aa228fdSmrg		     d????-?--*) different_mode=755;;
3569aa228fdSmrg		     *) false;;
3579aa228fdSmrg		   esac &&
3589aa228fdSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3599aa228fdSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3609aa228fdSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3619aa228fdSmrg		   }
3629aa228fdSmrg		 }
3639aa228fdSmrg	      then posix_mkdir=:
3649aa228fdSmrg	      fi
3659aa228fdSmrg	      rmdir "$tmpdir/d" "$tmpdir"
3669aa228fdSmrg	    else
3679aa228fdSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
3689aa228fdSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3699aa228fdSmrg	    fi
3709aa228fdSmrg	    trap '' 0;;
3719aa228fdSmrg	esac;;
3729aa228fdSmrg    esac
3739aa228fdSmrg
3749aa228fdSmrg    if
3759aa228fdSmrg      $posix_mkdir && (
3769aa228fdSmrg	umask $mkdir_umask &&
3779aa228fdSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3789aa228fdSmrg      )
3799aa228fdSmrg    then :
3809aa228fdSmrg    else
3819aa228fdSmrg
3829aa228fdSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3839aa228fdSmrg      # or it failed possibly due to a race condition.  Create the
3849aa228fdSmrg      # directory the slow way, step by step, checking for races as we go.
3859aa228fdSmrg
3869aa228fdSmrg      case $dstdir in
3878f65982aSmrg	/*) prefix='/';;
3888f65982aSmrg	-*) prefix='./';;
3898f65982aSmrg	*)  prefix='';;
3909aa228fdSmrg      esac
3919aa228fdSmrg
3928f65982aSmrg      eval "$initialize_posix_glob"
3939aa228fdSmrg
3949aa228fdSmrg      oIFS=$IFS
3959aa228fdSmrg      IFS=/
3968f65982aSmrg      $posix_glob set -f
3979aa228fdSmrg      set fnord $dstdir
3989aa228fdSmrg      shift
3998f65982aSmrg      $posix_glob set +f
4009aa228fdSmrg      IFS=$oIFS
4019aa228fdSmrg
4029aa228fdSmrg      prefixes=
4039aa228fdSmrg
4049aa228fdSmrg      for d
4059aa228fdSmrg      do
4069aa228fdSmrg	test -z "$d" && continue
4079aa228fdSmrg
4089aa228fdSmrg	prefix=$prefix$d
4099aa228fdSmrg	if test -d "$prefix"; then
4109aa228fdSmrg	  prefixes=
4119aa228fdSmrg	else
4129aa228fdSmrg	  if $posix_mkdir; then
4139aa228fdSmrg	    (umask=$mkdir_umask &&
4149aa228fdSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4159aa228fdSmrg	    # Don't fail if two instances are running concurrently.
4169aa228fdSmrg	    test -d "$prefix" || exit 1
4179aa228fdSmrg	  else
4189aa228fdSmrg	    case $prefix in
4199aa228fdSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4209aa228fdSmrg	      *) qprefix=$prefix;;
4219aa228fdSmrg	    esac
4229aa228fdSmrg	    prefixes="$prefixes '$qprefix'"
4239aa228fdSmrg	  fi
4249aa228fdSmrg	fi
4259aa228fdSmrg	prefix=$prefix/
4269aa228fdSmrg      done
4279aa228fdSmrg
4289aa228fdSmrg      if test -n "$prefixes"; then
4299aa228fdSmrg	# Don't fail if two instances are running concurrently.
4309aa228fdSmrg	(umask $mkdir_umask &&
4319aa228fdSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4329aa228fdSmrg	  test -d "$dstdir" || exit 1
4339aa228fdSmrg	obsolete_mkdir_used=true
4349aa228fdSmrg      fi
4359aa228fdSmrg    fi
4369aa228fdSmrg  fi
4379aa228fdSmrg
4389aa228fdSmrg  if test -n "$dir_arg"; then
4399aa228fdSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4409aa228fdSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4419aa228fdSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4429aa228fdSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4439aa228fdSmrg  else
4449aa228fdSmrg
4459aa228fdSmrg    # Make a couple of temp file names in the proper directory.
4469aa228fdSmrg    dsttmp=$dstdir/_inst.$$_
4479aa228fdSmrg    rmtmp=$dstdir/_rm.$$_
4489aa228fdSmrg
4499aa228fdSmrg    # Trap to clean up those temp files at exit.
4509aa228fdSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4519aa228fdSmrg
4529aa228fdSmrg    # Copy the file name to the temp name.
4539aa228fdSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4549aa228fdSmrg
4559aa228fdSmrg    # and set any options; do chmod last to preserve setuid bits.
4569aa228fdSmrg    #
4579aa228fdSmrg    # If any of these fail, we abort the whole thing.  If we want to
4589aa228fdSmrg    # ignore errors from any of these, just make sure not to ignore
4599aa228fdSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4609aa228fdSmrg    #
4618f65982aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4628f65982aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4638f65982aSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4648f65982aSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4658f65982aSmrg
4668f65982aSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4678f65982aSmrg    if $copy_on_change &&
4688f65982aSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4698f65982aSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4708f65982aSmrg
4718f65982aSmrg       eval "$initialize_posix_glob" &&
4728f65982aSmrg       $posix_glob set -f &&
4738f65982aSmrg       set X $old && old=:$2:$4:$5:$6 &&
4748f65982aSmrg       set X $new && new=:$2:$4:$5:$6 &&
4758f65982aSmrg       $posix_glob set +f &&
4768f65982aSmrg
4778f65982aSmrg       test "$old" = "$new" &&
4788f65982aSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4798f65982aSmrg    then
4808f65982aSmrg      rm -f "$dsttmp"
4818f65982aSmrg    else
4828f65982aSmrg      # Rename the file to the real destination.
4838f65982aSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4848f65982aSmrg
4858f65982aSmrg      # The rename failed, perhaps because mv can't rename something else
4868f65982aSmrg      # to itself, or perhaps because mv is so ancient that it does not
4878f65982aSmrg      # support -f.
4888f65982aSmrg      {
4898f65982aSmrg	# Now remove or move aside any old file at destination location.
4908f65982aSmrg	# We try this two ways since rm can't unlink itself on some
4918f65982aSmrg	# systems and the destination file might be busy for other
4928f65982aSmrg	# reasons.  In this case, the final cleanup might fail but the new
4938f65982aSmrg	# file should still install successfully.
4948f65982aSmrg	{
4958f65982aSmrg	  test ! -f "$dst" ||
4968f65982aSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4978f65982aSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4988f65982aSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4998f65982aSmrg	  } ||
5008f65982aSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
5018f65982aSmrg	    (exit 1); exit 1
5028f65982aSmrg	  }
5038f65982aSmrg	} &&
5048f65982aSmrg
5058f65982aSmrg	# Now rename the file to the real destination.
5068f65982aSmrg	$doit $mvcmd "$dsttmp" "$dst"
5078f65982aSmrg      }
5088f65982aSmrg    fi || exit 1
5099aa228fdSmrg
5109aa228fdSmrg    trap '' 0
5119aa228fdSmrg  fi
5129aa228fdSmrgdone
5139aa228fdSmrg
5149aa228fdSmrg# Local variables:
5159aa228fdSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5169aa228fdSmrg# time-stamp-start: "scriptversion="
5179aa228fdSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5188f65982aSmrg# time-stamp-time-zone: "UTC"
5198f65982aSmrg# time-stamp-end: "; # UTC"
5209aa228fdSmrg# End:
521