install-sh revision a733a5bf
11a30de1fSmrg#!/bin/sh
21a30de1fSmrg# install - install a program, script, or datafile
31a30de1fSmrg
4a733a5bfSmrgscriptversion=2011-11-20.07; # 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
38a733a5bfSmrg# '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
159a733a5bfSmrg	# Protect names problematic for 'test' and other utilities.
160a733a5bfSmrg	case $dst_arg in
161a733a5bfSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162a733a5bfSmrg	esac
163b7fb5eacSmrg	shift;;
1641a30de1fSmrg
165b7fb5eacSmrg    -T) no_target_directory=true;;
1661a30de1fSmrg
1671a30de1fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1681a30de1fSmrg
1691a30de1fSmrg    --)	shift
1701a30de1fSmrg	break;;
1711a30de1fSmrg
1721a30de1fSmrg    -*)	echo "$0: invalid option: $1" >&2
1731a30de1fSmrg	exit 1;;
1741a30de1fSmrg
1751a30de1fSmrg    *)  break;;
1761a30de1fSmrg  esac
177b7fb5eacSmrg  shift
1781a30de1fSmrgdone
1791a30de1fSmrg
180b7fb5eacSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1811a30de1fSmrg  # When -d is used, all remaining arguments are directories to create.
1821a30de1fSmrg  # When -t is used, the destination is already specified.
1831a30de1fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1841a30de1fSmrg  for arg
1851a30de1fSmrg  do
186b7fb5eacSmrg    if test -n "$dst_arg"; then
1871a30de1fSmrg      # $@ is not empty: it contains at least $arg.
188b7fb5eacSmrg      set fnord "$@" "$dst_arg"
1891a30de1fSmrg      shift # fnord
1901a30de1fSmrg    fi
1911a30de1fSmrg    shift # arg
192b7fb5eacSmrg    dst_arg=$arg
193a733a5bfSmrg    # Protect names problematic for 'test' and other utilities.
194a733a5bfSmrg    case $dst_arg in
195a733a5bfSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196a733a5bfSmrg    esac
1971a30de1fSmrg  done
1981a30de1fSmrgfi
1991a30de1fSmrg
2001a30de1fSmrgif test $# -eq 0; then
2011a30de1fSmrg  if test -z "$dir_arg"; then
2021a30de1fSmrg    echo "$0: no input file specified." >&2
2031a30de1fSmrg    exit 1
2041a30de1fSmrg  fi
205a733a5bfSmrg  # It's OK to call 'install-sh -d' without argument.
2061a30de1fSmrg  # This can happen when creating conditional directories.
2071a30de1fSmrg  exit 0
2081a30de1fSmrgfi
2091a30de1fSmrg
2101a30de1fSmrgif test -z "$dir_arg"; then
211a733a5bfSmrg  do_exit='(exit $ret); exit $ret'
212a733a5bfSmrg  trap "ret=129; $do_exit" 1
213a733a5bfSmrg  trap "ret=130; $do_exit" 2
214a733a5bfSmrg  trap "ret=141; $do_exit" 13
215a733a5bfSmrg  trap "ret=143; $do_exit" 15
2161a30de1fSmrg
2171a30de1fSmrg  # Set umask so as not to create temps with too-generous modes.
2181a30de1fSmrg  # However, 'strip' requires both read and write access to temps.
2191a30de1fSmrg  case $mode in
2201a30de1fSmrg    # Optimize common cases.
2211a30de1fSmrg    *644) cp_umask=133;;
2221a30de1fSmrg    *755) cp_umask=22;;
2231a30de1fSmrg
2241a30de1fSmrg    *[0-7])
2251a30de1fSmrg      if test -z "$stripcmd"; then
2261a30de1fSmrg	u_plus_rw=
2271a30de1fSmrg      else
2281a30de1fSmrg	u_plus_rw='% 200'
2291a30de1fSmrg      fi
2301a30de1fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2311a30de1fSmrg    *)
2321a30de1fSmrg      if test -z "$stripcmd"; then
2331a30de1fSmrg	u_plus_rw=
2341a30de1fSmrg      else
2351a30de1fSmrg	u_plus_rw=,u+rw
2361a30de1fSmrg      fi
2371a30de1fSmrg      cp_umask=$mode$u_plus_rw;;
2381a30de1fSmrg  esac
2391a30de1fSmrgfi
2401a30de1fSmrg
2411a30de1fSmrgfor src
2421a30de1fSmrgdo
243a733a5bfSmrg  # Protect names problematic for 'test' and other utilities.
2441a30de1fSmrg  case $src in
245a733a5bfSmrg    -* | [=\(\)!]) src=./$src;;
2461a30de1fSmrg  esac
2471a30de1fSmrg
2481a30de1fSmrg  if test -n "$dir_arg"; then
2491a30de1fSmrg    dst=$src
2501a30de1fSmrg    dstdir=$dst
2511a30de1fSmrg    test -d "$dstdir"
2521a30de1fSmrg    dstdir_status=$?
2531a30de1fSmrg  else
2541a30de1fSmrg
2551a30de1fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2561a30de1fSmrg    # might cause directories to be created, which would be especially bad
2571a30de1fSmrg    # if $src (and thus $dsttmp) contains '*'.
2581a30de1fSmrg    if test ! -f "$src" && test ! -d "$src"; then
2591a30de1fSmrg      echo "$0: $src does not exist." >&2
2601a30de1fSmrg      exit 1
2611a30de1fSmrg    fi
2621a30de1fSmrg
263b7fb5eacSmrg    if test -z "$dst_arg"; then
2641a30de1fSmrg      echo "$0: no destination specified." >&2
2651a30de1fSmrg      exit 1
2661a30de1fSmrg    fi
267b7fb5eacSmrg    dst=$dst_arg
2681a30de1fSmrg
2691a30de1fSmrg    # If destination is a directory, append the input filename; won't work
2701a30de1fSmrg    # if double slashes aren't ignored.
2711a30de1fSmrg    if test -d "$dst"; then
2721a30de1fSmrg      if test -n "$no_target_directory"; then
273b7fb5eacSmrg	echo "$0: $dst_arg: Is a directory" >&2
2741a30de1fSmrg	exit 1
2751a30de1fSmrg      fi
2761a30de1fSmrg      dstdir=$dst
2771a30de1fSmrg      dst=$dstdir/`basename "$src"`
2781a30de1fSmrg      dstdir_status=0
2791a30de1fSmrg    else
2801a30de1fSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2811a30de1fSmrg      dstdir=`
2821a30de1fSmrg	(dirname "$dst") 2>/dev/null ||
2831a30de1fSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2841a30de1fSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
2851a30de1fSmrg	     X"$dst" : 'X\(//\)$' \| \
2861a30de1fSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2871a30de1fSmrg	echo X"$dst" |
2881a30de1fSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2891a30de1fSmrg		   s//\1/
2901a30de1fSmrg		   q
2911a30de1fSmrg		 }
2921a30de1fSmrg		 /^X\(\/\/\)[^/].*/{
2931a30de1fSmrg		   s//\1/
2941a30de1fSmrg		   q
2951a30de1fSmrg		 }
2961a30de1fSmrg		 /^X\(\/\/\)$/{
2971a30de1fSmrg		   s//\1/
2981a30de1fSmrg		   q
2991a30de1fSmrg		 }
3001a30de1fSmrg		 /^X\(\/\).*/{
3011a30de1fSmrg		   s//\1/
3021a30de1fSmrg		   q
3031a30de1fSmrg		 }
3041a30de1fSmrg		 s/.*/./; q'
3051a30de1fSmrg      `
3061a30de1fSmrg
3071a30de1fSmrg      test -d "$dstdir"
3081a30de1fSmrg      dstdir_status=$?
3091a30de1fSmrg    fi
3101a30de1fSmrg  fi
3111a30de1fSmrg
3121a30de1fSmrg  obsolete_mkdir_used=false
3131a30de1fSmrg
3141a30de1fSmrg  if test $dstdir_status != 0; then
3151a30de1fSmrg    case $posix_mkdir in
3161a30de1fSmrg      '')
3171a30de1fSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
3181a30de1fSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
3191a30de1fSmrg	umask=`umask`
3201a30de1fSmrg	case $stripcmd.$umask in
3211a30de1fSmrg	  # Optimize common cases.
3221a30de1fSmrg	  *[2367][2367]) mkdir_umask=$umask;;
3231a30de1fSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3241a30de1fSmrg
3251a30de1fSmrg	  *[0-7])
3261a30de1fSmrg	    mkdir_umask=`expr $umask + 22 \
3271a30de1fSmrg	      - $umask % 100 % 40 + $umask % 20 \
3281a30de1fSmrg	      - $umask % 10 % 4 + $umask % 2
3291a30de1fSmrg	    `;;
3301a30de1fSmrg	  *) mkdir_umask=$umask,go-w;;
3311a30de1fSmrg	esac
3321a30de1fSmrg
3331a30de1fSmrg	# With -d, create the new directory with the user-specified mode.
3341a30de1fSmrg	# Otherwise, rely on $mkdir_umask.
3351a30de1fSmrg	if test -n "$dir_arg"; then
3361a30de1fSmrg	  mkdir_mode=-m$mode
3371a30de1fSmrg	else
3381a30de1fSmrg	  mkdir_mode=
3391a30de1fSmrg	fi
3401a30de1fSmrg
3411a30de1fSmrg	posix_mkdir=false
3421a30de1fSmrg	case $umask in
3431a30de1fSmrg	  *[123567][0-7][0-7])
3441a30de1fSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3451a30de1fSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3461a30de1fSmrg	    ;;
3471a30de1fSmrg	  *)
3481a30de1fSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3491a30de1fSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3501a30de1fSmrg
3511a30de1fSmrg	    if (umask $mkdir_umask &&
3521a30de1fSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3531a30de1fSmrg	    then
3541a30de1fSmrg	      if test -z "$dir_arg" || {
3551a30de1fSmrg		   # Check for POSIX incompatibilities with -m.
3561a30de1fSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357a733a5bfSmrg		   # other-writable bit of parent directory when it shouldn't.
3581a30de1fSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3591a30de1fSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3601a30de1fSmrg		   case $ls_ld_tmpdir in
3611a30de1fSmrg		     d????-?r-*) different_mode=700;;
3621a30de1fSmrg		     d????-?--*) different_mode=755;;
3631a30de1fSmrg		     *) false;;
3641a30de1fSmrg		   esac &&
3651a30de1fSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3661a30de1fSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3671a30de1fSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3681a30de1fSmrg		   }
3691a30de1fSmrg		 }
3701a30de1fSmrg	      then posix_mkdir=:
3711a30de1fSmrg	      fi
3721a30de1fSmrg	      rmdir "$tmpdir/d" "$tmpdir"
3731a30de1fSmrg	    else
3741a30de1fSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
3751a30de1fSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3761a30de1fSmrg	    fi
3771a30de1fSmrg	    trap '' 0;;
3781a30de1fSmrg	esac;;
3791a30de1fSmrg    esac
3801a30de1fSmrg
3811a30de1fSmrg    if
3821a30de1fSmrg      $posix_mkdir && (
3831a30de1fSmrg	umask $mkdir_umask &&
3841a30de1fSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3851a30de1fSmrg      )
3861a30de1fSmrg    then :
3871a30de1fSmrg    else
3881a30de1fSmrg
3891a30de1fSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3901a30de1fSmrg      # or it failed possibly due to a race condition.  Create the
3911a30de1fSmrg      # directory the slow way, step by step, checking for races as we go.
3921a30de1fSmrg
3931a30de1fSmrg      case $dstdir in
394b7fb5eacSmrg	/*) prefix='/';;
395a733a5bfSmrg	[-=\(\)!]*) prefix='./';;
396b7fb5eacSmrg	*)  prefix='';;
3971a30de1fSmrg      esac
3981a30de1fSmrg
399b7fb5eacSmrg      eval "$initialize_posix_glob"
4001a30de1fSmrg
4011a30de1fSmrg      oIFS=$IFS
4021a30de1fSmrg      IFS=/
403b7fb5eacSmrg      $posix_glob set -f
4041a30de1fSmrg      set fnord $dstdir
4051a30de1fSmrg      shift
406b7fb5eacSmrg      $posix_glob set +f
4071a30de1fSmrg      IFS=$oIFS
4081a30de1fSmrg
4091a30de1fSmrg      prefixes=
4101a30de1fSmrg
4111a30de1fSmrg      for d
4121a30de1fSmrg      do
413a733a5bfSmrg	test X"$d" = X && continue
4141a30de1fSmrg
4151a30de1fSmrg	prefix=$prefix$d
4161a30de1fSmrg	if test -d "$prefix"; then
4171a30de1fSmrg	  prefixes=
4181a30de1fSmrg	else
4191a30de1fSmrg	  if $posix_mkdir; then
4201a30de1fSmrg	    (umask=$mkdir_umask &&
4211a30de1fSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4221a30de1fSmrg	    # Don't fail if two instances are running concurrently.
4231a30de1fSmrg	    test -d "$prefix" || exit 1
4241a30de1fSmrg	  else
4251a30de1fSmrg	    case $prefix in
4261a30de1fSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4271a30de1fSmrg	      *) qprefix=$prefix;;
4281a30de1fSmrg	    esac
4291a30de1fSmrg	    prefixes="$prefixes '$qprefix'"
4301a30de1fSmrg	  fi
4311a30de1fSmrg	fi
4321a30de1fSmrg	prefix=$prefix/
4331a30de1fSmrg      done
4341a30de1fSmrg
4351a30de1fSmrg      if test -n "$prefixes"; then
4361a30de1fSmrg	# Don't fail if two instances are running concurrently.
4371a30de1fSmrg	(umask $mkdir_umask &&
4381a30de1fSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4391a30de1fSmrg	  test -d "$dstdir" || exit 1
4401a30de1fSmrg	obsolete_mkdir_used=true
4411a30de1fSmrg      fi
4421a30de1fSmrg    fi
4431a30de1fSmrg  fi
4441a30de1fSmrg
4451a30de1fSmrg  if test -n "$dir_arg"; then
4461a30de1fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4471a30de1fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4481a30de1fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4491a30de1fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4501a30de1fSmrg  else
4511a30de1fSmrg
4521a30de1fSmrg    # Make a couple of temp file names in the proper directory.
4531a30de1fSmrg    dsttmp=$dstdir/_inst.$$_
4541a30de1fSmrg    rmtmp=$dstdir/_rm.$$_
4551a30de1fSmrg
4561a30de1fSmrg    # Trap to clean up those temp files at exit.
4571a30de1fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4581a30de1fSmrg
4591a30de1fSmrg    # Copy the file name to the temp name.
4601a30de1fSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4611a30de1fSmrg
4621a30de1fSmrg    # and set any options; do chmod last to preserve setuid bits.
4631a30de1fSmrg    #
4641a30de1fSmrg    # If any of these fail, we abort the whole thing.  If we want to
4651a30de1fSmrg    # ignore errors from any of these, just make sure not to ignore
4661a30de1fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4671a30de1fSmrg    #
468b7fb5eacSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469b7fb5eacSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470b7fb5eacSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471b7fb5eacSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472b7fb5eacSmrg
473b7fb5eacSmrg    # If -C, don't bother to copy if it wouldn't change the file.
474b7fb5eacSmrg    if $copy_on_change &&
475b7fb5eacSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476b7fb5eacSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477b7fb5eacSmrg
478b7fb5eacSmrg       eval "$initialize_posix_glob" &&
479b7fb5eacSmrg       $posix_glob set -f &&
480b7fb5eacSmrg       set X $old && old=:$2:$4:$5:$6 &&
481b7fb5eacSmrg       set X $new && new=:$2:$4:$5:$6 &&
482b7fb5eacSmrg       $posix_glob set +f &&
483b7fb5eacSmrg
484b7fb5eacSmrg       test "$old" = "$new" &&
485b7fb5eacSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486b7fb5eacSmrg    then
487b7fb5eacSmrg      rm -f "$dsttmp"
488b7fb5eacSmrg    else
489b7fb5eacSmrg      # Rename the file to the real destination.
490b7fb5eacSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491b7fb5eacSmrg
492b7fb5eacSmrg      # The rename failed, perhaps because mv can't rename something else
493b7fb5eacSmrg      # to itself, or perhaps because mv is so ancient that it does not
494b7fb5eacSmrg      # support -f.
495b7fb5eacSmrg      {
496b7fb5eacSmrg	# Now remove or move aside any old file at destination location.
497b7fb5eacSmrg	# We try this two ways since rm can't unlink itself on some
498b7fb5eacSmrg	# systems and the destination file might be busy for other
499b7fb5eacSmrg	# reasons.  In this case, the final cleanup might fail but the new
500b7fb5eacSmrg	# file should still install successfully.
501b7fb5eacSmrg	{
502b7fb5eacSmrg	  test ! -f "$dst" ||
503b7fb5eacSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504b7fb5eacSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505b7fb5eacSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506b7fb5eacSmrg	  } ||
507b7fb5eacSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
508b7fb5eacSmrg	    (exit 1); exit 1
509b7fb5eacSmrg	  }
510b7fb5eacSmrg	} &&
511b7fb5eacSmrg
512b7fb5eacSmrg	# Now rename the file to the real destination.
513b7fb5eacSmrg	$doit $mvcmd "$dsttmp" "$dst"
514b7fb5eacSmrg      }
515b7fb5eacSmrg    fi || exit 1
5161a30de1fSmrg
5171a30de1fSmrg    trap '' 0
5181a30de1fSmrg  fi
5191a30de1fSmrgdone
5201a30de1fSmrg
5211a30de1fSmrg# Local variables:
5221a30de1fSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5231a30de1fSmrg# time-stamp-start: "scriptversion="
5241a30de1fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525b7fb5eacSmrg# time-stamp-time-zone: "UTC"
526b7fb5eacSmrg# time-stamp-end: "; # UTC"
5271a30de1fSmrg# End:
528