install-sh revision cf503b78
115fb4814Smrg#!/bin/sh
215fb4814Smrg# install - install a program, script, or datafile
315fb4814Smrg
4cf503b78Smrgscriptversion=2011-11-20.07; # UTC
515fb4814Smrg
615fb4814Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
715fb4814Smrg# later released in X11R6 (xc/config/util/install.sh) with the
815fb4814Smrg# following copyright and license.
915fb4814Smrg#
1015fb4814Smrg# Copyright (C) 1994 X Consortium
1115fb4814Smrg#
1215fb4814Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1315fb4814Smrg# of this software and associated documentation files (the "Software"), to
1415fb4814Smrg# deal in the Software without restriction, including without limitation the
1515fb4814Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1615fb4814Smrg# sell copies of the Software, and to permit persons to whom the Software is
1715fb4814Smrg# furnished to do so, subject to the following conditions:
1815fb4814Smrg#
1915fb4814Smrg# The above copyright notice and this permission notice shall be included in
2015fb4814Smrg# all copies or substantial portions of the Software.
2115fb4814Smrg#
2215fb4814Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2315fb4814Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2415fb4814Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2515fb4814Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2615fb4814Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2715fb4814Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2815fb4814Smrg#
2915fb4814Smrg# Except as contained in this notice, the name of the X Consortium shall not
3015fb4814Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3115fb4814Smrg# ings in this Software without prior written authorization from the X Consor-
3215fb4814Smrg# tium.
3315fb4814Smrg#
3415fb4814Smrg#
3515fb4814Smrg# FSF changes to this file are in the public domain.
3615fb4814Smrg#
3715fb4814Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38cf503b78Smrg# 'make' implicit rules from creating a file called install from it
3915fb4814Smrg# when there is no Makefile.
4015fb4814Smrg#
4115fb4814Smrg# This script is compatible with the BSD install script, but was written
42de78e416Smrg# from scratch.
43de78e416Smrg
44de78e416Smrgnl='
45de78e416Smrg'
46de78e416SmrgIFS=" ""	$nl"
4715fb4814Smrg
4815fb4814Smrg# set DOITPROG to echo to test this script
4915fb4814Smrg
5015fb4814Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51b4d38c65Smrgdoit=${DOITPROG-}
52de78e416Smrgif test -z "$doit"; then
53de78e416Smrg  doit_exec=exec
54de78e416Smrgelse
55de78e416Smrg  doit_exec=$doit
56de78e416Smrgfi
5715fb4814Smrg
58de78e416Smrg# Put in absolute file names if you don't have them in your path;
59de78e416Smrg# or use environment vars.
6015fb4814Smrg
61b4d38c65Smrgchgrpprog=${CHGRPPROG-chgrp}
62b4d38c65Smrgchmodprog=${CHMODPROG-chmod}
63b4d38c65Smrgchownprog=${CHOWNPROG-chown}
64b4d38c65Smrgcmpprog=${CMPPROG-cmp}
65b4d38c65Smrgcpprog=${CPPROG-cp}
66b4d38c65Smrgmkdirprog=${MKDIRPROG-mkdir}
67b4d38c65Smrgmvprog=${MVPROG-mv}
68b4d38c65Smrgrmprog=${RMPROG-rm}
69b4d38c65Smrgstripprog=${STRIPPROG-strip}
70b4d38c65Smrg
71b4d38c65Smrgposix_glob='?'
72b4d38c65Smrginitialize_posix_glob='
73b4d38c65Smrg  test "$posix_glob" != "?" || {
74b4d38c65Smrg    if (set -f) 2>/dev/null; then
75b4d38c65Smrg      posix_glob=
76b4d38c65Smrg    else
77b4d38c65Smrg      posix_glob=:
78b4d38c65Smrg    fi
79b4d38c65Smrg  }
80b4d38c65Smrg'
8115fb4814Smrg
82de78e416Smrgposix_mkdir=
83de78e416Smrg
84de78e416Smrg# Desired mode of installed file.
85de78e416Smrgmode=0755
86de78e416Smrg
87b4d38c65Smrgchgrpcmd=
88de78e416Smrgchmodcmd=$chmodprog
8915fb4814Smrgchowncmd=
90b4d38c65Smrgmvcmd=$mvprog
91b534f209Smrgrmcmd="$rmprog -f"
92b4d38c65Smrgstripcmd=
93b4d38c65Smrg
9415fb4814Smrgsrc=
9515fb4814Smrgdst=
9615fb4814Smrgdir_arg=
97b4d38c65Smrgdst_arg=
98b4d38c65Smrg
99b4d38c65Smrgcopy_on_change=false
10015fb4814Smrgno_target_directory=
10115fb4814Smrg
102b4d38c65Smrgusage="\
103b4d38c65SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10415fb4814Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10515fb4814Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10615fb4814Smrg   or: $0 [OPTION]... -d DIRECTORIES...
10715fb4814Smrg
10815fb4814SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10915fb4814SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11015fb4814SmrgIn the 4th, create DIRECTORIES.
11115fb4814Smrg
11215fb4814SmrgOptions:
113b4d38c65Smrg     --help     display this help and exit.
114b4d38c65Smrg     --version  display version info and exit.
115b4d38c65Smrg
116b4d38c65Smrg  -c            (ignored)
117b4d38c65Smrg  -C            install only if different (preserve the last data modification time)
118b4d38c65Smrg  -d            create directories instead of installing files.
119b4d38c65Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120b4d38c65Smrg  -m MODE       $chmodprog installed files to MODE.
121b4d38c65Smrg  -o USER       $chownprog installed files to USER.
122b4d38c65Smrg  -s            $stripprog installed files.
123b4d38c65Smrg  -t DIRECTORY  install into DIRECTORY.
124b4d38c65Smrg  -T            report an error if DSTFILE is a directory.
12515fb4814Smrg
12615fb4814SmrgEnvironment variables override the default commands:
127b4d38c65Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128b4d38c65Smrg  RMPROG STRIPPROG
12915fb4814Smrg"
13015fb4814Smrg
131de78e416Smrgwhile test $# -ne 0; do
13215fb4814Smrg  case $1 in
133b4d38c65Smrg    -c) ;;
13415fb4814Smrg
135b4d38c65Smrg    -C) copy_on_change=true;;
136b4d38c65Smrg
137b4d38c65Smrg    -d) dir_arg=true;;
13815fb4814Smrg
13915fb4814Smrg    -g) chgrpcmd="$chgrpprog $2"
140b4d38c65Smrg	shift;;
14115fb4814Smrg
14215fb4814Smrg    --help) echo "$usage"; exit $?;;
14315fb4814Smrg
144de78e416Smrg    -m) mode=$2
145de78e416Smrg	case $mode in
146de78e416Smrg	  *' '* | *'	'* | *'
147de78e416Smrg'*	  | *'*'* | *'?'* | *'['*)
148de78e416Smrg	    echo "$0: invalid mode: $mode" >&2
149de78e416Smrg	    exit 1;;
150de78e416Smrg	esac
151b4d38c65Smrg	shift;;
15215fb4814Smrg
15315fb4814Smrg    -o) chowncmd="$chownprog $2"
154b4d38c65Smrg	shift;;
15515fb4814Smrg
156b4d38c65Smrg    -s) stripcmd=$stripprog;;
15715fb4814Smrg
158b4d38c65Smrg    -t) dst_arg=$2
159cf503b78Smrg	# Protect names problematic for 'test' and other utilities.
160cf503b78Smrg	case $dst_arg in
161cf503b78Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162cf503b78Smrg	esac
163b4d38c65Smrg	shift;;
16415fb4814Smrg
165b4d38c65Smrg    -T) no_target_directory=true;;
16615fb4814Smrg
16715fb4814Smrg    --version) echo "$0 $scriptversion"; exit $?;;
16815fb4814Smrg
169de78e416Smrg    --)	shift
17015fb4814Smrg	break;;
171de78e416Smrg
172de78e416Smrg    -*)	echo "$0: invalid option: $1" >&2
173de78e416Smrg	exit 1;;
174de78e416Smrg
175de78e416Smrg    *)  break;;
17615fb4814Smrg  esac
177b4d38c65Smrg  shift
17815fb4814Smrgdone
17915fb4814Smrg
180b4d38c65Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181de78e416Smrg  # When -d is used, all remaining arguments are directories to create.
182de78e416Smrg  # When -t is used, the destination is already specified.
183de78e416Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184de78e416Smrg  for arg
185de78e416Smrg  do
186b4d38c65Smrg    if test -n "$dst_arg"; then
187de78e416Smrg      # $@ is not empty: it contains at least $arg.
188b4d38c65Smrg      set fnord "$@" "$dst_arg"
189de78e416Smrg      shift # fnord
190de78e416Smrg    fi
191de78e416Smrg    shift # arg
192b4d38c65Smrg    dst_arg=$arg
193cf503b78Smrg    # Protect names problematic for 'test' and other utilities.
194cf503b78Smrg    case $dst_arg in
195cf503b78Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196cf503b78Smrg    esac
197de78e416Smrg  done
198de78e416Smrgfi
199de78e416Smrg
200de78e416Smrgif test $# -eq 0; then
20115fb4814Smrg  if test -z "$dir_arg"; then
20215fb4814Smrg    echo "$0: no input file specified." >&2
20315fb4814Smrg    exit 1
20415fb4814Smrg  fi
205cf503b78Smrg  # It's OK to call 'install-sh -d' without argument.
20615fb4814Smrg  # This can happen when creating conditional directories.
20715fb4814Smrg  exit 0
20815fb4814Smrgfi
20915fb4814Smrg
210de78e416Smrgif test -z "$dir_arg"; then
211cf503b78Smrg  do_exit='(exit $ret); exit $ret'
212cf503b78Smrg  trap "ret=129; $do_exit" 1
213cf503b78Smrg  trap "ret=130; $do_exit" 2
214cf503b78Smrg  trap "ret=141; $do_exit" 13
215cf503b78Smrg  trap "ret=143; $do_exit" 15
216de78e416Smrg
217de78e416Smrg  # Set umask so as not to create temps with too-generous modes.
218de78e416Smrg  # However, 'strip' requires both read and write access to temps.
219de78e416Smrg  case $mode in
220de78e416Smrg    # Optimize common cases.
221de78e416Smrg    *644) cp_umask=133;;
222de78e416Smrg    *755) cp_umask=22;;
223de78e416Smrg
224de78e416Smrg    *[0-7])
225de78e416Smrg      if test -z "$stripcmd"; then
226de78e416Smrg	u_plus_rw=
227de78e416Smrg      else
228de78e416Smrg	u_plus_rw='% 200'
229de78e416Smrg      fi
230de78e416Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231de78e416Smrg    *)
232de78e416Smrg      if test -z "$stripcmd"; then
233de78e416Smrg	u_plus_rw=
234de78e416Smrg      else
235de78e416Smrg	u_plus_rw=,u+rw
236de78e416Smrg      fi
237de78e416Smrg      cp_umask=$mode$u_plus_rw;;
238de78e416Smrg  esac
239de78e416Smrgfi
240de78e416Smrg
24115fb4814Smrgfor src
24215fb4814Smrgdo
243cf503b78Smrg  # Protect names problematic for 'test' and other utilities.
24415fb4814Smrg  case $src in
245cf503b78Smrg    -* | [=\(\)!]) src=./$src;;
24615fb4814Smrg  esac
24715fb4814Smrg
24815fb4814Smrg  if test -n "$dir_arg"; then
24915fb4814Smrg    dst=$src
250de78e416Smrg    dstdir=$dst
251de78e416Smrg    test -d "$dstdir"
252de78e416Smrg    dstdir_status=$?
25315fb4814Smrg  else
254de78e416Smrg
25515fb4814Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
25615fb4814Smrg    # might cause directories to be created, which would be especially bad
25715fb4814Smrg    # if $src (and thus $dsttmp) contains '*'.
25815fb4814Smrg    if test ! -f "$src" && test ! -d "$src"; then
25915fb4814Smrg      echo "$0: $src does not exist." >&2
26015fb4814Smrg      exit 1
26115fb4814Smrg    fi
26215fb4814Smrg
263b4d38c65Smrg    if test -z "$dst_arg"; then
26415fb4814Smrg      echo "$0: no destination specified." >&2
26515fb4814Smrg      exit 1
26615fb4814Smrg    fi
267b4d38c65Smrg    dst=$dst_arg
26815fb4814Smrg
26915fb4814Smrg    # If destination is a directory, append the input filename; won't work
27015fb4814Smrg    # if double slashes aren't ignored.
27115fb4814Smrg    if test -d "$dst"; then
27215fb4814Smrg      if test -n "$no_target_directory"; then
273b4d38c65Smrg	echo "$0: $dst_arg: Is a directory" >&2
27415fb4814Smrg	exit 1
27515fb4814Smrg      fi
276de78e416Smrg      dstdir=$dst
277de78e416Smrg      dst=$dstdir/`basename "$src"`
278de78e416Smrg      dstdir_status=0
279de78e416Smrg    else
280de78e416Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281de78e416Smrg      dstdir=`
282de78e416Smrg	(dirname "$dst") 2>/dev/null ||
283de78e416Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284de78e416Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
285de78e416Smrg	     X"$dst" : 'X\(//\)$' \| \
286de78e416Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287de78e416Smrg	echo X"$dst" |
288de78e416Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289de78e416Smrg		   s//\1/
290de78e416Smrg		   q
291de78e416Smrg		 }
292de78e416Smrg		 /^X\(\/\/\)[^/].*/{
293de78e416Smrg		   s//\1/
294de78e416Smrg		   q
295de78e416Smrg		 }
296de78e416Smrg		 /^X\(\/\/\)$/{
297de78e416Smrg		   s//\1/
298de78e416Smrg		   q
299de78e416Smrg		 }
300de78e416Smrg		 /^X\(\/\).*/{
301de78e416Smrg		   s//\1/
302de78e416Smrg		   q
303de78e416Smrg		 }
304de78e416Smrg		 s/.*/./; q'
305de78e416Smrg      `
306de78e416Smrg
307de78e416Smrg      test -d "$dstdir"
308de78e416Smrg      dstdir_status=$?
30915fb4814Smrg    fi
31015fb4814Smrg  fi
31115fb4814Smrg
312de78e416Smrg  obsolete_mkdir_used=false
313de78e416Smrg
314de78e416Smrg  if test $dstdir_status != 0; then
315de78e416Smrg    case $posix_mkdir in
316de78e416Smrg      '')
317de78e416Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
318de78e416Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
319de78e416Smrg	umask=`umask`
320de78e416Smrg	case $stripcmd.$umask in
321de78e416Smrg	  # Optimize common cases.
322de78e416Smrg	  *[2367][2367]) mkdir_umask=$umask;;
323de78e416Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324de78e416Smrg
325de78e416Smrg	  *[0-7])
326de78e416Smrg	    mkdir_umask=`expr $umask + 22 \
327de78e416Smrg	      - $umask % 100 % 40 + $umask % 20 \
328de78e416Smrg	      - $umask % 10 % 4 + $umask % 2
329de78e416Smrg	    `;;
330de78e416Smrg	  *) mkdir_umask=$umask,go-w;;
331de78e416Smrg	esac
332de78e416Smrg
333de78e416Smrg	# With -d, create the new directory with the user-specified mode.
334de78e416Smrg	# Otherwise, rely on $mkdir_umask.
335de78e416Smrg	if test -n "$dir_arg"; then
336de78e416Smrg	  mkdir_mode=-m$mode
337de78e416Smrg	else
338de78e416Smrg	  mkdir_mode=
339de78e416Smrg	fi
340de78e416Smrg
341de78e416Smrg	posix_mkdir=false
342de78e416Smrg	case $umask in
343de78e416Smrg	  *[123567][0-7][0-7])
344de78e416Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345de78e416Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346de78e416Smrg	    ;;
347de78e416Smrg	  *)
348de78e416Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349de78e416Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350de78e416Smrg
351de78e416Smrg	    if (umask $mkdir_umask &&
352de78e416Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353de78e416Smrg	    then
354de78e416Smrg	      if test -z "$dir_arg" || {
355de78e416Smrg		   # Check for POSIX incompatibilities with -m.
356de78e416Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357cf503b78Smrg		   # other-writable bit of parent directory when it shouldn't.
358de78e416Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359de78e416Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360de78e416Smrg		   case $ls_ld_tmpdir in
361de78e416Smrg		     d????-?r-*) different_mode=700;;
362de78e416Smrg		     d????-?--*) different_mode=755;;
363de78e416Smrg		     *) false;;
364de78e416Smrg		   esac &&
365de78e416Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366de78e416Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367de78e416Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368de78e416Smrg		   }
369de78e416Smrg		 }
370de78e416Smrg	      then posix_mkdir=:
371de78e416Smrg	      fi
372de78e416Smrg	      rmdir "$tmpdir/d" "$tmpdir"
373de78e416Smrg	    else
374de78e416Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
375de78e416Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376de78e416Smrg	    fi
377de78e416Smrg	    trap '' 0;;
378de78e416Smrg	esac;;
379de78e416Smrg    esac
38015fb4814Smrg
381de78e416Smrg    if
382de78e416Smrg      $posix_mkdir && (
383de78e416Smrg	umask $mkdir_umask &&
384de78e416Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385de78e416Smrg      )
386de78e416Smrg    then :
387de78e416Smrg    else
38815fb4814Smrg
389de78e416Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390de78e416Smrg      # or it failed possibly due to a race condition.  Create the
391de78e416Smrg      # directory the slow way, step by step, checking for races as we go.
392de78e416Smrg
393de78e416Smrg      case $dstdir in
394b4d38c65Smrg	/*) prefix='/';;
395cf503b78Smrg	[-=\(\)!]*) prefix='./';;
396b4d38c65Smrg	*)  prefix='';;
397de78e416Smrg      esac
398de78e416Smrg
399b4d38c65Smrg      eval "$initialize_posix_glob"
400de78e416Smrg
401de78e416Smrg      oIFS=$IFS
402de78e416Smrg      IFS=/
403b4d38c65Smrg      $posix_glob set -f
404de78e416Smrg      set fnord $dstdir
40515fb4814Smrg      shift
406b4d38c65Smrg      $posix_glob set +f
407de78e416Smrg      IFS=$oIFS
408de78e416Smrg
409de78e416Smrg      prefixes=
410de78e416Smrg
411de78e416Smrg      for d
412de78e416Smrg      do
413cf503b78Smrg	test X"$d" = X && continue
414de78e416Smrg
415de78e416Smrg	prefix=$prefix$d
416de78e416Smrg	if test -d "$prefix"; then
417de78e416Smrg	  prefixes=
418de78e416Smrg	else
419de78e416Smrg	  if $posix_mkdir; then
420de78e416Smrg	    (umask=$mkdir_umask &&
421de78e416Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422de78e416Smrg	    # Don't fail if two instances are running concurrently.
423de78e416Smrg	    test -d "$prefix" || exit 1
424de78e416Smrg	  else
425de78e416Smrg	    case $prefix in
426de78e416Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427de78e416Smrg	      *) qprefix=$prefix;;
428de78e416Smrg	    esac
429de78e416Smrg	    prefixes="$prefixes '$qprefix'"
430de78e416Smrg	  fi
431de78e416Smrg	fi
432de78e416Smrg	prefix=$prefix/
433de78e416Smrg      done
434de78e416Smrg
435de78e416Smrg      if test -n "$prefixes"; then
436de78e416Smrg	# Don't fail if two instances are running concurrently.
437de78e416Smrg	(umask $mkdir_umask &&
438de78e416Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439de78e416Smrg	  test -d "$dstdir" || exit 1
440de78e416Smrg	obsolete_mkdir_used=true
44115fb4814Smrg      fi
442de78e416Smrg    fi
44315fb4814Smrg  fi
44415fb4814Smrg
44515fb4814Smrg  if test -n "$dir_arg"; then
446de78e416Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447de78e416Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448de78e416Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449de78e416Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
45015fb4814Smrg  else
45115fb4814Smrg
45215fb4814Smrg    # Make a couple of temp file names in the proper directory.
45315fb4814Smrg    dsttmp=$dstdir/_inst.$$_
45415fb4814Smrg    rmtmp=$dstdir/_rm.$$_
45515fb4814Smrg
45615fb4814Smrg    # Trap to clean up those temp files at exit.
45715fb4814Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45815fb4814Smrg
45915fb4814Smrg    # Copy the file name to the temp name.
460de78e416Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
46115fb4814Smrg
46215fb4814Smrg    # and set any options; do chmod last to preserve setuid bits.
46315fb4814Smrg    #
46415fb4814Smrg    # If any of these fail, we abort the whole thing.  If we want to
46515fb4814Smrg    # ignore errors from any of these, just make sure not to ignore
46615fb4814Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46715fb4814Smrg    #
468b4d38c65Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469b4d38c65Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470b4d38c65Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471b4d38c65Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472b4d38c65Smrg
473b4d38c65Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474b4d38c65Smrg    if $copy_on_change &&
475b4d38c65Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476b4d38c65Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477b4d38c65Smrg
478b4d38c65Smrg       eval "$initialize_posix_glob" &&
479b4d38c65Smrg       $posix_glob set -f &&
480b4d38c65Smrg       set X $old && old=:$2:$4:$5:$6 &&
481b4d38c65Smrg       set X $new && new=:$2:$4:$5:$6 &&
482b4d38c65Smrg       $posix_glob set +f &&
483b4d38c65Smrg
484b4d38c65Smrg       test "$old" = "$new" &&
485b4d38c65Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486b4d38c65Smrg    then
487b4d38c65Smrg      rm -f "$dsttmp"
488b4d38c65Smrg    else
489b4d38c65Smrg      # Rename the file to the real destination.
490b4d38c65Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491b4d38c65Smrg
492b4d38c65Smrg      # The rename failed, perhaps because mv can't rename something else
493b4d38c65Smrg      # to itself, or perhaps because mv is so ancient that it does not
494b4d38c65Smrg      # support -f.
495b4d38c65Smrg      {
496b4d38c65Smrg	# Now remove or move aside any old file at destination location.
497b4d38c65Smrg	# We try this two ways since rm can't unlink itself on some
498b4d38c65Smrg	# systems and the destination file might be busy for other
499b4d38c65Smrg	# reasons.  In this case, the final cleanup might fail but the new
500b4d38c65Smrg	# file should still install successfully.
501b4d38c65Smrg	{
502b4d38c65Smrg	  test ! -f "$dst" ||
503b4d38c65Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504b4d38c65Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505b4d38c65Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506b4d38c65Smrg	  } ||
507b4d38c65Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
508b4d38c65Smrg	    (exit 1); exit 1
509b4d38c65Smrg	  }
510b4d38c65Smrg	} &&
511b4d38c65Smrg
512b4d38c65Smrg	# Now rename the file to the real destination.
513b4d38c65Smrg	$doit $mvcmd "$dsttmp" "$dst"
514b4d38c65Smrg      }
515b4d38c65Smrg    fi || exit 1
51615fb4814Smrg
517de78e416Smrg    trap '' 0
518de78e416Smrg  fi
519de78e416Smrgdone
52015fb4814Smrg
52115fb4814Smrg# Local variables:
52215fb4814Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
52315fb4814Smrg# time-stamp-start: "scriptversion="
52415fb4814Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525b4d38c65Smrg# time-stamp-time-zone: "UTC"
526b4d38c65Smrg# time-stamp-end: "; # UTC"
52715fb4814Smrg# End:
528