install-sh revision dff01e5a
1c76ae52dSmrg#!/bin/sh
2c76ae52dSmrg# install - install a program, script, or datafile
3c76ae52dSmrg
4dff01e5aSmrgscriptversion=2011-11-20.07; # UTC
5c76ae52dSmrg
6c76ae52dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c76ae52dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8c76ae52dSmrg# following copyright and license.
9c76ae52dSmrg#
10c76ae52dSmrg# Copyright (C) 1994 X Consortium
11c76ae52dSmrg#
12c76ae52dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c76ae52dSmrg# of this software and associated documentation files (the "Software"), to
14c76ae52dSmrg# deal in the Software without restriction, including without limitation the
15c76ae52dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c76ae52dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17c76ae52dSmrg# furnished to do so, subject to the following conditions:
18c76ae52dSmrg#
19c76ae52dSmrg# The above copyright notice and this permission notice shall be included in
20c76ae52dSmrg# all copies or substantial portions of the Software.
21c76ae52dSmrg#
22c76ae52dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c76ae52dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c76ae52dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c76ae52dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c76ae52dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c76ae52dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c76ae52dSmrg#
29c76ae52dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30c76ae52dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c76ae52dSmrg# ings in this Software without prior written authorization from the X Consor-
32c76ae52dSmrg# tium.
33c76ae52dSmrg#
34c76ae52dSmrg#
35c76ae52dSmrg# FSF changes to this file are in the public domain.
36c76ae52dSmrg#
37c76ae52dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38dff01e5aSmrg# 'make' implicit rules from creating a file called install from it
39c76ae52dSmrg# when there is no Makefile.
40c76ae52dSmrg#
41c76ae52dSmrg# This script is compatible with the BSD install script, but was written
420d590c07Smrg# from scratch.
430d590c07Smrg
440d590c07Smrgnl='
450d590c07Smrg'
460d590c07SmrgIFS=" ""	$nl"
47c76ae52dSmrg
48c76ae52dSmrg# set DOITPROG to echo to test this script
49c76ae52dSmrg
50c76ae52dSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
510d590c07Smrgdoit=${DOITPROG-}
520d590c07Smrgif test -z "$doit"; then
530d590c07Smrg  doit_exec=exec
540d590c07Smrgelse
550d590c07Smrg  doit_exec=$doit
560d590c07Smrgfi
57c76ae52dSmrg
580d590c07Smrg# Put in absolute file names if you don't have them in your path;
590d590c07Smrg# or use environment vars.
600d590c07Smrg
610d590c07Smrgchgrpprog=${CHGRPPROG-chgrp}
620d590c07Smrgchmodprog=${CHMODPROG-chmod}
630d590c07Smrgchownprog=${CHOWNPROG-chown}
640d590c07Smrgcmpprog=${CMPPROG-cmp}
650d590c07Smrgcpprog=${CPPROG-cp}
660d590c07Smrgmkdirprog=${MKDIRPROG-mkdir}
670d590c07Smrgmvprog=${MVPROG-mv}
680d590c07Smrgrmprog=${RMPROG-rm}
690d590c07Smrgstripprog=${STRIPPROG-strip}
700d590c07Smrg
710d590c07Smrgposix_glob='?'
720d590c07Smrginitialize_posix_glob='
730d590c07Smrg  test "$posix_glob" != "?" || {
740d590c07Smrg    if (set -f) 2>/dev/null; then
750d590c07Smrg      posix_glob=
760d590c07Smrg    else
770d590c07Smrg      posix_glob=:
780d590c07Smrg    fi
790d590c07Smrg  }
800d590c07Smrg'
81c76ae52dSmrg
820d590c07Smrgposix_mkdir=
830d590c07Smrg
840d590c07Smrg# Desired mode of installed file.
850d590c07Smrgmode=0755
86c76ae52dSmrg
87c76ae52dSmrgchgrpcmd=
880d590c07Smrgchmodcmd=$chmodprog
890d590c07Smrgchowncmd=
900d590c07Smrgmvcmd=$mvprog
91c76ae52dSmrgrmcmd="$rmprog -f"
920d590c07Smrgstripcmd=
930d590c07Smrg
94c76ae52dSmrgsrc=
95c76ae52dSmrgdst=
96c76ae52dSmrgdir_arg=
970d590c07Smrgdst_arg=
980d590c07Smrg
990d590c07Smrgcopy_on_change=false
100c76ae52dSmrgno_target_directory=
101c76ae52dSmrg
1020d590c07Smrgusage="\
1030d590c07SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104c76ae52dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105c76ae52dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106c76ae52dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107c76ae52dSmrg
108c76ae52dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109c76ae52dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110c76ae52dSmrgIn the 4th, create DIRECTORIES.
111c76ae52dSmrg
112c76ae52dSmrgOptions:
1130d590c07Smrg     --help     display this help and exit.
1140d590c07Smrg     --version  display version info and exit.
1150d590c07Smrg
1160d590c07Smrg  -c            (ignored)
1170d590c07Smrg  -C            install only if different (preserve the last data modification time)
1180d590c07Smrg  -d            create directories instead of installing files.
1190d590c07Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1200d590c07Smrg  -m MODE       $chmodprog installed files to MODE.
1210d590c07Smrg  -o USER       $chownprog installed files to USER.
1220d590c07Smrg  -s            $stripprog installed files.
1230d590c07Smrg  -t DIRECTORY  install into DIRECTORY.
1240d590c07Smrg  -T            report an error if DSTFILE is a directory.
125c76ae52dSmrg
126c76ae52dSmrgEnvironment variables override the default commands:
1270d590c07Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1280d590c07Smrg  RMPROG STRIPPROG
129c76ae52dSmrg"
130c76ae52dSmrg
1310d590c07Smrgwhile test $# -ne 0; do
132c76ae52dSmrg  case $1 in
1330d590c07Smrg    -c) ;;
1340d590c07Smrg
1350d590c07Smrg    -C) copy_on_change=true;;
136c76ae52dSmrg
1370d590c07Smrg    -d) dir_arg=true;;
138c76ae52dSmrg
139c76ae52dSmrg    -g) chgrpcmd="$chgrpprog $2"
1400d590c07Smrg	shift;;
141c76ae52dSmrg
142c76ae52dSmrg    --help) echo "$usage"; exit $?;;
143c76ae52dSmrg
1440d590c07Smrg    -m) mode=$2
1450d590c07Smrg	case $mode in
1460d590c07Smrg	  *' '* | *'	'* | *'
1470d590c07Smrg'*	  | *'*'* | *'?'* | *'['*)
1480d590c07Smrg	    echo "$0: invalid mode: $mode" >&2
1490d590c07Smrg	    exit 1;;
1500d590c07Smrg	esac
1510d590c07Smrg	shift;;
152c76ae52dSmrg
153c76ae52dSmrg    -o) chowncmd="$chownprog $2"
1540d590c07Smrg	shift;;
155c76ae52dSmrg
1560d590c07Smrg    -s) stripcmd=$stripprog;;
157c76ae52dSmrg
1580d590c07Smrg    -t) dst_arg=$2
159dff01e5aSmrg	# Protect names problematic for 'test' and other utilities.
1602836776bSmrg	case $dst_arg in
1612836776bSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
1622836776bSmrg	esac
1630d590c07Smrg	shift;;
164c76ae52dSmrg
1650d590c07Smrg    -T) no_target_directory=true;;
166c76ae52dSmrg
167c76ae52dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
168c76ae52dSmrg
1690d590c07Smrg    --)	shift
170c76ae52dSmrg	break;;
1710d590c07Smrg
1720d590c07Smrg    -*)	echo "$0: invalid option: $1" >&2
1730d590c07Smrg	exit 1;;
1740d590c07Smrg
1750d590c07Smrg    *)  break;;
176c76ae52dSmrg  esac
1770d590c07Smrg  shift
178c76ae52dSmrgdone
179c76ae52dSmrg
1800d590c07Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1810d590c07Smrg  # When -d is used, all remaining arguments are directories to create.
1820d590c07Smrg  # When -t is used, the destination is already specified.
1830d590c07Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1840d590c07Smrg  for arg
1850d590c07Smrg  do
1860d590c07Smrg    if test -n "$dst_arg"; then
1870d590c07Smrg      # $@ is not empty: it contains at least $arg.
1880d590c07Smrg      set fnord "$@" "$dst_arg"
1890d590c07Smrg      shift # fnord
1900d590c07Smrg    fi
1910d590c07Smrg    shift # arg
1920d590c07Smrg    dst_arg=$arg
193dff01e5aSmrg    # Protect names problematic for 'test' and other utilities.
1942836776bSmrg    case $dst_arg in
1952836776bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1962836776bSmrg    esac
1970d590c07Smrg  done
1980d590c07Smrgfi
1990d590c07Smrg
2000d590c07Smrgif test $# -eq 0; then
201c76ae52dSmrg  if test -z "$dir_arg"; then
202c76ae52dSmrg    echo "$0: no input file specified." >&2
203c76ae52dSmrg    exit 1
204c76ae52dSmrg  fi
205dff01e5aSmrg  # It's OK to call 'install-sh -d' without argument.
206c76ae52dSmrg  # This can happen when creating conditional directories.
207c76ae52dSmrg  exit 0
208c76ae52dSmrgfi
209c76ae52dSmrg
2100d590c07Smrgif test -z "$dir_arg"; then
2112836776bSmrg  do_exit='(exit $ret); exit $ret'
2122836776bSmrg  trap "ret=129; $do_exit" 1
2132836776bSmrg  trap "ret=130; $do_exit" 2
2142836776bSmrg  trap "ret=141; $do_exit" 13
2152836776bSmrg  trap "ret=143; $do_exit" 15
2160d590c07Smrg
2170d590c07Smrg  # Set umask so as not to create temps with too-generous modes.
2180d590c07Smrg  # However, 'strip' requires both read and write access to temps.
2190d590c07Smrg  case $mode in
2200d590c07Smrg    # Optimize common cases.
2210d590c07Smrg    *644) cp_umask=133;;
2220d590c07Smrg    *755) cp_umask=22;;
2230d590c07Smrg
2240d590c07Smrg    *[0-7])
2250d590c07Smrg      if test -z "$stripcmd"; then
2260d590c07Smrg	u_plus_rw=
2270d590c07Smrg      else
2280d590c07Smrg	u_plus_rw='% 200'
2290d590c07Smrg      fi
2300d590c07Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2310d590c07Smrg    *)
2320d590c07Smrg      if test -z "$stripcmd"; then
2330d590c07Smrg	u_plus_rw=
2340d590c07Smrg      else
2350d590c07Smrg	u_plus_rw=,u+rw
2360d590c07Smrg      fi
2370d590c07Smrg      cp_umask=$mode$u_plus_rw;;
2380d590c07Smrg  esac
2390d590c07Smrgfi
2400d590c07Smrg
241c76ae52dSmrgfor src
242c76ae52dSmrgdo
243dff01e5aSmrg  # Protect names problematic for 'test' and other utilities.
244c76ae52dSmrg  case $src in
2452836776bSmrg    -* | [=\(\)!]) src=./$src;;
246c76ae52dSmrg  esac
247c76ae52dSmrg
248c76ae52dSmrg  if test -n "$dir_arg"; then
249c76ae52dSmrg    dst=$src
2500d590c07Smrg    dstdir=$dst
2510d590c07Smrg    test -d "$dstdir"
2520d590c07Smrg    dstdir_status=$?
253c76ae52dSmrg  else
2540d590c07Smrg
255c76ae52dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256c76ae52dSmrg    # might cause directories to be created, which would be especially bad
257c76ae52dSmrg    # if $src (and thus $dsttmp) contains '*'.
258c76ae52dSmrg    if test ! -f "$src" && test ! -d "$src"; then
259c76ae52dSmrg      echo "$0: $src does not exist." >&2
260c76ae52dSmrg      exit 1
261c76ae52dSmrg    fi
262c76ae52dSmrg
2630d590c07Smrg    if test -z "$dst_arg"; then
264c76ae52dSmrg      echo "$0: no destination specified." >&2
265c76ae52dSmrg      exit 1
266c76ae52dSmrg    fi
2670d590c07Smrg    dst=$dst_arg
268c76ae52dSmrg
269c76ae52dSmrg    # If destination is a directory, append the input filename; won't work
270c76ae52dSmrg    # if double slashes aren't ignored.
271c76ae52dSmrg    if test -d "$dst"; then
272c76ae52dSmrg      if test -n "$no_target_directory"; then
2730d590c07Smrg	echo "$0: $dst_arg: Is a directory" >&2
274c76ae52dSmrg	exit 1
275c76ae52dSmrg      fi
2760d590c07Smrg      dstdir=$dst
2770d590c07Smrg      dst=$dstdir/`basename "$src"`
2780d590c07Smrg      dstdir_status=0
2790d590c07Smrg    else
2800d590c07Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2810d590c07Smrg      dstdir=`
2820d590c07Smrg	(dirname "$dst") 2>/dev/null ||
2830d590c07Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2840d590c07Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2850d590c07Smrg	     X"$dst" : 'X\(//\)$' \| \
2860d590c07Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2870d590c07Smrg	echo X"$dst" |
2880d590c07Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2890d590c07Smrg		   s//\1/
2900d590c07Smrg		   q
2910d590c07Smrg		 }
2920d590c07Smrg		 /^X\(\/\/\)[^/].*/{
2930d590c07Smrg		   s//\1/
2940d590c07Smrg		   q
2950d590c07Smrg		 }
2960d590c07Smrg		 /^X\(\/\/\)$/{
2970d590c07Smrg		   s//\1/
2980d590c07Smrg		   q
2990d590c07Smrg		 }
3000d590c07Smrg		 /^X\(\/\).*/{
3010d590c07Smrg		   s//\1/
3020d590c07Smrg		   q
3030d590c07Smrg		 }
3040d590c07Smrg		 s/.*/./; q'
3050d590c07Smrg      `
3060d590c07Smrg
3070d590c07Smrg      test -d "$dstdir"
3080d590c07Smrg      dstdir_status=$?
309c76ae52dSmrg    fi
310c76ae52dSmrg  fi
311c76ae52dSmrg
3120d590c07Smrg  obsolete_mkdir_used=false
3130d590c07Smrg
3140d590c07Smrg  if test $dstdir_status != 0; then
3150d590c07Smrg    case $posix_mkdir in
3160d590c07Smrg      '')
3170d590c07Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3180d590c07Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3190d590c07Smrg	umask=`umask`
3200d590c07Smrg	case $stripcmd.$umask in
3210d590c07Smrg	  # Optimize common cases.
3220d590c07Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3230d590c07Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3240d590c07Smrg
3250d590c07Smrg	  *[0-7])
3260d590c07Smrg	    mkdir_umask=`expr $umask + 22 \
3270d590c07Smrg	      - $umask % 100 % 40 + $umask % 20 \
3280d590c07Smrg	      - $umask % 10 % 4 + $umask % 2
3290d590c07Smrg	    `;;
3300d590c07Smrg	  *) mkdir_umask=$umask,go-w;;
3310d590c07Smrg	esac
3320d590c07Smrg
3330d590c07Smrg	# With -d, create the new directory with the user-specified mode.
3340d590c07Smrg	# Otherwise, rely on $mkdir_umask.
3350d590c07Smrg	if test -n "$dir_arg"; then
3360d590c07Smrg	  mkdir_mode=-m$mode
3370d590c07Smrg	else
3380d590c07Smrg	  mkdir_mode=
3390d590c07Smrg	fi
3400d590c07Smrg
3410d590c07Smrg	posix_mkdir=false
3420d590c07Smrg	case $umask in
3430d590c07Smrg	  *[123567][0-7][0-7])
3440d590c07Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3450d590c07Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3460d590c07Smrg	    ;;
3470d590c07Smrg	  *)
3480d590c07Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3490d590c07Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3500d590c07Smrg
3510d590c07Smrg	    if (umask $mkdir_umask &&
3520d590c07Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3530d590c07Smrg	    then
3540d590c07Smrg	      if test -z "$dir_arg" || {
3550d590c07Smrg		   # Check for POSIX incompatibilities with -m.
3560d590c07Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357dff01e5aSmrg		   # other-writable bit of parent directory when it shouldn't.
3580d590c07Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3590d590c07Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3600d590c07Smrg		   case $ls_ld_tmpdir in
3610d590c07Smrg		     d????-?r-*) different_mode=700;;
3620d590c07Smrg		     d????-?--*) different_mode=755;;
3630d590c07Smrg		     *) false;;
3640d590c07Smrg		   esac &&
3650d590c07Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3660d590c07Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3670d590c07Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3680d590c07Smrg		   }
3690d590c07Smrg		 }
3700d590c07Smrg	      then posix_mkdir=:
3710d590c07Smrg	      fi
3720d590c07Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3730d590c07Smrg	    else
3740d590c07Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3750d590c07Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3760d590c07Smrg	    fi
3770d590c07Smrg	    trap '' 0;;
3780d590c07Smrg	esac;;
3790d590c07Smrg    esac
380c76ae52dSmrg
3810d590c07Smrg    if
3820d590c07Smrg      $posix_mkdir && (
3830d590c07Smrg	umask $mkdir_umask &&
3840d590c07Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3850d590c07Smrg      )
3860d590c07Smrg    then :
3870d590c07Smrg    else
388c76ae52dSmrg
3890d590c07Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3900d590c07Smrg      # or it failed possibly due to a race condition.  Create the
3910d590c07Smrg      # directory the slow way, step by step, checking for races as we go.
392c76ae52dSmrg
3930d590c07Smrg      case $dstdir in
3940d590c07Smrg	/*) prefix='/';;
3952836776bSmrg	[-=\(\)!]*) prefix='./';;
3960d590c07Smrg	*)  prefix='';;
3970d590c07Smrg      esac
398c76ae52dSmrg
3990d590c07Smrg      eval "$initialize_posix_glob"
400c76ae52dSmrg
4010d590c07Smrg      oIFS=$IFS
4020d590c07Smrg      IFS=/
4030d590c07Smrg      $posix_glob set -f
4040d590c07Smrg      set fnord $dstdir
405c76ae52dSmrg      shift
4060d590c07Smrg      $posix_glob set +f
4070d590c07Smrg      IFS=$oIFS
4080d590c07Smrg
4090d590c07Smrg      prefixes=
4100d590c07Smrg
4110d590c07Smrg      for d
4120d590c07Smrg      do
4132836776bSmrg	test X"$d" = X && continue
4140d590c07Smrg
4150d590c07Smrg	prefix=$prefix$d
4160d590c07Smrg	if test -d "$prefix"; then
4170d590c07Smrg	  prefixes=
4180d590c07Smrg	else
4190d590c07Smrg	  if $posix_mkdir; then
4200d590c07Smrg	    (umask=$mkdir_umask &&
4210d590c07Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4220d590c07Smrg	    # Don't fail if two instances are running concurrently.
4230d590c07Smrg	    test -d "$prefix" || exit 1
4240d590c07Smrg	  else
4250d590c07Smrg	    case $prefix in
4260d590c07Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4270d590c07Smrg	      *) qprefix=$prefix;;
4280d590c07Smrg	    esac
4290d590c07Smrg	    prefixes="$prefixes '$qprefix'"
4300d590c07Smrg	  fi
4310d590c07Smrg	fi
4320d590c07Smrg	prefix=$prefix/
4330d590c07Smrg      done
4340d590c07Smrg
4350d590c07Smrg      if test -n "$prefixes"; then
4360d590c07Smrg	# Don't fail if two instances are running concurrently.
4370d590c07Smrg	(umask $mkdir_umask &&
4380d590c07Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4390d590c07Smrg	  test -d "$dstdir" || exit 1
4400d590c07Smrg	obsolete_mkdir_used=true
441c76ae52dSmrg      fi
4420d590c07Smrg    fi
443c76ae52dSmrg  fi
444c76ae52dSmrg
445c76ae52dSmrg  if test -n "$dir_arg"; then
4460d590c07Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4470d590c07Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4480d590c07Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4490d590c07Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450c76ae52dSmrg  else
451c76ae52dSmrg
452c76ae52dSmrg    # Make a couple of temp file names in the proper directory.
453c76ae52dSmrg    dsttmp=$dstdir/_inst.$$_
454c76ae52dSmrg    rmtmp=$dstdir/_rm.$$_
455c76ae52dSmrg
456c76ae52dSmrg    # Trap to clean up those temp files at exit.
457c76ae52dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458c76ae52dSmrg
459c76ae52dSmrg    # Copy the file name to the temp name.
4600d590c07Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461c76ae52dSmrg
462c76ae52dSmrg    # and set any options; do chmod last to preserve setuid bits.
463c76ae52dSmrg    #
464c76ae52dSmrg    # If any of these fail, we abort the whole thing.  If we want to
465c76ae52dSmrg    # ignore errors from any of these, just make sure not to ignore
466c76ae52dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467c76ae52dSmrg    #
4680d590c07Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4690d590c07Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4700d590c07Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4710d590c07Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4720d590c07Smrg
4730d590c07Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4740d590c07Smrg    if $copy_on_change &&
4750d590c07Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4760d590c07Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4770d590c07Smrg
4780d590c07Smrg       eval "$initialize_posix_glob" &&
4790d590c07Smrg       $posix_glob set -f &&
4800d590c07Smrg       set X $old && old=:$2:$4:$5:$6 &&
4810d590c07Smrg       set X $new && new=:$2:$4:$5:$6 &&
4820d590c07Smrg       $posix_glob set +f &&
4830d590c07Smrg
4840d590c07Smrg       test "$old" = "$new" &&
4850d590c07Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4860d590c07Smrg    then
4870d590c07Smrg      rm -f "$dsttmp"
4880d590c07Smrg    else
4890d590c07Smrg      # Rename the file to the real destination.
4900d590c07Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4910d590c07Smrg
4920d590c07Smrg      # The rename failed, perhaps because mv can't rename something else
4930d590c07Smrg      # to itself, or perhaps because mv is so ancient that it does not
4940d590c07Smrg      # support -f.
4950d590c07Smrg      {
4960d590c07Smrg	# Now remove or move aside any old file at destination location.
4970d590c07Smrg	# We try this two ways since rm can't unlink itself on some
4980d590c07Smrg	# systems and the destination file might be busy for other
4990d590c07Smrg	# reasons.  In this case, the final cleanup might fail but the new
5000d590c07Smrg	# file should still install successfully.
5010d590c07Smrg	{
5020d590c07Smrg	  test ! -f "$dst" ||
5030d590c07Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5040d590c07Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5050d590c07Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5060d590c07Smrg	  } ||
5070d590c07Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5080d590c07Smrg	    (exit 1); exit 1
5090d590c07Smrg	  }
5100d590c07Smrg	} &&
5110d590c07Smrg
5120d590c07Smrg	# Now rename the file to the real destination.
5130d590c07Smrg	$doit $mvcmd "$dsttmp" "$dst"
5140d590c07Smrg      }
5150d590c07Smrg    fi || exit 1
5160d590c07Smrg
5170d590c07Smrg    trap '' 0
5180d590c07Smrg  fi
519c76ae52dSmrgdone
520c76ae52dSmrg
521c76ae52dSmrg# Local variables:
522c76ae52dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523c76ae52dSmrg# time-stamp-start: "scriptversion="
524c76ae52dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5250d590c07Smrg# time-stamp-time-zone: "UTC"
5260d590c07Smrg# time-stamp-end: "; # UTC"
527c76ae52dSmrg# End:
528