install-sh revision 1ac89add
145bc899bSmrg#!/bin/sh
245bc899bSmrg# install - install a program, script, or datafile
345bc899bSmrg
41ac89addSmrgscriptversion=2006-12-25.00
545bc899bSmrg
645bc899bSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
745bc899bSmrg# later released in X11R6 (xc/config/util/install.sh) with the
845bc899bSmrg# following copyright and license.
945bc899bSmrg#
1045bc899bSmrg# Copyright (C) 1994 X Consortium
1145bc899bSmrg#
1245bc899bSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1345bc899bSmrg# of this software and associated documentation files (the "Software"), to
1445bc899bSmrg# deal in the Software without restriction, including without limitation the
1545bc899bSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1645bc899bSmrg# sell copies of the Software, and to permit persons to whom the Software is
1745bc899bSmrg# furnished to do so, subject to the following conditions:
1845bc899bSmrg#
1945bc899bSmrg# The above copyright notice and this permission notice shall be included in
2045bc899bSmrg# all copies or substantial portions of the Software.
2145bc899bSmrg#
2245bc899bSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2345bc899bSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2445bc899bSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2545bc899bSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2645bc899bSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2745bc899bSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2845bc899bSmrg#
2945bc899bSmrg# Except as contained in this notice, the name of the X Consortium shall not
3045bc899bSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
3145bc899bSmrg# ings in this Software without prior written authorization from the X Consor-
3245bc899bSmrg# tium.
3345bc899bSmrg#
3445bc899bSmrg#
3545bc899bSmrg# FSF changes to this file are in the public domain.
3645bc899bSmrg#
3745bc899bSmrg# Calling this script install-sh is preferred over install.sh, to prevent
3845bc899bSmrg# `make' implicit rules from creating a file called install from it
3945bc899bSmrg# when there is no Makefile.
4045bc899bSmrg#
4145bc899bSmrg# This script is compatible with the BSD install script, but was written
421ac89addSmrg# from scratch.
431ac89addSmrg
441ac89addSmrgnl='
451ac89addSmrg'
461ac89addSmrgIFS=" ""	$nl"
4745bc899bSmrg
4845bc899bSmrg# set DOITPROG to echo to test this script
4945bc899bSmrg
5045bc899bSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
511ac89addSmrgdoit=${DOITPROG-}
521ac89addSmrgif test -z "$doit"; then
531ac89addSmrg  doit_exec=exec
541ac89addSmrgelse
551ac89addSmrg  doit_exec=$doit
561ac89addSmrgfi
5745bc899bSmrg
581ac89addSmrg# Put in absolute file names if you don't have them in your path;
591ac89addSmrg# or use environment vars.
601ac89addSmrg
611ac89addSmrgchgrpprog=${CHGRPPROG-chgrp}
621ac89addSmrgchmodprog=${CHMODPROG-chmod}
631ac89addSmrgchownprog=${CHOWNPROG-chown}
641ac89addSmrgcmpprog=${CMPPROG-cmp}
651ac89addSmrgcpprog=${CPPROG-cp}
661ac89addSmrgmkdirprog=${MKDIRPROG-mkdir}
671ac89addSmrgmvprog=${MVPROG-mv}
681ac89addSmrgrmprog=${RMPROG-rm}
691ac89addSmrgstripprog=${STRIPPROG-strip}
701ac89addSmrg
711ac89addSmrgposix_glob='?'
721ac89addSmrginitialize_posix_glob='
731ac89addSmrg  test "$posix_glob" != "?" || {
741ac89addSmrg    if (set -f) 2>/dev/null; then
751ac89addSmrg      posix_glob=
761ac89addSmrg    else
771ac89addSmrg      posix_glob=:
781ac89addSmrg    fi
791ac89addSmrg  }
801ac89addSmrg'
8145bc899bSmrg
821ac89addSmrgposix_mkdir=
831ac89addSmrg
841ac89addSmrg# Desired mode of installed file.
851ac89addSmrgmode=0755
8645bc899bSmrg
8745bc899bSmrgchgrpcmd=
881ac89addSmrgchmodcmd=$chmodprog
891ac89addSmrgchowncmd=
901ac89addSmrgmvcmd=$mvprog
9145bc899bSmrgrmcmd="$rmprog -f"
921ac89addSmrgstripcmd=
931ac89addSmrg
9445bc899bSmrgsrc=
9545bc899bSmrgdst=
9645bc899bSmrgdir_arg=
971ac89addSmrgdst_arg=
981ac89addSmrg
991ac89addSmrgcopy_on_change=false
10045bc899bSmrgno_target_directory=
10145bc899bSmrg
1021ac89addSmrgusage="\
1031ac89addSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10445bc899bSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10545bc899bSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10645bc899bSmrg   or: $0 [OPTION]... -d DIRECTORIES...
10745bc899bSmrg
10845bc899bSmrgIn the 1st form, copy SRCFILE to DSTFILE.
10945bc899bSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11045bc899bSmrgIn the 4th, create DIRECTORIES.
11145bc899bSmrg
11245bc899bSmrgOptions:
1131ac89addSmrg     --help     display this help and exit.
1141ac89addSmrg     --version  display version info and exit.
1151ac89addSmrg
1161ac89addSmrg  -c            (ignored)
1171ac89addSmrg  -C            install only if different (preserve the last data modification time)
1181ac89addSmrg  -d            create directories instead of installing files.
1191ac89addSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1201ac89addSmrg  -m MODE       $chmodprog installed files to MODE.
1211ac89addSmrg  -o USER       $chownprog installed files to USER.
1221ac89addSmrg  -s            $stripprog installed files.
1231ac89addSmrg  -t DIRECTORY  install into DIRECTORY.
1241ac89addSmrg  -T            report an error if DSTFILE is a directory.
12545bc899bSmrg
12645bc899bSmrgEnvironment variables override the default commands:
1271ac89addSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1281ac89addSmrg  RMPROG STRIPPROG
12945bc899bSmrg"
13045bc899bSmrg
1311ac89addSmrgwhile test $# -ne 0; do
13245bc899bSmrg  case $1 in
1331ac89addSmrg    -c) ;;
1341ac89addSmrg
1351ac89addSmrg    -C) copy_on_change=true;;
13645bc899bSmrg
1371ac89addSmrg    -d) dir_arg=true;;
13845bc899bSmrg
13945bc899bSmrg    -g) chgrpcmd="$chgrpprog $2"
1401ac89addSmrg	shift;;
14145bc899bSmrg
14245bc899bSmrg    --help) echo "$usage"; exit $?;;
14345bc899bSmrg
1441ac89addSmrg    -m) mode=$2
1451ac89addSmrg	case $mode in
1461ac89addSmrg	  *' '* | *'	'* | *'
1471ac89addSmrg'*	  | *'*'* | *'?'* | *'['*)
1481ac89addSmrg	    echo "$0: invalid mode: $mode" >&2
1491ac89addSmrg	    exit 1;;
1501ac89addSmrg	esac
1511ac89addSmrg	shift;;
15245bc899bSmrg
15345bc899bSmrg    -o) chowncmd="$chownprog $2"
1541ac89addSmrg	shift;;
15545bc899bSmrg
1561ac89addSmrg    -s) stripcmd=$stripprog;;
15745bc899bSmrg
1581ac89addSmrg    -t) dst_arg=$2
1591ac89addSmrg	shift;;
16045bc899bSmrg
1611ac89addSmrg    -T) no_target_directory=true;;
16245bc899bSmrg
16345bc899bSmrg    --version) echo "$0 $scriptversion"; exit $?;;
16445bc899bSmrg
1651ac89addSmrg    --)	shift
16645bc899bSmrg	break;;
1671ac89addSmrg
1681ac89addSmrg    -*)	echo "$0: invalid option: $1" >&2
1691ac89addSmrg	exit 1;;
1701ac89addSmrg
1711ac89addSmrg    *)  break;;
17245bc899bSmrg  esac
1731ac89addSmrg  shift
17445bc899bSmrgdone
17545bc899bSmrg
1761ac89addSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1771ac89addSmrg  # When -d is used, all remaining arguments are directories to create.
1781ac89addSmrg  # When -t is used, the destination is already specified.
1791ac89addSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1801ac89addSmrg  for arg
1811ac89addSmrg  do
1821ac89addSmrg    if test -n "$dst_arg"; then
1831ac89addSmrg      # $@ is not empty: it contains at least $arg.
1841ac89addSmrg      set fnord "$@" "$dst_arg"
1851ac89addSmrg      shift # fnord
1861ac89addSmrg    fi
1871ac89addSmrg    shift # arg
1881ac89addSmrg    dst_arg=$arg
1891ac89addSmrg  done
1901ac89addSmrgfi
1911ac89addSmrg
1921ac89addSmrgif test $# -eq 0; then
19345bc899bSmrg  if test -z "$dir_arg"; then
19445bc899bSmrg    echo "$0: no input file specified." >&2
19545bc899bSmrg    exit 1
19645bc899bSmrg  fi
19745bc899bSmrg  # It's OK to call `install-sh -d' without argument.
19845bc899bSmrg  # This can happen when creating conditional directories.
19945bc899bSmrg  exit 0
20045bc899bSmrgfi
20145bc899bSmrg
2021ac89addSmrgif test -z "$dir_arg"; then
2031ac89addSmrg  trap '(exit $?); exit' 1 2 13 15
2041ac89addSmrg
2051ac89addSmrg  # Set umask so as not to create temps with too-generous modes.
2061ac89addSmrg  # However, 'strip' requires both read and write access to temps.
2071ac89addSmrg  case $mode in
2081ac89addSmrg    # Optimize common cases.
2091ac89addSmrg    *644) cp_umask=133;;
2101ac89addSmrg    *755) cp_umask=22;;
2111ac89addSmrg
2121ac89addSmrg    *[0-7])
2131ac89addSmrg      if test -z "$stripcmd"; then
2141ac89addSmrg	u_plus_rw=
2151ac89addSmrg      else
2161ac89addSmrg	u_plus_rw='% 200'
2171ac89addSmrg      fi
2181ac89addSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2191ac89addSmrg    *)
2201ac89addSmrg      if test -z "$stripcmd"; then
2211ac89addSmrg	u_plus_rw=
2221ac89addSmrg      else
2231ac89addSmrg	u_plus_rw=,u+rw
2241ac89addSmrg      fi
2251ac89addSmrg      cp_umask=$mode$u_plus_rw;;
2261ac89addSmrg  esac
2271ac89addSmrgfi
2281ac89addSmrg
22945bc899bSmrgfor src
23045bc899bSmrgdo
23145bc899bSmrg  # Protect names starting with `-'.
23245bc899bSmrg  case $src in
2331ac89addSmrg    -*) src=./$src;;
23445bc899bSmrg  esac
23545bc899bSmrg
23645bc899bSmrg  if test -n "$dir_arg"; then
23745bc899bSmrg    dst=$src
2381ac89addSmrg    dstdir=$dst
2391ac89addSmrg    test -d "$dstdir"
2401ac89addSmrg    dstdir_status=$?
24145bc899bSmrg  else
2421ac89addSmrg
24345bc899bSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24445bc899bSmrg    # might cause directories to be created, which would be especially bad
24545bc899bSmrg    # if $src (and thus $dsttmp) contains '*'.
24645bc899bSmrg    if test ! -f "$src" && test ! -d "$src"; then
24745bc899bSmrg      echo "$0: $src does not exist." >&2
24845bc899bSmrg      exit 1
24945bc899bSmrg    fi
25045bc899bSmrg
2511ac89addSmrg    if test -z "$dst_arg"; then
25245bc899bSmrg      echo "$0: no destination specified." >&2
25345bc899bSmrg      exit 1
25445bc899bSmrg    fi
25545bc899bSmrg
2561ac89addSmrg    dst=$dst_arg
25745bc899bSmrg    # Protect names starting with `-'.
25845bc899bSmrg    case $dst in
2591ac89addSmrg      -*) dst=./$dst;;
26045bc899bSmrg    esac
26145bc899bSmrg
26245bc899bSmrg    # If destination is a directory, append the input filename; won't work
26345bc899bSmrg    # if double slashes aren't ignored.
26445bc899bSmrg    if test -d "$dst"; then
26545bc899bSmrg      if test -n "$no_target_directory"; then
2661ac89addSmrg	echo "$0: $dst_arg: Is a directory" >&2
26745bc899bSmrg	exit 1
26845bc899bSmrg      fi
2691ac89addSmrg      dstdir=$dst
2701ac89addSmrg      dst=$dstdir/`basename "$src"`
2711ac89addSmrg      dstdir_status=0
2721ac89addSmrg    else
2731ac89addSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2741ac89addSmrg      dstdir=`
2751ac89addSmrg	(dirname "$dst") 2>/dev/null ||
2761ac89addSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2771ac89addSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
2781ac89addSmrg	     X"$dst" : 'X\(//\)$' \| \
2791ac89addSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2801ac89addSmrg	echo X"$dst" |
2811ac89addSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2821ac89addSmrg		   s//\1/
2831ac89addSmrg		   q
2841ac89addSmrg		 }
2851ac89addSmrg		 /^X\(\/\/\)[^/].*/{
2861ac89addSmrg		   s//\1/
2871ac89addSmrg		   q
2881ac89addSmrg		 }
2891ac89addSmrg		 /^X\(\/\/\)$/{
2901ac89addSmrg		   s//\1/
2911ac89addSmrg		   q
2921ac89addSmrg		 }
2931ac89addSmrg		 /^X\(\/\).*/{
2941ac89addSmrg		   s//\1/
2951ac89addSmrg		   q
2961ac89addSmrg		 }
2971ac89addSmrg		 s/.*/./; q'
2981ac89addSmrg      `
2991ac89addSmrg
3001ac89addSmrg      test -d "$dstdir"
3011ac89addSmrg      dstdir_status=$?
30245bc899bSmrg    fi
30345bc899bSmrg  fi
30445bc899bSmrg
3051ac89addSmrg  obsolete_mkdir_used=false
3061ac89addSmrg
3071ac89addSmrg  if test $dstdir_status != 0; then
3081ac89addSmrg    case $posix_mkdir in
3091ac89addSmrg      '')
3101ac89addSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
3111ac89addSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
3121ac89addSmrg	umask=`umask`
3131ac89addSmrg	case $stripcmd.$umask in
3141ac89addSmrg	  # Optimize common cases.
3151ac89addSmrg	  *[2367][2367]) mkdir_umask=$umask;;
3161ac89addSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3171ac89addSmrg
3181ac89addSmrg	  *[0-7])
3191ac89addSmrg	    mkdir_umask=`expr $umask + 22 \
3201ac89addSmrg	      - $umask % 100 % 40 + $umask % 20 \
3211ac89addSmrg	      - $umask % 10 % 4 + $umask % 2
3221ac89addSmrg	    `;;
3231ac89addSmrg	  *) mkdir_umask=$umask,go-w;;
3241ac89addSmrg	esac
3251ac89addSmrg
3261ac89addSmrg	# With -d, create the new directory with the user-specified mode.
3271ac89addSmrg	# Otherwise, rely on $mkdir_umask.
3281ac89addSmrg	if test -n "$dir_arg"; then
3291ac89addSmrg	  mkdir_mode=-m$mode
3301ac89addSmrg	else
3311ac89addSmrg	  mkdir_mode=
3321ac89addSmrg	fi
3331ac89addSmrg
3341ac89addSmrg	posix_mkdir=false
3351ac89addSmrg	case $umask in
3361ac89addSmrg	  *[123567][0-7][0-7])
3371ac89addSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3381ac89addSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3391ac89addSmrg	    ;;
3401ac89addSmrg	  *)
3411ac89addSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3421ac89addSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3431ac89addSmrg
3441ac89addSmrg	    if (umask $mkdir_umask &&
3451ac89addSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3461ac89addSmrg	    then
3471ac89addSmrg	      if test -z "$dir_arg" || {
3481ac89addSmrg		   # Check for POSIX incompatibilities with -m.
3491ac89addSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3501ac89addSmrg		   # other-writeable bit of parent directory when it shouldn't.
3511ac89addSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3521ac89addSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3531ac89addSmrg		   case $ls_ld_tmpdir in
3541ac89addSmrg		     d????-?r-*) different_mode=700;;
3551ac89addSmrg		     d????-?--*) different_mode=755;;
3561ac89addSmrg		     *) false;;
3571ac89addSmrg		   esac &&
3581ac89addSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3591ac89addSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3601ac89addSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3611ac89addSmrg		   }
3621ac89addSmrg		 }
3631ac89addSmrg	      then posix_mkdir=:
3641ac89addSmrg	      fi
3651ac89addSmrg	      rmdir "$tmpdir/d" "$tmpdir"
3661ac89addSmrg	    else
3671ac89addSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
3681ac89addSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3691ac89addSmrg	    fi
3701ac89addSmrg	    trap '' 0;;
3711ac89addSmrg	esac;;
3721ac89addSmrg    esac
37345bc899bSmrg
3741ac89addSmrg    if
3751ac89addSmrg      $posix_mkdir && (
3761ac89addSmrg	umask $mkdir_umask &&
3771ac89addSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3781ac89addSmrg      )
3791ac89addSmrg    then :
3801ac89addSmrg    else
38145bc899bSmrg
3821ac89addSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3831ac89addSmrg      # or it failed possibly due to a race condition.  Create the
3841ac89addSmrg      # directory the slow way, step by step, checking for races as we go.
38545bc899bSmrg
3861ac89addSmrg      case $dstdir in
3871ac89addSmrg	/*) prefix='/';;
3881ac89addSmrg	-*) prefix='./';;
3891ac89addSmrg	*)  prefix='';;
3901ac89addSmrg      esac
39145bc899bSmrg
3921ac89addSmrg      eval "$initialize_posix_glob"
39345bc899bSmrg
3941ac89addSmrg      oIFS=$IFS
3951ac89addSmrg      IFS=/
3961ac89addSmrg      $posix_glob set -f
3971ac89addSmrg      set fnord $dstdir
39845bc899bSmrg      shift
3991ac89addSmrg      $posix_glob set +f
4001ac89addSmrg      IFS=$oIFS
4011ac89addSmrg
4021ac89addSmrg      prefixes=
4031ac89addSmrg
4041ac89addSmrg      for d
4051ac89addSmrg      do
4061ac89addSmrg	test -z "$d" && continue
4071ac89addSmrg
4081ac89addSmrg	prefix=$prefix$d
4091ac89addSmrg	if test -d "$prefix"; then
4101ac89addSmrg	  prefixes=
4111ac89addSmrg	else
4121ac89addSmrg	  if $posix_mkdir; then
4131ac89addSmrg	    (umask=$mkdir_umask &&
4141ac89addSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4151ac89addSmrg	    # Don't fail if two instances are running concurrently.
4161ac89addSmrg	    test -d "$prefix" || exit 1
4171ac89addSmrg	  else
4181ac89addSmrg	    case $prefix in
4191ac89addSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4201ac89addSmrg	      *) qprefix=$prefix;;
4211ac89addSmrg	    esac
4221ac89addSmrg	    prefixes="$prefixes '$qprefix'"
4231ac89addSmrg	  fi
4241ac89addSmrg	fi
4251ac89addSmrg	prefix=$prefix/
4261ac89addSmrg      done
4271ac89addSmrg
4281ac89addSmrg      if test -n "$prefixes"; then
4291ac89addSmrg	# Don't fail if two instances are running concurrently.
4301ac89addSmrg	(umask $mkdir_umask &&
4311ac89addSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4321ac89addSmrg	  test -d "$dstdir" || exit 1
4331ac89addSmrg	obsolete_mkdir_used=true
43445bc899bSmrg      fi
4351ac89addSmrg    fi
43645bc899bSmrg  fi
43745bc899bSmrg
43845bc899bSmrg  if test -n "$dir_arg"; then
4391ac89addSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4401ac89addSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4411ac89addSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4421ac89addSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44345bc899bSmrg  else
44445bc899bSmrg
44545bc899bSmrg    # Make a couple of temp file names in the proper directory.
44645bc899bSmrg    dsttmp=$dstdir/_inst.$$_
44745bc899bSmrg    rmtmp=$dstdir/_rm.$$_
44845bc899bSmrg
44945bc899bSmrg    # Trap to clean up those temp files at exit.
45045bc899bSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45145bc899bSmrg
45245bc899bSmrg    # Copy the file name to the temp name.
4531ac89addSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45445bc899bSmrg
45545bc899bSmrg    # and set any options; do chmod last to preserve setuid bits.
45645bc899bSmrg    #
45745bc899bSmrg    # If any of these fail, we abort the whole thing.  If we want to
45845bc899bSmrg    # ignore errors from any of these, just make sure not to ignore
45945bc899bSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46045bc899bSmrg    #
4611ac89addSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4621ac89addSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4631ac89addSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4641ac89addSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4651ac89addSmrg
4661ac89addSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4671ac89addSmrg    if $copy_on_change &&
4681ac89addSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4691ac89addSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4701ac89addSmrg
4711ac89addSmrg       eval "$initialize_posix_glob" &&
4721ac89addSmrg       $posix_glob set -f &&
4731ac89addSmrg       set X $old && old=:$2:$4:$5:$6 &&
4741ac89addSmrg       set X $new && new=:$2:$4:$5:$6 &&
4751ac89addSmrg       $posix_glob set +f &&
4761ac89addSmrg
4771ac89addSmrg       test "$old" = "$new" &&
4781ac89addSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4791ac89addSmrg    then
4801ac89addSmrg      rm -f "$dsttmp"
4811ac89addSmrg    else
4821ac89addSmrg      # Rename the file to the real destination.
4831ac89addSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4841ac89addSmrg
4851ac89addSmrg      # The rename failed, perhaps because mv can't rename something else
4861ac89addSmrg      # to itself, or perhaps because mv is so ancient that it does not
4871ac89addSmrg      # support -f.
4881ac89addSmrg      {
4891ac89addSmrg	# Now remove or move aside any old file at destination location.
4901ac89addSmrg	# We try this two ways since rm can't unlink itself on some
4911ac89addSmrg	# systems and the destination file might be busy for other
4921ac89addSmrg	# reasons.  In this case, the final cleanup might fail but the new
4931ac89addSmrg	# file should still install successfully.
4941ac89addSmrg	{
4951ac89addSmrg	  test ! -f "$dst" ||
4961ac89addSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4971ac89addSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4981ac89addSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4991ac89addSmrg	  } ||
5001ac89addSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
5011ac89addSmrg	    (exit 1); exit 1
5021ac89addSmrg	  }
5031ac89addSmrg	} &&
5041ac89addSmrg
5051ac89addSmrg	# Now rename the file to the real destination.
5061ac89addSmrg	$doit $mvcmd "$dsttmp" "$dst"
5071ac89addSmrg      }
5081ac89addSmrg    fi || exit 1
5091ac89addSmrg
5101ac89addSmrg    trap '' 0
5111ac89addSmrg  fi
51245bc899bSmrgdone
51345bc899bSmrg
51445bc899bSmrg# Local variables:
51545bc899bSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51645bc899bSmrg# time-stamp-start: "scriptversion="
51745bc899bSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51845bc899bSmrg# time-stamp-end: "$"
51945bc899bSmrg# End:
520