install-sh revision 2d62a77c
1a99f82b8Smrg#!/bin/sh
2a99f82b8Smrg# install - install a program, script, or datafile
32d62a77cSmrg
42d62a77cSmrgscriptversion=2009-04-28.21; # UTC
52d62a77cSmrg
6a99f82b8Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a99f82b8Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8a99f82b8Smrg# following copyright and license.
9a99f82b8Smrg#
10a99f82b8Smrg# Copyright (C) 1994 X Consortium
11a99f82b8Smrg#
12a99f82b8Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a99f82b8Smrg# of this software and associated documentation files (the "Software"), to
14a99f82b8Smrg# deal in the Software without restriction, including without limitation the
15a99f82b8Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a99f82b8Smrg# sell copies of the Software, and to permit persons to whom the Software is
17a99f82b8Smrg# furnished to do so, subject to the following conditions:
18a99f82b8Smrg#
19a99f82b8Smrg# The above copyright notice and this permission notice shall be included in
20a99f82b8Smrg# all copies or substantial portions of the Software.
21a99f82b8Smrg#
22a99f82b8Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a99f82b8Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a99f82b8Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a99f82b8Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a99f82b8Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a99f82b8Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a99f82b8Smrg#
29a99f82b8Smrg# Except as contained in this notice, the name of the X Consortium shall not
30a99f82b8Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a99f82b8Smrg# ings in this Software without prior written authorization from the X Consor-
32a99f82b8Smrg# tium.
33a99f82b8Smrg#
34a99f82b8Smrg#
35a99f82b8Smrg# FSF changes to this file are in the public domain.
36a99f82b8Smrg#
37a99f82b8Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38a99f82b8Smrg# `make' implicit rules from creating a file called install from it
39a99f82b8Smrg# when there is no Makefile.
40a99f82b8Smrg#
41a99f82b8Smrg# This script is compatible with the BSD install script, but was written
422d62a77cSmrg# from scratch.
43a99f82b8Smrg
442d62a77cSmrgnl='
452d62a77cSmrg'
462d62a77cSmrgIFS=" ""	$nl"
47a99f82b8Smrg
48a99f82b8Smrg# set DOITPROG to echo to test this script
49a99f82b8Smrg
50a99f82b8Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
512d62a77cSmrgdoit=${DOITPROG-}
522d62a77cSmrgif test -z "$doit"; then
532d62a77cSmrg  doit_exec=exec
54a99f82b8Smrgelse
552d62a77cSmrg  doit_exec=$doit
56a99f82b8Smrgfi
57a99f82b8Smrg
582d62a77cSmrg# Put in absolute file names if you don't have them in your path;
592d62a77cSmrg# or use environment vars.
602d62a77cSmrg
612d62a77cSmrgchgrpprog=${CHGRPPROG-chgrp}
622d62a77cSmrgchmodprog=${CHMODPROG-chmod}
632d62a77cSmrgchownprog=${CHOWNPROG-chown}
642d62a77cSmrgcmpprog=${CMPPROG-cmp}
652d62a77cSmrgcpprog=${CPPROG-cp}
662d62a77cSmrgmkdirprog=${MKDIRPROG-mkdir}
672d62a77cSmrgmvprog=${MVPROG-mv}
682d62a77cSmrgrmprog=${RMPROG-rm}
692d62a77cSmrgstripprog=${STRIPPROG-strip}
702d62a77cSmrg
712d62a77cSmrgposix_glob='?'
722d62a77cSmrginitialize_posix_glob='
732d62a77cSmrg  test "$posix_glob" != "?" || {
742d62a77cSmrg    if (set -f) 2>/dev/null; then
752d62a77cSmrg      posix_glob=
762d62a77cSmrg    else
772d62a77cSmrg      posix_glob=:
782d62a77cSmrg    fi
792d62a77cSmrg  }
802d62a77cSmrg'
812d62a77cSmrg
822d62a77cSmrgposix_mkdir=
832d62a77cSmrg
842d62a77cSmrg# Desired mode of installed file.
852d62a77cSmrgmode=0755
862d62a77cSmrg
872d62a77cSmrgchgrpcmd=
882d62a77cSmrgchmodcmd=$chmodprog
892d62a77cSmrgchowncmd=
902d62a77cSmrgmvcmd=$mvprog
912d62a77cSmrgrmcmd="$rmprog -f"
922d62a77cSmrgstripcmd=
93a99f82b8Smrg
942d62a77cSmrgsrc=
952d62a77cSmrgdst=
962d62a77cSmrgdir_arg=
972d62a77cSmrgdst_arg=
98a99f82b8Smrg
992d62a77cSmrgcopy_on_change=false
1002d62a77cSmrgno_target_directory=
101a99f82b8Smrg
1022d62a77cSmrgusage="\
1032d62a77cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1042d62a77cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1052d62a77cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1062d62a77cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107a99f82b8Smrg
1082d62a77cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
1092d62a77cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1102d62a77cSmrgIn the 4th, create DIRECTORIES.
111a99f82b8Smrg
1122d62a77cSmrgOptions:
1132d62a77cSmrg     --help     display this help and exit.
1142d62a77cSmrg     --version  display version info and exit.
115a99f82b8Smrg
1162d62a77cSmrg  -c            (ignored)
1172d62a77cSmrg  -C            install only if different (preserve the last data modification time)
1182d62a77cSmrg  -d            create directories instead of installing files.
1192d62a77cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1202d62a77cSmrg  -m MODE       $chmodprog installed files to MODE.
1212d62a77cSmrg  -o USER       $chownprog installed files to USER.
1222d62a77cSmrg  -s            $stripprog installed files.
1232d62a77cSmrg  -t DIRECTORY  install into DIRECTORY.
1242d62a77cSmrg  -T            report an error if DSTFILE is a directory.
125a99f82b8Smrg
1262d62a77cSmrgEnvironment variables override the default commands:
1272d62a77cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1282d62a77cSmrg  RMPROG STRIPPROG
1292d62a77cSmrg"
130a99f82b8Smrg
1312d62a77cSmrgwhile test $# -ne 0; do
1322d62a77cSmrg  case $1 in
1332d62a77cSmrg    -c) ;;
134a99f82b8Smrg
1352d62a77cSmrg    -C) copy_on_change=true;;
136a99f82b8Smrg
1372d62a77cSmrg    -d) dir_arg=true;;
138a99f82b8Smrg
1392d62a77cSmrg    -g) chgrpcmd="$chgrpprog $2"
1402d62a77cSmrg	shift;;
141a99f82b8Smrg
1422d62a77cSmrg    --help) echo "$usage"; exit $?;;
143a99f82b8Smrg
1442d62a77cSmrg    -m) mode=$2
1452d62a77cSmrg	case $mode in
1462d62a77cSmrg	  *' '* | *'	'* | *'
1472d62a77cSmrg'*	  | *'*'* | *'?'* | *'['*)
1482d62a77cSmrg	    echo "$0: invalid mode: $mode" >&2
1492d62a77cSmrg	    exit 1;;
1502d62a77cSmrg	esac
1512d62a77cSmrg	shift;;
152a99f82b8Smrg
1532d62a77cSmrg    -o) chowncmd="$chownprog $2"
1542d62a77cSmrg	shift;;
155a99f82b8Smrg
1562d62a77cSmrg    -s) stripcmd=$stripprog;;
157a99f82b8Smrg
1582d62a77cSmrg    -t) dst_arg=$2
1592d62a77cSmrg	shift;;
160a99f82b8Smrg
1612d62a77cSmrg    -T) no_target_directory=true;;
162a99f82b8Smrg
1632d62a77cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164a99f82b8Smrg
1652d62a77cSmrg    --)	shift
1662d62a77cSmrg	break;;
167a99f82b8Smrg
1682d62a77cSmrg    -*)	echo "$0: invalid option: $1" >&2
1692d62a77cSmrg	exit 1;;
170a99f82b8Smrg
1712d62a77cSmrg    *)  break;;
1722d62a77cSmrg  esac
1732d62a77cSmrg  shift
1742d62a77cSmrgdone
175a99f82b8Smrg
1762d62a77cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1772d62a77cSmrg  # When -d is used, all remaining arguments are directories to create.
1782d62a77cSmrg  # When -t is used, the destination is already specified.
1792d62a77cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1802d62a77cSmrg  for arg
1812d62a77cSmrg  do
1822d62a77cSmrg    if test -n "$dst_arg"; then
1832d62a77cSmrg      # $@ is not empty: it contains at least $arg.
1842d62a77cSmrg      set fnord "$@" "$dst_arg"
1852d62a77cSmrg      shift # fnord
1862d62a77cSmrg    fi
1872d62a77cSmrg    shift # arg
1882d62a77cSmrg    dst_arg=$arg
1892d62a77cSmrg  done
1902d62a77cSmrgfi
191a99f82b8Smrg
1922d62a77cSmrgif test $# -eq 0; then
1932d62a77cSmrg  if test -z "$dir_arg"; then
1942d62a77cSmrg    echo "$0: no input file specified." >&2
1952d62a77cSmrg    exit 1
1962d62a77cSmrg  fi
1972d62a77cSmrg  # It's OK to call `install-sh -d' without argument.
1982d62a77cSmrg  # This can happen when creating conditional directories.
1992d62a77cSmrg  exit 0
2002d62a77cSmrgfi
201a99f82b8Smrg
2022d62a77cSmrgif test -z "$dir_arg"; then
2032d62a77cSmrg  trap '(exit $?); exit' 1 2 13 15
2042d62a77cSmrg
2052d62a77cSmrg  # Set umask so as not to create temps with too-generous modes.
2062d62a77cSmrg  # However, 'strip' requires both read and write access to temps.
2072d62a77cSmrg  case $mode in
2082d62a77cSmrg    # Optimize common cases.
2092d62a77cSmrg    *644) cp_umask=133;;
2102d62a77cSmrg    *755) cp_umask=22;;
2112d62a77cSmrg
2122d62a77cSmrg    *[0-7])
2132d62a77cSmrg      if test -z "$stripcmd"; then
2142d62a77cSmrg	u_plus_rw=
2152d62a77cSmrg      else
2162d62a77cSmrg	u_plus_rw='% 200'
2172d62a77cSmrg      fi
2182d62a77cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2192d62a77cSmrg    *)
2202d62a77cSmrg      if test -z "$stripcmd"; then
2212d62a77cSmrg	u_plus_rw=
2222d62a77cSmrg      else
2232d62a77cSmrg	u_plus_rw=,u+rw
2242d62a77cSmrg      fi
2252d62a77cSmrg      cp_umask=$mode$u_plus_rw;;
2262d62a77cSmrg  esac
2272d62a77cSmrgfi
228a99f82b8Smrg
2292d62a77cSmrgfor src
2302d62a77cSmrgdo
2312d62a77cSmrg  # Protect names starting with `-'.
2322d62a77cSmrg  case $src in
2332d62a77cSmrg    -*) src=./$src;;
2342d62a77cSmrg  esac
2352d62a77cSmrg
2362d62a77cSmrg  if test -n "$dir_arg"; then
2372d62a77cSmrg    dst=$src
2382d62a77cSmrg    dstdir=$dst
2392d62a77cSmrg    test -d "$dstdir"
2402d62a77cSmrg    dstdir_status=$?
2412d62a77cSmrg  else
2422d62a77cSmrg
2432d62a77cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2442d62a77cSmrg    # might cause directories to be created, which would be especially bad
2452d62a77cSmrg    # if $src (and thus $dsttmp) contains '*'.
2462d62a77cSmrg    if test ! -f "$src" && test ! -d "$src"; then
2472d62a77cSmrg      echo "$0: $src does not exist." >&2
2482d62a77cSmrg      exit 1
2492d62a77cSmrg    fi
2502d62a77cSmrg
2512d62a77cSmrg    if test -z "$dst_arg"; then
2522d62a77cSmrg      echo "$0: no destination specified." >&2
2532d62a77cSmrg      exit 1
2542d62a77cSmrg    fi
2552d62a77cSmrg
2562d62a77cSmrg    dst=$dst_arg
2572d62a77cSmrg    # Protect names starting with `-'.
2582d62a77cSmrg    case $dst in
2592d62a77cSmrg      -*) dst=./$dst;;
2602d62a77cSmrg    esac
261a99f82b8Smrg
2622d62a77cSmrg    # If destination is a directory, append the input filename; won't work
2632d62a77cSmrg    # if double slashes aren't ignored.
2642d62a77cSmrg    if test -d "$dst"; then
2652d62a77cSmrg      if test -n "$no_target_directory"; then
2662d62a77cSmrg	echo "$0: $dst_arg: Is a directory" >&2
2672d62a77cSmrg	exit 1
2682d62a77cSmrg      fi
2692d62a77cSmrg      dstdir=$dst
2702d62a77cSmrg      dst=$dstdir/`basename "$src"`
2712d62a77cSmrg      dstdir_status=0
2722d62a77cSmrg    else
2732d62a77cSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2742d62a77cSmrg      dstdir=`
2752d62a77cSmrg	(dirname "$dst") 2>/dev/null ||
2762d62a77cSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2772d62a77cSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
2782d62a77cSmrg	     X"$dst" : 'X\(//\)$' \| \
2792d62a77cSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2802d62a77cSmrg	echo X"$dst" |
2812d62a77cSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2822d62a77cSmrg		   s//\1/
2832d62a77cSmrg		   q
2842d62a77cSmrg		 }
2852d62a77cSmrg		 /^X\(\/\/\)[^/].*/{
2862d62a77cSmrg		   s//\1/
2872d62a77cSmrg		   q
2882d62a77cSmrg		 }
2892d62a77cSmrg		 /^X\(\/\/\)$/{
2902d62a77cSmrg		   s//\1/
2912d62a77cSmrg		   q
2922d62a77cSmrg		 }
2932d62a77cSmrg		 /^X\(\/\).*/{
2942d62a77cSmrg		   s//\1/
2952d62a77cSmrg		   q
2962d62a77cSmrg		 }
2972d62a77cSmrg		 s/.*/./; q'
2982d62a77cSmrg      `
2992d62a77cSmrg
3002d62a77cSmrg      test -d "$dstdir"
3012d62a77cSmrg      dstdir_status=$?
3022d62a77cSmrg    fi
3032d62a77cSmrg  fi
3042d62a77cSmrg
3052d62a77cSmrg  obsolete_mkdir_used=false
3062d62a77cSmrg
3072d62a77cSmrg  if test $dstdir_status != 0; then
3082d62a77cSmrg    case $posix_mkdir in
3092d62a77cSmrg      '')
3102d62a77cSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
3112d62a77cSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
3122d62a77cSmrg	umask=`umask`
3132d62a77cSmrg	case $stripcmd.$umask in
3142d62a77cSmrg	  # Optimize common cases.
3152d62a77cSmrg	  *[2367][2367]) mkdir_umask=$umask;;
3162d62a77cSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3172d62a77cSmrg
3182d62a77cSmrg	  *[0-7])
3192d62a77cSmrg	    mkdir_umask=`expr $umask + 22 \
3202d62a77cSmrg	      - $umask % 100 % 40 + $umask % 20 \
3212d62a77cSmrg	      - $umask % 10 % 4 + $umask % 2
3222d62a77cSmrg	    `;;
3232d62a77cSmrg	  *) mkdir_umask=$umask,go-w;;
3242d62a77cSmrg	esac
3252d62a77cSmrg
3262d62a77cSmrg	# With -d, create the new directory with the user-specified mode.
3272d62a77cSmrg	# Otherwise, rely on $mkdir_umask.
3282d62a77cSmrg	if test -n "$dir_arg"; then
3292d62a77cSmrg	  mkdir_mode=-m$mode
330a99f82b8Smrg	else
3312d62a77cSmrg	  mkdir_mode=
332a99f82b8Smrg	fi
333a99f82b8Smrg
3342d62a77cSmrg	posix_mkdir=false
3352d62a77cSmrg	case $umask in
3362d62a77cSmrg	  *[123567][0-7][0-7])
3372d62a77cSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3382d62a77cSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3392d62a77cSmrg	    ;;
3402d62a77cSmrg	  *)
3412d62a77cSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3422d62a77cSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3432d62a77cSmrg
3442d62a77cSmrg	    if (umask $mkdir_umask &&
3452d62a77cSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3462d62a77cSmrg	    then
3472d62a77cSmrg	      if test -z "$dir_arg" || {
3482d62a77cSmrg		   # Check for POSIX incompatibilities with -m.
3492d62a77cSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3502d62a77cSmrg		   # other-writeable bit of parent directory when it shouldn't.
3512d62a77cSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3522d62a77cSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3532d62a77cSmrg		   case $ls_ld_tmpdir in
3542d62a77cSmrg		     d????-?r-*) different_mode=700;;
3552d62a77cSmrg		     d????-?--*) different_mode=755;;
3562d62a77cSmrg		     *) false;;
3572d62a77cSmrg		   esac &&
3582d62a77cSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3592d62a77cSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3602d62a77cSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3612d62a77cSmrg		   }
3622d62a77cSmrg		 }
3632d62a77cSmrg	      then posix_mkdir=:
3642d62a77cSmrg	      fi
3652d62a77cSmrg	      rmdir "$tmpdir/d" "$tmpdir"
3662d62a77cSmrg	    else
3672d62a77cSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
3682d62a77cSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3692d62a77cSmrg	    fi
3702d62a77cSmrg	    trap '' 0;;
3712d62a77cSmrg	esac;;
3722d62a77cSmrg    esac
373a99f82b8Smrg
3742d62a77cSmrg    if
3752d62a77cSmrg      $posix_mkdir && (
3762d62a77cSmrg	umask $mkdir_umask &&
3772d62a77cSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3782d62a77cSmrg      )
3792d62a77cSmrg    then :
3802d62a77cSmrg    else
3812d62a77cSmrg
3822d62a77cSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3832d62a77cSmrg      # or it failed possibly due to a race condition.  Create the
3842d62a77cSmrg      # directory the slow way, step by step, checking for races as we go.
3852d62a77cSmrg
3862d62a77cSmrg      case $dstdir in
3872d62a77cSmrg	/*) prefix='/';;
3882d62a77cSmrg	-*) prefix='./';;
3892d62a77cSmrg	*)  prefix='';;
3902d62a77cSmrg      esac
3912d62a77cSmrg
3922d62a77cSmrg      eval "$initialize_posix_glob"
3932d62a77cSmrg
3942d62a77cSmrg      oIFS=$IFS
3952d62a77cSmrg      IFS=/
3962d62a77cSmrg      $posix_glob set -f
3972d62a77cSmrg      set fnord $dstdir
3982d62a77cSmrg      shift
3992d62a77cSmrg      $posix_glob set +f
4002d62a77cSmrg      IFS=$oIFS
4012d62a77cSmrg
4022d62a77cSmrg      prefixes=
4032d62a77cSmrg
4042d62a77cSmrg      for d
4052d62a77cSmrg      do
4062d62a77cSmrg	test -z "$d" && continue
4072d62a77cSmrg
4082d62a77cSmrg	prefix=$prefix$d
4092d62a77cSmrg	if test -d "$prefix"; then
4102d62a77cSmrg	  prefixes=
4112d62a77cSmrg	else
4122d62a77cSmrg	  if $posix_mkdir; then
4132d62a77cSmrg	    (umask=$mkdir_umask &&
4142d62a77cSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4152d62a77cSmrg	    # Don't fail if two instances are running concurrently.
4162d62a77cSmrg	    test -d "$prefix" || exit 1
4172d62a77cSmrg	  else
4182d62a77cSmrg	    case $prefix in
4192d62a77cSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4202d62a77cSmrg	      *) qprefix=$prefix;;
4212d62a77cSmrg	    esac
4222d62a77cSmrg	    prefixes="$prefixes '$qprefix'"
4232d62a77cSmrg	  fi
4242d62a77cSmrg	fi
4252d62a77cSmrg	prefix=$prefix/
4262d62a77cSmrg      done
4272d62a77cSmrg
4282d62a77cSmrg      if test -n "$prefixes"; then
4292d62a77cSmrg	# Don't fail if two instances are running concurrently.
4302d62a77cSmrg	(umask $mkdir_umask &&
4312d62a77cSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4322d62a77cSmrg	  test -d "$dstdir" || exit 1
4332d62a77cSmrg	obsolete_mkdir_used=true
4342d62a77cSmrg      fi
4352d62a77cSmrg    fi
4362d62a77cSmrg  fi
4372d62a77cSmrg
4382d62a77cSmrg  if test -n "$dir_arg"; then
4392d62a77cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4402d62a77cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4412d62a77cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4422d62a77cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4432d62a77cSmrg  else
4442d62a77cSmrg
4452d62a77cSmrg    # Make a couple of temp file names in the proper directory.
4462d62a77cSmrg    dsttmp=$dstdir/_inst.$$_
4472d62a77cSmrg    rmtmp=$dstdir/_rm.$$_
4482d62a77cSmrg
4492d62a77cSmrg    # Trap to clean up those temp files at exit.
4502d62a77cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4512d62a77cSmrg
4522d62a77cSmrg    # Copy the file name to the temp name.
4532d62a77cSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4542d62a77cSmrg
4552d62a77cSmrg    # and set any options; do chmod last to preserve setuid bits.
4562d62a77cSmrg    #
4572d62a77cSmrg    # If any of these fail, we abort the whole thing.  If we want to
4582d62a77cSmrg    # ignore errors from any of these, just make sure not to ignore
4592d62a77cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4602d62a77cSmrg    #
4612d62a77cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4622d62a77cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4632d62a77cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4642d62a77cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4652d62a77cSmrg
4662d62a77cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4672d62a77cSmrg    if $copy_on_change &&
4682d62a77cSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4692d62a77cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4702d62a77cSmrg
4712d62a77cSmrg       eval "$initialize_posix_glob" &&
4722d62a77cSmrg       $posix_glob set -f &&
4732d62a77cSmrg       set X $old && old=:$2:$4:$5:$6 &&
4742d62a77cSmrg       set X $new && new=:$2:$4:$5:$6 &&
4752d62a77cSmrg       $posix_glob set +f &&
4762d62a77cSmrg
4772d62a77cSmrg       test "$old" = "$new" &&
4782d62a77cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4792d62a77cSmrg    then
4802d62a77cSmrg      rm -f "$dsttmp"
4812d62a77cSmrg    else
4822d62a77cSmrg      # Rename the file to the real destination.
4832d62a77cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4842d62a77cSmrg
4852d62a77cSmrg      # The rename failed, perhaps because mv can't rename something else
4862d62a77cSmrg      # to itself, or perhaps because mv is so ancient that it does not
4872d62a77cSmrg      # support -f.
4882d62a77cSmrg      {
4892d62a77cSmrg	# Now remove or move aside any old file at destination location.
4902d62a77cSmrg	# We try this two ways since rm can't unlink itself on some
4912d62a77cSmrg	# systems and the destination file might be busy for other
4922d62a77cSmrg	# reasons.  In this case, the final cleanup might fail but the new
4932d62a77cSmrg	# file should still install successfully.
4942d62a77cSmrg	{
4952d62a77cSmrg	  test ! -f "$dst" ||
4962d62a77cSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4972d62a77cSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4982d62a77cSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4992d62a77cSmrg	  } ||
5002d62a77cSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
5012d62a77cSmrg	    (exit 1); exit 1
5022d62a77cSmrg	  }
5032d62a77cSmrg	} &&
5042d62a77cSmrg
5052d62a77cSmrg	# Now rename the file to the real destination.
5062d62a77cSmrg	$doit $mvcmd "$dsttmp" "$dst"
5072d62a77cSmrg      }
5082d62a77cSmrg    fi || exit 1
5092d62a77cSmrg
5102d62a77cSmrg    trap '' 0
5112d62a77cSmrg  fi
5122d62a77cSmrgdone
513a99f82b8Smrg
5142d62a77cSmrg# Local variables:
5152d62a77cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5162d62a77cSmrg# time-stamp-start: "scriptversion="
5172d62a77cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5182d62a77cSmrg# time-stamp-time-zone: "UTC"
5192d62a77cSmrg# time-stamp-end: "; # UTC"
5202d62a77cSmrg# End:
521