install-sh revision 18552c8a
1301ea0f4Smrg#!/bin/sh
2301ea0f4Smrg# install - install a program, script, or datafile
3301ea0f4Smrg
418552c8aSmrgscriptversion=2011-11-20.07; # UTC
5301ea0f4Smrg
6301ea0f4Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7301ea0f4Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8301ea0f4Smrg# following copyright and license.
9301ea0f4Smrg#
10301ea0f4Smrg# Copyright (C) 1994 X Consortium
11301ea0f4Smrg#
12301ea0f4Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13301ea0f4Smrg# of this software and associated documentation files (the "Software"), to
14301ea0f4Smrg# deal in the Software without restriction, including without limitation the
15301ea0f4Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16301ea0f4Smrg# sell copies of the Software, and to permit persons to whom the Software is
17301ea0f4Smrg# furnished to do so, subject to the following conditions:
18301ea0f4Smrg#
19301ea0f4Smrg# The above copyright notice and this permission notice shall be included in
20301ea0f4Smrg# all copies or substantial portions of the Software.
21301ea0f4Smrg#
22301ea0f4Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23301ea0f4Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24301ea0f4Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25301ea0f4Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26301ea0f4Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27301ea0f4Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28301ea0f4Smrg#
29301ea0f4Smrg# Except as contained in this notice, the name of the X Consortium shall not
30301ea0f4Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31301ea0f4Smrg# ings in this Software without prior written authorization from the X Consor-
32301ea0f4Smrg# tium.
33301ea0f4Smrg#
34301ea0f4Smrg#
35301ea0f4Smrg# FSF changes to this file are in the public domain.
36301ea0f4Smrg#
37301ea0f4Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3818552c8aSmrg# 'make' implicit rules from creating a file called install from it
39301ea0f4Smrg# when there is no Makefile.
40301ea0f4Smrg#
41301ea0f4Smrg# This script is compatible with the BSD install script, but was written
420cc67336Smrg# from scratch.
430cc67336Smrg
440cc67336Smrgnl='
450cc67336Smrg'
460cc67336SmrgIFS=" ""	$nl"
47301ea0f4Smrg
48301ea0f4Smrg# set DOITPROG to echo to test this script
49301ea0f4Smrg
50301ea0f4Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
510cc67336Smrgdoit=${DOITPROG-}
520cc67336Smrgif test -z "$doit"; then
530cc67336Smrg  doit_exec=exec
540cc67336Smrgelse
550cc67336Smrg  doit_exec=$doit
560cc67336Smrgfi
57301ea0f4Smrg
580cc67336Smrg# Put in absolute file names if you don't have them in your path;
590cc67336Smrg# or use environment vars.
600cc67336Smrg
610cc67336Smrgchgrpprog=${CHGRPPROG-chgrp}
620cc67336Smrgchmodprog=${CHMODPROG-chmod}
630cc67336Smrgchownprog=${CHOWNPROG-chown}
640cc67336Smrgcmpprog=${CMPPROG-cmp}
650cc67336Smrgcpprog=${CPPROG-cp}
660cc67336Smrgmkdirprog=${MKDIRPROG-mkdir}
670cc67336Smrgmvprog=${MVPROG-mv}
680cc67336Smrgrmprog=${RMPROG-rm}
690cc67336Smrgstripprog=${STRIPPROG-strip}
700cc67336Smrg
710cc67336Smrgposix_glob='?'
720cc67336Smrginitialize_posix_glob='
730cc67336Smrg  test "$posix_glob" != "?" || {
740cc67336Smrg    if (set -f) 2>/dev/null; then
750cc67336Smrg      posix_glob=
760cc67336Smrg    else
770cc67336Smrg      posix_glob=:
780cc67336Smrg    fi
790cc67336Smrg  }
800cc67336Smrg'
81301ea0f4Smrg
820cc67336Smrgposix_mkdir=
830cc67336Smrg
840cc67336Smrg# Desired mode of installed file.
850cc67336Smrgmode=0755
86301ea0f4Smrg
87301ea0f4Smrgchgrpcmd=
880cc67336Smrgchmodcmd=$chmodprog
890cc67336Smrgchowncmd=
900cc67336Smrgmvcmd=$mvprog
91301ea0f4Smrgrmcmd="$rmprog -f"
920cc67336Smrgstripcmd=
930cc67336Smrg
94301ea0f4Smrgsrc=
95301ea0f4Smrgdst=
96301ea0f4Smrgdir_arg=
970cc67336Smrgdst_arg=
980cc67336Smrg
990cc67336Smrgcopy_on_change=false
100301ea0f4Smrgno_target_directory=
101301ea0f4Smrg
1020cc67336Smrgusage="\
1030cc67336SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104301ea0f4Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105301ea0f4Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106301ea0f4Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107301ea0f4Smrg
108301ea0f4SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109301ea0f4SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110301ea0f4SmrgIn the 4th, create DIRECTORIES.
111301ea0f4Smrg
112301ea0f4SmrgOptions:
1130cc67336Smrg     --help     display this help and exit.
1140cc67336Smrg     --version  display version info and exit.
1150cc67336Smrg
1160cc67336Smrg  -c            (ignored)
1170cc67336Smrg  -C            install only if different (preserve the last data modification time)
1180cc67336Smrg  -d            create directories instead of installing files.
1190cc67336Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1200cc67336Smrg  -m MODE       $chmodprog installed files to MODE.
1210cc67336Smrg  -o USER       $chownprog installed files to USER.
1220cc67336Smrg  -s            $stripprog installed files.
1230cc67336Smrg  -t DIRECTORY  install into DIRECTORY.
1240cc67336Smrg  -T            report an error if DSTFILE is a directory.
125301ea0f4Smrg
126301ea0f4SmrgEnvironment variables override the default commands:
1270cc67336Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1280cc67336Smrg  RMPROG STRIPPROG
129301ea0f4Smrg"
130301ea0f4Smrg
1310cc67336Smrgwhile test $# -ne 0; do
132301ea0f4Smrg  case $1 in
1330cc67336Smrg    -c) ;;
1340cc67336Smrg
1350cc67336Smrg    -C) copy_on_change=true;;
136301ea0f4Smrg
1370cc67336Smrg    -d) dir_arg=true;;
138301ea0f4Smrg
139301ea0f4Smrg    -g) chgrpcmd="$chgrpprog $2"
1400cc67336Smrg	shift;;
141301ea0f4Smrg
142301ea0f4Smrg    --help) echo "$usage"; exit $?;;
143301ea0f4Smrg
1440cc67336Smrg    -m) mode=$2
1450cc67336Smrg	case $mode in
1460cc67336Smrg	  *' '* | *'	'* | *'
1470cc67336Smrg'*	  | *'*'* | *'?'* | *'['*)
1480cc67336Smrg	    echo "$0: invalid mode: $mode" >&2
1490cc67336Smrg	    exit 1;;
1500cc67336Smrg	esac
1510cc67336Smrg	shift;;
152301ea0f4Smrg
153301ea0f4Smrg    -o) chowncmd="$chownprog $2"
1540cc67336Smrg	shift;;
155301ea0f4Smrg
1560cc67336Smrg    -s) stripcmd=$stripprog;;
157301ea0f4Smrg
1580cc67336Smrg    -t) dst_arg=$2
15918552c8aSmrg	# Protect names problematic for 'test' and other utilities.
160213fdd94Smrg	case $dst_arg in
161213fdd94Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162213fdd94Smrg	esac
1630cc67336Smrg	shift;;
164301ea0f4Smrg
1650cc67336Smrg    -T) no_target_directory=true;;
166301ea0f4Smrg
167301ea0f4Smrg    --version) echo "$0 $scriptversion"; exit $?;;
168301ea0f4Smrg
1690cc67336Smrg    --)	shift
170301ea0f4Smrg	break;;
1710cc67336Smrg
1720cc67336Smrg    -*)	echo "$0: invalid option: $1" >&2
1730cc67336Smrg	exit 1;;
1740cc67336Smrg
1750cc67336Smrg    *)  break;;
176301ea0f4Smrg  esac
1770cc67336Smrg  shift
178301ea0f4Smrgdone
179301ea0f4Smrg
1800cc67336Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1810cc67336Smrg  # When -d is used, all remaining arguments are directories to create.
1820cc67336Smrg  # When -t is used, the destination is already specified.
1830cc67336Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1840cc67336Smrg  for arg
1850cc67336Smrg  do
1860cc67336Smrg    if test -n "$dst_arg"; then
1870cc67336Smrg      # $@ is not empty: it contains at least $arg.
1880cc67336Smrg      set fnord "$@" "$dst_arg"
1890cc67336Smrg      shift # fnord
1900cc67336Smrg    fi
1910cc67336Smrg    shift # arg
1920cc67336Smrg    dst_arg=$arg
19318552c8aSmrg    # Protect names problematic for 'test' and other utilities.
194213fdd94Smrg    case $dst_arg in
195213fdd94Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196213fdd94Smrg    esac
1970cc67336Smrg  done
1980cc67336Smrgfi
1990cc67336Smrg
2000cc67336Smrgif test $# -eq 0; then
201301ea0f4Smrg  if test -z "$dir_arg"; then
202301ea0f4Smrg    echo "$0: no input file specified." >&2
203301ea0f4Smrg    exit 1
204301ea0f4Smrg  fi
20518552c8aSmrg  # It's OK to call 'install-sh -d' without argument.
206301ea0f4Smrg  # This can happen when creating conditional directories.
207301ea0f4Smrg  exit 0
208301ea0f4Smrgfi
209301ea0f4Smrg
2100cc67336Smrgif test -z "$dir_arg"; then
211213fdd94Smrg  do_exit='(exit $ret); exit $ret'
212213fdd94Smrg  trap "ret=129; $do_exit" 1
213213fdd94Smrg  trap "ret=130; $do_exit" 2
214213fdd94Smrg  trap "ret=141; $do_exit" 13
215213fdd94Smrg  trap "ret=143; $do_exit" 15
2160cc67336Smrg
2170cc67336Smrg  # Set umask so as not to create temps with too-generous modes.
2180cc67336Smrg  # However, 'strip' requires both read and write access to temps.
2190cc67336Smrg  case $mode in
2200cc67336Smrg    # Optimize common cases.
2210cc67336Smrg    *644) cp_umask=133;;
2220cc67336Smrg    *755) cp_umask=22;;
2230cc67336Smrg
2240cc67336Smrg    *[0-7])
2250cc67336Smrg      if test -z "$stripcmd"; then
2260cc67336Smrg	u_plus_rw=
2270cc67336Smrg      else
2280cc67336Smrg	u_plus_rw='% 200'
2290cc67336Smrg      fi
2300cc67336Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2310cc67336Smrg    *)
2320cc67336Smrg      if test -z "$stripcmd"; then
2330cc67336Smrg	u_plus_rw=
2340cc67336Smrg      else
2350cc67336Smrg	u_plus_rw=,u+rw
2360cc67336Smrg      fi
2370cc67336Smrg      cp_umask=$mode$u_plus_rw;;
2380cc67336Smrg  esac
2390cc67336Smrgfi
2400cc67336Smrg
241301ea0f4Smrgfor src
242301ea0f4Smrgdo
24318552c8aSmrg  # Protect names problematic for 'test' and other utilities.
244301ea0f4Smrg  case $src in
245213fdd94Smrg    -* | [=\(\)!]) src=./$src;;
246301ea0f4Smrg  esac
247301ea0f4Smrg
248301ea0f4Smrg  if test -n "$dir_arg"; then
249301ea0f4Smrg    dst=$src
2500cc67336Smrg    dstdir=$dst
2510cc67336Smrg    test -d "$dstdir"
2520cc67336Smrg    dstdir_status=$?
253301ea0f4Smrg  else
2540cc67336Smrg
255301ea0f4Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256301ea0f4Smrg    # might cause directories to be created, which would be especially bad
257301ea0f4Smrg    # if $src (and thus $dsttmp) contains '*'.
258301ea0f4Smrg    if test ! -f "$src" && test ! -d "$src"; then
259301ea0f4Smrg      echo "$0: $src does not exist." >&2
260301ea0f4Smrg      exit 1
261301ea0f4Smrg    fi
262301ea0f4Smrg
2630cc67336Smrg    if test -z "$dst_arg"; then
264301ea0f4Smrg      echo "$0: no destination specified." >&2
265301ea0f4Smrg      exit 1
266301ea0f4Smrg    fi
2670cc67336Smrg    dst=$dst_arg
268301ea0f4Smrg
269301ea0f4Smrg    # If destination is a directory, append the input filename; won't work
270301ea0f4Smrg    # if double slashes aren't ignored.
271301ea0f4Smrg    if test -d "$dst"; then
272301ea0f4Smrg      if test -n "$no_target_directory"; then
2730cc67336Smrg	echo "$0: $dst_arg: Is a directory" >&2
274301ea0f4Smrg	exit 1
275301ea0f4Smrg      fi
2760cc67336Smrg      dstdir=$dst
2770cc67336Smrg      dst=$dstdir/`basename "$src"`
2780cc67336Smrg      dstdir_status=0
2790cc67336Smrg    else
2800cc67336Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2810cc67336Smrg      dstdir=`
2820cc67336Smrg	(dirname "$dst") 2>/dev/null ||
2830cc67336Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2840cc67336Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2850cc67336Smrg	     X"$dst" : 'X\(//\)$' \| \
2860cc67336Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2870cc67336Smrg	echo X"$dst" |
2880cc67336Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2890cc67336Smrg		   s//\1/
2900cc67336Smrg		   q
2910cc67336Smrg		 }
2920cc67336Smrg		 /^X\(\/\/\)[^/].*/{
2930cc67336Smrg		   s//\1/
2940cc67336Smrg		   q
2950cc67336Smrg		 }
2960cc67336Smrg		 /^X\(\/\/\)$/{
2970cc67336Smrg		   s//\1/
2980cc67336Smrg		   q
2990cc67336Smrg		 }
3000cc67336Smrg		 /^X\(\/\).*/{
3010cc67336Smrg		   s//\1/
3020cc67336Smrg		   q
3030cc67336Smrg		 }
3040cc67336Smrg		 s/.*/./; q'
3050cc67336Smrg      `
3060cc67336Smrg
3070cc67336Smrg      test -d "$dstdir"
3080cc67336Smrg      dstdir_status=$?
309301ea0f4Smrg    fi
310301ea0f4Smrg  fi
311301ea0f4Smrg
3120cc67336Smrg  obsolete_mkdir_used=false
3130cc67336Smrg
3140cc67336Smrg  if test $dstdir_status != 0; then
3150cc67336Smrg    case $posix_mkdir in
3160cc67336Smrg      '')
3170cc67336Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3180cc67336Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3190cc67336Smrg	umask=`umask`
3200cc67336Smrg	case $stripcmd.$umask in
3210cc67336Smrg	  # Optimize common cases.
3220cc67336Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3230cc67336Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3240cc67336Smrg
3250cc67336Smrg	  *[0-7])
3260cc67336Smrg	    mkdir_umask=`expr $umask + 22 \
3270cc67336Smrg	      - $umask % 100 % 40 + $umask % 20 \
3280cc67336Smrg	      - $umask % 10 % 4 + $umask % 2
3290cc67336Smrg	    `;;
3300cc67336Smrg	  *) mkdir_umask=$umask,go-w;;
3310cc67336Smrg	esac
3320cc67336Smrg
3330cc67336Smrg	# With -d, create the new directory with the user-specified mode.
3340cc67336Smrg	# Otherwise, rely on $mkdir_umask.
3350cc67336Smrg	if test -n "$dir_arg"; then
3360cc67336Smrg	  mkdir_mode=-m$mode
3370cc67336Smrg	else
3380cc67336Smrg	  mkdir_mode=
3390cc67336Smrg	fi
3400cc67336Smrg
3410cc67336Smrg	posix_mkdir=false
3420cc67336Smrg	case $umask in
3430cc67336Smrg	  *[123567][0-7][0-7])
3440cc67336Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3450cc67336Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3460cc67336Smrg	    ;;
3470cc67336Smrg	  *)
3480cc67336Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3490cc67336Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3500cc67336Smrg
3510cc67336Smrg	    if (umask $mkdir_umask &&
3520cc67336Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3530cc67336Smrg	    then
3540cc67336Smrg	      if test -z "$dir_arg" || {
3550cc67336Smrg		   # Check for POSIX incompatibilities with -m.
3560cc67336Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35718552c8aSmrg		   # other-writable bit of parent directory when it shouldn't.
3580cc67336Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3590cc67336Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3600cc67336Smrg		   case $ls_ld_tmpdir in
3610cc67336Smrg		     d????-?r-*) different_mode=700;;
3620cc67336Smrg		     d????-?--*) different_mode=755;;
3630cc67336Smrg		     *) false;;
3640cc67336Smrg		   esac &&
3650cc67336Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3660cc67336Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3670cc67336Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3680cc67336Smrg		   }
3690cc67336Smrg		 }
3700cc67336Smrg	      then posix_mkdir=:
3710cc67336Smrg	      fi
3720cc67336Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3730cc67336Smrg	    else
3740cc67336Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3750cc67336Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3760cc67336Smrg	    fi
3770cc67336Smrg	    trap '' 0;;
3780cc67336Smrg	esac;;
3790cc67336Smrg    esac
380301ea0f4Smrg
3810cc67336Smrg    if
3820cc67336Smrg      $posix_mkdir && (
3830cc67336Smrg	umask $mkdir_umask &&
3840cc67336Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3850cc67336Smrg      )
3860cc67336Smrg    then :
3870cc67336Smrg    else
388301ea0f4Smrg
3890cc67336Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3900cc67336Smrg      # or it failed possibly due to a race condition.  Create the
3910cc67336Smrg      # directory the slow way, step by step, checking for races as we go.
392301ea0f4Smrg
3930cc67336Smrg      case $dstdir in
3940cc67336Smrg	/*) prefix='/';;
395213fdd94Smrg	[-=\(\)!]*) prefix='./';;
3960cc67336Smrg	*)  prefix='';;
3970cc67336Smrg      esac
398301ea0f4Smrg
3990cc67336Smrg      eval "$initialize_posix_glob"
400301ea0f4Smrg
4010cc67336Smrg      oIFS=$IFS
4020cc67336Smrg      IFS=/
4030cc67336Smrg      $posix_glob set -f
4040cc67336Smrg      set fnord $dstdir
405301ea0f4Smrg      shift
4060cc67336Smrg      $posix_glob set +f
4070cc67336Smrg      IFS=$oIFS
4080cc67336Smrg
4090cc67336Smrg      prefixes=
4100cc67336Smrg
4110cc67336Smrg      for d
4120cc67336Smrg      do
413213fdd94Smrg	test X"$d" = X && continue
4140cc67336Smrg
4150cc67336Smrg	prefix=$prefix$d
4160cc67336Smrg	if test -d "$prefix"; then
4170cc67336Smrg	  prefixes=
4180cc67336Smrg	else
4190cc67336Smrg	  if $posix_mkdir; then
4200cc67336Smrg	    (umask=$mkdir_umask &&
4210cc67336Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4220cc67336Smrg	    # Don't fail if two instances are running concurrently.
4230cc67336Smrg	    test -d "$prefix" || exit 1
4240cc67336Smrg	  else
4250cc67336Smrg	    case $prefix in
4260cc67336Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4270cc67336Smrg	      *) qprefix=$prefix;;
4280cc67336Smrg	    esac
4290cc67336Smrg	    prefixes="$prefixes '$qprefix'"
4300cc67336Smrg	  fi
4310cc67336Smrg	fi
4320cc67336Smrg	prefix=$prefix/
4330cc67336Smrg      done
4340cc67336Smrg
4350cc67336Smrg      if test -n "$prefixes"; then
4360cc67336Smrg	# Don't fail if two instances are running concurrently.
4370cc67336Smrg	(umask $mkdir_umask &&
4380cc67336Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4390cc67336Smrg	  test -d "$dstdir" || exit 1
4400cc67336Smrg	obsolete_mkdir_used=true
441301ea0f4Smrg      fi
4420cc67336Smrg    fi
443301ea0f4Smrg  fi
444301ea0f4Smrg
445301ea0f4Smrg  if test -n "$dir_arg"; then
4460cc67336Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4470cc67336Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4480cc67336Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4490cc67336Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450301ea0f4Smrg  else
451301ea0f4Smrg
452301ea0f4Smrg    # Make a couple of temp file names in the proper directory.
453301ea0f4Smrg    dsttmp=$dstdir/_inst.$$_
454301ea0f4Smrg    rmtmp=$dstdir/_rm.$$_
455301ea0f4Smrg
456301ea0f4Smrg    # Trap to clean up those temp files at exit.
457301ea0f4Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458301ea0f4Smrg
459301ea0f4Smrg    # Copy the file name to the temp name.
4600cc67336Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461301ea0f4Smrg
462301ea0f4Smrg    # and set any options; do chmod last to preserve setuid bits.
463301ea0f4Smrg    #
464301ea0f4Smrg    # If any of these fail, we abort the whole thing.  If we want to
465301ea0f4Smrg    # ignore errors from any of these, just make sure not to ignore
466301ea0f4Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467301ea0f4Smrg    #
4680cc67336Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4690cc67336Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4700cc67336Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4710cc67336Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4720cc67336Smrg
4730cc67336Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4740cc67336Smrg    if $copy_on_change &&
4750cc67336Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4760cc67336Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4770cc67336Smrg
4780cc67336Smrg       eval "$initialize_posix_glob" &&
4790cc67336Smrg       $posix_glob set -f &&
4800cc67336Smrg       set X $old && old=:$2:$4:$5:$6 &&
4810cc67336Smrg       set X $new && new=:$2:$4:$5:$6 &&
4820cc67336Smrg       $posix_glob set +f &&
4830cc67336Smrg
4840cc67336Smrg       test "$old" = "$new" &&
4850cc67336Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4860cc67336Smrg    then
4870cc67336Smrg      rm -f "$dsttmp"
4880cc67336Smrg    else
4890cc67336Smrg      # Rename the file to the real destination.
4900cc67336Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4910cc67336Smrg
4920cc67336Smrg      # The rename failed, perhaps because mv can't rename something else
4930cc67336Smrg      # to itself, or perhaps because mv is so ancient that it does not
4940cc67336Smrg      # support -f.
4950cc67336Smrg      {
4960cc67336Smrg	# Now remove or move aside any old file at destination location.
4970cc67336Smrg	# We try this two ways since rm can't unlink itself on some
4980cc67336Smrg	# systems and the destination file might be busy for other
4990cc67336Smrg	# reasons.  In this case, the final cleanup might fail but the new
5000cc67336Smrg	# file should still install successfully.
5010cc67336Smrg	{
5020cc67336Smrg	  test ! -f "$dst" ||
5030cc67336Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5040cc67336Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5050cc67336Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5060cc67336Smrg	  } ||
5070cc67336Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5080cc67336Smrg	    (exit 1); exit 1
5090cc67336Smrg	  }
5100cc67336Smrg	} &&
5110cc67336Smrg
5120cc67336Smrg	# Now rename the file to the real destination.
5130cc67336Smrg	$doit $mvcmd "$dsttmp" "$dst"
5140cc67336Smrg      }
5150cc67336Smrg    fi || exit 1
5160cc67336Smrg
5170cc67336Smrg    trap '' 0
5180cc67336Smrg  fi
519301ea0f4Smrgdone
520301ea0f4Smrg
521301ea0f4Smrg# Local variables:
522301ea0f4Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523301ea0f4Smrg# time-stamp-start: "scriptversion="
524301ea0f4Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525ef6fb4a5Smrg# time-stamp-time-zone: "UTC"
526ef6fb4a5Smrg# time-stamp-end: "; # UTC"
527301ea0f4Smrg# End:
528