install-sh revision b7fb5eac
11a30de1fSmrg#!/bin/sh
21a30de1fSmrg# install - install a program, script, or datafile
31a30de1fSmrg
4b7fb5eacSmrgscriptversion=2009-04-28.21; # UTC
51a30de1fSmrg
61a30de1fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
71a30de1fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
81a30de1fSmrg# following copyright and license.
91a30de1fSmrg#
101a30de1fSmrg# Copyright (C) 1994 X Consortium
111a30de1fSmrg#
121a30de1fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
131a30de1fSmrg# of this software and associated documentation files (the "Software"), to
141a30de1fSmrg# deal in the Software without restriction, including without limitation the
151a30de1fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
161a30de1fSmrg# sell copies of the Software, and to permit persons to whom the Software is
171a30de1fSmrg# furnished to do so, subject to the following conditions:
181a30de1fSmrg#
191a30de1fSmrg# The above copyright notice and this permission notice shall be included in
201a30de1fSmrg# all copies or substantial portions of the Software.
211a30de1fSmrg#
221a30de1fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
231a30de1fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
241a30de1fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
251a30de1fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
261a30de1fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
271a30de1fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
281a30de1fSmrg#
291a30de1fSmrg# Except as contained in this notice, the name of the X Consortium shall not
301a30de1fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
311a30de1fSmrg# ings in this Software without prior written authorization from the X Consor-
321a30de1fSmrg# tium.
331a30de1fSmrg#
341a30de1fSmrg#
351a30de1fSmrg# FSF changes to this file are in the public domain.
361a30de1fSmrg#
371a30de1fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
381a30de1fSmrg# `make' implicit rules from creating a file called install from it
391a30de1fSmrg# when there is no Makefile.
401a30de1fSmrg#
411a30de1fSmrg# This script is compatible with the BSD install script, but was written
421a30de1fSmrg# from scratch.
431a30de1fSmrg
441a30de1fSmrgnl='
451a30de1fSmrg'
461a30de1fSmrgIFS=" ""	$nl"
471a30de1fSmrg
481a30de1fSmrg# set DOITPROG to echo to test this script
491a30de1fSmrg
501a30de1fSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51b7fb5eacSmrgdoit=${DOITPROG-}
521a30de1fSmrgif test -z "$doit"; then
531a30de1fSmrg  doit_exec=exec
541a30de1fSmrgelse
551a30de1fSmrg  doit_exec=$doit
561a30de1fSmrgfi
571a30de1fSmrg
581a30de1fSmrg# Put in absolute file names if you don't have them in your path;
591a30de1fSmrg# or use environment vars.
601a30de1fSmrg
61b7fb5eacSmrgchgrpprog=${CHGRPPROG-chgrp}
62b7fb5eacSmrgchmodprog=${CHMODPROG-chmod}
63b7fb5eacSmrgchownprog=${CHOWNPROG-chown}
64b7fb5eacSmrgcmpprog=${CMPPROG-cmp}
65b7fb5eacSmrgcpprog=${CPPROG-cp}
66b7fb5eacSmrgmkdirprog=${MKDIRPROG-mkdir}
67b7fb5eacSmrgmvprog=${MVPROG-mv}
68b7fb5eacSmrgrmprog=${RMPROG-rm}
69b7fb5eacSmrgstripprog=${STRIPPROG-strip}
70b7fb5eacSmrg
71b7fb5eacSmrgposix_glob='?'
72b7fb5eacSmrginitialize_posix_glob='
73b7fb5eacSmrg  test "$posix_glob" != "?" || {
74b7fb5eacSmrg    if (set -f) 2>/dev/null; then
75b7fb5eacSmrg      posix_glob=
76b7fb5eacSmrg    else
77b7fb5eacSmrg      posix_glob=:
78b7fb5eacSmrg    fi
79b7fb5eacSmrg  }
80b7fb5eacSmrg'
811a30de1fSmrg
821a30de1fSmrgposix_mkdir=
831a30de1fSmrg
841a30de1fSmrg# Desired mode of installed file.
851a30de1fSmrgmode=0755
861a30de1fSmrg
87b7fb5eacSmrgchgrpcmd=
881a30de1fSmrgchmodcmd=$chmodprog
891a30de1fSmrgchowncmd=
90b7fb5eacSmrgmvcmd=$mvprog
911a30de1fSmrgrmcmd="$rmprog -f"
92b7fb5eacSmrgstripcmd=
93b7fb5eacSmrg
941a30de1fSmrgsrc=
951a30de1fSmrgdst=
961a30de1fSmrgdir_arg=
97b7fb5eacSmrgdst_arg=
98b7fb5eacSmrg
99b7fb5eacSmrgcopy_on_change=false
1001a30de1fSmrgno_target_directory=
1011a30de1fSmrg
102b7fb5eacSmrgusage="\
103b7fb5eacSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1041a30de1fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1051a30de1fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1061a30de1fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
1071a30de1fSmrg
1081a30de1fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
1091a30de1fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1101a30de1fSmrgIn the 4th, create DIRECTORIES.
1111a30de1fSmrg
1121a30de1fSmrgOptions:
113b7fb5eacSmrg     --help     display this help and exit.
114b7fb5eacSmrg     --version  display version info and exit.
115b7fb5eacSmrg
116b7fb5eacSmrg  -c            (ignored)
117b7fb5eacSmrg  -C            install only if different (preserve the last data modification time)
118b7fb5eacSmrg  -d            create directories instead of installing files.
119b7fb5eacSmrg  -g GROUP      $chgrpprog installed files to GROUP.
120b7fb5eacSmrg  -m MODE       $chmodprog installed files to MODE.
121b7fb5eacSmrg  -o USER       $chownprog installed files to USER.
122b7fb5eacSmrg  -s            $stripprog installed files.
123b7fb5eacSmrg  -t DIRECTORY  install into DIRECTORY.
124b7fb5eacSmrg  -T            report an error if DSTFILE is a directory.
1251a30de1fSmrg
1261a30de1fSmrgEnvironment variables override the default commands:
127b7fb5eacSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128b7fb5eacSmrg  RMPROG STRIPPROG
1291a30de1fSmrg"
1301a30de1fSmrg
1311a30de1fSmrgwhile test $# -ne 0; do
1321a30de1fSmrg  case $1 in
133b7fb5eacSmrg    -c) ;;
134b7fb5eacSmrg
135b7fb5eacSmrg    -C) copy_on_change=true;;
1361a30de1fSmrg
137b7fb5eacSmrg    -d) dir_arg=true;;
1381a30de1fSmrg
1391a30de1fSmrg    -g) chgrpcmd="$chgrpprog $2"
140b7fb5eacSmrg	shift;;
1411a30de1fSmrg
1421a30de1fSmrg    --help) echo "$usage"; exit $?;;
1431a30de1fSmrg
1441a30de1fSmrg    -m) mode=$2
1451a30de1fSmrg	case $mode in
1461a30de1fSmrg	  *' '* | *'	'* | *'
1471a30de1fSmrg'*	  | *'*'* | *'?'* | *'['*)
1481a30de1fSmrg	    echo "$0: invalid mode: $mode" >&2
1491a30de1fSmrg	    exit 1;;
1501a30de1fSmrg	esac
151b7fb5eacSmrg	shift;;
1521a30de1fSmrg
1531a30de1fSmrg    -o) chowncmd="$chownprog $2"
154b7fb5eacSmrg	shift;;
1551a30de1fSmrg
156b7fb5eacSmrg    -s) stripcmd=$stripprog;;
1571a30de1fSmrg
158b7fb5eacSmrg    -t) dst_arg=$2
159b7fb5eacSmrg	shift;;
1601a30de1fSmrg
161b7fb5eacSmrg    -T) no_target_directory=true;;
1621a30de1fSmrg
1631a30de1fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1641a30de1fSmrg
1651a30de1fSmrg    --)	shift
1661a30de1fSmrg	break;;
1671a30de1fSmrg
1681a30de1fSmrg    -*)	echo "$0: invalid option: $1" >&2
1691a30de1fSmrg	exit 1;;
1701a30de1fSmrg
1711a30de1fSmrg    *)  break;;
1721a30de1fSmrg  esac
173b7fb5eacSmrg  shift
1741a30de1fSmrgdone
1751a30de1fSmrg
176b7fb5eacSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1771a30de1fSmrg  # When -d is used, all remaining arguments are directories to create.
1781a30de1fSmrg  # When -t is used, the destination is already specified.
1791a30de1fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1801a30de1fSmrg  for arg
1811a30de1fSmrg  do
182b7fb5eacSmrg    if test -n "$dst_arg"; then
1831a30de1fSmrg      # $@ is not empty: it contains at least $arg.
184b7fb5eacSmrg      set fnord "$@" "$dst_arg"
1851a30de1fSmrg      shift # fnord
1861a30de1fSmrg    fi
1871a30de1fSmrg    shift # arg
188b7fb5eacSmrg    dst_arg=$arg
1891a30de1fSmrg  done
1901a30de1fSmrgfi
1911a30de1fSmrg
1921a30de1fSmrgif test $# -eq 0; then
1931a30de1fSmrg  if test -z "$dir_arg"; then
1941a30de1fSmrg    echo "$0: no input file specified." >&2
1951a30de1fSmrg    exit 1
1961a30de1fSmrg  fi
1971a30de1fSmrg  # It's OK to call `install-sh -d' without argument.
1981a30de1fSmrg  # This can happen when creating conditional directories.
1991a30de1fSmrg  exit 0
2001a30de1fSmrgfi
2011a30de1fSmrg
2021a30de1fSmrgif test -z "$dir_arg"; then
2031a30de1fSmrg  trap '(exit $?); exit' 1 2 13 15
2041a30de1fSmrg
2051a30de1fSmrg  # Set umask so as not to create temps with too-generous modes.
2061a30de1fSmrg  # However, 'strip' requires both read and write access to temps.
2071a30de1fSmrg  case $mode in
2081a30de1fSmrg    # Optimize common cases.
2091a30de1fSmrg    *644) cp_umask=133;;
2101a30de1fSmrg    *755) cp_umask=22;;
2111a30de1fSmrg
2121a30de1fSmrg    *[0-7])
2131a30de1fSmrg      if test -z "$stripcmd"; then
2141a30de1fSmrg	u_plus_rw=
2151a30de1fSmrg      else
2161a30de1fSmrg	u_plus_rw='% 200'
2171a30de1fSmrg      fi
2181a30de1fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2191a30de1fSmrg    *)
2201a30de1fSmrg      if test -z "$stripcmd"; then
2211a30de1fSmrg	u_plus_rw=
2221a30de1fSmrg      else
2231a30de1fSmrg	u_plus_rw=,u+rw
2241a30de1fSmrg      fi
2251a30de1fSmrg      cp_umask=$mode$u_plus_rw;;
2261a30de1fSmrg  esac
2271a30de1fSmrgfi
2281a30de1fSmrg
2291a30de1fSmrgfor src
2301a30de1fSmrgdo
2311a30de1fSmrg  # Protect names starting with `-'.
2321a30de1fSmrg  case $src in
233b7fb5eacSmrg    -*) src=./$src;;
2341a30de1fSmrg  esac
2351a30de1fSmrg
2361a30de1fSmrg  if test -n "$dir_arg"; then
2371a30de1fSmrg    dst=$src
2381a30de1fSmrg    dstdir=$dst
2391a30de1fSmrg    test -d "$dstdir"
2401a30de1fSmrg    dstdir_status=$?
2411a30de1fSmrg  else
2421a30de1fSmrg
2431a30de1fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2441a30de1fSmrg    # might cause directories to be created, which would be especially bad
2451a30de1fSmrg    # if $src (and thus $dsttmp) contains '*'.
2461a30de1fSmrg    if test ! -f "$src" && test ! -d "$src"; then
2471a30de1fSmrg      echo "$0: $src does not exist." >&2
2481a30de1fSmrg      exit 1
2491a30de1fSmrg    fi
2501a30de1fSmrg
251b7fb5eacSmrg    if test -z "$dst_arg"; then
2521a30de1fSmrg      echo "$0: no destination specified." >&2
2531a30de1fSmrg      exit 1
2541a30de1fSmrg    fi
2551a30de1fSmrg
256b7fb5eacSmrg    dst=$dst_arg
2571a30de1fSmrg    # Protect names starting with `-'.
2581a30de1fSmrg    case $dst in
259b7fb5eacSmrg      -*) dst=./$dst;;
2601a30de1fSmrg    esac
2611a30de1fSmrg
2621a30de1fSmrg    # If destination is a directory, append the input filename; won't work
2631a30de1fSmrg    # if double slashes aren't ignored.
2641a30de1fSmrg    if test -d "$dst"; then
2651a30de1fSmrg      if test -n "$no_target_directory"; then
266b7fb5eacSmrg	echo "$0: $dst_arg: Is a directory" >&2
2671a30de1fSmrg	exit 1
2681a30de1fSmrg      fi
2691a30de1fSmrg      dstdir=$dst
2701a30de1fSmrg      dst=$dstdir/`basename "$src"`
2711a30de1fSmrg      dstdir_status=0
2721a30de1fSmrg    else
2731a30de1fSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2741a30de1fSmrg      dstdir=`
2751a30de1fSmrg	(dirname "$dst") 2>/dev/null ||
2761a30de1fSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2771a30de1fSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
2781a30de1fSmrg	     X"$dst" : 'X\(//\)$' \| \
2791a30de1fSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2801a30de1fSmrg	echo X"$dst" |
2811a30de1fSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2821a30de1fSmrg		   s//\1/
2831a30de1fSmrg		   q
2841a30de1fSmrg		 }
2851a30de1fSmrg		 /^X\(\/\/\)[^/].*/{
2861a30de1fSmrg		   s//\1/
2871a30de1fSmrg		   q
2881a30de1fSmrg		 }
2891a30de1fSmrg		 /^X\(\/\/\)$/{
2901a30de1fSmrg		   s//\1/
2911a30de1fSmrg		   q
2921a30de1fSmrg		 }
2931a30de1fSmrg		 /^X\(\/\).*/{
2941a30de1fSmrg		   s//\1/
2951a30de1fSmrg		   q
2961a30de1fSmrg		 }
2971a30de1fSmrg		 s/.*/./; q'
2981a30de1fSmrg      `
2991a30de1fSmrg
3001a30de1fSmrg      test -d "$dstdir"
3011a30de1fSmrg      dstdir_status=$?
3021a30de1fSmrg    fi
3031a30de1fSmrg  fi
3041a30de1fSmrg
3051a30de1fSmrg  obsolete_mkdir_used=false
3061a30de1fSmrg
3071a30de1fSmrg  if test $dstdir_status != 0; then
3081a30de1fSmrg    case $posix_mkdir in
3091a30de1fSmrg      '')
3101a30de1fSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
3111a30de1fSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
3121a30de1fSmrg	umask=`umask`
3131a30de1fSmrg	case $stripcmd.$umask in
3141a30de1fSmrg	  # Optimize common cases.
3151a30de1fSmrg	  *[2367][2367]) mkdir_umask=$umask;;
3161a30de1fSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3171a30de1fSmrg
3181a30de1fSmrg	  *[0-7])
3191a30de1fSmrg	    mkdir_umask=`expr $umask + 22 \
3201a30de1fSmrg	      - $umask % 100 % 40 + $umask % 20 \
3211a30de1fSmrg	      - $umask % 10 % 4 + $umask % 2
3221a30de1fSmrg	    `;;
3231a30de1fSmrg	  *) mkdir_umask=$umask,go-w;;
3241a30de1fSmrg	esac
3251a30de1fSmrg
3261a30de1fSmrg	# With -d, create the new directory with the user-specified mode.
3271a30de1fSmrg	# Otherwise, rely on $mkdir_umask.
3281a30de1fSmrg	if test -n "$dir_arg"; then
3291a30de1fSmrg	  mkdir_mode=-m$mode
3301a30de1fSmrg	else
3311a30de1fSmrg	  mkdir_mode=
3321a30de1fSmrg	fi
3331a30de1fSmrg
3341a30de1fSmrg	posix_mkdir=false
3351a30de1fSmrg	case $umask in
3361a30de1fSmrg	  *[123567][0-7][0-7])
3371a30de1fSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3381a30de1fSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3391a30de1fSmrg	    ;;
3401a30de1fSmrg	  *)
3411a30de1fSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3421a30de1fSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3431a30de1fSmrg
3441a30de1fSmrg	    if (umask $mkdir_umask &&
3451a30de1fSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3461a30de1fSmrg	    then
3471a30de1fSmrg	      if test -z "$dir_arg" || {
3481a30de1fSmrg		   # Check for POSIX incompatibilities with -m.
3491a30de1fSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3501a30de1fSmrg		   # other-writeable bit of parent directory when it shouldn't.
3511a30de1fSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3521a30de1fSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3531a30de1fSmrg		   case $ls_ld_tmpdir in
3541a30de1fSmrg		     d????-?r-*) different_mode=700;;
3551a30de1fSmrg		     d????-?--*) different_mode=755;;
3561a30de1fSmrg		     *) false;;
3571a30de1fSmrg		   esac &&
3581a30de1fSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3591a30de1fSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3601a30de1fSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3611a30de1fSmrg		   }
3621a30de1fSmrg		 }
3631a30de1fSmrg	      then posix_mkdir=:
3641a30de1fSmrg	      fi
3651a30de1fSmrg	      rmdir "$tmpdir/d" "$tmpdir"
3661a30de1fSmrg	    else
3671a30de1fSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
3681a30de1fSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3691a30de1fSmrg	    fi
3701a30de1fSmrg	    trap '' 0;;
3711a30de1fSmrg	esac;;
3721a30de1fSmrg    esac
3731a30de1fSmrg
3741a30de1fSmrg    if
3751a30de1fSmrg      $posix_mkdir && (
3761a30de1fSmrg	umask $mkdir_umask &&
3771a30de1fSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3781a30de1fSmrg      )
3791a30de1fSmrg    then :
3801a30de1fSmrg    else
3811a30de1fSmrg
3821a30de1fSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3831a30de1fSmrg      # or it failed possibly due to a race condition.  Create the
3841a30de1fSmrg      # directory the slow way, step by step, checking for races as we go.
3851a30de1fSmrg
3861a30de1fSmrg      case $dstdir in
387b7fb5eacSmrg	/*) prefix='/';;
388b7fb5eacSmrg	-*) prefix='./';;
389b7fb5eacSmrg	*)  prefix='';;
3901a30de1fSmrg      esac
3911a30de1fSmrg
392b7fb5eacSmrg      eval "$initialize_posix_glob"
3931a30de1fSmrg
3941a30de1fSmrg      oIFS=$IFS
3951a30de1fSmrg      IFS=/
396b7fb5eacSmrg      $posix_glob set -f
3971a30de1fSmrg      set fnord $dstdir
3981a30de1fSmrg      shift
399b7fb5eacSmrg      $posix_glob set +f
4001a30de1fSmrg      IFS=$oIFS
4011a30de1fSmrg
4021a30de1fSmrg      prefixes=
4031a30de1fSmrg
4041a30de1fSmrg      for d
4051a30de1fSmrg      do
4061a30de1fSmrg	test -z "$d" && continue
4071a30de1fSmrg
4081a30de1fSmrg	prefix=$prefix$d
4091a30de1fSmrg	if test -d "$prefix"; then
4101a30de1fSmrg	  prefixes=
4111a30de1fSmrg	else
4121a30de1fSmrg	  if $posix_mkdir; then
4131a30de1fSmrg	    (umask=$mkdir_umask &&
4141a30de1fSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4151a30de1fSmrg	    # Don't fail if two instances are running concurrently.
4161a30de1fSmrg	    test -d "$prefix" || exit 1
4171a30de1fSmrg	  else
4181a30de1fSmrg	    case $prefix in
4191a30de1fSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4201a30de1fSmrg	      *) qprefix=$prefix;;
4211a30de1fSmrg	    esac
4221a30de1fSmrg	    prefixes="$prefixes '$qprefix'"
4231a30de1fSmrg	  fi
4241a30de1fSmrg	fi
4251a30de1fSmrg	prefix=$prefix/
4261a30de1fSmrg      done
4271a30de1fSmrg
4281a30de1fSmrg      if test -n "$prefixes"; then
4291a30de1fSmrg	# Don't fail if two instances are running concurrently.
4301a30de1fSmrg	(umask $mkdir_umask &&
4311a30de1fSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4321a30de1fSmrg	  test -d "$dstdir" || exit 1
4331a30de1fSmrg	obsolete_mkdir_used=true
4341a30de1fSmrg      fi
4351a30de1fSmrg    fi
4361a30de1fSmrg  fi
4371a30de1fSmrg
4381a30de1fSmrg  if test -n "$dir_arg"; then
4391a30de1fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4401a30de1fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4411a30de1fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4421a30de1fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4431a30de1fSmrg  else
4441a30de1fSmrg
4451a30de1fSmrg    # Make a couple of temp file names in the proper directory.
4461a30de1fSmrg    dsttmp=$dstdir/_inst.$$_
4471a30de1fSmrg    rmtmp=$dstdir/_rm.$$_
4481a30de1fSmrg
4491a30de1fSmrg    # Trap to clean up those temp files at exit.
4501a30de1fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4511a30de1fSmrg
4521a30de1fSmrg    # Copy the file name to the temp name.
4531a30de1fSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4541a30de1fSmrg
4551a30de1fSmrg    # and set any options; do chmod last to preserve setuid bits.
4561a30de1fSmrg    #
4571a30de1fSmrg    # If any of these fail, we abort the whole thing.  If we want to
4581a30de1fSmrg    # ignore errors from any of these, just make sure not to ignore
4591a30de1fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4601a30de1fSmrg    #
461b7fb5eacSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462b7fb5eacSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463b7fb5eacSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464b7fb5eacSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465b7fb5eacSmrg
466b7fb5eacSmrg    # If -C, don't bother to copy if it wouldn't change the file.
467b7fb5eacSmrg    if $copy_on_change &&
468b7fb5eacSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469b7fb5eacSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470b7fb5eacSmrg
471b7fb5eacSmrg       eval "$initialize_posix_glob" &&
472b7fb5eacSmrg       $posix_glob set -f &&
473b7fb5eacSmrg       set X $old && old=:$2:$4:$5:$6 &&
474b7fb5eacSmrg       set X $new && new=:$2:$4:$5:$6 &&
475b7fb5eacSmrg       $posix_glob set +f &&
476b7fb5eacSmrg
477b7fb5eacSmrg       test "$old" = "$new" &&
478b7fb5eacSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479b7fb5eacSmrg    then
480b7fb5eacSmrg      rm -f "$dsttmp"
481b7fb5eacSmrg    else
482b7fb5eacSmrg      # Rename the file to the real destination.
483b7fb5eacSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484b7fb5eacSmrg
485b7fb5eacSmrg      # The rename failed, perhaps because mv can't rename something else
486b7fb5eacSmrg      # to itself, or perhaps because mv is so ancient that it does not
487b7fb5eacSmrg      # support -f.
488b7fb5eacSmrg      {
489b7fb5eacSmrg	# Now remove or move aside any old file at destination location.
490b7fb5eacSmrg	# We try this two ways since rm can't unlink itself on some
491b7fb5eacSmrg	# systems and the destination file might be busy for other
492b7fb5eacSmrg	# reasons.  In this case, the final cleanup might fail but the new
493b7fb5eacSmrg	# file should still install successfully.
494b7fb5eacSmrg	{
495b7fb5eacSmrg	  test ! -f "$dst" ||
496b7fb5eacSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497b7fb5eacSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498b7fb5eacSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499b7fb5eacSmrg	  } ||
500b7fb5eacSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
501b7fb5eacSmrg	    (exit 1); exit 1
502b7fb5eacSmrg	  }
503b7fb5eacSmrg	} &&
504b7fb5eacSmrg
505b7fb5eacSmrg	# Now rename the file to the real destination.
506b7fb5eacSmrg	$doit $mvcmd "$dsttmp" "$dst"
507b7fb5eacSmrg      }
508b7fb5eacSmrg    fi || exit 1
5091a30de1fSmrg
5101a30de1fSmrg    trap '' 0
5111a30de1fSmrg  fi
5121a30de1fSmrgdone
5131a30de1fSmrg
5141a30de1fSmrg# Local variables:
5151a30de1fSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5161a30de1fSmrg# time-stamp-start: "scriptversion="
5171a30de1fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518b7fb5eacSmrg# time-stamp-time-zone: "UTC"
519b7fb5eacSmrg# time-stamp-end: "; # UTC"
5201a30de1fSmrg# End:
521