install-sh revision 123e2cc7
17da8b7e3Smrg#!/bin/sh
27da8b7e3Smrg# install - install a program, script, or datafile
37da8b7e3Smrg
4123e2cc7Smrgscriptversion=2009-04-28.21; # UTC
57da8b7e3Smrg
67da8b7e3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77da8b7e3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87da8b7e3Smrg# following copyright and license.
97da8b7e3Smrg#
107da8b7e3Smrg# Copyright (C) 1994 X Consortium
117da8b7e3Smrg#
127da8b7e3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137da8b7e3Smrg# of this software and associated documentation files (the "Software"), to
147da8b7e3Smrg# deal in the Software without restriction, including without limitation the
157da8b7e3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167da8b7e3Smrg# sell copies of the Software, and to permit persons to whom the Software is
177da8b7e3Smrg# furnished to do so, subject to the following conditions:
187da8b7e3Smrg#
197da8b7e3Smrg# The above copyright notice and this permission notice shall be included in
207da8b7e3Smrg# all copies or substantial portions of the Software.
217da8b7e3Smrg#
227da8b7e3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237da8b7e3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247da8b7e3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257da8b7e3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267da8b7e3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277da8b7e3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287da8b7e3Smrg#
297da8b7e3Smrg# Except as contained in this notice, the name of the X Consortium shall not
307da8b7e3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317da8b7e3Smrg# ings in this Software without prior written authorization from the X Consor-
327da8b7e3Smrg# tium.
337da8b7e3Smrg#
347da8b7e3Smrg#
357da8b7e3Smrg# FSF changes to this file are in the public domain.
367da8b7e3Smrg#
377da8b7e3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
387da8b7e3Smrg# `make' implicit rules from creating a file called install from it
397da8b7e3Smrg# when there is no Makefile.
407da8b7e3Smrg#
417da8b7e3Smrg# This script is compatible with the BSD install script, but was written
42123e2cc7Smrg# from scratch.
43123e2cc7Smrg
44123e2cc7Smrgnl='
45123e2cc7Smrg'
46123e2cc7SmrgIFS=" ""	$nl"
477da8b7e3Smrg
487da8b7e3Smrg# set DOITPROG to echo to test this script
497da8b7e3Smrg
507da8b7e3Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51123e2cc7Smrgdoit=${DOITPROG-}
52123e2cc7Smrgif test -z "$doit"; then
53123e2cc7Smrg  doit_exec=exec
54123e2cc7Smrgelse
55123e2cc7Smrg  doit_exec=$doit
56123e2cc7Smrgfi
577da8b7e3Smrg
58123e2cc7Smrg# Put in absolute file names if you don't have them in your path;
59123e2cc7Smrg# or use environment vars.
60123e2cc7Smrg
61123e2cc7Smrgchgrpprog=${CHGRPPROG-chgrp}
62123e2cc7Smrgchmodprog=${CHMODPROG-chmod}
63123e2cc7Smrgchownprog=${CHOWNPROG-chown}
64123e2cc7Smrgcmpprog=${CMPPROG-cmp}
65123e2cc7Smrgcpprog=${CPPROG-cp}
66123e2cc7Smrgmkdirprog=${MKDIRPROG-mkdir}
67123e2cc7Smrgmvprog=${MVPROG-mv}
68123e2cc7Smrgrmprog=${RMPROG-rm}
69123e2cc7Smrgstripprog=${STRIPPROG-strip}
70123e2cc7Smrg
71123e2cc7Smrgposix_glob='?'
72123e2cc7Smrginitialize_posix_glob='
73123e2cc7Smrg  test "$posix_glob" != "?" || {
74123e2cc7Smrg    if (set -f) 2>/dev/null; then
75123e2cc7Smrg      posix_glob=
76123e2cc7Smrg    else
77123e2cc7Smrg      posix_glob=:
78123e2cc7Smrg    fi
79123e2cc7Smrg  }
80123e2cc7Smrg'
817da8b7e3Smrg
82123e2cc7Smrgposix_mkdir=
83123e2cc7Smrg
84123e2cc7Smrg# Desired mode of installed file.
85123e2cc7Smrgmode=0755
867da8b7e3Smrg
877da8b7e3Smrgchgrpcmd=
88123e2cc7Smrgchmodcmd=$chmodprog
89123e2cc7Smrgchowncmd=
90123e2cc7Smrgmvcmd=$mvprog
917da8b7e3Smrgrmcmd="$rmprog -f"
92123e2cc7Smrgstripcmd=
93123e2cc7Smrg
947da8b7e3Smrgsrc=
957da8b7e3Smrgdst=
967da8b7e3Smrgdir_arg=
97123e2cc7Smrgdst_arg=
98123e2cc7Smrg
99123e2cc7Smrgcopy_on_change=false
1007da8b7e3Smrgno_target_directory=
1017da8b7e3Smrg
102123e2cc7Smrgusage="\
103123e2cc7SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1047da8b7e3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1057da8b7e3Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1067da8b7e3Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1077da8b7e3Smrg
1087da8b7e3SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1097da8b7e3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1107da8b7e3SmrgIn the 4th, create DIRECTORIES.
1117da8b7e3Smrg
1127da8b7e3SmrgOptions:
113123e2cc7Smrg     --help     display this help and exit.
114123e2cc7Smrg     --version  display version info and exit.
115123e2cc7Smrg
116123e2cc7Smrg  -c            (ignored)
117123e2cc7Smrg  -C            install only if different (preserve the last data modification time)
118123e2cc7Smrg  -d            create directories instead of installing files.
119123e2cc7Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120123e2cc7Smrg  -m MODE       $chmodprog installed files to MODE.
121123e2cc7Smrg  -o USER       $chownprog installed files to USER.
122123e2cc7Smrg  -s            $stripprog installed files.
123123e2cc7Smrg  -t DIRECTORY  install into DIRECTORY.
124123e2cc7Smrg  -T            report an error if DSTFILE is a directory.
1257da8b7e3Smrg
1267da8b7e3SmrgEnvironment variables override the default commands:
127123e2cc7Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128123e2cc7Smrg  RMPROG STRIPPROG
1297da8b7e3Smrg"
1307da8b7e3Smrg
131123e2cc7Smrgwhile test $# -ne 0; do
1327da8b7e3Smrg  case $1 in
133123e2cc7Smrg    -c) ;;
134123e2cc7Smrg
135123e2cc7Smrg    -C) copy_on_change=true;;
1367da8b7e3Smrg
137123e2cc7Smrg    -d) dir_arg=true;;
1387da8b7e3Smrg
1397da8b7e3Smrg    -g) chgrpcmd="$chgrpprog $2"
140123e2cc7Smrg	shift;;
1417da8b7e3Smrg
1427da8b7e3Smrg    --help) echo "$usage"; exit $?;;
1437da8b7e3Smrg
144123e2cc7Smrg    -m) mode=$2
145123e2cc7Smrg	case $mode in
146123e2cc7Smrg	  *' '* | *'	'* | *'
147123e2cc7Smrg'*	  | *'*'* | *'?'* | *'['*)
148123e2cc7Smrg	    echo "$0: invalid mode: $mode" >&2
149123e2cc7Smrg	    exit 1;;
150123e2cc7Smrg	esac
151123e2cc7Smrg	shift;;
1527da8b7e3Smrg
1537da8b7e3Smrg    -o) chowncmd="$chownprog $2"
154123e2cc7Smrg	shift;;
1557da8b7e3Smrg
156123e2cc7Smrg    -s) stripcmd=$stripprog;;
1577da8b7e3Smrg
158123e2cc7Smrg    -t) dst_arg=$2
159123e2cc7Smrg	shift;;
1607da8b7e3Smrg
161123e2cc7Smrg    -T) no_target_directory=true;;
1627da8b7e3Smrg
1637da8b7e3Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1647da8b7e3Smrg
165123e2cc7Smrg    --)	shift
1667da8b7e3Smrg	break;;
167123e2cc7Smrg
168123e2cc7Smrg    -*)	echo "$0: invalid option: $1" >&2
169123e2cc7Smrg	exit 1;;
170123e2cc7Smrg
171123e2cc7Smrg    *)  break;;
1727da8b7e3Smrg  esac
173123e2cc7Smrg  shift
1747da8b7e3Smrgdone
1757da8b7e3Smrg
176123e2cc7Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177123e2cc7Smrg  # When -d is used, all remaining arguments are directories to create.
178123e2cc7Smrg  # When -t is used, the destination is already specified.
179123e2cc7Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180123e2cc7Smrg  for arg
181123e2cc7Smrg  do
182123e2cc7Smrg    if test -n "$dst_arg"; then
183123e2cc7Smrg      # $@ is not empty: it contains at least $arg.
184123e2cc7Smrg      set fnord "$@" "$dst_arg"
185123e2cc7Smrg      shift # fnord
186123e2cc7Smrg    fi
187123e2cc7Smrg    shift # arg
188123e2cc7Smrg    dst_arg=$arg
189123e2cc7Smrg  done
190123e2cc7Smrgfi
191123e2cc7Smrg
192123e2cc7Smrgif test $# -eq 0; then
1937da8b7e3Smrg  if test -z "$dir_arg"; then
1947da8b7e3Smrg    echo "$0: no input file specified." >&2
1957da8b7e3Smrg    exit 1
1967da8b7e3Smrg  fi
1977da8b7e3Smrg  # It's OK to call `install-sh -d' without argument.
1987da8b7e3Smrg  # This can happen when creating conditional directories.
1997da8b7e3Smrg  exit 0
2007da8b7e3Smrgfi
2017da8b7e3Smrg
202123e2cc7Smrgif test -z "$dir_arg"; then
203123e2cc7Smrg  trap '(exit $?); exit' 1 2 13 15
204123e2cc7Smrg
205123e2cc7Smrg  # Set umask so as not to create temps with too-generous modes.
206123e2cc7Smrg  # However, 'strip' requires both read and write access to temps.
207123e2cc7Smrg  case $mode in
208123e2cc7Smrg    # Optimize common cases.
209123e2cc7Smrg    *644) cp_umask=133;;
210123e2cc7Smrg    *755) cp_umask=22;;
211123e2cc7Smrg
212123e2cc7Smrg    *[0-7])
213123e2cc7Smrg      if test -z "$stripcmd"; then
214123e2cc7Smrg	u_plus_rw=
215123e2cc7Smrg      else
216123e2cc7Smrg	u_plus_rw='% 200'
217123e2cc7Smrg      fi
218123e2cc7Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219123e2cc7Smrg    *)
220123e2cc7Smrg      if test -z "$stripcmd"; then
221123e2cc7Smrg	u_plus_rw=
222123e2cc7Smrg      else
223123e2cc7Smrg	u_plus_rw=,u+rw
224123e2cc7Smrg      fi
225123e2cc7Smrg      cp_umask=$mode$u_plus_rw;;
226123e2cc7Smrg  esac
227123e2cc7Smrgfi
228123e2cc7Smrg
2297da8b7e3Smrgfor src
2307da8b7e3Smrgdo
2317da8b7e3Smrg  # Protect names starting with `-'.
2327da8b7e3Smrg  case $src in
233123e2cc7Smrg    -*) src=./$src;;
2347da8b7e3Smrg  esac
2357da8b7e3Smrg
2367da8b7e3Smrg  if test -n "$dir_arg"; then
2377da8b7e3Smrg    dst=$src
238123e2cc7Smrg    dstdir=$dst
239123e2cc7Smrg    test -d "$dstdir"
240123e2cc7Smrg    dstdir_status=$?
2417da8b7e3Smrg  else
242123e2cc7Smrg
2437da8b7e3Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2447da8b7e3Smrg    # might cause directories to be created, which would be especially bad
2457da8b7e3Smrg    # if $src (and thus $dsttmp) contains '*'.
2467da8b7e3Smrg    if test ! -f "$src" && test ! -d "$src"; then
2477da8b7e3Smrg      echo "$0: $src does not exist." >&2
2487da8b7e3Smrg      exit 1
2497da8b7e3Smrg    fi
2507da8b7e3Smrg
251123e2cc7Smrg    if test -z "$dst_arg"; then
2527da8b7e3Smrg      echo "$0: no destination specified." >&2
2537da8b7e3Smrg      exit 1
2547da8b7e3Smrg    fi
2557da8b7e3Smrg
256123e2cc7Smrg    dst=$dst_arg
2577da8b7e3Smrg    # Protect names starting with `-'.
2587da8b7e3Smrg    case $dst in
259123e2cc7Smrg      -*) dst=./$dst;;
2607da8b7e3Smrg    esac
2617da8b7e3Smrg
2627da8b7e3Smrg    # If destination is a directory, append the input filename; won't work
2637da8b7e3Smrg    # if double slashes aren't ignored.
2647da8b7e3Smrg    if test -d "$dst"; then
2657da8b7e3Smrg      if test -n "$no_target_directory"; then
266123e2cc7Smrg	echo "$0: $dst_arg: Is a directory" >&2
2677da8b7e3Smrg	exit 1
2687da8b7e3Smrg      fi
269123e2cc7Smrg      dstdir=$dst
270123e2cc7Smrg      dst=$dstdir/`basename "$src"`
271123e2cc7Smrg      dstdir_status=0
272123e2cc7Smrg    else
273123e2cc7Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274123e2cc7Smrg      dstdir=`
275123e2cc7Smrg	(dirname "$dst") 2>/dev/null ||
276123e2cc7Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277123e2cc7Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
278123e2cc7Smrg	     X"$dst" : 'X\(//\)$' \| \
279123e2cc7Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280123e2cc7Smrg	echo X"$dst" |
281123e2cc7Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282123e2cc7Smrg		   s//\1/
283123e2cc7Smrg		   q
284123e2cc7Smrg		 }
285123e2cc7Smrg		 /^X\(\/\/\)[^/].*/{
286123e2cc7Smrg		   s//\1/
287123e2cc7Smrg		   q
288123e2cc7Smrg		 }
289123e2cc7Smrg		 /^X\(\/\/\)$/{
290123e2cc7Smrg		   s//\1/
291123e2cc7Smrg		   q
292123e2cc7Smrg		 }
293123e2cc7Smrg		 /^X\(\/\).*/{
294123e2cc7Smrg		   s//\1/
295123e2cc7Smrg		   q
296123e2cc7Smrg		 }
297123e2cc7Smrg		 s/.*/./; q'
298123e2cc7Smrg      `
299123e2cc7Smrg
300123e2cc7Smrg      test -d "$dstdir"
301123e2cc7Smrg      dstdir_status=$?
3027da8b7e3Smrg    fi
3037da8b7e3Smrg  fi
3047da8b7e3Smrg
305123e2cc7Smrg  obsolete_mkdir_used=false
306123e2cc7Smrg
307123e2cc7Smrg  if test $dstdir_status != 0; then
308123e2cc7Smrg    case $posix_mkdir in
309123e2cc7Smrg      '')
310123e2cc7Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
311123e2cc7Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
312123e2cc7Smrg	umask=`umask`
313123e2cc7Smrg	case $stripcmd.$umask in
314123e2cc7Smrg	  # Optimize common cases.
315123e2cc7Smrg	  *[2367][2367]) mkdir_umask=$umask;;
316123e2cc7Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317123e2cc7Smrg
318123e2cc7Smrg	  *[0-7])
319123e2cc7Smrg	    mkdir_umask=`expr $umask + 22 \
320123e2cc7Smrg	      - $umask % 100 % 40 + $umask % 20 \
321123e2cc7Smrg	      - $umask % 10 % 4 + $umask % 2
322123e2cc7Smrg	    `;;
323123e2cc7Smrg	  *) mkdir_umask=$umask,go-w;;
324123e2cc7Smrg	esac
325123e2cc7Smrg
326123e2cc7Smrg	# With -d, create the new directory with the user-specified mode.
327123e2cc7Smrg	# Otherwise, rely on $mkdir_umask.
328123e2cc7Smrg	if test -n "$dir_arg"; then
329123e2cc7Smrg	  mkdir_mode=-m$mode
330123e2cc7Smrg	else
331123e2cc7Smrg	  mkdir_mode=
332123e2cc7Smrg	fi
333123e2cc7Smrg
334123e2cc7Smrg	posix_mkdir=false
335123e2cc7Smrg	case $umask in
336123e2cc7Smrg	  *[123567][0-7][0-7])
337123e2cc7Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338123e2cc7Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339123e2cc7Smrg	    ;;
340123e2cc7Smrg	  *)
341123e2cc7Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342123e2cc7Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343123e2cc7Smrg
344123e2cc7Smrg	    if (umask $mkdir_umask &&
345123e2cc7Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346123e2cc7Smrg	    then
347123e2cc7Smrg	      if test -z "$dir_arg" || {
348123e2cc7Smrg		   # Check for POSIX incompatibilities with -m.
349123e2cc7Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350123e2cc7Smrg		   # other-writeable bit of parent directory when it shouldn't.
351123e2cc7Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352123e2cc7Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353123e2cc7Smrg		   case $ls_ld_tmpdir in
354123e2cc7Smrg		     d????-?r-*) different_mode=700;;
355123e2cc7Smrg		     d????-?--*) different_mode=755;;
356123e2cc7Smrg		     *) false;;
357123e2cc7Smrg		   esac &&
358123e2cc7Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359123e2cc7Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360123e2cc7Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361123e2cc7Smrg		   }
362123e2cc7Smrg		 }
363123e2cc7Smrg	      then posix_mkdir=:
364123e2cc7Smrg	      fi
365123e2cc7Smrg	      rmdir "$tmpdir/d" "$tmpdir"
366123e2cc7Smrg	    else
367123e2cc7Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
368123e2cc7Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369123e2cc7Smrg	    fi
370123e2cc7Smrg	    trap '' 0;;
371123e2cc7Smrg	esac;;
372123e2cc7Smrg    esac
3737da8b7e3Smrg
374123e2cc7Smrg    if
375123e2cc7Smrg      $posix_mkdir && (
376123e2cc7Smrg	umask $mkdir_umask &&
377123e2cc7Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378123e2cc7Smrg      )
379123e2cc7Smrg    then :
380123e2cc7Smrg    else
3817da8b7e3Smrg
382123e2cc7Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383123e2cc7Smrg      # or it failed possibly due to a race condition.  Create the
384123e2cc7Smrg      # directory the slow way, step by step, checking for races as we go.
3857da8b7e3Smrg
386123e2cc7Smrg      case $dstdir in
387123e2cc7Smrg	/*) prefix='/';;
388123e2cc7Smrg	-*) prefix='./';;
389123e2cc7Smrg	*)  prefix='';;
390123e2cc7Smrg      esac
3917da8b7e3Smrg
392123e2cc7Smrg      eval "$initialize_posix_glob"
3937da8b7e3Smrg
394123e2cc7Smrg      oIFS=$IFS
395123e2cc7Smrg      IFS=/
396123e2cc7Smrg      $posix_glob set -f
397123e2cc7Smrg      set fnord $dstdir
3987da8b7e3Smrg      shift
399123e2cc7Smrg      $posix_glob set +f
400123e2cc7Smrg      IFS=$oIFS
401123e2cc7Smrg
402123e2cc7Smrg      prefixes=
403123e2cc7Smrg
404123e2cc7Smrg      for d
405123e2cc7Smrg      do
406123e2cc7Smrg	test -z "$d" && continue
407123e2cc7Smrg
408123e2cc7Smrg	prefix=$prefix$d
409123e2cc7Smrg	if test -d "$prefix"; then
410123e2cc7Smrg	  prefixes=
411123e2cc7Smrg	else
412123e2cc7Smrg	  if $posix_mkdir; then
413123e2cc7Smrg	    (umask=$mkdir_umask &&
414123e2cc7Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415123e2cc7Smrg	    # Don't fail if two instances are running concurrently.
416123e2cc7Smrg	    test -d "$prefix" || exit 1
417123e2cc7Smrg	  else
418123e2cc7Smrg	    case $prefix in
419123e2cc7Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420123e2cc7Smrg	      *) qprefix=$prefix;;
421123e2cc7Smrg	    esac
422123e2cc7Smrg	    prefixes="$prefixes '$qprefix'"
423123e2cc7Smrg	  fi
424123e2cc7Smrg	fi
425123e2cc7Smrg	prefix=$prefix/
426123e2cc7Smrg      done
427123e2cc7Smrg
428123e2cc7Smrg      if test -n "$prefixes"; then
429123e2cc7Smrg	# Don't fail if two instances are running concurrently.
430123e2cc7Smrg	(umask $mkdir_umask &&
431123e2cc7Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432123e2cc7Smrg	  test -d "$dstdir" || exit 1
433123e2cc7Smrg	obsolete_mkdir_used=true
4347da8b7e3Smrg      fi
435123e2cc7Smrg    fi
4367da8b7e3Smrg  fi
4377da8b7e3Smrg
4387da8b7e3Smrg  if test -n "$dir_arg"; then
439123e2cc7Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440123e2cc7Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441123e2cc7Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442123e2cc7Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4437da8b7e3Smrg  else
4447da8b7e3Smrg
4457da8b7e3Smrg    # Make a couple of temp file names in the proper directory.
4467da8b7e3Smrg    dsttmp=$dstdir/_inst.$$_
4477da8b7e3Smrg    rmtmp=$dstdir/_rm.$$_
4487da8b7e3Smrg
4497da8b7e3Smrg    # Trap to clean up those temp files at exit.
4507da8b7e3Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4517da8b7e3Smrg
4527da8b7e3Smrg    # Copy the file name to the temp name.
453123e2cc7Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4547da8b7e3Smrg
4557da8b7e3Smrg    # and set any options; do chmod last to preserve setuid bits.
4567da8b7e3Smrg    #
4577da8b7e3Smrg    # If any of these fail, we abort the whole thing.  If we want to
4587da8b7e3Smrg    # ignore errors from any of these, just make sure not to ignore
4597da8b7e3Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4607da8b7e3Smrg    #
461123e2cc7Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462123e2cc7Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463123e2cc7Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464123e2cc7Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465123e2cc7Smrg
466123e2cc7Smrg    # If -C, don't bother to copy if it wouldn't change the file.
467123e2cc7Smrg    if $copy_on_change &&
468123e2cc7Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469123e2cc7Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470123e2cc7Smrg
471123e2cc7Smrg       eval "$initialize_posix_glob" &&
472123e2cc7Smrg       $posix_glob set -f &&
473123e2cc7Smrg       set X $old && old=:$2:$4:$5:$6 &&
474123e2cc7Smrg       set X $new && new=:$2:$4:$5:$6 &&
475123e2cc7Smrg       $posix_glob set +f &&
476123e2cc7Smrg
477123e2cc7Smrg       test "$old" = "$new" &&
478123e2cc7Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479123e2cc7Smrg    then
480123e2cc7Smrg      rm -f "$dsttmp"
481123e2cc7Smrg    else
482123e2cc7Smrg      # Rename the file to the real destination.
483123e2cc7Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484123e2cc7Smrg
485123e2cc7Smrg      # The rename failed, perhaps because mv can't rename something else
486123e2cc7Smrg      # to itself, or perhaps because mv is so ancient that it does not
487123e2cc7Smrg      # support -f.
488123e2cc7Smrg      {
489123e2cc7Smrg	# Now remove or move aside any old file at destination location.
490123e2cc7Smrg	# We try this two ways since rm can't unlink itself on some
491123e2cc7Smrg	# systems and the destination file might be busy for other
492123e2cc7Smrg	# reasons.  In this case, the final cleanup might fail but the new
493123e2cc7Smrg	# file should still install successfully.
494123e2cc7Smrg	{
495123e2cc7Smrg	  test ! -f "$dst" ||
496123e2cc7Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497123e2cc7Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498123e2cc7Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499123e2cc7Smrg	  } ||
500123e2cc7Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
501123e2cc7Smrg	    (exit 1); exit 1
502123e2cc7Smrg	  }
503123e2cc7Smrg	} &&
504123e2cc7Smrg
505123e2cc7Smrg	# Now rename the file to the real destination.
506123e2cc7Smrg	$doit $mvcmd "$dsttmp" "$dst"
507123e2cc7Smrg      }
508123e2cc7Smrg    fi || exit 1
509123e2cc7Smrg
510123e2cc7Smrg    trap '' 0
511123e2cc7Smrg  fi
5127da8b7e3Smrgdone
5137da8b7e3Smrg
5147da8b7e3Smrg# Local variables:
5157da8b7e3Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5167da8b7e3Smrg# time-stamp-start: "scriptversion="
5177da8b7e3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518123e2cc7Smrg# time-stamp-time-zone: "UTC"
519123e2cc7Smrg# time-stamp-end: "; # UTC"
5207da8b7e3Smrg# End:
521