install-sh revision 2227fc13
1644fc5c1Stron#!/bin/sh
2644fc5c1Stron# install - install a program, script, or datafile
32227fc13Smrg
42227fc13Smrgscriptversion=2009-04-28.21; # UTC
52227fc13Smrg
6644fc5c1Stron# This originates from X11R5 (mit/util/scripts/install.sh), which was
7644fc5c1Stron# later released in X11R6 (xc/config/util/install.sh) with the
8644fc5c1Stron# following copyright and license.
9644fc5c1Stron#
10644fc5c1Stron# Copyright (C) 1994 X Consortium
11644fc5c1Stron#
12644fc5c1Stron# Permission is hereby granted, free of charge, to any person obtaining a copy
13644fc5c1Stron# of this software and associated documentation files (the "Software"), to
14644fc5c1Stron# deal in the Software without restriction, including without limitation the
15644fc5c1Stron# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16644fc5c1Stron# sell copies of the Software, and to permit persons to whom the Software is
17644fc5c1Stron# furnished to do so, subject to the following conditions:
18644fc5c1Stron#
19644fc5c1Stron# The above copyright notice and this permission notice shall be included in
20644fc5c1Stron# all copies or substantial portions of the Software.
21644fc5c1Stron#
22644fc5c1Stron# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23644fc5c1Stron# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24644fc5c1Stron# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25644fc5c1Stron# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26644fc5c1Stron# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27644fc5c1Stron# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28644fc5c1Stron#
29644fc5c1Stron# Except as contained in this notice, the name of the X Consortium shall not
30644fc5c1Stron# be used in advertising or otherwise to promote the sale, use or other deal-
31644fc5c1Stron# ings in this Software without prior written authorization from the X Consor-
32644fc5c1Stron# tium.
33644fc5c1Stron#
34644fc5c1Stron#
35644fc5c1Stron# FSF changes to this file are in the public domain.
36644fc5c1Stron#
37644fc5c1Stron# Calling this script install-sh is preferred over install.sh, to prevent
38644fc5c1Stron# `make' implicit rules from creating a file called install from it
39644fc5c1Stron# when there is no Makefile.
40644fc5c1Stron#
41644fc5c1Stron# This script is compatible with the BSD install script, but was written
422227fc13Smrg# from scratch.
43644fc5c1Stron
442227fc13Smrgnl='
452227fc13Smrg'
462227fc13SmrgIFS=" ""	$nl"
47644fc5c1Stron
48644fc5c1Stron# set DOITPROG to echo to test this script
49644fc5c1Stron
50644fc5c1Stron# Don't use :- since 4.3BSD and earlier shells don't like it.
512227fc13Smrgdoit=${DOITPROG-}
522227fc13Smrgif test -z "$doit"; then
532227fc13Smrg  doit_exec=exec
54644fc5c1Stronelse
552227fc13Smrg  doit_exec=$doit
56644fc5c1Stronfi
57644fc5c1Stron
582227fc13Smrg# Put in absolute file names if you don't have them in your path;
592227fc13Smrg# or use environment vars.
602227fc13Smrg
612227fc13Smrgchgrpprog=${CHGRPPROG-chgrp}
622227fc13Smrgchmodprog=${CHMODPROG-chmod}
632227fc13Smrgchownprog=${CHOWNPROG-chown}
642227fc13Smrgcmpprog=${CMPPROG-cmp}
652227fc13Smrgcpprog=${CPPROG-cp}
662227fc13Smrgmkdirprog=${MKDIRPROG-mkdir}
672227fc13Smrgmvprog=${MVPROG-mv}
682227fc13Smrgrmprog=${RMPROG-rm}
692227fc13Smrgstripprog=${STRIPPROG-strip}
702227fc13Smrg
712227fc13Smrgposix_glob='?'
722227fc13Smrginitialize_posix_glob='
732227fc13Smrg  test "$posix_glob" != "?" || {
742227fc13Smrg    if (set -f) 2>/dev/null; then
752227fc13Smrg      posix_glob=
762227fc13Smrg    else
772227fc13Smrg      posix_glob=:
782227fc13Smrg    fi
792227fc13Smrg  }
802227fc13Smrg'
812227fc13Smrg
822227fc13Smrgposix_mkdir=
832227fc13Smrg
842227fc13Smrg# Desired mode of installed file.
852227fc13Smrgmode=0755
862227fc13Smrg
872227fc13Smrgchgrpcmd=
882227fc13Smrgchmodcmd=$chmodprog
892227fc13Smrgchowncmd=
902227fc13Smrgmvcmd=$mvprog
912227fc13Smrgrmcmd="$rmprog -f"
922227fc13Smrgstripcmd=
93644fc5c1Stron
942227fc13Smrgsrc=
952227fc13Smrgdst=
962227fc13Smrgdir_arg=
972227fc13Smrgdst_arg=
98644fc5c1Stron
992227fc13Smrgcopy_on_change=false
1002227fc13Smrgno_target_directory=
101644fc5c1Stron
1022227fc13Smrgusage="\
1032227fc13SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1042227fc13Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1052227fc13Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1062227fc13Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107644fc5c1Stron
1082227fc13SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1092227fc13SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1102227fc13SmrgIn the 4th, create DIRECTORIES.
111644fc5c1Stron
1122227fc13SmrgOptions:
1132227fc13Smrg     --help     display this help and exit.
1142227fc13Smrg     --version  display version info and exit.
115644fc5c1Stron
1162227fc13Smrg  -c            (ignored)
1172227fc13Smrg  -C            install only if different (preserve the last data modification time)
1182227fc13Smrg  -d            create directories instead of installing files.
1192227fc13Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1202227fc13Smrg  -m MODE       $chmodprog installed files to MODE.
1212227fc13Smrg  -o USER       $chownprog installed files to USER.
1222227fc13Smrg  -s            $stripprog installed files.
1232227fc13Smrg  -t DIRECTORY  install into DIRECTORY.
1242227fc13Smrg  -T            report an error if DSTFILE is a directory.
125644fc5c1Stron
1262227fc13SmrgEnvironment variables override the default commands:
1272227fc13Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1282227fc13Smrg  RMPROG STRIPPROG
1292227fc13Smrg"
130644fc5c1Stron
1312227fc13Smrgwhile test $# -ne 0; do
1322227fc13Smrg  case $1 in
1332227fc13Smrg    -c) ;;
134644fc5c1Stron
1352227fc13Smrg    -C) copy_on_change=true;;
136644fc5c1Stron
1372227fc13Smrg    -d) dir_arg=true;;
138644fc5c1Stron
1392227fc13Smrg    -g) chgrpcmd="$chgrpprog $2"
1402227fc13Smrg	shift;;
141644fc5c1Stron
1422227fc13Smrg    --help) echo "$usage"; exit $?;;
143644fc5c1Stron
1442227fc13Smrg    -m) mode=$2
1452227fc13Smrg	case $mode in
1462227fc13Smrg	  *' '* | *'	'* | *'
1472227fc13Smrg'*	  | *'*'* | *'?'* | *'['*)
1482227fc13Smrg	    echo "$0: invalid mode: $mode" >&2
1492227fc13Smrg	    exit 1;;
1502227fc13Smrg	esac
1512227fc13Smrg	shift;;
152644fc5c1Stron
1532227fc13Smrg    -o) chowncmd="$chownprog $2"
1542227fc13Smrg	shift;;
155644fc5c1Stron
1562227fc13Smrg    -s) stripcmd=$stripprog;;
157644fc5c1Stron
1582227fc13Smrg    -t) dst_arg=$2
1592227fc13Smrg	shift;;
160644fc5c1Stron
1612227fc13Smrg    -T) no_target_directory=true;;
162644fc5c1Stron
1632227fc13Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164644fc5c1Stron
1652227fc13Smrg    --)	shift
1662227fc13Smrg	break;;
167644fc5c1Stron
1682227fc13Smrg    -*)	echo "$0: invalid option: $1" >&2
1692227fc13Smrg	exit 1;;
170644fc5c1Stron
1712227fc13Smrg    *)  break;;
1722227fc13Smrg  esac
1732227fc13Smrg  shift
1742227fc13Smrgdone
175644fc5c1Stron
1762227fc13Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1772227fc13Smrg  # When -d is used, all remaining arguments are directories to create.
1782227fc13Smrg  # When -t is used, the destination is already specified.
1792227fc13Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1802227fc13Smrg  for arg
1812227fc13Smrg  do
1822227fc13Smrg    if test -n "$dst_arg"; then
1832227fc13Smrg      # $@ is not empty: it contains at least $arg.
1842227fc13Smrg      set fnord "$@" "$dst_arg"
1852227fc13Smrg      shift # fnord
1862227fc13Smrg    fi
1872227fc13Smrg    shift # arg
1882227fc13Smrg    dst_arg=$arg
1892227fc13Smrg  done
1902227fc13Smrgfi
191644fc5c1Stron
1922227fc13Smrgif test $# -eq 0; then
1932227fc13Smrg  if test -z "$dir_arg"; then
1942227fc13Smrg    echo "$0: no input file specified." >&2
1952227fc13Smrg    exit 1
1962227fc13Smrg  fi
1972227fc13Smrg  # It's OK to call `install-sh -d' without argument.
1982227fc13Smrg  # This can happen when creating conditional directories.
1992227fc13Smrg  exit 0
2002227fc13Smrgfi
201644fc5c1Stron
2022227fc13Smrgif test -z "$dir_arg"; then
2032227fc13Smrg  trap '(exit $?); exit' 1 2 13 15
2042227fc13Smrg
2052227fc13Smrg  # Set umask so as not to create temps with too-generous modes.
2062227fc13Smrg  # However, 'strip' requires both read and write access to temps.
2072227fc13Smrg  case $mode in
2082227fc13Smrg    # Optimize common cases.
2092227fc13Smrg    *644) cp_umask=133;;
2102227fc13Smrg    *755) cp_umask=22;;
2112227fc13Smrg
2122227fc13Smrg    *[0-7])
2132227fc13Smrg      if test -z "$stripcmd"; then
2142227fc13Smrg	u_plus_rw=
2152227fc13Smrg      else
2162227fc13Smrg	u_plus_rw='% 200'
2172227fc13Smrg      fi
2182227fc13Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2192227fc13Smrg    *)
2202227fc13Smrg      if test -z "$stripcmd"; then
2212227fc13Smrg	u_plus_rw=
2222227fc13Smrg      else
2232227fc13Smrg	u_plus_rw=,u+rw
2242227fc13Smrg      fi
2252227fc13Smrg      cp_umask=$mode$u_plus_rw;;
2262227fc13Smrg  esac
2272227fc13Smrgfi
228644fc5c1Stron
2292227fc13Smrgfor src
2302227fc13Smrgdo
2312227fc13Smrg  # Protect names starting with `-'.
2322227fc13Smrg  case $src in
2332227fc13Smrg    -*) src=./$src;;
2342227fc13Smrg  esac
2352227fc13Smrg
2362227fc13Smrg  if test -n "$dir_arg"; then
2372227fc13Smrg    dst=$src
2382227fc13Smrg    dstdir=$dst
2392227fc13Smrg    test -d "$dstdir"
2402227fc13Smrg    dstdir_status=$?
2412227fc13Smrg  else
2422227fc13Smrg
2432227fc13Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2442227fc13Smrg    # might cause directories to be created, which would be especially bad
2452227fc13Smrg    # if $src (and thus $dsttmp) contains '*'.
2462227fc13Smrg    if test ! -f "$src" && test ! -d "$src"; then
2472227fc13Smrg      echo "$0: $src does not exist." >&2
2482227fc13Smrg      exit 1
2492227fc13Smrg    fi
2502227fc13Smrg
2512227fc13Smrg    if test -z "$dst_arg"; then
2522227fc13Smrg      echo "$0: no destination specified." >&2
2532227fc13Smrg      exit 1
2542227fc13Smrg    fi
2552227fc13Smrg
2562227fc13Smrg    dst=$dst_arg
2572227fc13Smrg    # Protect names starting with `-'.
2582227fc13Smrg    case $dst in
2592227fc13Smrg      -*) dst=./$dst;;
2602227fc13Smrg    esac
261644fc5c1Stron
2622227fc13Smrg    # If destination is a directory, append the input filename; won't work
2632227fc13Smrg    # if double slashes aren't ignored.
2642227fc13Smrg    if test -d "$dst"; then
2652227fc13Smrg      if test -n "$no_target_directory"; then
2662227fc13Smrg	echo "$0: $dst_arg: Is a directory" >&2
2672227fc13Smrg	exit 1
2682227fc13Smrg      fi
2692227fc13Smrg      dstdir=$dst
2702227fc13Smrg      dst=$dstdir/`basename "$src"`
2712227fc13Smrg      dstdir_status=0
2722227fc13Smrg    else
2732227fc13Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2742227fc13Smrg      dstdir=`
2752227fc13Smrg	(dirname "$dst") 2>/dev/null ||
2762227fc13Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2772227fc13Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2782227fc13Smrg	     X"$dst" : 'X\(//\)$' \| \
2792227fc13Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2802227fc13Smrg	echo X"$dst" |
2812227fc13Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2822227fc13Smrg		   s//\1/
2832227fc13Smrg		   q
2842227fc13Smrg		 }
2852227fc13Smrg		 /^X\(\/\/\)[^/].*/{
2862227fc13Smrg		   s//\1/
2872227fc13Smrg		   q
2882227fc13Smrg		 }
2892227fc13Smrg		 /^X\(\/\/\)$/{
2902227fc13Smrg		   s//\1/
2912227fc13Smrg		   q
2922227fc13Smrg		 }
2932227fc13Smrg		 /^X\(\/\).*/{
2942227fc13Smrg		   s//\1/
2952227fc13Smrg		   q
2962227fc13Smrg		 }
2972227fc13Smrg		 s/.*/./; q'
2982227fc13Smrg      `
2992227fc13Smrg
3002227fc13Smrg      test -d "$dstdir"
3012227fc13Smrg      dstdir_status=$?
3022227fc13Smrg    fi
3032227fc13Smrg  fi
3042227fc13Smrg
3052227fc13Smrg  obsolete_mkdir_used=false
3062227fc13Smrg
3072227fc13Smrg  if test $dstdir_status != 0; then
3082227fc13Smrg    case $posix_mkdir in
3092227fc13Smrg      '')
3102227fc13Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3112227fc13Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3122227fc13Smrg	umask=`umask`
3132227fc13Smrg	case $stripcmd.$umask in
3142227fc13Smrg	  # Optimize common cases.
3152227fc13Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3162227fc13Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3172227fc13Smrg
3182227fc13Smrg	  *[0-7])
3192227fc13Smrg	    mkdir_umask=`expr $umask + 22 \
3202227fc13Smrg	      - $umask % 100 % 40 + $umask % 20 \
3212227fc13Smrg	      - $umask % 10 % 4 + $umask % 2
3222227fc13Smrg	    `;;
3232227fc13Smrg	  *) mkdir_umask=$umask,go-w;;
3242227fc13Smrg	esac
3252227fc13Smrg
3262227fc13Smrg	# With -d, create the new directory with the user-specified mode.
3272227fc13Smrg	# Otherwise, rely on $mkdir_umask.
3282227fc13Smrg	if test -n "$dir_arg"; then
3292227fc13Smrg	  mkdir_mode=-m$mode
330644fc5c1Stron	else
3312227fc13Smrg	  mkdir_mode=
332644fc5c1Stron	fi
333644fc5c1Stron
3342227fc13Smrg	posix_mkdir=false
3352227fc13Smrg	case $umask in
3362227fc13Smrg	  *[123567][0-7][0-7])
3372227fc13Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3382227fc13Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3392227fc13Smrg	    ;;
3402227fc13Smrg	  *)
3412227fc13Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3422227fc13Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3432227fc13Smrg
3442227fc13Smrg	    if (umask $mkdir_umask &&
3452227fc13Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3462227fc13Smrg	    then
3472227fc13Smrg	      if test -z "$dir_arg" || {
3482227fc13Smrg		   # Check for POSIX incompatibilities with -m.
3492227fc13Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3502227fc13Smrg		   # other-writeable bit of parent directory when it shouldn't.
3512227fc13Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3522227fc13Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3532227fc13Smrg		   case $ls_ld_tmpdir in
3542227fc13Smrg		     d????-?r-*) different_mode=700;;
3552227fc13Smrg		     d????-?--*) different_mode=755;;
3562227fc13Smrg		     *) false;;
3572227fc13Smrg		   esac &&
3582227fc13Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3592227fc13Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3602227fc13Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3612227fc13Smrg		   }
3622227fc13Smrg		 }
3632227fc13Smrg	      then posix_mkdir=:
3642227fc13Smrg	      fi
3652227fc13Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3662227fc13Smrg	    else
3672227fc13Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3682227fc13Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3692227fc13Smrg	    fi
3702227fc13Smrg	    trap '' 0;;
3712227fc13Smrg	esac;;
3722227fc13Smrg    esac
373644fc5c1Stron
3742227fc13Smrg    if
3752227fc13Smrg      $posix_mkdir && (
3762227fc13Smrg	umask $mkdir_umask &&
3772227fc13Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3782227fc13Smrg      )
3792227fc13Smrg    then :
3802227fc13Smrg    else
3812227fc13Smrg
3822227fc13Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3832227fc13Smrg      # or it failed possibly due to a race condition.  Create the
3842227fc13Smrg      # directory the slow way, step by step, checking for races as we go.
3852227fc13Smrg
3862227fc13Smrg      case $dstdir in
3872227fc13Smrg	/*) prefix='/';;
3882227fc13Smrg	-*) prefix='./';;
3892227fc13Smrg	*)  prefix='';;
3902227fc13Smrg      esac
3912227fc13Smrg
3922227fc13Smrg      eval "$initialize_posix_glob"
3932227fc13Smrg
3942227fc13Smrg      oIFS=$IFS
3952227fc13Smrg      IFS=/
3962227fc13Smrg      $posix_glob set -f
3972227fc13Smrg      set fnord $dstdir
3982227fc13Smrg      shift
3992227fc13Smrg      $posix_glob set +f
4002227fc13Smrg      IFS=$oIFS
4012227fc13Smrg
4022227fc13Smrg      prefixes=
4032227fc13Smrg
4042227fc13Smrg      for d
4052227fc13Smrg      do
4062227fc13Smrg	test -z "$d" && continue
4072227fc13Smrg
4082227fc13Smrg	prefix=$prefix$d
4092227fc13Smrg	if test -d "$prefix"; then
4102227fc13Smrg	  prefixes=
4112227fc13Smrg	else
4122227fc13Smrg	  if $posix_mkdir; then
4132227fc13Smrg	    (umask=$mkdir_umask &&
4142227fc13Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4152227fc13Smrg	    # Don't fail if two instances are running concurrently.
4162227fc13Smrg	    test -d "$prefix" || exit 1
4172227fc13Smrg	  else
4182227fc13Smrg	    case $prefix in
4192227fc13Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4202227fc13Smrg	      *) qprefix=$prefix;;
4212227fc13Smrg	    esac
4222227fc13Smrg	    prefixes="$prefixes '$qprefix'"
4232227fc13Smrg	  fi
4242227fc13Smrg	fi
4252227fc13Smrg	prefix=$prefix/
4262227fc13Smrg      done
4272227fc13Smrg
4282227fc13Smrg      if test -n "$prefixes"; then
4292227fc13Smrg	# Don't fail if two instances are running concurrently.
4302227fc13Smrg	(umask $mkdir_umask &&
4312227fc13Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4322227fc13Smrg	  test -d "$dstdir" || exit 1
4332227fc13Smrg	obsolete_mkdir_used=true
4342227fc13Smrg      fi
4352227fc13Smrg    fi
4362227fc13Smrg  fi
4372227fc13Smrg
4382227fc13Smrg  if test -n "$dir_arg"; then
4392227fc13Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4402227fc13Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4412227fc13Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4422227fc13Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4432227fc13Smrg  else
4442227fc13Smrg
4452227fc13Smrg    # Make a couple of temp file names in the proper directory.
4462227fc13Smrg    dsttmp=$dstdir/_inst.$$_
4472227fc13Smrg    rmtmp=$dstdir/_rm.$$_
4482227fc13Smrg
4492227fc13Smrg    # Trap to clean up those temp files at exit.
4502227fc13Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4512227fc13Smrg
4522227fc13Smrg    # Copy the file name to the temp name.
4532227fc13Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4542227fc13Smrg
4552227fc13Smrg    # and set any options; do chmod last to preserve setuid bits.
4562227fc13Smrg    #
4572227fc13Smrg    # If any of these fail, we abort the whole thing.  If we want to
4582227fc13Smrg    # ignore errors from any of these, just make sure not to ignore
4592227fc13Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4602227fc13Smrg    #
4612227fc13Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4622227fc13Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4632227fc13Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4642227fc13Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4652227fc13Smrg
4662227fc13Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4672227fc13Smrg    if $copy_on_change &&
4682227fc13Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4692227fc13Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4702227fc13Smrg
4712227fc13Smrg       eval "$initialize_posix_glob" &&
4722227fc13Smrg       $posix_glob set -f &&
4732227fc13Smrg       set X $old && old=:$2:$4:$5:$6 &&
4742227fc13Smrg       set X $new && new=:$2:$4:$5:$6 &&
4752227fc13Smrg       $posix_glob set +f &&
4762227fc13Smrg
4772227fc13Smrg       test "$old" = "$new" &&
4782227fc13Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4792227fc13Smrg    then
4802227fc13Smrg      rm -f "$dsttmp"
4812227fc13Smrg    else
4822227fc13Smrg      # Rename the file to the real destination.
4832227fc13Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4842227fc13Smrg
4852227fc13Smrg      # The rename failed, perhaps because mv can't rename something else
4862227fc13Smrg      # to itself, or perhaps because mv is so ancient that it does not
4872227fc13Smrg      # support -f.
4882227fc13Smrg      {
4892227fc13Smrg	# Now remove or move aside any old file at destination location.
4902227fc13Smrg	# We try this two ways since rm can't unlink itself on some
4912227fc13Smrg	# systems and the destination file might be busy for other
4922227fc13Smrg	# reasons.  In this case, the final cleanup might fail but the new
4932227fc13Smrg	# file should still install successfully.
4942227fc13Smrg	{
4952227fc13Smrg	  test ! -f "$dst" ||
4962227fc13Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4972227fc13Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4982227fc13Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4992227fc13Smrg	  } ||
5002227fc13Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5012227fc13Smrg	    (exit 1); exit 1
5022227fc13Smrg	  }
5032227fc13Smrg	} &&
5042227fc13Smrg
5052227fc13Smrg	# Now rename the file to the real destination.
5062227fc13Smrg	$doit $mvcmd "$dsttmp" "$dst"
5072227fc13Smrg      }
5082227fc13Smrg    fi || exit 1
5092227fc13Smrg
5102227fc13Smrg    trap '' 0
5112227fc13Smrg  fi
5122227fc13Smrgdone
513644fc5c1Stron
5142227fc13Smrg# Local variables:
5152227fc13Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5162227fc13Smrg# time-stamp-start: "scriptversion="
5172227fc13Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5182227fc13Smrg# time-stamp-time-zone: "UTC"
5192227fc13Smrg# time-stamp-end: "; # UTC"
5202227fc13Smrg# End:
521