install-sh revision d31dbc53
132b578d3Smrg#!/bin/sh
232b578d3Smrg# install - install a program, script, or datafile
332b578d3Smrg
4d31dbc53Smrgscriptversion=2006-10-14.15
532b578d3Smrg
632b578d3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
732b578d3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
832b578d3Smrg# following copyright and license.
932b578d3Smrg#
1032b578d3Smrg# Copyright (C) 1994 X Consortium
1132b578d3Smrg#
1232b578d3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1332b578d3Smrg# of this software and associated documentation files (the "Software"), to
1432b578d3Smrg# deal in the Software without restriction, including without limitation the
1532b578d3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1632b578d3Smrg# sell copies of the Software, and to permit persons to whom the Software is
1732b578d3Smrg# furnished to do so, subject to the following conditions:
1832b578d3Smrg#
1932b578d3Smrg# The above copyright notice and this permission notice shall be included in
2032b578d3Smrg# all copies or substantial portions of the Software.
2132b578d3Smrg#
2232b578d3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2332b578d3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2432b578d3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2532b578d3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2632b578d3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2732b578d3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2832b578d3Smrg#
2932b578d3Smrg# Except as contained in this notice, the name of the X Consortium shall not
3032b578d3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3132b578d3Smrg# ings in this Software without prior written authorization from the X Consor-
3232b578d3Smrg# tium.
3332b578d3Smrg#
3432b578d3Smrg#
3532b578d3Smrg# FSF changes to this file are in the public domain.
3632b578d3Smrg#
3732b578d3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3832b578d3Smrg# `make' implicit rules from creating a file called install from it
3932b578d3Smrg# when there is no Makefile.
4032b578d3Smrg#
4132b578d3Smrg# This script is compatible with the BSD install script, but was written
4232b578d3Smrg# from scratch.
4332b578d3Smrg
4432b578d3Smrgnl='
4532b578d3Smrg'
4632b578d3SmrgIFS=" ""	$nl"
4732b578d3Smrg
4832b578d3Smrg# set DOITPROG to echo to test this script
4932b578d3Smrg
5032b578d3Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51d31dbc53Smrgdoit="${DOITPROG-}"
5232b578d3Smrgif test -z "$doit"; then
5332b578d3Smrg  doit_exec=exec
5432b578d3Smrgelse
5532b578d3Smrg  doit_exec=$doit
5632b578d3Smrgfi
5732b578d3Smrg
5832b578d3Smrg# Put in absolute file names if you don't have them in your path;
5932b578d3Smrg# or use environment vars.
6032b578d3Smrg
61d31dbc53Smrgmvprog="${MVPROG-mv}"
62d31dbc53Smrgcpprog="${CPPROG-cp}"
63d31dbc53Smrgchmodprog="${CHMODPROG-chmod}"
64d31dbc53Smrgchownprog="${CHOWNPROG-chown}"
65d31dbc53Smrgchgrpprog="${CHGRPPROG-chgrp}"
66d31dbc53Smrgstripprog="${STRIPPROG-strip}"
67d31dbc53Smrgrmprog="${RMPROG-rm}"
68d31dbc53Smrgmkdirprog="${MKDIRPROG-mkdir}"
6932b578d3Smrg
70d31dbc53Smrgposix_glob=
7132b578d3Smrgposix_mkdir=
7232b578d3Smrg
7332b578d3Smrg# Desired mode of installed file.
7432b578d3Smrgmode=0755
7532b578d3Smrg
7632b578d3Smrgchmodcmd=$chmodprog
7732b578d3Smrgchowncmd=
78d31dbc53Smrgchgrpcmd=
7932b578d3Smrgstripcmd=
80d31dbc53Smrgrmcmd="$rmprog -f"
81d31dbc53Smrgmvcmd="$mvprog"
8232b578d3Smrgsrc=
8332b578d3Smrgdst=
8432b578d3Smrgdir_arg=
85d31dbc53Smrgdstarg=
8632b578d3Smrgno_target_directory=
8732b578d3Smrg
88d31dbc53Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8932b578d3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9032b578d3Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9132b578d3Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9232b578d3Smrg
9332b578d3SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9432b578d3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9532b578d3SmrgIn the 4th, create DIRECTORIES.
9632b578d3Smrg
9732b578d3SmrgOptions:
98d31dbc53Smrg-c         (ignored)
99d31dbc53Smrg-d         create directories instead of installing files.
100d31dbc53Smrg-g GROUP   $chgrpprog installed files to GROUP.
101d31dbc53Smrg-m MODE    $chmodprog installed files to MODE.
102d31dbc53Smrg-o USER    $chownprog installed files to USER.
103d31dbc53Smrg-s         $stripprog installed files.
104d31dbc53Smrg-t DIRECTORY  install into DIRECTORY.
105d31dbc53Smrg-T         report an error if DSTFILE is a directory.
106d31dbc53Smrg--help     display this help and exit.
107d31dbc53Smrg--version  display version info and exit.
10832b578d3Smrg
10932b578d3SmrgEnvironment variables override the default commands:
110d31dbc53Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
11132b578d3Smrg"
11232b578d3Smrg
11332b578d3Smrgwhile test $# -ne 0; do
11432b578d3Smrg  case $1 in
115d31dbc53Smrg    -c) shift
116d31dbc53Smrg        continue;;
11732b578d3Smrg
118d31dbc53Smrg    -d) dir_arg=true
119d31dbc53Smrg        shift
120d31dbc53Smrg        continue;;
12132b578d3Smrg
12232b578d3Smrg    -g) chgrpcmd="$chgrpprog $2"
123d31dbc53Smrg        shift
124d31dbc53Smrg        shift
125d31dbc53Smrg        continue;;
12632b578d3Smrg
12732b578d3Smrg    --help) echo "$usage"; exit $?;;
12832b578d3Smrg
12932b578d3Smrg    -m) mode=$2
130d31dbc53Smrg        shift
131d31dbc53Smrg        shift
13232b578d3Smrg	case $mode in
13332b578d3Smrg	  *' '* | *'	'* | *'
13432b578d3Smrg'*	  | *'*'* | *'?'* | *'['*)
13532b578d3Smrg	    echo "$0: invalid mode: $mode" >&2
13632b578d3Smrg	    exit 1;;
13732b578d3Smrg	esac
138d31dbc53Smrg        continue;;
13932b578d3Smrg
14032b578d3Smrg    -o) chowncmd="$chownprog $2"
141d31dbc53Smrg        shift
142d31dbc53Smrg        shift
143d31dbc53Smrg        continue;;
14432b578d3Smrg
145d31dbc53Smrg    -s) stripcmd=$stripprog
146d31dbc53Smrg        shift
147d31dbc53Smrg        continue;;
14832b578d3Smrg
149d31dbc53Smrg    -t) dstarg=$2
150d31dbc53Smrg	shift
151d31dbc53Smrg	shift
152d31dbc53Smrg	continue;;
15332b578d3Smrg
154d31dbc53Smrg    -T) no_target_directory=true
155d31dbc53Smrg	shift
156d31dbc53Smrg	continue;;
15732b578d3Smrg
15832b578d3Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15932b578d3Smrg
16032b578d3Smrg    --)	shift
16132b578d3Smrg	break;;
16232b578d3Smrg
16332b578d3Smrg    -*)	echo "$0: invalid option: $1" >&2
16432b578d3Smrg	exit 1;;
16532b578d3Smrg
16632b578d3Smrg    *)  break;;
16732b578d3Smrg  esac
16832b578d3Smrgdone
16932b578d3Smrg
170d31dbc53Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
17132b578d3Smrg  # When -d is used, all remaining arguments are directories to create.
17232b578d3Smrg  # When -t is used, the destination is already specified.
17332b578d3Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
17432b578d3Smrg  for arg
17532b578d3Smrg  do
176d31dbc53Smrg    if test -n "$dstarg"; then
17732b578d3Smrg      # $@ is not empty: it contains at least $arg.
178d31dbc53Smrg      set fnord "$@" "$dstarg"
17932b578d3Smrg      shift # fnord
18032b578d3Smrg    fi
18132b578d3Smrg    shift # arg
182d31dbc53Smrg    dstarg=$arg
18332b578d3Smrg  done
18432b578d3Smrgfi
18532b578d3Smrg
18632b578d3Smrgif test $# -eq 0; then
18732b578d3Smrg  if test -z "$dir_arg"; then
18832b578d3Smrg    echo "$0: no input file specified." >&2
18932b578d3Smrg    exit 1
19032b578d3Smrg  fi
19132b578d3Smrg  # It's OK to call `install-sh -d' without argument.
19232b578d3Smrg  # This can happen when creating conditional directories.
19332b578d3Smrg  exit 0
19432b578d3Smrgfi
19532b578d3Smrg
19632b578d3Smrgif test -z "$dir_arg"; then
19732b578d3Smrg  trap '(exit $?); exit' 1 2 13 15
19832b578d3Smrg
19932b578d3Smrg  # Set umask so as not to create temps with too-generous modes.
20032b578d3Smrg  # However, 'strip' requires both read and write access to temps.
20132b578d3Smrg  case $mode in
20232b578d3Smrg    # Optimize common cases.
20332b578d3Smrg    *644) cp_umask=133;;
20432b578d3Smrg    *755) cp_umask=22;;
20532b578d3Smrg
20632b578d3Smrg    *[0-7])
20732b578d3Smrg      if test -z "$stripcmd"; then
20832b578d3Smrg	u_plus_rw=
20932b578d3Smrg      else
21032b578d3Smrg	u_plus_rw='% 200'
21132b578d3Smrg      fi
21232b578d3Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21332b578d3Smrg    *)
21432b578d3Smrg      if test -z "$stripcmd"; then
21532b578d3Smrg	u_plus_rw=
21632b578d3Smrg      else
21732b578d3Smrg	u_plus_rw=,u+rw
21832b578d3Smrg      fi
21932b578d3Smrg      cp_umask=$mode$u_plus_rw;;
22032b578d3Smrg  esac
22132b578d3Smrgfi
22232b578d3Smrg
22332b578d3Smrgfor src
22432b578d3Smrgdo
22532b578d3Smrg  # Protect names starting with `-'.
22632b578d3Smrg  case $src in
227d31dbc53Smrg    -*) src=./$src ;;
22832b578d3Smrg  esac
22932b578d3Smrg
23032b578d3Smrg  if test -n "$dir_arg"; then
23132b578d3Smrg    dst=$src
23232b578d3Smrg    dstdir=$dst
23332b578d3Smrg    test -d "$dstdir"
23432b578d3Smrg    dstdir_status=$?
23532b578d3Smrg  else
23632b578d3Smrg
23732b578d3Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
23832b578d3Smrg    # might cause directories to be created, which would be especially bad
23932b578d3Smrg    # if $src (and thus $dsttmp) contains '*'.
24032b578d3Smrg    if test ! -f "$src" && test ! -d "$src"; then
24132b578d3Smrg      echo "$0: $src does not exist." >&2
24232b578d3Smrg      exit 1
24332b578d3Smrg    fi
24432b578d3Smrg
245d31dbc53Smrg    if test -z "$dstarg"; then
24632b578d3Smrg      echo "$0: no destination specified." >&2
24732b578d3Smrg      exit 1
24832b578d3Smrg    fi
24932b578d3Smrg
250d31dbc53Smrg    dst=$dstarg
25132b578d3Smrg    # Protect names starting with `-'.
25232b578d3Smrg    case $dst in
253d31dbc53Smrg      -*) dst=./$dst ;;
25432b578d3Smrg    esac
25532b578d3Smrg
25632b578d3Smrg    # If destination is a directory, append the input filename; won't work
25732b578d3Smrg    # if double slashes aren't ignored.
25832b578d3Smrg    if test -d "$dst"; then
25932b578d3Smrg      if test -n "$no_target_directory"; then
260d31dbc53Smrg	echo "$0: $dstarg: Is a directory" >&2
26132b578d3Smrg	exit 1
26232b578d3Smrg      fi
26332b578d3Smrg      dstdir=$dst
26432b578d3Smrg      dst=$dstdir/`basename "$src"`
26532b578d3Smrg      dstdir_status=0
26632b578d3Smrg    else
26732b578d3Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
26832b578d3Smrg      dstdir=`
26932b578d3Smrg	(dirname "$dst") 2>/dev/null ||
27032b578d3Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27132b578d3Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27232b578d3Smrg	     X"$dst" : 'X\(//\)$' \| \
27332b578d3Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
27432b578d3Smrg	echo X"$dst" |
27532b578d3Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
27632b578d3Smrg		   s//\1/
27732b578d3Smrg		   q
27832b578d3Smrg		 }
27932b578d3Smrg		 /^X\(\/\/\)[^/].*/{
28032b578d3Smrg		   s//\1/
28132b578d3Smrg		   q
28232b578d3Smrg		 }
28332b578d3Smrg		 /^X\(\/\/\)$/{
28432b578d3Smrg		   s//\1/
28532b578d3Smrg		   q
28632b578d3Smrg		 }
28732b578d3Smrg		 /^X\(\/\).*/{
28832b578d3Smrg		   s//\1/
28932b578d3Smrg		   q
29032b578d3Smrg		 }
29132b578d3Smrg		 s/.*/./; q'
29232b578d3Smrg      `
29332b578d3Smrg
29432b578d3Smrg      test -d "$dstdir"
29532b578d3Smrg      dstdir_status=$?
29632b578d3Smrg    fi
29732b578d3Smrg  fi
29832b578d3Smrg
29932b578d3Smrg  obsolete_mkdir_used=false
30032b578d3Smrg
30132b578d3Smrg  if test $dstdir_status != 0; then
30232b578d3Smrg    case $posix_mkdir in
30332b578d3Smrg      '')
30432b578d3Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
30532b578d3Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
30632b578d3Smrg	umask=`umask`
30732b578d3Smrg	case $stripcmd.$umask in
30832b578d3Smrg	  # Optimize common cases.
30932b578d3Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31032b578d3Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31132b578d3Smrg
31232b578d3Smrg	  *[0-7])
31332b578d3Smrg	    mkdir_umask=`expr $umask + 22 \
31432b578d3Smrg	      - $umask % 100 % 40 + $umask % 20 \
31532b578d3Smrg	      - $umask % 10 % 4 + $umask % 2
31632b578d3Smrg	    `;;
31732b578d3Smrg	  *) mkdir_umask=$umask,go-w;;
31832b578d3Smrg	esac
31932b578d3Smrg
32032b578d3Smrg	# With -d, create the new directory with the user-specified mode.
32132b578d3Smrg	# Otherwise, rely on $mkdir_umask.
32232b578d3Smrg	if test -n "$dir_arg"; then
32332b578d3Smrg	  mkdir_mode=-m$mode
32432b578d3Smrg	else
32532b578d3Smrg	  mkdir_mode=
32632b578d3Smrg	fi
32732b578d3Smrg
32832b578d3Smrg	posix_mkdir=false
32932b578d3Smrg	case $umask in
33032b578d3Smrg	  *[123567][0-7][0-7])
33132b578d3Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33232b578d3Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33332b578d3Smrg	    ;;
33432b578d3Smrg	  *)
33532b578d3Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
33632b578d3Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
33732b578d3Smrg
33832b578d3Smrg	    if (umask $mkdir_umask &&
33932b578d3Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34032b578d3Smrg	    then
34132b578d3Smrg	      if test -z "$dir_arg" || {
34232b578d3Smrg		   # Check for POSIX incompatibilities with -m.
34332b578d3Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
34432b578d3Smrg		   # other-writeable bit of parent directory when it shouldn't.
34532b578d3Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
34632b578d3Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
34732b578d3Smrg		   case $ls_ld_tmpdir in
34832b578d3Smrg		     d????-?r-*) different_mode=700;;
34932b578d3Smrg		     d????-?--*) different_mode=755;;
35032b578d3Smrg		     *) false;;
35132b578d3Smrg		   esac &&
35232b578d3Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35332b578d3Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
35432b578d3Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
35532b578d3Smrg		   }
35632b578d3Smrg		 }
35732b578d3Smrg	      then posix_mkdir=:
35832b578d3Smrg	      fi
35932b578d3Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36032b578d3Smrg	    else
36132b578d3Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36232b578d3Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36332b578d3Smrg	    fi
36432b578d3Smrg	    trap '' 0;;
36532b578d3Smrg	esac;;
36632b578d3Smrg    esac
36732b578d3Smrg
36832b578d3Smrg    if
36932b578d3Smrg      $posix_mkdir && (
37032b578d3Smrg	umask $mkdir_umask &&
37132b578d3Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37232b578d3Smrg      )
37332b578d3Smrg    then :
37432b578d3Smrg    else
37532b578d3Smrg
37632b578d3Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
37732b578d3Smrg      # or it failed possibly due to a race condition.  Create the
37832b578d3Smrg      # directory the slow way, step by step, checking for races as we go.
37932b578d3Smrg
38032b578d3Smrg      case $dstdir in
381d31dbc53Smrg	/*) prefix=/ ;;
382d31dbc53Smrg	-*) prefix=./ ;;
383d31dbc53Smrg	*)  prefix= ;;
38432b578d3Smrg      esac
38532b578d3Smrg
386d31dbc53Smrg      case $posix_glob in
387d31dbc53Smrg        '')
388d31dbc53Smrg	  if (set -f) 2>/dev/null; then
389d31dbc53Smrg	    posix_glob=true
390d31dbc53Smrg	  else
391d31dbc53Smrg	    posix_glob=false
392d31dbc53Smrg	  fi ;;
393d31dbc53Smrg      esac
39432b578d3Smrg
39532b578d3Smrg      oIFS=$IFS
39632b578d3Smrg      IFS=/
397d31dbc53Smrg      $posix_glob && set -f
39832b578d3Smrg      set fnord $dstdir
39932b578d3Smrg      shift
400d31dbc53Smrg      $posix_glob && set +f
40132b578d3Smrg      IFS=$oIFS
40232b578d3Smrg
40332b578d3Smrg      prefixes=
40432b578d3Smrg
40532b578d3Smrg      for d
40632b578d3Smrg      do
40732b578d3Smrg	test -z "$d" && continue
40832b578d3Smrg
40932b578d3Smrg	prefix=$prefix$d
41032b578d3Smrg	if test -d "$prefix"; then
41132b578d3Smrg	  prefixes=
41232b578d3Smrg	else
41332b578d3Smrg	  if $posix_mkdir; then
41432b578d3Smrg	    (umask=$mkdir_umask &&
41532b578d3Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41632b578d3Smrg	    # Don't fail if two instances are running concurrently.
41732b578d3Smrg	    test -d "$prefix" || exit 1
41832b578d3Smrg	  else
41932b578d3Smrg	    case $prefix in
42032b578d3Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42132b578d3Smrg	      *) qprefix=$prefix;;
42232b578d3Smrg	    esac
42332b578d3Smrg	    prefixes="$prefixes '$qprefix'"
42432b578d3Smrg	  fi
42532b578d3Smrg	fi
42632b578d3Smrg	prefix=$prefix/
42732b578d3Smrg      done
42832b578d3Smrg
42932b578d3Smrg      if test -n "$prefixes"; then
43032b578d3Smrg	# Don't fail if two instances are running concurrently.
43132b578d3Smrg	(umask $mkdir_umask &&
43232b578d3Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43332b578d3Smrg	  test -d "$dstdir" || exit 1
43432b578d3Smrg	obsolete_mkdir_used=true
43532b578d3Smrg      fi
43632b578d3Smrg    fi
43732b578d3Smrg  fi
43832b578d3Smrg
43932b578d3Smrg  if test -n "$dir_arg"; then
44032b578d3Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44132b578d3Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44232b578d3Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44332b578d3Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44432b578d3Smrg  else
44532b578d3Smrg
44632b578d3Smrg    # Make a couple of temp file names in the proper directory.
44732b578d3Smrg    dsttmp=$dstdir/_inst.$$_
44832b578d3Smrg    rmtmp=$dstdir/_rm.$$_
44932b578d3Smrg
45032b578d3Smrg    # Trap to clean up those temp files at exit.
45132b578d3Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45232b578d3Smrg
45332b578d3Smrg    # Copy the file name to the temp name.
45432b578d3Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45532b578d3Smrg
45632b578d3Smrg    # and set any options; do chmod last to preserve setuid bits.
45732b578d3Smrg    #
45832b578d3Smrg    # If any of these fail, we abort the whole thing.  If we want to
45932b578d3Smrg    # ignore errors from any of these, just make sure not to ignore
46032b578d3Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46132b578d3Smrg    #
462d31dbc53Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
463d31dbc53Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
464d31dbc53Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
465d31dbc53Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
466d31dbc53Smrg
467d31dbc53Smrg    # Now rename the file to the real destination.
468d31dbc53Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
469d31dbc53Smrg      || {
470d31dbc53Smrg	   # The rename failed, perhaps because mv can't rename something else
471d31dbc53Smrg	   # to itself, or perhaps because mv is so ancient that it does not
472d31dbc53Smrg	   # support -f.
473d31dbc53Smrg
474d31dbc53Smrg	   # Now remove or move aside any old file at destination location.
475d31dbc53Smrg	   # We try this two ways since rm can't unlink itself on some
476d31dbc53Smrg	   # systems and the destination file might be busy for other
477d31dbc53Smrg	   # reasons.  In this case, the final cleanup might fail but the new
478d31dbc53Smrg	   # file should still install successfully.
479d31dbc53Smrg	   {
480d31dbc53Smrg	     if test -f "$dst"; then
481d31dbc53Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
482d31dbc53Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
483d31dbc53Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
484d31dbc53Smrg	       || {
485d31dbc53Smrg		 echo "$0: cannot unlink or rename $dst" >&2
486d31dbc53Smrg		 (exit 1); exit 1
487d31dbc53Smrg	       }
488d31dbc53Smrg	     else
489d31dbc53Smrg	       :
490d31dbc53Smrg	     fi
491d31dbc53Smrg	   } &&
492d31dbc53Smrg
493d31dbc53Smrg	   # Now rename the file to the real destination.
494d31dbc53Smrg	   $doit $mvcmd "$dsttmp" "$dst"
495d31dbc53Smrg	 }
496d31dbc53Smrg    } || exit 1
49732b578d3Smrg
49832b578d3Smrg    trap '' 0
49932b578d3Smrg  fi
50032b578d3Smrgdone
50132b578d3Smrg
50232b578d3Smrg# Local variables:
50332b578d3Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
50432b578d3Smrg# time-stamp-start: "scriptversion="
50532b578d3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
50632b578d3Smrg# time-stamp-end: "$"
50732b578d3Smrg# End:
508