install-sh revision 21c2f794
121c2f794Smrg#!/bin/sh
221c2f794Smrg# install - install a program, script, or datafile
321c2f794Smrg
421c2f794Smrgscriptversion=2006-10-14.15
521c2f794Smrg
621c2f794Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
721c2f794Smrg# later released in X11R6 (xc/config/util/install.sh) with the
821c2f794Smrg# following copyright and license.
921c2f794Smrg#
1021c2f794Smrg# Copyright (C) 1994 X Consortium
1121c2f794Smrg#
1221c2f794Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1321c2f794Smrg# of this software and associated documentation files (the "Software"), to
1421c2f794Smrg# deal in the Software without restriction, including without limitation the
1521c2f794Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1621c2f794Smrg# sell copies of the Software, and to permit persons to whom the Software is
1721c2f794Smrg# furnished to do so, subject to the following conditions:
1821c2f794Smrg#
1921c2f794Smrg# The above copyright notice and this permission notice shall be included in
2021c2f794Smrg# all copies or substantial portions of the Software.
2121c2f794Smrg#
2221c2f794Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2321c2f794Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2421c2f794Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2521c2f794Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2621c2f794Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2721c2f794Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2821c2f794Smrg#
2921c2f794Smrg# Except as contained in this notice, the name of the X Consortium shall not
3021c2f794Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3121c2f794Smrg# ings in this Software without prior written authorization from the X Consor-
3221c2f794Smrg# tium.
3321c2f794Smrg#
3421c2f794Smrg#
3521c2f794Smrg# FSF changes to this file are in the public domain.
3621c2f794Smrg#
3721c2f794Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3821c2f794Smrg# `make' implicit rules from creating a file called install from it
3921c2f794Smrg# when there is no Makefile.
4021c2f794Smrg#
4121c2f794Smrg# This script is compatible with the BSD install script, but was written
4221c2f794Smrg# from scratch.
4321c2f794Smrg
4421c2f794Smrgnl='
4521c2f794Smrg'
4621c2f794SmrgIFS=" ""	$nl"
4721c2f794Smrg
4821c2f794Smrg# set DOITPROG to echo to test this script
4921c2f794Smrg
5021c2f794Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5121c2f794Smrgdoit="${DOITPROG-}"
5221c2f794Smrgif test -z "$doit"; then
5321c2f794Smrg  doit_exec=exec
5421c2f794Smrgelse
5521c2f794Smrg  doit_exec=$doit
5621c2f794Smrgfi
5721c2f794Smrg
5821c2f794Smrg# Put in absolute file names if you don't have them in your path;
5921c2f794Smrg# or use environment vars.
6021c2f794Smrg
6121c2f794Smrgmvprog="${MVPROG-mv}"
6221c2f794Smrgcpprog="${CPPROG-cp}"
6321c2f794Smrgchmodprog="${CHMODPROG-chmod}"
6421c2f794Smrgchownprog="${CHOWNPROG-chown}"
6521c2f794Smrgchgrpprog="${CHGRPPROG-chgrp}"
6621c2f794Smrgstripprog="${STRIPPROG-strip}"
6721c2f794Smrgrmprog="${RMPROG-rm}"
6821c2f794Smrgmkdirprog="${MKDIRPROG-mkdir}"
6921c2f794Smrg
7021c2f794Smrgposix_glob=
7121c2f794Smrgposix_mkdir=
7221c2f794Smrg
7321c2f794Smrg# Desired mode of installed file.
7421c2f794Smrgmode=0755
7521c2f794Smrg
7621c2f794Smrgchmodcmd=$chmodprog
7721c2f794Smrgchowncmd=
7821c2f794Smrgchgrpcmd=
7921c2f794Smrgstripcmd=
8021c2f794Smrgrmcmd="$rmprog -f"
8121c2f794Smrgmvcmd="$mvprog"
8221c2f794Smrgsrc=
8321c2f794Smrgdst=
8421c2f794Smrgdir_arg=
8521c2f794Smrgdstarg=
8621c2f794Smrgno_target_directory=
8721c2f794Smrg
8821c2f794Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8921c2f794Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9021c2f794Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9121c2f794Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9221c2f794Smrg
9321c2f794SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9421c2f794SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9521c2f794SmrgIn the 4th, create DIRECTORIES.
9621c2f794Smrg
9721c2f794SmrgOptions:
9821c2f794Smrg-c         (ignored)
9921c2f794Smrg-d         create directories instead of installing files.
10021c2f794Smrg-g GROUP   $chgrpprog installed files to GROUP.
10121c2f794Smrg-m MODE    $chmodprog installed files to MODE.
10221c2f794Smrg-o USER    $chownprog installed files to USER.
10321c2f794Smrg-s         $stripprog installed files.
10421c2f794Smrg-t DIRECTORY  install into DIRECTORY.
10521c2f794Smrg-T         report an error if DSTFILE is a directory.
10621c2f794Smrg--help     display this help and exit.
10721c2f794Smrg--version  display version info and exit.
10821c2f794Smrg
10921c2f794SmrgEnvironment variables override the default commands:
11021c2f794Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
11121c2f794Smrg"
11221c2f794Smrg
11321c2f794Smrgwhile test $# -ne 0; do
11421c2f794Smrg  case $1 in
11521c2f794Smrg    -c) shift
11621c2f794Smrg        continue;;
11721c2f794Smrg
11821c2f794Smrg    -d) dir_arg=true
11921c2f794Smrg        shift
12021c2f794Smrg        continue;;
12121c2f794Smrg
12221c2f794Smrg    -g) chgrpcmd="$chgrpprog $2"
12321c2f794Smrg        shift
12421c2f794Smrg        shift
12521c2f794Smrg        continue;;
12621c2f794Smrg
12721c2f794Smrg    --help) echo "$usage"; exit $?;;
12821c2f794Smrg
12921c2f794Smrg    -m) mode=$2
13021c2f794Smrg        shift
13121c2f794Smrg        shift
13221c2f794Smrg	case $mode in
13321c2f794Smrg	  *' '* | *'	'* | *'
13421c2f794Smrg'*	  | *'*'* | *'?'* | *'['*)
13521c2f794Smrg	    echo "$0: invalid mode: $mode" >&2
13621c2f794Smrg	    exit 1;;
13721c2f794Smrg	esac
13821c2f794Smrg        continue;;
13921c2f794Smrg
14021c2f794Smrg    -o) chowncmd="$chownprog $2"
14121c2f794Smrg        shift
14221c2f794Smrg        shift
14321c2f794Smrg        continue;;
14421c2f794Smrg
14521c2f794Smrg    -s) stripcmd=$stripprog
14621c2f794Smrg        shift
14721c2f794Smrg        continue;;
14821c2f794Smrg
14921c2f794Smrg    -t) dstarg=$2
15021c2f794Smrg	shift
15121c2f794Smrg	shift
15221c2f794Smrg	continue;;
15321c2f794Smrg
15421c2f794Smrg    -T) no_target_directory=true
15521c2f794Smrg	shift
15621c2f794Smrg	continue;;
15721c2f794Smrg
15821c2f794Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15921c2f794Smrg
16021c2f794Smrg    --)	shift
16121c2f794Smrg	break;;
16221c2f794Smrg
16321c2f794Smrg    -*)	echo "$0: invalid option: $1" >&2
16421c2f794Smrg	exit 1;;
16521c2f794Smrg
16621c2f794Smrg    *)  break;;
16721c2f794Smrg  esac
16821c2f794Smrgdone
16921c2f794Smrg
17021c2f794Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
17121c2f794Smrg  # When -d is used, all remaining arguments are directories to create.
17221c2f794Smrg  # When -t is used, the destination is already specified.
17321c2f794Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
17421c2f794Smrg  for arg
17521c2f794Smrg  do
17621c2f794Smrg    if test -n "$dstarg"; then
17721c2f794Smrg      # $@ is not empty: it contains at least $arg.
17821c2f794Smrg      set fnord "$@" "$dstarg"
17921c2f794Smrg      shift # fnord
18021c2f794Smrg    fi
18121c2f794Smrg    shift # arg
18221c2f794Smrg    dstarg=$arg
18321c2f794Smrg  done
18421c2f794Smrgfi
18521c2f794Smrg
18621c2f794Smrgif test $# -eq 0; then
18721c2f794Smrg  if test -z "$dir_arg"; then
18821c2f794Smrg    echo "$0: no input file specified." >&2
18921c2f794Smrg    exit 1
19021c2f794Smrg  fi
19121c2f794Smrg  # It's OK to call `install-sh -d' without argument.
19221c2f794Smrg  # This can happen when creating conditional directories.
19321c2f794Smrg  exit 0
19421c2f794Smrgfi
19521c2f794Smrg
19621c2f794Smrgif test -z "$dir_arg"; then
19721c2f794Smrg  trap '(exit $?); exit' 1 2 13 15
19821c2f794Smrg
19921c2f794Smrg  # Set umask so as not to create temps with too-generous modes.
20021c2f794Smrg  # However, 'strip' requires both read and write access to temps.
20121c2f794Smrg  case $mode in
20221c2f794Smrg    # Optimize common cases.
20321c2f794Smrg    *644) cp_umask=133;;
20421c2f794Smrg    *755) cp_umask=22;;
20521c2f794Smrg
20621c2f794Smrg    *[0-7])
20721c2f794Smrg      if test -z "$stripcmd"; then
20821c2f794Smrg	u_plus_rw=
20921c2f794Smrg      else
21021c2f794Smrg	u_plus_rw='% 200'
21121c2f794Smrg      fi
21221c2f794Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21321c2f794Smrg    *)
21421c2f794Smrg      if test -z "$stripcmd"; then
21521c2f794Smrg	u_plus_rw=
21621c2f794Smrg      else
21721c2f794Smrg	u_plus_rw=,u+rw
21821c2f794Smrg      fi
21921c2f794Smrg      cp_umask=$mode$u_plus_rw;;
22021c2f794Smrg  esac
22121c2f794Smrgfi
22221c2f794Smrg
22321c2f794Smrgfor src
22421c2f794Smrgdo
22521c2f794Smrg  # Protect names starting with `-'.
22621c2f794Smrg  case $src in
22721c2f794Smrg    -*) src=./$src ;;
22821c2f794Smrg  esac
22921c2f794Smrg
23021c2f794Smrg  if test -n "$dir_arg"; then
23121c2f794Smrg    dst=$src
23221c2f794Smrg    dstdir=$dst
23321c2f794Smrg    test -d "$dstdir"
23421c2f794Smrg    dstdir_status=$?
23521c2f794Smrg  else
23621c2f794Smrg
23721c2f794Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
23821c2f794Smrg    # might cause directories to be created, which would be especially bad
23921c2f794Smrg    # if $src (and thus $dsttmp) contains '*'.
24021c2f794Smrg    if test ! -f "$src" && test ! -d "$src"; then
24121c2f794Smrg      echo "$0: $src does not exist." >&2
24221c2f794Smrg      exit 1
24321c2f794Smrg    fi
24421c2f794Smrg
24521c2f794Smrg    if test -z "$dstarg"; then
24621c2f794Smrg      echo "$0: no destination specified." >&2
24721c2f794Smrg      exit 1
24821c2f794Smrg    fi
24921c2f794Smrg
25021c2f794Smrg    dst=$dstarg
25121c2f794Smrg    # Protect names starting with `-'.
25221c2f794Smrg    case $dst in
25321c2f794Smrg      -*) dst=./$dst ;;
25421c2f794Smrg    esac
25521c2f794Smrg
25621c2f794Smrg    # If destination is a directory, append the input filename; won't work
25721c2f794Smrg    # if double slashes aren't ignored.
25821c2f794Smrg    if test -d "$dst"; then
25921c2f794Smrg      if test -n "$no_target_directory"; then
26021c2f794Smrg	echo "$0: $dstarg: Is a directory" >&2
26121c2f794Smrg	exit 1
26221c2f794Smrg      fi
26321c2f794Smrg      dstdir=$dst
26421c2f794Smrg      dst=$dstdir/`basename "$src"`
26521c2f794Smrg      dstdir_status=0
26621c2f794Smrg    else
26721c2f794Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
26821c2f794Smrg      dstdir=`
26921c2f794Smrg	(dirname "$dst") 2>/dev/null ||
27021c2f794Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27121c2f794Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27221c2f794Smrg	     X"$dst" : 'X\(//\)$' \| \
27321c2f794Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
27421c2f794Smrg	echo X"$dst" |
27521c2f794Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
27621c2f794Smrg		   s//\1/
27721c2f794Smrg		   q
27821c2f794Smrg		 }
27921c2f794Smrg		 /^X\(\/\/\)[^/].*/{
28021c2f794Smrg		   s//\1/
28121c2f794Smrg		   q
28221c2f794Smrg		 }
28321c2f794Smrg		 /^X\(\/\/\)$/{
28421c2f794Smrg		   s//\1/
28521c2f794Smrg		   q
28621c2f794Smrg		 }
28721c2f794Smrg		 /^X\(\/\).*/{
28821c2f794Smrg		   s//\1/
28921c2f794Smrg		   q
29021c2f794Smrg		 }
29121c2f794Smrg		 s/.*/./; q'
29221c2f794Smrg      `
29321c2f794Smrg
29421c2f794Smrg      test -d "$dstdir"
29521c2f794Smrg      dstdir_status=$?
29621c2f794Smrg    fi
29721c2f794Smrg  fi
29821c2f794Smrg
29921c2f794Smrg  obsolete_mkdir_used=false
30021c2f794Smrg
30121c2f794Smrg  if test $dstdir_status != 0; then
30221c2f794Smrg    case $posix_mkdir in
30321c2f794Smrg      '')
30421c2f794Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
30521c2f794Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
30621c2f794Smrg	umask=`umask`
30721c2f794Smrg	case $stripcmd.$umask in
30821c2f794Smrg	  # Optimize common cases.
30921c2f794Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31021c2f794Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31121c2f794Smrg
31221c2f794Smrg	  *[0-7])
31321c2f794Smrg	    mkdir_umask=`expr $umask + 22 \
31421c2f794Smrg	      - $umask % 100 % 40 + $umask % 20 \
31521c2f794Smrg	      - $umask % 10 % 4 + $umask % 2
31621c2f794Smrg	    `;;
31721c2f794Smrg	  *) mkdir_umask=$umask,go-w;;
31821c2f794Smrg	esac
31921c2f794Smrg
32021c2f794Smrg	# With -d, create the new directory with the user-specified mode.
32121c2f794Smrg	# Otherwise, rely on $mkdir_umask.
32221c2f794Smrg	if test -n "$dir_arg"; then
32321c2f794Smrg	  mkdir_mode=-m$mode
32421c2f794Smrg	else
32521c2f794Smrg	  mkdir_mode=
32621c2f794Smrg	fi
32721c2f794Smrg
32821c2f794Smrg	posix_mkdir=false
32921c2f794Smrg	case $umask in
33021c2f794Smrg	  *[123567][0-7][0-7])
33121c2f794Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33221c2f794Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33321c2f794Smrg	    ;;
33421c2f794Smrg	  *)
33521c2f794Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
33621c2f794Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
33721c2f794Smrg
33821c2f794Smrg	    if (umask $mkdir_umask &&
33921c2f794Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34021c2f794Smrg	    then
34121c2f794Smrg	      if test -z "$dir_arg" || {
34221c2f794Smrg		   # Check for POSIX incompatibilities with -m.
34321c2f794Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
34421c2f794Smrg		   # other-writeable bit of parent directory when it shouldn't.
34521c2f794Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
34621c2f794Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
34721c2f794Smrg		   case $ls_ld_tmpdir in
34821c2f794Smrg		     d????-?r-*) different_mode=700;;
34921c2f794Smrg		     d????-?--*) different_mode=755;;
35021c2f794Smrg		     *) false;;
35121c2f794Smrg		   esac &&
35221c2f794Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35321c2f794Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
35421c2f794Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
35521c2f794Smrg		   }
35621c2f794Smrg		 }
35721c2f794Smrg	      then posix_mkdir=:
35821c2f794Smrg	      fi
35921c2f794Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36021c2f794Smrg	    else
36121c2f794Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36221c2f794Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36321c2f794Smrg	    fi
36421c2f794Smrg	    trap '' 0;;
36521c2f794Smrg	esac;;
36621c2f794Smrg    esac
36721c2f794Smrg
36821c2f794Smrg    if
36921c2f794Smrg      $posix_mkdir && (
37021c2f794Smrg	umask $mkdir_umask &&
37121c2f794Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37221c2f794Smrg      )
37321c2f794Smrg    then :
37421c2f794Smrg    else
37521c2f794Smrg
37621c2f794Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
37721c2f794Smrg      # or it failed possibly due to a race condition.  Create the
37821c2f794Smrg      # directory the slow way, step by step, checking for races as we go.
37921c2f794Smrg
38021c2f794Smrg      case $dstdir in
38121c2f794Smrg	/*) prefix=/ ;;
38221c2f794Smrg	-*) prefix=./ ;;
38321c2f794Smrg	*)  prefix= ;;
38421c2f794Smrg      esac
38521c2f794Smrg
38621c2f794Smrg      case $posix_glob in
38721c2f794Smrg        '')
38821c2f794Smrg	  if (set -f) 2>/dev/null; then
38921c2f794Smrg	    posix_glob=true
39021c2f794Smrg	  else
39121c2f794Smrg	    posix_glob=false
39221c2f794Smrg	  fi ;;
39321c2f794Smrg      esac
39421c2f794Smrg
39521c2f794Smrg      oIFS=$IFS
39621c2f794Smrg      IFS=/
39721c2f794Smrg      $posix_glob && set -f
39821c2f794Smrg      set fnord $dstdir
39921c2f794Smrg      shift
40021c2f794Smrg      $posix_glob && set +f
40121c2f794Smrg      IFS=$oIFS
40221c2f794Smrg
40321c2f794Smrg      prefixes=
40421c2f794Smrg
40521c2f794Smrg      for d
40621c2f794Smrg      do
40721c2f794Smrg	test -z "$d" && continue
40821c2f794Smrg
40921c2f794Smrg	prefix=$prefix$d
41021c2f794Smrg	if test -d "$prefix"; then
41121c2f794Smrg	  prefixes=
41221c2f794Smrg	else
41321c2f794Smrg	  if $posix_mkdir; then
41421c2f794Smrg	    (umask=$mkdir_umask &&
41521c2f794Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41621c2f794Smrg	    # Don't fail if two instances are running concurrently.
41721c2f794Smrg	    test -d "$prefix" || exit 1
41821c2f794Smrg	  else
41921c2f794Smrg	    case $prefix in
42021c2f794Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42121c2f794Smrg	      *) qprefix=$prefix;;
42221c2f794Smrg	    esac
42321c2f794Smrg	    prefixes="$prefixes '$qprefix'"
42421c2f794Smrg	  fi
42521c2f794Smrg	fi
42621c2f794Smrg	prefix=$prefix/
42721c2f794Smrg      done
42821c2f794Smrg
42921c2f794Smrg      if test -n "$prefixes"; then
43021c2f794Smrg	# Don't fail if two instances are running concurrently.
43121c2f794Smrg	(umask $mkdir_umask &&
43221c2f794Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43321c2f794Smrg	  test -d "$dstdir" || exit 1
43421c2f794Smrg	obsolete_mkdir_used=true
43521c2f794Smrg      fi
43621c2f794Smrg    fi
43721c2f794Smrg  fi
43821c2f794Smrg
43921c2f794Smrg  if test -n "$dir_arg"; then
44021c2f794Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44121c2f794Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44221c2f794Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44321c2f794Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44421c2f794Smrg  else
44521c2f794Smrg
44621c2f794Smrg    # Make a couple of temp file names in the proper directory.
44721c2f794Smrg    dsttmp=$dstdir/_inst.$$_
44821c2f794Smrg    rmtmp=$dstdir/_rm.$$_
44921c2f794Smrg
45021c2f794Smrg    # Trap to clean up those temp files at exit.
45121c2f794Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45221c2f794Smrg
45321c2f794Smrg    # Copy the file name to the temp name.
45421c2f794Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45521c2f794Smrg
45621c2f794Smrg    # and set any options; do chmod last to preserve setuid bits.
45721c2f794Smrg    #
45821c2f794Smrg    # If any of these fail, we abort the whole thing.  If we want to
45921c2f794Smrg    # ignore errors from any of these, just make sure not to ignore
46021c2f794Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46121c2f794Smrg    #
46221c2f794Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
46321c2f794Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
46421c2f794Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
46521c2f794Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46621c2f794Smrg
46721c2f794Smrg    # Now rename the file to the real destination.
46821c2f794Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
46921c2f794Smrg      || {
47021c2f794Smrg	   # The rename failed, perhaps because mv can't rename something else
47121c2f794Smrg	   # to itself, or perhaps because mv is so ancient that it does not
47221c2f794Smrg	   # support -f.
47321c2f794Smrg
47421c2f794Smrg	   # Now remove or move aside any old file at destination location.
47521c2f794Smrg	   # We try this two ways since rm can't unlink itself on some
47621c2f794Smrg	   # systems and the destination file might be busy for other
47721c2f794Smrg	   # reasons.  In this case, the final cleanup might fail but the new
47821c2f794Smrg	   # file should still install successfully.
47921c2f794Smrg	   {
48021c2f794Smrg	     if test -f "$dst"; then
48121c2f794Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
48221c2f794Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
48321c2f794Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
48421c2f794Smrg	       || {
48521c2f794Smrg		 echo "$0: cannot unlink or rename $dst" >&2
48621c2f794Smrg		 (exit 1); exit 1
48721c2f794Smrg	       }
48821c2f794Smrg	     else
48921c2f794Smrg	       :
49021c2f794Smrg	     fi
49121c2f794Smrg	   } &&
49221c2f794Smrg
49321c2f794Smrg	   # Now rename the file to the real destination.
49421c2f794Smrg	   $doit $mvcmd "$dsttmp" "$dst"
49521c2f794Smrg	 }
49621c2f794Smrg    } || exit 1
49721c2f794Smrg
49821c2f794Smrg    trap '' 0
49921c2f794Smrg  fi
50021c2f794Smrgdone
50121c2f794Smrg
50221c2f794Smrg# Local variables:
50321c2f794Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
50421c2f794Smrg# time-stamp-start: "scriptversion="
50521c2f794Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
50621c2f794Smrg# time-stamp-end: "$"
50721c2f794Smrg# End:
508