install-sh revision 64ce7165
164ce7165Smrg#!/bin/sh
264ce7165Smrg# install - install a program, script, or datafile
364ce7165Smrg
464ce7165Smrgscriptversion=2006-10-14.15
564ce7165Smrg
664ce7165Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
764ce7165Smrg# later released in X11R6 (xc/config/util/install.sh) with the
864ce7165Smrg# following copyright and license.
964ce7165Smrg#
1064ce7165Smrg# Copyright (C) 1994 X Consortium
1164ce7165Smrg#
1264ce7165Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1364ce7165Smrg# of this software and associated documentation files (the "Software"), to
1464ce7165Smrg# deal in the Software without restriction, including without limitation the
1564ce7165Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1664ce7165Smrg# sell copies of the Software, and to permit persons to whom the Software is
1764ce7165Smrg# furnished to do so, subject to the following conditions:
1864ce7165Smrg#
1964ce7165Smrg# The above copyright notice and this permission notice shall be included in
2064ce7165Smrg# all copies or substantial portions of the Software.
2164ce7165Smrg#
2264ce7165Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2364ce7165Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2464ce7165Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2564ce7165Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2664ce7165Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2764ce7165Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2864ce7165Smrg#
2964ce7165Smrg# Except as contained in this notice, the name of the X Consortium shall not
3064ce7165Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3164ce7165Smrg# ings in this Software without prior written authorization from the X Consor-
3264ce7165Smrg# tium.
3364ce7165Smrg#
3464ce7165Smrg#
3564ce7165Smrg# FSF changes to this file are in the public domain.
3664ce7165Smrg#
3764ce7165Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3864ce7165Smrg# `make' implicit rules from creating a file called install from it
3964ce7165Smrg# when there is no Makefile.
4064ce7165Smrg#
4164ce7165Smrg# This script is compatible with the BSD install script, but was written
4264ce7165Smrg# from scratch.
4364ce7165Smrg
4464ce7165Smrgnl='
4564ce7165Smrg'
4664ce7165SmrgIFS=" ""	$nl"
4764ce7165Smrg
4864ce7165Smrg# set DOITPROG to echo to test this script
4964ce7165Smrg
5064ce7165Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5164ce7165Smrgdoit="${DOITPROG-}"
5264ce7165Smrgif test -z "$doit"; then
5364ce7165Smrg  doit_exec=exec
5464ce7165Smrgelse
5564ce7165Smrg  doit_exec=$doit
5664ce7165Smrgfi
5764ce7165Smrg
5864ce7165Smrg# Put in absolute file names if you don't have them in your path;
5964ce7165Smrg# or use environment vars.
6064ce7165Smrg
6164ce7165Smrgmvprog="${MVPROG-mv}"
6264ce7165Smrgcpprog="${CPPROG-cp}"
6364ce7165Smrgchmodprog="${CHMODPROG-chmod}"
6464ce7165Smrgchownprog="${CHOWNPROG-chown}"
6564ce7165Smrgchgrpprog="${CHGRPPROG-chgrp}"
6664ce7165Smrgstripprog="${STRIPPROG-strip}"
6764ce7165Smrgrmprog="${RMPROG-rm}"
6864ce7165Smrgmkdirprog="${MKDIRPROG-mkdir}"
6964ce7165Smrg
7064ce7165Smrgposix_glob=
7164ce7165Smrgposix_mkdir=
7264ce7165Smrg
7364ce7165Smrg# Desired mode of installed file.
7464ce7165Smrgmode=0755
7564ce7165Smrg
7664ce7165Smrgchmodcmd=$chmodprog
7764ce7165Smrgchowncmd=
7864ce7165Smrgchgrpcmd=
7964ce7165Smrgstripcmd=
8064ce7165Smrgrmcmd="$rmprog -f"
8164ce7165Smrgmvcmd="$mvprog"
8264ce7165Smrgsrc=
8364ce7165Smrgdst=
8464ce7165Smrgdir_arg=
8564ce7165Smrgdstarg=
8664ce7165Smrgno_target_directory=
8764ce7165Smrg
8864ce7165Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8964ce7165Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9064ce7165Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9164ce7165Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9264ce7165Smrg
9364ce7165SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9464ce7165SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9564ce7165SmrgIn the 4th, create DIRECTORIES.
9664ce7165Smrg
9764ce7165SmrgOptions:
9864ce7165Smrg-c         (ignored)
9964ce7165Smrg-d         create directories instead of installing files.
10064ce7165Smrg-g GROUP   $chgrpprog installed files to GROUP.
10164ce7165Smrg-m MODE    $chmodprog installed files to MODE.
10264ce7165Smrg-o USER    $chownprog installed files to USER.
10364ce7165Smrg-s         $stripprog installed files.
10464ce7165Smrg-t DIRECTORY  install into DIRECTORY.
10564ce7165Smrg-T         report an error if DSTFILE is a directory.
10664ce7165Smrg--help     display this help and exit.
10764ce7165Smrg--version  display version info and exit.
10864ce7165Smrg
10964ce7165SmrgEnvironment variables override the default commands:
11064ce7165Smrg  CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
11164ce7165Smrg"
11264ce7165Smrg
11364ce7165Smrgwhile test $# -ne 0; do
11464ce7165Smrg  case $1 in
11564ce7165Smrg    -c) shift
11664ce7165Smrg        continue;;
11764ce7165Smrg
11864ce7165Smrg    -d) dir_arg=true
11964ce7165Smrg        shift
12064ce7165Smrg        continue;;
12164ce7165Smrg
12264ce7165Smrg    -g) chgrpcmd="$chgrpprog $2"
12364ce7165Smrg        shift
12464ce7165Smrg        shift
12564ce7165Smrg        continue;;
12664ce7165Smrg
12764ce7165Smrg    --help) echo "$usage"; exit $?;;
12864ce7165Smrg
12964ce7165Smrg    -m) mode=$2
13064ce7165Smrg        shift
13164ce7165Smrg        shift
13264ce7165Smrg	case $mode in
13364ce7165Smrg	  *' '* | *'	'* | *'
13464ce7165Smrg'*	  | *'*'* | *'?'* | *'['*)
13564ce7165Smrg	    echo "$0: invalid mode: $mode" >&2
13664ce7165Smrg	    exit 1;;
13764ce7165Smrg	esac
13864ce7165Smrg        continue;;
13964ce7165Smrg
14064ce7165Smrg    -o) chowncmd="$chownprog $2"
14164ce7165Smrg        shift
14264ce7165Smrg        shift
14364ce7165Smrg        continue;;
14464ce7165Smrg
14564ce7165Smrg    -s) stripcmd=$stripprog
14664ce7165Smrg        shift
14764ce7165Smrg        continue;;
14864ce7165Smrg
14964ce7165Smrg    -t) dstarg=$2
15064ce7165Smrg	shift
15164ce7165Smrg	shift
15264ce7165Smrg	continue;;
15364ce7165Smrg
15464ce7165Smrg    -T) no_target_directory=true
15564ce7165Smrg	shift
15664ce7165Smrg	continue;;
15764ce7165Smrg
15864ce7165Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15964ce7165Smrg
16064ce7165Smrg    --)	shift
16164ce7165Smrg	break;;
16264ce7165Smrg
16364ce7165Smrg    -*)	echo "$0: invalid option: $1" >&2
16464ce7165Smrg	exit 1;;
16564ce7165Smrg
16664ce7165Smrg    *)  break;;
16764ce7165Smrg  esac
16864ce7165Smrgdone
16964ce7165Smrg
17064ce7165Smrgif test $# -ne 0 && test -z "$dir_arg$dstarg"; then
17164ce7165Smrg  # When -d is used, all remaining arguments are directories to create.
17264ce7165Smrg  # When -t is used, the destination is already specified.
17364ce7165Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
17464ce7165Smrg  for arg
17564ce7165Smrg  do
17664ce7165Smrg    if test -n "$dstarg"; then
17764ce7165Smrg      # $@ is not empty: it contains at least $arg.
17864ce7165Smrg      set fnord "$@" "$dstarg"
17964ce7165Smrg      shift # fnord
18064ce7165Smrg    fi
18164ce7165Smrg    shift # arg
18264ce7165Smrg    dstarg=$arg
18364ce7165Smrg  done
18464ce7165Smrgfi
18564ce7165Smrg
18664ce7165Smrgif test $# -eq 0; then
18764ce7165Smrg  if test -z "$dir_arg"; then
18864ce7165Smrg    echo "$0: no input file specified." >&2
18964ce7165Smrg    exit 1
19064ce7165Smrg  fi
19164ce7165Smrg  # It's OK to call `install-sh -d' without argument.
19264ce7165Smrg  # This can happen when creating conditional directories.
19364ce7165Smrg  exit 0
19464ce7165Smrgfi
19564ce7165Smrg
19664ce7165Smrgif test -z "$dir_arg"; then
19764ce7165Smrg  trap '(exit $?); exit' 1 2 13 15
19864ce7165Smrg
19964ce7165Smrg  # Set umask so as not to create temps with too-generous modes.
20064ce7165Smrg  # However, 'strip' requires both read and write access to temps.
20164ce7165Smrg  case $mode in
20264ce7165Smrg    # Optimize common cases.
20364ce7165Smrg    *644) cp_umask=133;;
20464ce7165Smrg    *755) cp_umask=22;;
20564ce7165Smrg
20664ce7165Smrg    *[0-7])
20764ce7165Smrg      if test -z "$stripcmd"; then
20864ce7165Smrg	u_plus_rw=
20964ce7165Smrg      else
21064ce7165Smrg	u_plus_rw='% 200'
21164ce7165Smrg      fi
21264ce7165Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21364ce7165Smrg    *)
21464ce7165Smrg      if test -z "$stripcmd"; then
21564ce7165Smrg	u_plus_rw=
21664ce7165Smrg      else
21764ce7165Smrg	u_plus_rw=,u+rw
21864ce7165Smrg      fi
21964ce7165Smrg      cp_umask=$mode$u_plus_rw;;
22064ce7165Smrg  esac
22164ce7165Smrgfi
22264ce7165Smrg
22364ce7165Smrgfor src
22464ce7165Smrgdo
22564ce7165Smrg  # Protect names starting with `-'.
22664ce7165Smrg  case $src in
22764ce7165Smrg    -*) src=./$src ;;
22864ce7165Smrg  esac
22964ce7165Smrg
23064ce7165Smrg  if test -n "$dir_arg"; then
23164ce7165Smrg    dst=$src
23264ce7165Smrg    dstdir=$dst
23364ce7165Smrg    test -d "$dstdir"
23464ce7165Smrg    dstdir_status=$?
23564ce7165Smrg  else
23664ce7165Smrg
23764ce7165Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
23864ce7165Smrg    # might cause directories to be created, which would be especially bad
23964ce7165Smrg    # if $src (and thus $dsttmp) contains '*'.
24064ce7165Smrg    if test ! -f "$src" && test ! -d "$src"; then
24164ce7165Smrg      echo "$0: $src does not exist." >&2
24264ce7165Smrg      exit 1
24364ce7165Smrg    fi
24464ce7165Smrg
24564ce7165Smrg    if test -z "$dstarg"; then
24664ce7165Smrg      echo "$0: no destination specified." >&2
24764ce7165Smrg      exit 1
24864ce7165Smrg    fi
24964ce7165Smrg
25064ce7165Smrg    dst=$dstarg
25164ce7165Smrg    # Protect names starting with `-'.
25264ce7165Smrg    case $dst in
25364ce7165Smrg      -*) dst=./$dst ;;
25464ce7165Smrg    esac
25564ce7165Smrg
25664ce7165Smrg    # If destination is a directory, append the input filename; won't work
25764ce7165Smrg    # if double slashes aren't ignored.
25864ce7165Smrg    if test -d "$dst"; then
25964ce7165Smrg      if test -n "$no_target_directory"; then
26064ce7165Smrg	echo "$0: $dstarg: Is a directory" >&2
26164ce7165Smrg	exit 1
26264ce7165Smrg      fi
26364ce7165Smrg      dstdir=$dst
26464ce7165Smrg      dst=$dstdir/`basename "$src"`
26564ce7165Smrg      dstdir_status=0
26664ce7165Smrg    else
26764ce7165Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
26864ce7165Smrg      dstdir=`
26964ce7165Smrg	(dirname "$dst") 2>/dev/null ||
27064ce7165Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27164ce7165Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27264ce7165Smrg	     X"$dst" : 'X\(//\)$' \| \
27364ce7165Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
27464ce7165Smrg	echo X"$dst" |
27564ce7165Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
27664ce7165Smrg		   s//\1/
27764ce7165Smrg		   q
27864ce7165Smrg		 }
27964ce7165Smrg		 /^X\(\/\/\)[^/].*/{
28064ce7165Smrg		   s//\1/
28164ce7165Smrg		   q
28264ce7165Smrg		 }
28364ce7165Smrg		 /^X\(\/\/\)$/{
28464ce7165Smrg		   s//\1/
28564ce7165Smrg		   q
28664ce7165Smrg		 }
28764ce7165Smrg		 /^X\(\/\).*/{
28864ce7165Smrg		   s//\1/
28964ce7165Smrg		   q
29064ce7165Smrg		 }
29164ce7165Smrg		 s/.*/./; q'
29264ce7165Smrg      `
29364ce7165Smrg
29464ce7165Smrg      test -d "$dstdir"
29564ce7165Smrg      dstdir_status=$?
29664ce7165Smrg    fi
29764ce7165Smrg  fi
29864ce7165Smrg
29964ce7165Smrg  obsolete_mkdir_used=false
30064ce7165Smrg
30164ce7165Smrg  if test $dstdir_status != 0; then
30264ce7165Smrg    case $posix_mkdir in
30364ce7165Smrg      '')
30464ce7165Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
30564ce7165Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
30664ce7165Smrg	umask=`umask`
30764ce7165Smrg	case $stripcmd.$umask in
30864ce7165Smrg	  # Optimize common cases.
30964ce7165Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31064ce7165Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31164ce7165Smrg
31264ce7165Smrg	  *[0-7])
31364ce7165Smrg	    mkdir_umask=`expr $umask + 22 \
31464ce7165Smrg	      - $umask % 100 % 40 + $umask % 20 \
31564ce7165Smrg	      - $umask % 10 % 4 + $umask % 2
31664ce7165Smrg	    `;;
31764ce7165Smrg	  *) mkdir_umask=$umask,go-w;;
31864ce7165Smrg	esac
31964ce7165Smrg
32064ce7165Smrg	# With -d, create the new directory with the user-specified mode.
32164ce7165Smrg	# Otherwise, rely on $mkdir_umask.
32264ce7165Smrg	if test -n "$dir_arg"; then
32364ce7165Smrg	  mkdir_mode=-m$mode
32464ce7165Smrg	else
32564ce7165Smrg	  mkdir_mode=
32664ce7165Smrg	fi
32764ce7165Smrg
32864ce7165Smrg	posix_mkdir=false
32964ce7165Smrg	case $umask in
33064ce7165Smrg	  *[123567][0-7][0-7])
33164ce7165Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33264ce7165Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33364ce7165Smrg	    ;;
33464ce7165Smrg	  *)
33564ce7165Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
33664ce7165Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
33764ce7165Smrg
33864ce7165Smrg	    if (umask $mkdir_umask &&
33964ce7165Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34064ce7165Smrg	    then
34164ce7165Smrg	      if test -z "$dir_arg" || {
34264ce7165Smrg		   # Check for POSIX incompatibilities with -m.
34364ce7165Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
34464ce7165Smrg		   # other-writeable bit of parent directory when it shouldn't.
34564ce7165Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
34664ce7165Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
34764ce7165Smrg		   case $ls_ld_tmpdir in
34864ce7165Smrg		     d????-?r-*) different_mode=700;;
34964ce7165Smrg		     d????-?--*) different_mode=755;;
35064ce7165Smrg		     *) false;;
35164ce7165Smrg		   esac &&
35264ce7165Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35364ce7165Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
35464ce7165Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
35564ce7165Smrg		   }
35664ce7165Smrg		 }
35764ce7165Smrg	      then posix_mkdir=:
35864ce7165Smrg	      fi
35964ce7165Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36064ce7165Smrg	    else
36164ce7165Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36264ce7165Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36364ce7165Smrg	    fi
36464ce7165Smrg	    trap '' 0;;
36564ce7165Smrg	esac;;
36664ce7165Smrg    esac
36764ce7165Smrg
36864ce7165Smrg    if
36964ce7165Smrg      $posix_mkdir && (
37064ce7165Smrg	umask $mkdir_umask &&
37164ce7165Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37264ce7165Smrg      )
37364ce7165Smrg    then :
37464ce7165Smrg    else
37564ce7165Smrg
37664ce7165Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
37764ce7165Smrg      # or it failed possibly due to a race condition.  Create the
37864ce7165Smrg      # directory the slow way, step by step, checking for races as we go.
37964ce7165Smrg
38064ce7165Smrg      case $dstdir in
38164ce7165Smrg	/*) prefix=/ ;;
38264ce7165Smrg	-*) prefix=./ ;;
38364ce7165Smrg	*)  prefix= ;;
38464ce7165Smrg      esac
38564ce7165Smrg
38664ce7165Smrg      case $posix_glob in
38764ce7165Smrg        '')
38864ce7165Smrg	  if (set -f) 2>/dev/null; then
38964ce7165Smrg	    posix_glob=true
39064ce7165Smrg	  else
39164ce7165Smrg	    posix_glob=false
39264ce7165Smrg	  fi ;;
39364ce7165Smrg      esac
39464ce7165Smrg
39564ce7165Smrg      oIFS=$IFS
39664ce7165Smrg      IFS=/
39764ce7165Smrg      $posix_glob && set -f
39864ce7165Smrg      set fnord $dstdir
39964ce7165Smrg      shift
40064ce7165Smrg      $posix_glob && set +f
40164ce7165Smrg      IFS=$oIFS
40264ce7165Smrg
40364ce7165Smrg      prefixes=
40464ce7165Smrg
40564ce7165Smrg      for d
40664ce7165Smrg      do
40764ce7165Smrg	test -z "$d" && continue
40864ce7165Smrg
40964ce7165Smrg	prefix=$prefix$d
41064ce7165Smrg	if test -d "$prefix"; then
41164ce7165Smrg	  prefixes=
41264ce7165Smrg	else
41364ce7165Smrg	  if $posix_mkdir; then
41464ce7165Smrg	    (umask=$mkdir_umask &&
41564ce7165Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41664ce7165Smrg	    # Don't fail if two instances are running concurrently.
41764ce7165Smrg	    test -d "$prefix" || exit 1
41864ce7165Smrg	  else
41964ce7165Smrg	    case $prefix in
42064ce7165Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42164ce7165Smrg	      *) qprefix=$prefix;;
42264ce7165Smrg	    esac
42364ce7165Smrg	    prefixes="$prefixes '$qprefix'"
42464ce7165Smrg	  fi
42564ce7165Smrg	fi
42664ce7165Smrg	prefix=$prefix/
42764ce7165Smrg      done
42864ce7165Smrg
42964ce7165Smrg      if test -n "$prefixes"; then
43064ce7165Smrg	# Don't fail if two instances are running concurrently.
43164ce7165Smrg	(umask $mkdir_umask &&
43264ce7165Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43364ce7165Smrg	  test -d "$dstdir" || exit 1
43464ce7165Smrg	obsolete_mkdir_used=true
43564ce7165Smrg      fi
43664ce7165Smrg    fi
43764ce7165Smrg  fi
43864ce7165Smrg
43964ce7165Smrg  if test -n "$dir_arg"; then
44064ce7165Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44164ce7165Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44264ce7165Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44364ce7165Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44464ce7165Smrg  else
44564ce7165Smrg
44664ce7165Smrg    # Make a couple of temp file names in the proper directory.
44764ce7165Smrg    dsttmp=$dstdir/_inst.$$_
44864ce7165Smrg    rmtmp=$dstdir/_rm.$$_
44964ce7165Smrg
45064ce7165Smrg    # Trap to clean up those temp files at exit.
45164ce7165Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45264ce7165Smrg
45364ce7165Smrg    # Copy the file name to the temp name.
45464ce7165Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45564ce7165Smrg
45664ce7165Smrg    # and set any options; do chmod last to preserve setuid bits.
45764ce7165Smrg    #
45864ce7165Smrg    # If any of these fail, we abort the whole thing.  If we want to
45964ce7165Smrg    # ignore errors from any of these, just make sure not to ignore
46064ce7165Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46164ce7165Smrg    #
46264ce7165Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
46364ce7165Smrg      && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
46464ce7165Smrg      && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
46564ce7165Smrg      && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46664ce7165Smrg
46764ce7165Smrg    # Now rename the file to the real destination.
46864ce7165Smrg    { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
46964ce7165Smrg      || {
47064ce7165Smrg	   # The rename failed, perhaps because mv can't rename something else
47164ce7165Smrg	   # to itself, or perhaps because mv is so ancient that it does not
47264ce7165Smrg	   # support -f.
47364ce7165Smrg
47464ce7165Smrg	   # Now remove or move aside any old file at destination location.
47564ce7165Smrg	   # We try this two ways since rm can't unlink itself on some
47664ce7165Smrg	   # systems and the destination file might be busy for other
47764ce7165Smrg	   # reasons.  In this case, the final cleanup might fail but the new
47864ce7165Smrg	   # file should still install successfully.
47964ce7165Smrg	   {
48064ce7165Smrg	     if test -f "$dst"; then
48164ce7165Smrg	       $doit $rmcmd -f "$dst" 2>/dev/null \
48264ce7165Smrg	       || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
48364ce7165Smrg		     && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
48464ce7165Smrg	       || {
48564ce7165Smrg		 echo "$0: cannot unlink or rename $dst" >&2
48664ce7165Smrg		 (exit 1); exit 1
48764ce7165Smrg	       }
48864ce7165Smrg	     else
48964ce7165Smrg	       :
49064ce7165Smrg	     fi
49164ce7165Smrg	   } &&
49264ce7165Smrg
49364ce7165Smrg	   # Now rename the file to the real destination.
49464ce7165Smrg	   $doit $mvcmd "$dsttmp" "$dst"
49564ce7165Smrg	 }
49664ce7165Smrg    } || exit 1
49764ce7165Smrg
49864ce7165Smrg    trap '' 0
49964ce7165Smrg  fi
50064ce7165Smrgdone
50164ce7165Smrg
50264ce7165Smrg# Local variables:
50364ce7165Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
50464ce7165Smrg# time-stamp-start: "scriptversion="
50564ce7165Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
50664ce7165Smrg# time-stamp-end: "$"
50764ce7165Smrg# End:
508