install-sh revision b4ee4795
11ab64890Smrg#!/bin/sh
21ab64890Smrg# install - install a program, script, or datafile
31ab64890Smrg
4b4ee4795Smrgscriptversion=2009-04-28.21; # UTC
51ab64890Smrg
61ab64890Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71ab64890Smrg# later released in X11R6 (xc/config/util/install.sh) with the
81ab64890Smrg# following copyright and license.
91ab64890Smrg#
101ab64890Smrg# Copyright (C) 1994 X Consortium
111ab64890Smrg#
121ab64890Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131ab64890Smrg# of this software and associated documentation files (the "Software"), to
141ab64890Smrg# deal in the Software without restriction, including without limitation the
151ab64890Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161ab64890Smrg# sell copies of the Software, and to permit persons to whom the Software is
171ab64890Smrg# furnished to do so, subject to the following conditions:
181ab64890Smrg#
191ab64890Smrg# The above copyright notice and this permission notice shall be included in
201ab64890Smrg# all copies or substantial portions of the Software.
211ab64890Smrg#
221ab64890Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231ab64890Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241ab64890Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251ab64890Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261ab64890Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271ab64890Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281ab64890Smrg#
291ab64890Smrg# Except as contained in this notice, the name of the X Consortium shall not
301ab64890Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
311ab64890Smrg# ings in this Software without prior written authorization from the X Consor-
321ab64890Smrg# tium.
331ab64890Smrg#
341ab64890Smrg#
351ab64890Smrg# FSF changes to this file are in the public domain.
361ab64890Smrg#
371ab64890Smrg# Calling this script install-sh is preferred over install.sh, to prevent
381ab64890Smrg# `make' implicit rules from creating a file called install from it
391ab64890Smrg# when there is no Makefile.
401ab64890Smrg#
411ab64890Smrg# This script is compatible with the BSD install script, but was written
421ab64890Smrg# from scratch.
431ab64890Smrg
441ab64890Smrgnl='
451ab64890Smrg'
461ab64890SmrgIFS=" ""	$nl"
471ab64890Smrg
481ab64890Smrg# set DOITPROG to echo to test this script
491ab64890Smrg
501ab64890Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
511ab64890Smrgdoit=${DOITPROG-}
521ab64890Smrgif test -z "$doit"; then
531ab64890Smrg  doit_exec=exec
541ab64890Smrgelse
551ab64890Smrg  doit_exec=$doit
561ab64890Smrgfi
571ab64890Smrg
581ab64890Smrg# Put in absolute file names if you don't have them in your path;
591ab64890Smrg# or use environment vars.
601ab64890Smrg
611ab64890Smrgchgrpprog=${CHGRPPROG-chgrp}
621ab64890Smrgchmodprog=${CHMODPROG-chmod}
631ab64890Smrgchownprog=${CHOWNPROG-chown}
641ab64890Smrgcmpprog=${CMPPROG-cmp}
651ab64890Smrgcpprog=${CPPROG-cp}
661ab64890Smrgmkdirprog=${MKDIRPROG-mkdir}
671ab64890Smrgmvprog=${MVPROG-mv}
681ab64890Smrgrmprog=${RMPROG-rm}
691ab64890Smrgstripprog=${STRIPPROG-strip}
701ab64890Smrg
711ab64890Smrgposix_glob='?'
721ab64890Smrginitialize_posix_glob='
731ab64890Smrg  test "$posix_glob" != "?" || {
741ab64890Smrg    if (set -f) 2>/dev/null; then
751ab64890Smrg      posix_glob=
761ab64890Smrg    else
771ab64890Smrg      posix_glob=:
781ab64890Smrg    fi
791ab64890Smrg  }
801ab64890Smrg'
811ab64890Smrg
821ab64890Smrgposix_mkdir=
831ab64890Smrg
841ab64890Smrg# Desired mode of installed file.
851ab64890Smrgmode=0755
861ab64890Smrg
871ab64890Smrgchgrpcmd=
881ab64890Smrgchmodcmd=$chmodprog
891ab64890Smrgchowncmd=
901ab64890Smrgmvcmd=$mvprog
911ab64890Smrgrmcmd="$rmprog -f"
921ab64890Smrgstripcmd=
931ab64890Smrg
941ab64890Smrgsrc=
951ab64890Smrgdst=
961ab64890Smrgdir_arg=
971ab64890Smrgdst_arg=
981ab64890Smrg
991ab64890Smrgcopy_on_change=false
1001ab64890Smrgno_target_directory=
1011ab64890Smrg
1021ab64890Smrgusage="\
1031ab64890SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1041ab64890Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1051ab64890Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1061ab64890Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1071ab64890Smrg
1081ab64890SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1091ab64890SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1101ab64890SmrgIn the 4th, create DIRECTORIES.
1111ab64890Smrg
1121ab64890SmrgOptions:
1131ab64890Smrg     --help     display this help and exit.
1141ab64890Smrg     --version  display version info and exit.
1151ab64890Smrg
1161ab64890Smrg  -c            (ignored)
1171ab64890Smrg  -C            install only if different (preserve the last data modification time)
1181ab64890Smrg  -d            create directories instead of installing files.
1191ab64890Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1201ab64890Smrg  -m MODE       $chmodprog installed files to MODE.
1211ab64890Smrg  -o USER       $chownprog installed files to USER.
1221ab64890Smrg  -s            $stripprog installed files.
1231ab64890Smrg  -t DIRECTORY  install into DIRECTORY.
1241ab64890Smrg  -T            report an error if DSTFILE is a directory.
1251ab64890Smrg
1261ab64890SmrgEnvironment variables override the default commands:
1271ab64890Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1281ab64890Smrg  RMPROG STRIPPROG
1291ab64890Smrg"
1301ab64890Smrg
1311ab64890Smrgwhile test $# -ne 0; do
1321ab64890Smrg  case $1 in
1331ab64890Smrg    -c) ;;
1341ab64890Smrg
1351ab64890Smrg    -C) copy_on_change=true;;
1361ab64890Smrg
1371ab64890Smrg    -d) dir_arg=true;;
1381ab64890Smrg
1391ab64890Smrg    -g) chgrpcmd="$chgrpprog $2"
1401ab64890Smrg	shift;;
1411ab64890Smrg
1421ab64890Smrg    --help) echo "$usage"; exit $?;;
1431ab64890Smrg
1441ab64890Smrg    -m) mode=$2
1451ab64890Smrg	case $mode in
1461ab64890Smrg	  *' '* | *'	'* | *'
1471ab64890Smrg'*	  | *'*'* | *'?'* | *'['*)
1481ab64890Smrg	    echo "$0: invalid mode: $mode" >&2
1491ab64890Smrg	    exit 1;;
1501ab64890Smrg	esac
1511ab64890Smrg	shift;;
1521ab64890Smrg
1531ab64890Smrg    -o) chowncmd="$chownprog $2"
1541ab64890Smrg	shift;;
1551ab64890Smrg
1561ab64890Smrg    -s) stripcmd=$stripprog;;
1571ab64890Smrg
1581ab64890Smrg    -t) dst_arg=$2
1591ab64890Smrg	shift;;
1601ab64890Smrg
1611ab64890Smrg    -T) no_target_directory=true;;
1621ab64890Smrg
1631ab64890Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1641ab64890Smrg
1651ab64890Smrg    --)	shift
1661ab64890Smrg	break;;
1671ab64890Smrg
1681ab64890Smrg    -*)	echo "$0: invalid option: $1" >&2
1691ab64890Smrg	exit 1;;
1701ab64890Smrg
1711ab64890Smrg    *)  break;;
1721ab64890Smrg  esac
1731ab64890Smrg  shift
1741ab64890Smrgdone
1751ab64890Smrg
1761ab64890Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1771ab64890Smrg  # When -d is used, all remaining arguments are directories to create.
1781ab64890Smrg  # When -t is used, the destination is already specified.
1791ab64890Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1801ab64890Smrg  for arg
1811ab64890Smrg  do
1821ab64890Smrg    if test -n "$dst_arg"; then
1831ab64890Smrg      # $@ is not empty: it contains at least $arg.
1841ab64890Smrg      set fnord "$@" "$dst_arg"
1851ab64890Smrg      shift # fnord
1861ab64890Smrg    fi
1871ab64890Smrg    shift # arg
1881ab64890Smrg    dst_arg=$arg
1891ab64890Smrg  done
1901ab64890Smrgfi
1911ab64890Smrg
1921ab64890Smrgif test $# -eq 0; then
1931ab64890Smrg  if test -z "$dir_arg"; then
1941ab64890Smrg    echo "$0: no input file specified." >&2
1951ab64890Smrg    exit 1
1961ab64890Smrg  fi
1971ab64890Smrg  # It's OK to call `install-sh -d' without argument.
1981ab64890Smrg  # This can happen when creating conditional directories.
1991ab64890Smrg  exit 0
2001ab64890Smrgfi
2011ab64890Smrg
2021ab64890Smrgif test -z "$dir_arg"; then
2031ab64890Smrg  trap '(exit $?); exit' 1 2 13 15
2041ab64890Smrg
2051ab64890Smrg  # Set umask so as not to create temps with too-generous modes.
2061ab64890Smrg  # However, 'strip' requires both read and write access to temps.
2071ab64890Smrg  case $mode in
2081ab64890Smrg    # Optimize common cases.
2091ab64890Smrg    *644) cp_umask=133;;
2101ab64890Smrg    *755) cp_umask=22;;
2111ab64890Smrg
2121ab64890Smrg    *[0-7])
2131ab64890Smrg      if test -z "$stripcmd"; then
2141ab64890Smrg	u_plus_rw=
2151ab64890Smrg      else
2161ab64890Smrg	u_plus_rw='% 200'
2171ab64890Smrg      fi
2181ab64890Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2191ab64890Smrg    *)
2201ab64890Smrg      if test -z "$stripcmd"; then
2211ab64890Smrg	u_plus_rw=
2221ab64890Smrg      else
2231ab64890Smrg	u_plus_rw=,u+rw
2241ab64890Smrg      fi
2251ab64890Smrg      cp_umask=$mode$u_plus_rw;;
2261ab64890Smrg  esac
2271ab64890Smrgfi
2281ab64890Smrg
2291ab64890Smrgfor src
2301ab64890Smrgdo
2311ab64890Smrg  # Protect names starting with `-'.
2321ab64890Smrg  case $src in
2331ab64890Smrg    -*) src=./$src;;
2341ab64890Smrg  esac
2351ab64890Smrg
2361ab64890Smrg  if test -n "$dir_arg"; then
2371ab64890Smrg    dst=$src
2381ab64890Smrg    dstdir=$dst
2391ab64890Smrg    test -d "$dstdir"
2401ab64890Smrg    dstdir_status=$?
2411ab64890Smrg  else
2421ab64890Smrg
2431ab64890Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2441ab64890Smrg    # might cause directories to be created, which would be especially bad
2451ab64890Smrg    # if $src (and thus $dsttmp) contains '*'.
2461ab64890Smrg    if test ! -f "$src" && test ! -d "$src"; then
2471ab64890Smrg      echo "$0: $src does not exist." >&2
2481ab64890Smrg      exit 1
2491ab64890Smrg    fi
2501ab64890Smrg
2511ab64890Smrg    if test -z "$dst_arg"; then
2521ab64890Smrg      echo "$0: no destination specified." >&2
2531ab64890Smrg      exit 1
2541ab64890Smrg    fi
2551ab64890Smrg
2561ab64890Smrg    dst=$dst_arg
2571ab64890Smrg    # Protect names starting with `-'.
2581ab64890Smrg    case $dst in
2591ab64890Smrg      -*) dst=./$dst;;
2601ab64890Smrg    esac
2611ab64890Smrg
2621ab64890Smrg    # If destination is a directory, append the input filename; won't work
2631ab64890Smrg    # if double slashes aren't ignored.
2641ab64890Smrg    if test -d "$dst"; then
2651ab64890Smrg      if test -n "$no_target_directory"; then
2661ab64890Smrg	echo "$0: $dst_arg: Is a directory" >&2
2671ab64890Smrg	exit 1
2681ab64890Smrg      fi
2691ab64890Smrg      dstdir=$dst
2701ab64890Smrg      dst=$dstdir/`basename "$src"`
2711ab64890Smrg      dstdir_status=0
2721ab64890Smrg    else
2731ab64890Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2741ab64890Smrg      dstdir=`
2751ab64890Smrg	(dirname "$dst") 2>/dev/null ||
2761ab64890Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2771ab64890Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2781ab64890Smrg	     X"$dst" : 'X\(//\)$' \| \
2791ab64890Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2801ab64890Smrg	echo X"$dst" |
2811ab64890Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2821ab64890Smrg		   s//\1/
2831ab64890Smrg		   q
2841ab64890Smrg		 }
2851ab64890Smrg		 /^X\(\/\/\)[^/].*/{
2861ab64890Smrg		   s//\1/
2871ab64890Smrg		   q
2881ab64890Smrg		 }
2891ab64890Smrg		 /^X\(\/\/\)$/{
2901ab64890Smrg		   s//\1/
2911ab64890Smrg		   q
2921ab64890Smrg		 }
2931ab64890Smrg		 /^X\(\/\).*/{
2941ab64890Smrg		   s//\1/
2951ab64890Smrg		   q
2961ab64890Smrg		 }
2971ab64890Smrg		 s/.*/./; q'
2981ab64890Smrg      `
2991ab64890Smrg
3001ab64890Smrg      test -d "$dstdir"
3011ab64890Smrg      dstdir_status=$?
3021ab64890Smrg    fi
3031ab64890Smrg  fi
3041ab64890Smrg
3051ab64890Smrg  obsolete_mkdir_used=false
3061ab64890Smrg
3071ab64890Smrg  if test $dstdir_status != 0; then
3081ab64890Smrg    case $posix_mkdir in
3091ab64890Smrg      '')
3101ab64890Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3111ab64890Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3121ab64890Smrg	umask=`umask`
3131ab64890Smrg	case $stripcmd.$umask in
3141ab64890Smrg	  # Optimize common cases.
3151ab64890Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3161ab64890Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3171ab64890Smrg
3181ab64890Smrg	  *[0-7])
3191ab64890Smrg	    mkdir_umask=`expr $umask + 22 \
3201ab64890Smrg	      - $umask % 100 % 40 + $umask % 20 \
3211ab64890Smrg	      - $umask % 10 % 4 + $umask % 2
3221ab64890Smrg	    `;;
3231ab64890Smrg	  *) mkdir_umask=$umask,go-w;;
3241ab64890Smrg	esac
3251ab64890Smrg
3261ab64890Smrg	# With -d, create the new directory with the user-specified mode.
3271ab64890Smrg	# Otherwise, rely on $mkdir_umask.
3281ab64890Smrg	if test -n "$dir_arg"; then
3291ab64890Smrg	  mkdir_mode=-m$mode
3301ab64890Smrg	else
3311ab64890Smrg	  mkdir_mode=
3321ab64890Smrg	fi
3331ab64890Smrg
3341ab64890Smrg	posix_mkdir=false
3351ab64890Smrg	case $umask in
3361ab64890Smrg	  *[123567][0-7][0-7])
3371ab64890Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3381ab64890Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3391ab64890Smrg	    ;;
3401ab64890Smrg	  *)
3411ab64890Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3421ab64890Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3431ab64890Smrg
3441ab64890Smrg	    if (umask $mkdir_umask &&
3451ab64890Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3461ab64890Smrg	    then
3471ab64890Smrg	      if test -z "$dir_arg" || {
3481ab64890Smrg		   # Check for POSIX incompatibilities with -m.
3491ab64890Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3501ab64890Smrg		   # other-writeable bit of parent directory when it shouldn't.
3511ab64890Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3521ab64890Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3531ab64890Smrg		   case $ls_ld_tmpdir in
3541ab64890Smrg		     d????-?r-*) different_mode=700;;
3551ab64890Smrg		     d????-?--*) different_mode=755;;
3561ab64890Smrg		     *) false;;
3571ab64890Smrg		   esac &&
3581ab64890Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3591ab64890Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3601ab64890Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3611ab64890Smrg		   }
3621ab64890Smrg		 }
3631ab64890Smrg	      then posix_mkdir=:
3641ab64890Smrg	      fi
3651ab64890Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3661ab64890Smrg	    else
3671ab64890Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3681ab64890Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3691ab64890Smrg	    fi
3701ab64890Smrg	    trap '' 0;;
3711ab64890Smrg	esac;;
3721ab64890Smrg    esac
3731ab64890Smrg
3741ab64890Smrg    if
3751ab64890Smrg      $posix_mkdir && (
3761ab64890Smrg	umask $mkdir_umask &&
3771ab64890Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3781ab64890Smrg      )
3791ab64890Smrg    then :
3801ab64890Smrg    else
3811ab64890Smrg
3821ab64890Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3831ab64890Smrg      # or it failed possibly due to a race condition.  Create the
3841ab64890Smrg      # directory the slow way, step by step, checking for races as we go.
3851ab64890Smrg
3861ab64890Smrg      case $dstdir in
3871ab64890Smrg	/*) prefix='/';;
3881ab64890Smrg	-*) prefix='./';;
3891ab64890Smrg	*)  prefix='';;
3901ab64890Smrg      esac
3911ab64890Smrg
3921ab64890Smrg      eval "$initialize_posix_glob"
3931ab64890Smrg
3941ab64890Smrg      oIFS=$IFS
3951ab64890Smrg      IFS=/
3961ab64890Smrg      $posix_glob set -f
3971ab64890Smrg      set fnord $dstdir
3981ab64890Smrg      shift
3991ab64890Smrg      $posix_glob set +f
4001ab64890Smrg      IFS=$oIFS
4011ab64890Smrg
4021ab64890Smrg      prefixes=
4031ab64890Smrg
4041ab64890Smrg      for d
4051ab64890Smrg      do
4061ab64890Smrg	test -z "$d" && continue
4071ab64890Smrg
4081ab64890Smrg	prefix=$prefix$d
4091ab64890Smrg	if test -d "$prefix"; then
4101ab64890Smrg	  prefixes=
4111ab64890Smrg	else
4121ab64890Smrg	  if $posix_mkdir; then
4131ab64890Smrg	    (umask=$mkdir_umask &&
4141ab64890Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4151ab64890Smrg	    # Don't fail if two instances are running concurrently.
4161ab64890Smrg	    test -d "$prefix" || exit 1
4171ab64890Smrg	  else
4181ab64890Smrg	    case $prefix in
4191ab64890Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4201ab64890Smrg	      *) qprefix=$prefix;;
4211ab64890Smrg	    esac
4221ab64890Smrg	    prefixes="$prefixes '$qprefix'"
4231ab64890Smrg	  fi
4241ab64890Smrg	fi
4251ab64890Smrg	prefix=$prefix/
4261ab64890Smrg      done
4271ab64890Smrg
4281ab64890Smrg      if test -n "$prefixes"; then
4291ab64890Smrg	# Don't fail if two instances are running concurrently.
4301ab64890Smrg	(umask $mkdir_umask &&
4311ab64890Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4321ab64890Smrg	  test -d "$dstdir" || exit 1
4331ab64890Smrg	obsolete_mkdir_used=true
4341ab64890Smrg      fi
4351ab64890Smrg    fi
4361ab64890Smrg  fi
4371ab64890Smrg
4381ab64890Smrg  if test -n "$dir_arg"; then
4391ab64890Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4401ab64890Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4411ab64890Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4421ab64890Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4431ab64890Smrg  else
4441ab64890Smrg
4451ab64890Smrg    # Make a couple of temp file names in the proper directory.
4461ab64890Smrg    dsttmp=$dstdir/_inst.$$_
4471ab64890Smrg    rmtmp=$dstdir/_rm.$$_
4481ab64890Smrg
4491ab64890Smrg    # Trap to clean up those temp files at exit.
4501ab64890Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4511ab64890Smrg
4521ab64890Smrg    # Copy the file name to the temp name.
4531ab64890Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4541ab64890Smrg
4551ab64890Smrg    # and set any options; do chmod last to preserve setuid bits.
4561ab64890Smrg    #
4571ab64890Smrg    # If any of these fail, we abort the whole thing.  If we want to
4581ab64890Smrg    # ignore errors from any of these, just make sure not to ignore
4591ab64890Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4601ab64890Smrg    #
4611ab64890Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4621ab64890Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4631ab64890Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4641ab64890Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4651ab64890Smrg
4661ab64890Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4671ab64890Smrg    if $copy_on_change &&
4681ab64890Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4691ab64890Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4701ab64890Smrg
4711ab64890Smrg       eval "$initialize_posix_glob" &&
4721ab64890Smrg       $posix_glob set -f &&
4731ab64890Smrg       set X $old && old=:$2:$4:$5:$6 &&
4741ab64890Smrg       set X $new && new=:$2:$4:$5:$6 &&
4751ab64890Smrg       $posix_glob set +f &&
4761ab64890Smrg
4771ab64890Smrg       test "$old" = "$new" &&
4781ab64890Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4791ab64890Smrg    then
4801ab64890Smrg      rm -f "$dsttmp"
4811ab64890Smrg    else
4821ab64890Smrg      # Rename the file to the real destination.
4831ab64890Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4841ab64890Smrg
4851ab64890Smrg      # The rename failed, perhaps because mv can't rename something else
4861ab64890Smrg      # to itself, or perhaps because mv is so ancient that it does not
4871ab64890Smrg      # support -f.
4881ab64890Smrg      {
4891ab64890Smrg	# Now remove or move aside any old file at destination location.
4901ab64890Smrg	# We try this two ways since rm can't unlink itself on some
4911ab64890Smrg	# systems and the destination file might be busy for other
4921ab64890Smrg	# reasons.  In this case, the final cleanup might fail but the new
4931ab64890Smrg	# file should still install successfully.
4941ab64890Smrg	{
4951ab64890Smrg	  test ! -f "$dst" ||
4961ab64890Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4971ab64890Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4981ab64890Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4991ab64890Smrg	  } ||
5001ab64890Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5011ab64890Smrg	    (exit 1); exit 1
5021ab64890Smrg	  }
5031ab64890Smrg	} &&
5041ab64890Smrg
5051ab64890Smrg	# Now rename the file to the real destination.
5061ab64890Smrg	$doit $mvcmd "$dsttmp" "$dst"
5071ab64890Smrg      }
5081ab64890Smrg    fi || exit 1
5091ab64890Smrg
5101ab64890Smrg    trap '' 0
5111ab64890Smrg  fi
5121ab64890Smrgdone
5131ab64890Smrg
5141ab64890Smrg# Local variables:
5151ab64890Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5161ab64890Smrg# time-stamp-start: "scriptversion="
5171ab64890Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518b4ee4795Smrg# time-stamp-time-zone: "UTC"
519b4ee4795Smrg# time-stamp-end: "; # UTC"
5201ab64890Smrg# End:
521