install-sh revision 8b6d6341
11abf7346Smrg#!/bin/sh
21abf7346Smrg# install - install a program, script, or datafile
31abf7346Smrg
48b6d6341Smrgscriptversion=2009-04-28.21; # UTC
51abf7346Smrg
61abf7346Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71abf7346Smrg# later released in X11R6 (xc/config/util/install.sh) with the
81abf7346Smrg# following copyright and license.
91abf7346Smrg#
101abf7346Smrg# Copyright (C) 1994 X Consortium
111abf7346Smrg#
121abf7346Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131abf7346Smrg# of this software and associated documentation files (the "Software"), to
141abf7346Smrg# deal in the Software without restriction, including without limitation the
151abf7346Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161abf7346Smrg# sell copies of the Software, and to permit persons to whom the Software is
171abf7346Smrg# furnished to do so, subject to the following conditions:
181abf7346Smrg#
191abf7346Smrg# The above copyright notice and this permission notice shall be included in
201abf7346Smrg# all copies or substantial portions of the Software.
211abf7346Smrg#
221abf7346Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231abf7346Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241abf7346Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251abf7346Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261abf7346Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271abf7346Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281abf7346Smrg#
291abf7346Smrg# Except as contained in this notice, the name of the X Consortium shall not
301abf7346Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
311abf7346Smrg# ings in this Software without prior written authorization from the X Consor-
321abf7346Smrg# tium.
331abf7346Smrg#
341abf7346Smrg#
351abf7346Smrg# FSF changes to this file are in the public domain.
361abf7346Smrg#
371abf7346Smrg# Calling this script install-sh is preferred over install.sh, to prevent
381abf7346Smrg# `make' implicit rules from creating a file called install from it
391abf7346Smrg# when there is no Makefile.
401abf7346Smrg#
411abf7346Smrg# This script is compatible with the BSD install script, but was written
421abf7346Smrg# from scratch.
431abf7346Smrg
441abf7346Smrgnl='
451abf7346Smrg'
461abf7346SmrgIFS=" ""	$nl"
471abf7346Smrg
481abf7346Smrg# set DOITPROG to echo to test this script
491abf7346Smrg
501abf7346Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
518b6d6341Smrgdoit=${DOITPROG-}
521abf7346Smrgif test -z "$doit"; then
531abf7346Smrg  doit_exec=exec
541abf7346Smrgelse
551abf7346Smrg  doit_exec=$doit
561abf7346Smrgfi
571abf7346Smrg
581abf7346Smrg# Put in absolute file names if you don't have them in your path;
591abf7346Smrg# or use environment vars.
601abf7346Smrg
618b6d6341Smrgchgrpprog=${CHGRPPROG-chgrp}
628b6d6341Smrgchmodprog=${CHMODPROG-chmod}
638b6d6341Smrgchownprog=${CHOWNPROG-chown}
648b6d6341Smrgcmpprog=${CMPPROG-cmp}
658b6d6341Smrgcpprog=${CPPROG-cp}
668b6d6341Smrgmkdirprog=${MKDIRPROG-mkdir}
678b6d6341Smrgmvprog=${MVPROG-mv}
688b6d6341Smrgrmprog=${RMPROG-rm}
698b6d6341Smrgstripprog=${STRIPPROG-strip}
708b6d6341Smrg
718b6d6341Smrgposix_glob='?'
728b6d6341Smrginitialize_posix_glob='
738b6d6341Smrg  test "$posix_glob" != "?" || {
748b6d6341Smrg    if (set -f) 2>/dev/null; then
758b6d6341Smrg      posix_glob=
768b6d6341Smrg    else
778b6d6341Smrg      posix_glob=:
788b6d6341Smrg    fi
798b6d6341Smrg  }
808b6d6341Smrg'
811abf7346Smrg
821abf7346Smrgposix_mkdir=
831abf7346Smrg
841abf7346Smrg# Desired mode of installed file.
851abf7346Smrgmode=0755
861abf7346Smrg
878b6d6341Smrgchgrpcmd=
881abf7346Smrgchmodcmd=$chmodprog
891abf7346Smrgchowncmd=
908b6d6341Smrgmvcmd=$mvprog
911abf7346Smrgrmcmd="$rmprog -f"
928b6d6341Smrgstripcmd=
938b6d6341Smrg
941abf7346Smrgsrc=
951abf7346Smrgdst=
961abf7346Smrgdir_arg=
978b6d6341Smrgdst_arg=
988b6d6341Smrg
998b6d6341Smrgcopy_on_change=false
1001abf7346Smrgno_target_directory=
1011abf7346Smrg
1028b6d6341Smrgusage="\
1038b6d6341SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1041abf7346Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1051abf7346Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1061abf7346Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1071abf7346Smrg
1081abf7346SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1091abf7346SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1101abf7346SmrgIn the 4th, create DIRECTORIES.
1111abf7346Smrg
1121abf7346SmrgOptions:
1138b6d6341Smrg     --help     display this help and exit.
1148b6d6341Smrg     --version  display version info and exit.
1158b6d6341Smrg
1168b6d6341Smrg  -c            (ignored)
1178b6d6341Smrg  -C            install only if different (preserve the last data modification time)
1188b6d6341Smrg  -d            create directories instead of installing files.
1198b6d6341Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1208b6d6341Smrg  -m MODE       $chmodprog installed files to MODE.
1218b6d6341Smrg  -o USER       $chownprog installed files to USER.
1228b6d6341Smrg  -s            $stripprog installed files.
1238b6d6341Smrg  -t DIRECTORY  install into DIRECTORY.
1248b6d6341Smrg  -T            report an error if DSTFILE is a directory.
1251abf7346Smrg
1261abf7346SmrgEnvironment variables override the default commands:
1278b6d6341Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1288b6d6341Smrg  RMPROG STRIPPROG
1291abf7346Smrg"
1301abf7346Smrg
1311abf7346Smrgwhile test $# -ne 0; do
1321abf7346Smrg  case $1 in
1338b6d6341Smrg    -c) ;;
1348b6d6341Smrg
1358b6d6341Smrg    -C) copy_on_change=true;;
1361abf7346Smrg
1378b6d6341Smrg    -d) dir_arg=true;;
1381abf7346Smrg
1391abf7346Smrg    -g) chgrpcmd="$chgrpprog $2"
1408b6d6341Smrg	shift;;
1411abf7346Smrg
1421abf7346Smrg    --help) echo "$usage"; exit $?;;
1431abf7346Smrg
1441abf7346Smrg    -m) mode=$2
1451abf7346Smrg	case $mode in
1461abf7346Smrg	  *' '* | *'	'* | *'
1471abf7346Smrg'*	  | *'*'* | *'?'* | *'['*)
1481abf7346Smrg	    echo "$0: invalid mode: $mode" >&2
1491abf7346Smrg	    exit 1;;
1501abf7346Smrg	esac
1518b6d6341Smrg	shift;;
1521abf7346Smrg
1531abf7346Smrg    -o) chowncmd="$chownprog $2"
1548b6d6341Smrg	shift;;
1551abf7346Smrg
1568b6d6341Smrg    -s) stripcmd=$stripprog;;
1571abf7346Smrg
1588b6d6341Smrg    -t) dst_arg=$2
1598b6d6341Smrg	shift;;
1601abf7346Smrg
1618b6d6341Smrg    -T) no_target_directory=true;;
1621abf7346Smrg
1631abf7346Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1641abf7346Smrg
1651abf7346Smrg    --)	shift
1661abf7346Smrg	break;;
1671abf7346Smrg
1681abf7346Smrg    -*)	echo "$0: invalid option: $1" >&2
1691abf7346Smrg	exit 1;;
1701abf7346Smrg
1711abf7346Smrg    *)  break;;
1721abf7346Smrg  esac
1738b6d6341Smrg  shift
1741abf7346Smrgdone
1751abf7346Smrg
1768b6d6341Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1771abf7346Smrg  # When -d is used, all remaining arguments are directories to create.
1781abf7346Smrg  # When -t is used, the destination is already specified.
1791abf7346Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1801abf7346Smrg  for arg
1811abf7346Smrg  do
1828b6d6341Smrg    if test -n "$dst_arg"; then
1831abf7346Smrg      # $@ is not empty: it contains at least $arg.
1848b6d6341Smrg      set fnord "$@" "$dst_arg"
1851abf7346Smrg      shift # fnord
1861abf7346Smrg    fi
1871abf7346Smrg    shift # arg
1888b6d6341Smrg    dst_arg=$arg
1891abf7346Smrg  done
1901abf7346Smrgfi
1911abf7346Smrg
1921abf7346Smrgif test $# -eq 0; then
1931abf7346Smrg  if test -z "$dir_arg"; then
1941abf7346Smrg    echo "$0: no input file specified." >&2
1951abf7346Smrg    exit 1
1961abf7346Smrg  fi
1971abf7346Smrg  # It's OK to call `install-sh -d' without argument.
1981abf7346Smrg  # This can happen when creating conditional directories.
1991abf7346Smrg  exit 0
2001abf7346Smrgfi
2011abf7346Smrg
2021abf7346Smrgif test -z "$dir_arg"; then
2031abf7346Smrg  trap '(exit $?); exit' 1 2 13 15
2041abf7346Smrg
2051abf7346Smrg  # Set umask so as not to create temps with too-generous modes.
2061abf7346Smrg  # However, 'strip' requires both read and write access to temps.
2071abf7346Smrg  case $mode in
2081abf7346Smrg    # Optimize common cases.
2091abf7346Smrg    *644) cp_umask=133;;
2101abf7346Smrg    *755) cp_umask=22;;
2111abf7346Smrg
2121abf7346Smrg    *[0-7])
2131abf7346Smrg      if test -z "$stripcmd"; then
2141abf7346Smrg	u_plus_rw=
2151abf7346Smrg      else
2161abf7346Smrg	u_plus_rw='% 200'
2171abf7346Smrg      fi
2181abf7346Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2191abf7346Smrg    *)
2201abf7346Smrg      if test -z "$stripcmd"; then
2211abf7346Smrg	u_plus_rw=
2221abf7346Smrg      else
2231abf7346Smrg	u_plus_rw=,u+rw
2241abf7346Smrg      fi
2251abf7346Smrg      cp_umask=$mode$u_plus_rw;;
2261abf7346Smrg  esac
2271abf7346Smrgfi
2281abf7346Smrg
2291abf7346Smrgfor src
2301abf7346Smrgdo
2311abf7346Smrg  # Protect names starting with `-'.
2321abf7346Smrg  case $src in
2338b6d6341Smrg    -*) src=./$src;;
2341abf7346Smrg  esac
2351abf7346Smrg
2361abf7346Smrg  if test -n "$dir_arg"; then
2371abf7346Smrg    dst=$src
2381abf7346Smrg    dstdir=$dst
2391abf7346Smrg    test -d "$dstdir"
2401abf7346Smrg    dstdir_status=$?
2411abf7346Smrg  else
2421abf7346Smrg
2431abf7346Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2441abf7346Smrg    # might cause directories to be created, which would be especially bad
2451abf7346Smrg    # if $src (and thus $dsttmp) contains '*'.
2461abf7346Smrg    if test ! -f "$src" && test ! -d "$src"; then
2471abf7346Smrg      echo "$0: $src does not exist." >&2
2481abf7346Smrg      exit 1
2491abf7346Smrg    fi
2501abf7346Smrg
2518b6d6341Smrg    if test -z "$dst_arg"; then
2521abf7346Smrg      echo "$0: no destination specified." >&2
2531abf7346Smrg      exit 1
2541abf7346Smrg    fi
2551abf7346Smrg
2568b6d6341Smrg    dst=$dst_arg
2571abf7346Smrg    # Protect names starting with `-'.
2581abf7346Smrg    case $dst in
2598b6d6341Smrg      -*) dst=./$dst;;
2601abf7346Smrg    esac
2611abf7346Smrg
2621abf7346Smrg    # If destination is a directory, append the input filename; won't work
2631abf7346Smrg    # if double slashes aren't ignored.
2641abf7346Smrg    if test -d "$dst"; then
2651abf7346Smrg      if test -n "$no_target_directory"; then
2668b6d6341Smrg	echo "$0: $dst_arg: Is a directory" >&2
2671abf7346Smrg	exit 1
2681abf7346Smrg      fi
2691abf7346Smrg      dstdir=$dst
2701abf7346Smrg      dst=$dstdir/`basename "$src"`
2711abf7346Smrg      dstdir_status=0
2721abf7346Smrg    else
2731abf7346Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2741abf7346Smrg      dstdir=`
2751abf7346Smrg	(dirname "$dst") 2>/dev/null ||
2761abf7346Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2771abf7346Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2781abf7346Smrg	     X"$dst" : 'X\(//\)$' \| \
2791abf7346Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2801abf7346Smrg	echo X"$dst" |
2811abf7346Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2821abf7346Smrg		   s//\1/
2831abf7346Smrg		   q
2841abf7346Smrg		 }
2851abf7346Smrg		 /^X\(\/\/\)[^/].*/{
2861abf7346Smrg		   s//\1/
2871abf7346Smrg		   q
2881abf7346Smrg		 }
2891abf7346Smrg		 /^X\(\/\/\)$/{
2901abf7346Smrg		   s//\1/
2911abf7346Smrg		   q
2921abf7346Smrg		 }
2931abf7346Smrg		 /^X\(\/\).*/{
2941abf7346Smrg		   s//\1/
2951abf7346Smrg		   q
2961abf7346Smrg		 }
2971abf7346Smrg		 s/.*/./; q'
2981abf7346Smrg      `
2991abf7346Smrg
3001abf7346Smrg      test -d "$dstdir"
3011abf7346Smrg      dstdir_status=$?
3021abf7346Smrg    fi
3031abf7346Smrg  fi
3041abf7346Smrg
3051abf7346Smrg  obsolete_mkdir_used=false
3061abf7346Smrg
3071abf7346Smrg  if test $dstdir_status != 0; then
3081abf7346Smrg    case $posix_mkdir in
3091abf7346Smrg      '')
3101abf7346Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3111abf7346Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3121abf7346Smrg	umask=`umask`
3131abf7346Smrg	case $stripcmd.$umask in
3141abf7346Smrg	  # Optimize common cases.
3151abf7346Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3161abf7346Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3171abf7346Smrg
3181abf7346Smrg	  *[0-7])
3191abf7346Smrg	    mkdir_umask=`expr $umask + 22 \
3201abf7346Smrg	      - $umask % 100 % 40 + $umask % 20 \
3211abf7346Smrg	      - $umask % 10 % 4 + $umask % 2
3221abf7346Smrg	    `;;
3231abf7346Smrg	  *) mkdir_umask=$umask,go-w;;
3241abf7346Smrg	esac
3251abf7346Smrg
3261abf7346Smrg	# With -d, create the new directory with the user-specified mode.
3271abf7346Smrg	# Otherwise, rely on $mkdir_umask.
3281abf7346Smrg	if test -n "$dir_arg"; then
3291abf7346Smrg	  mkdir_mode=-m$mode
3301abf7346Smrg	else
3311abf7346Smrg	  mkdir_mode=
3321abf7346Smrg	fi
3331abf7346Smrg
3341abf7346Smrg	posix_mkdir=false
3351abf7346Smrg	case $umask in
3361abf7346Smrg	  *[123567][0-7][0-7])
3371abf7346Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3381abf7346Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3391abf7346Smrg	    ;;
3401abf7346Smrg	  *)
3411abf7346Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3421abf7346Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3431abf7346Smrg
3441abf7346Smrg	    if (umask $mkdir_umask &&
3451abf7346Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3461abf7346Smrg	    then
3471abf7346Smrg	      if test -z "$dir_arg" || {
3481abf7346Smrg		   # Check for POSIX incompatibilities with -m.
3491abf7346Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3501abf7346Smrg		   # other-writeable bit of parent directory when it shouldn't.
3511abf7346Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3521abf7346Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3531abf7346Smrg		   case $ls_ld_tmpdir in
3541abf7346Smrg		     d????-?r-*) different_mode=700;;
3551abf7346Smrg		     d????-?--*) different_mode=755;;
3561abf7346Smrg		     *) false;;
3571abf7346Smrg		   esac &&
3581abf7346Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3591abf7346Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3601abf7346Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3611abf7346Smrg		   }
3621abf7346Smrg		 }
3631abf7346Smrg	      then posix_mkdir=:
3641abf7346Smrg	      fi
3651abf7346Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3661abf7346Smrg	    else
3671abf7346Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3681abf7346Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3691abf7346Smrg	    fi
3701abf7346Smrg	    trap '' 0;;
3711abf7346Smrg	esac;;
3721abf7346Smrg    esac
3731abf7346Smrg
3741abf7346Smrg    if
3751abf7346Smrg      $posix_mkdir && (
3761abf7346Smrg	umask $mkdir_umask &&
3771abf7346Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3781abf7346Smrg      )
3791abf7346Smrg    then :
3801abf7346Smrg    else
3811abf7346Smrg
3821abf7346Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3831abf7346Smrg      # or it failed possibly due to a race condition.  Create the
3841abf7346Smrg      # directory the slow way, step by step, checking for races as we go.
3851abf7346Smrg
3861abf7346Smrg      case $dstdir in
3878b6d6341Smrg	/*) prefix='/';;
3888b6d6341Smrg	-*) prefix='./';;
3898b6d6341Smrg	*)  prefix='';;
3901abf7346Smrg      esac
3911abf7346Smrg
3928b6d6341Smrg      eval "$initialize_posix_glob"
3931abf7346Smrg
3941abf7346Smrg      oIFS=$IFS
3951abf7346Smrg      IFS=/
3968b6d6341Smrg      $posix_glob set -f
3971abf7346Smrg      set fnord $dstdir
3981abf7346Smrg      shift
3998b6d6341Smrg      $posix_glob set +f
4001abf7346Smrg      IFS=$oIFS
4011abf7346Smrg
4021abf7346Smrg      prefixes=
4031abf7346Smrg
4041abf7346Smrg      for d
4051abf7346Smrg      do
4061abf7346Smrg	test -z "$d" && continue
4071abf7346Smrg
4081abf7346Smrg	prefix=$prefix$d
4091abf7346Smrg	if test -d "$prefix"; then
4101abf7346Smrg	  prefixes=
4111abf7346Smrg	else
4121abf7346Smrg	  if $posix_mkdir; then
4131abf7346Smrg	    (umask=$mkdir_umask &&
4141abf7346Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4151abf7346Smrg	    # Don't fail if two instances are running concurrently.
4161abf7346Smrg	    test -d "$prefix" || exit 1
4171abf7346Smrg	  else
4181abf7346Smrg	    case $prefix in
4191abf7346Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4201abf7346Smrg	      *) qprefix=$prefix;;
4211abf7346Smrg	    esac
4221abf7346Smrg	    prefixes="$prefixes '$qprefix'"
4231abf7346Smrg	  fi
4241abf7346Smrg	fi
4251abf7346Smrg	prefix=$prefix/
4261abf7346Smrg      done
4271abf7346Smrg
4281abf7346Smrg      if test -n "$prefixes"; then
4291abf7346Smrg	# Don't fail if two instances are running concurrently.
4301abf7346Smrg	(umask $mkdir_umask &&
4311abf7346Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4321abf7346Smrg	  test -d "$dstdir" || exit 1
4331abf7346Smrg	obsolete_mkdir_used=true
4341abf7346Smrg      fi
4351abf7346Smrg    fi
4361abf7346Smrg  fi
4371abf7346Smrg
4381abf7346Smrg  if test -n "$dir_arg"; then
4391abf7346Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4401abf7346Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4411abf7346Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4421abf7346Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4431abf7346Smrg  else
4441abf7346Smrg
4451abf7346Smrg    # Make a couple of temp file names in the proper directory.
4461abf7346Smrg    dsttmp=$dstdir/_inst.$$_
4471abf7346Smrg    rmtmp=$dstdir/_rm.$$_
4481abf7346Smrg
4491abf7346Smrg    # Trap to clean up those temp files at exit.
4501abf7346Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4511abf7346Smrg
4521abf7346Smrg    # Copy the file name to the temp name.
4531abf7346Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4541abf7346Smrg
4551abf7346Smrg    # and set any options; do chmod last to preserve setuid bits.
4561abf7346Smrg    #
4571abf7346Smrg    # If any of these fail, we abort the whole thing.  If we want to
4581abf7346Smrg    # ignore errors from any of these, just make sure not to ignore
4591abf7346Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4601abf7346Smrg    #
4618b6d6341Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4628b6d6341Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4638b6d6341Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4648b6d6341Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4658b6d6341Smrg
4668b6d6341Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4678b6d6341Smrg    if $copy_on_change &&
4688b6d6341Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4698b6d6341Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4708b6d6341Smrg
4718b6d6341Smrg       eval "$initialize_posix_glob" &&
4728b6d6341Smrg       $posix_glob set -f &&
4738b6d6341Smrg       set X $old && old=:$2:$4:$5:$6 &&
4748b6d6341Smrg       set X $new && new=:$2:$4:$5:$6 &&
4758b6d6341Smrg       $posix_glob set +f &&
4768b6d6341Smrg
4778b6d6341Smrg       test "$old" = "$new" &&
4788b6d6341Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4798b6d6341Smrg    then
4808b6d6341Smrg      rm -f "$dsttmp"
4818b6d6341Smrg    else
4828b6d6341Smrg      # Rename the file to the real destination.
4838b6d6341Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4848b6d6341Smrg
4858b6d6341Smrg      # The rename failed, perhaps because mv can't rename something else
4868b6d6341Smrg      # to itself, or perhaps because mv is so ancient that it does not
4878b6d6341Smrg      # support -f.
4888b6d6341Smrg      {
4898b6d6341Smrg	# Now remove or move aside any old file at destination location.
4908b6d6341Smrg	# We try this two ways since rm can't unlink itself on some
4918b6d6341Smrg	# systems and the destination file might be busy for other
4928b6d6341Smrg	# reasons.  In this case, the final cleanup might fail but the new
4938b6d6341Smrg	# file should still install successfully.
4948b6d6341Smrg	{
4958b6d6341Smrg	  test ! -f "$dst" ||
4968b6d6341Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4978b6d6341Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4988b6d6341Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4998b6d6341Smrg	  } ||
5008b6d6341Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5018b6d6341Smrg	    (exit 1); exit 1
5028b6d6341Smrg	  }
5038b6d6341Smrg	} &&
5048b6d6341Smrg
5058b6d6341Smrg	# Now rename the file to the real destination.
5068b6d6341Smrg	$doit $mvcmd "$dsttmp" "$dst"
5078b6d6341Smrg      }
5088b6d6341Smrg    fi || exit 1
5091abf7346Smrg
5101abf7346Smrg    trap '' 0
5111abf7346Smrg  fi
5121abf7346Smrgdone
5131abf7346Smrg
5141abf7346Smrg# Local variables:
5151abf7346Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5161abf7346Smrg# time-stamp-start: "scriptversion="
5171abf7346Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5188b6d6341Smrg# time-stamp-time-zone: "UTC"
5198b6d6341Smrg# time-stamp-end: "; # UTC"
5201abf7346Smrg# End:
521