install-sh revision 98af18c5
16f03b1f6Smrg#!/bin/sh
26f03b1f6Smrg# install - install a program, script, or datafile
36f03b1f6Smrg
498af18c5Smrgscriptversion=2009-04-28.21; # UTC
56f03b1f6Smrg
66f03b1f6Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
76f03b1f6Smrg# later released in X11R6 (xc/config/util/install.sh) with the
86f03b1f6Smrg# following copyright and license.
96f03b1f6Smrg#
106f03b1f6Smrg# Copyright (C) 1994 X Consortium
116f03b1f6Smrg#
126f03b1f6Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
136f03b1f6Smrg# of this software and associated documentation files (the "Software"), to
146f03b1f6Smrg# deal in the Software without restriction, including without limitation the
156f03b1f6Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
166f03b1f6Smrg# sell copies of the Software, and to permit persons to whom the Software is
176f03b1f6Smrg# furnished to do so, subject to the following conditions:
186f03b1f6Smrg#
196f03b1f6Smrg# The above copyright notice and this permission notice shall be included in
206f03b1f6Smrg# all copies or substantial portions of the Software.
216f03b1f6Smrg#
226f03b1f6Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
236f03b1f6Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
246f03b1f6Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
256f03b1f6Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
266f03b1f6Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
276f03b1f6Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
286f03b1f6Smrg#
296f03b1f6Smrg# Except as contained in this notice, the name of the X Consortium shall not
306f03b1f6Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
316f03b1f6Smrg# ings in this Software without prior written authorization from the X Consor-
326f03b1f6Smrg# tium.
336f03b1f6Smrg#
346f03b1f6Smrg#
356f03b1f6Smrg# FSF changes to this file are in the public domain.
366f03b1f6Smrg#
376f03b1f6Smrg# Calling this script install-sh is preferred over install.sh, to prevent
386f03b1f6Smrg# `make' implicit rules from creating a file called install from it
396f03b1f6Smrg# when there is no Makefile.
406f03b1f6Smrg#
416f03b1f6Smrg# This script is compatible with the BSD install script, but was written
4298af18c5Smrg# from scratch.
4398af18c5Smrg
4498af18c5Smrgnl='
4598af18c5Smrg'
4698af18c5SmrgIFS=" ""	$nl"
476f03b1f6Smrg
486f03b1f6Smrg# set DOITPROG to echo to test this script
496f03b1f6Smrg
506f03b1f6Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5198af18c5Smrgdoit=${DOITPROG-}
5298af18c5Smrgif test -z "$doit"; then
5398af18c5Smrg  doit_exec=exec
5498af18c5Smrgelse
5598af18c5Smrg  doit_exec=$doit
5698af18c5Smrgfi
576f03b1f6Smrg
5898af18c5Smrg# Put in absolute file names if you don't have them in your path;
5998af18c5Smrg# or use environment vars.
6098af18c5Smrg
6198af18c5Smrgchgrpprog=${CHGRPPROG-chgrp}
6298af18c5Smrgchmodprog=${CHMODPROG-chmod}
6398af18c5Smrgchownprog=${CHOWNPROG-chown}
6498af18c5Smrgcmpprog=${CMPPROG-cmp}
6598af18c5Smrgcpprog=${CPPROG-cp}
6698af18c5Smrgmkdirprog=${MKDIRPROG-mkdir}
6798af18c5Smrgmvprog=${MVPROG-mv}
6898af18c5Smrgrmprog=${RMPROG-rm}
6998af18c5Smrgstripprog=${STRIPPROG-strip}
7098af18c5Smrg
7198af18c5Smrgposix_glob='?'
7298af18c5Smrginitialize_posix_glob='
7398af18c5Smrg  test "$posix_glob" != "?" || {
7498af18c5Smrg    if (set -f) 2>/dev/null; then
7598af18c5Smrg      posix_glob=
7698af18c5Smrg    else
7798af18c5Smrg      posix_glob=:
7898af18c5Smrg    fi
7998af18c5Smrg  }
8098af18c5Smrg'
816f03b1f6Smrg
8298af18c5Smrgposix_mkdir=
8398af18c5Smrg
8498af18c5Smrg# Desired mode of installed file.
8598af18c5Smrgmode=0755
866f03b1f6Smrg
876f03b1f6Smrgchgrpcmd=
8898af18c5Smrgchmodcmd=$chmodprog
8998af18c5Smrgchowncmd=
9098af18c5Smrgmvcmd=$mvprog
916f03b1f6Smrgrmcmd="$rmprog -f"
9298af18c5Smrgstripcmd=
9398af18c5Smrg
946f03b1f6Smrgsrc=
956f03b1f6Smrgdst=
966f03b1f6Smrgdir_arg=
9798af18c5Smrgdst_arg=
9898af18c5Smrg
9998af18c5Smrgcopy_on_change=false
1006f03b1f6Smrgno_target_directory=
1016f03b1f6Smrg
10298af18c5Smrgusage="\
10398af18c5SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
1046f03b1f6Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
1056f03b1f6Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
1066f03b1f6Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1076f03b1f6Smrg
1086f03b1f6SmrgIn the 1st form, copy SRCFILE to DSTFILE.
1096f03b1f6SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
1106f03b1f6SmrgIn the 4th, create DIRECTORIES.
1116f03b1f6Smrg
1126f03b1f6SmrgOptions:
11398af18c5Smrg     --help     display this help and exit.
11498af18c5Smrg     --version  display version info and exit.
11598af18c5Smrg
11698af18c5Smrg  -c            (ignored)
11798af18c5Smrg  -C            install only if different (preserve the last data modification time)
11898af18c5Smrg  -d            create directories instead of installing files.
11998af18c5Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12098af18c5Smrg  -m MODE       $chmodprog installed files to MODE.
12198af18c5Smrg  -o USER       $chownprog installed files to USER.
12298af18c5Smrg  -s            $stripprog installed files.
12398af18c5Smrg  -t DIRECTORY  install into DIRECTORY.
12498af18c5Smrg  -T            report an error if DSTFILE is a directory.
1256f03b1f6Smrg
1266f03b1f6SmrgEnvironment variables override the default commands:
12798af18c5Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12898af18c5Smrg  RMPROG STRIPPROG
1296f03b1f6Smrg"
1306f03b1f6Smrg
13198af18c5Smrgwhile test $# -ne 0; do
1326f03b1f6Smrg  case $1 in
13398af18c5Smrg    -c) ;;
13498af18c5Smrg
13598af18c5Smrg    -C) copy_on_change=true;;
1366f03b1f6Smrg
13798af18c5Smrg    -d) dir_arg=true;;
1386f03b1f6Smrg
1396f03b1f6Smrg    -g) chgrpcmd="$chgrpprog $2"
14098af18c5Smrg	shift;;
1416f03b1f6Smrg
1426f03b1f6Smrg    --help) echo "$usage"; exit $?;;
1436f03b1f6Smrg
14498af18c5Smrg    -m) mode=$2
14598af18c5Smrg	case $mode in
14698af18c5Smrg	  *' '* | *'	'* | *'
14798af18c5Smrg'*	  | *'*'* | *'?'* | *'['*)
14898af18c5Smrg	    echo "$0: invalid mode: $mode" >&2
14998af18c5Smrg	    exit 1;;
15098af18c5Smrg	esac
15198af18c5Smrg	shift;;
1526f03b1f6Smrg
1536f03b1f6Smrg    -o) chowncmd="$chownprog $2"
15498af18c5Smrg	shift;;
1556f03b1f6Smrg
15698af18c5Smrg    -s) stripcmd=$stripprog;;
1576f03b1f6Smrg
15898af18c5Smrg    -t) dst_arg=$2
15998af18c5Smrg	shift;;
1606f03b1f6Smrg
16198af18c5Smrg    -T) no_target_directory=true;;
1626f03b1f6Smrg
1636f03b1f6Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1646f03b1f6Smrg
16598af18c5Smrg    --)	shift
1666f03b1f6Smrg	break;;
16798af18c5Smrg
16898af18c5Smrg    -*)	echo "$0: invalid option: $1" >&2
16998af18c5Smrg	exit 1;;
17098af18c5Smrg
17198af18c5Smrg    *)  break;;
1726f03b1f6Smrg  esac
17398af18c5Smrg  shift
1746f03b1f6Smrgdone
1756f03b1f6Smrg
17698af18c5Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17798af18c5Smrg  # When -d is used, all remaining arguments are directories to create.
17898af18c5Smrg  # When -t is used, the destination is already specified.
17998af18c5Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18098af18c5Smrg  for arg
18198af18c5Smrg  do
18298af18c5Smrg    if test -n "$dst_arg"; then
18398af18c5Smrg      # $@ is not empty: it contains at least $arg.
18498af18c5Smrg      set fnord "$@" "$dst_arg"
18598af18c5Smrg      shift # fnord
18698af18c5Smrg    fi
18798af18c5Smrg    shift # arg
18898af18c5Smrg    dst_arg=$arg
18998af18c5Smrg  done
19098af18c5Smrgfi
19198af18c5Smrg
19298af18c5Smrgif test $# -eq 0; then
1936f03b1f6Smrg  if test -z "$dir_arg"; then
1946f03b1f6Smrg    echo "$0: no input file specified." >&2
1956f03b1f6Smrg    exit 1
1966f03b1f6Smrg  fi
1976f03b1f6Smrg  # It's OK to call `install-sh -d' without argument.
1986f03b1f6Smrg  # This can happen when creating conditional directories.
1996f03b1f6Smrg  exit 0
2006f03b1f6Smrgfi
2016f03b1f6Smrg
20298af18c5Smrgif test -z "$dir_arg"; then
20398af18c5Smrg  trap '(exit $?); exit' 1 2 13 15
20498af18c5Smrg
20598af18c5Smrg  # Set umask so as not to create temps with too-generous modes.
20698af18c5Smrg  # However, 'strip' requires both read and write access to temps.
20798af18c5Smrg  case $mode in
20898af18c5Smrg    # Optimize common cases.
20998af18c5Smrg    *644) cp_umask=133;;
21098af18c5Smrg    *755) cp_umask=22;;
21198af18c5Smrg
21298af18c5Smrg    *[0-7])
21398af18c5Smrg      if test -z "$stripcmd"; then
21498af18c5Smrg	u_plus_rw=
21598af18c5Smrg      else
21698af18c5Smrg	u_plus_rw='% 200'
21798af18c5Smrg      fi
21898af18c5Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21998af18c5Smrg    *)
22098af18c5Smrg      if test -z "$stripcmd"; then
22198af18c5Smrg	u_plus_rw=
22298af18c5Smrg      else
22398af18c5Smrg	u_plus_rw=,u+rw
22498af18c5Smrg      fi
22598af18c5Smrg      cp_umask=$mode$u_plus_rw;;
22698af18c5Smrg  esac
22798af18c5Smrgfi
22898af18c5Smrg
2296f03b1f6Smrgfor src
2306f03b1f6Smrgdo
2316f03b1f6Smrg  # Protect names starting with `-'.
2326f03b1f6Smrg  case $src in
23398af18c5Smrg    -*) src=./$src;;
2346f03b1f6Smrg  esac
2356f03b1f6Smrg
2366f03b1f6Smrg  if test -n "$dir_arg"; then
2376f03b1f6Smrg    dst=$src
23898af18c5Smrg    dstdir=$dst
23998af18c5Smrg    test -d "$dstdir"
24098af18c5Smrg    dstdir_status=$?
2416f03b1f6Smrg  else
24298af18c5Smrg
2436f03b1f6Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2446f03b1f6Smrg    # might cause directories to be created, which would be especially bad
2456f03b1f6Smrg    # if $src (and thus $dsttmp) contains '*'.
2466f03b1f6Smrg    if test ! -f "$src" && test ! -d "$src"; then
2476f03b1f6Smrg      echo "$0: $src does not exist." >&2
2486f03b1f6Smrg      exit 1
2496f03b1f6Smrg    fi
2506f03b1f6Smrg
25198af18c5Smrg    if test -z "$dst_arg"; then
2526f03b1f6Smrg      echo "$0: no destination specified." >&2
2536f03b1f6Smrg      exit 1
2546f03b1f6Smrg    fi
2556f03b1f6Smrg
25698af18c5Smrg    dst=$dst_arg
2576f03b1f6Smrg    # Protect names starting with `-'.
2586f03b1f6Smrg    case $dst in
25998af18c5Smrg      -*) dst=./$dst;;
2606f03b1f6Smrg    esac
2616f03b1f6Smrg
2626f03b1f6Smrg    # If destination is a directory, append the input filename; won't work
2636f03b1f6Smrg    # if double slashes aren't ignored.
2646f03b1f6Smrg    if test -d "$dst"; then
2656f03b1f6Smrg      if test -n "$no_target_directory"; then
26698af18c5Smrg	echo "$0: $dst_arg: Is a directory" >&2
2676f03b1f6Smrg	exit 1
2686f03b1f6Smrg      fi
26998af18c5Smrg      dstdir=$dst
27098af18c5Smrg      dst=$dstdir/`basename "$src"`
27198af18c5Smrg      dstdir_status=0
27298af18c5Smrg    else
27398af18c5Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27498af18c5Smrg      dstdir=`
27598af18c5Smrg	(dirname "$dst") 2>/dev/null ||
27698af18c5Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27798af18c5Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27898af18c5Smrg	     X"$dst" : 'X\(//\)$' \| \
27998af18c5Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28098af18c5Smrg	echo X"$dst" |
28198af18c5Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28298af18c5Smrg		   s//\1/
28398af18c5Smrg		   q
28498af18c5Smrg		 }
28598af18c5Smrg		 /^X\(\/\/\)[^/].*/{
28698af18c5Smrg		   s//\1/
28798af18c5Smrg		   q
28898af18c5Smrg		 }
28998af18c5Smrg		 /^X\(\/\/\)$/{
29098af18c5Smrg		   s//\1/
29198af18c5Smrg		   q
29298af18c5Smrg		 }
29398af18c5Smrg		 /^X\(\/\).*/{
29498af18c5Smrg		   s//\1/
29598af18c5Smrg		   q
29698af18c5Smrg		 }
29798af18c5Smrg		 s/.*/./; q'
29898af18c5Smrg      `
29998af18c5Smrg
30098af18c5Smrg      test -d "$dstdir"
30198af18c5Smrg      dstdir_status=$?
3026f03b1f6Smrg    fi
3036f03b1f6Smrg  fi
3046f03b1f6Smrg
30598af18c5Smrg  obsolete_mkdir_used=false
30698af18c5Smrg
30798af18c5Smrg  if test $dstdir_status != 0; then
30898af18c5Smrg    case $posix_mkdir in
30998af18c5Smrg      '')
31098af18c5Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31198af18c5Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31298af18c5Smrg	umask=`umask`
31398af18c5Smrg	case $stripcmd.$umask in
31498af18c5Smrg	  # Optimize common cases.
31598af18c5Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31698af18c5Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31798af18c5Smrg
31898af18c5Smrg	  *[0-7])
31998af18c5Smrg	    mkdir_umask=`expr $umask + 22 \
32098af18c5Smrg	      - $umask % 100 % 40 + $umask % 20 \
32198af18c5Smrg	      - $umask % 10 % 4 + $umask % 2
32298af18c5Smrg	    `;;
32398af18c5Smrg	  *) mkdir_umask=$umask,go-w;;
32498af18c5Smrg	esac
32598af18c5Smrg
32698af18c5Smrg	# With -d, create the new directory with the user-specified mode.
32798af18c5Smrg	# Otherwise, rely on $mkdir_umask.
32898af18c5Smrg	if test -n "$dir_arg"; then
32998af18c5Smrg	  mkdir_mode=-m$mode
33098af18c5Smrg	else
33198af18c5Smrg	  mkdir_mode=
33298af18c5Smrg	fi
33398af18c5Smrg
33498af18c5Smrg	posix_mkdir=false
33598af18c5Smrg	case $umask in
33698af18c5Smrg	  *[123567][0-7][0-7])
33798af18c5Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33898af18c5Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33998af18c5Smrg	    ;;
34098af18c5Smrg	  *)
34198af18c5Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34298af18c5Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34398af18c5Smrg
34498af18c5Smrg	    if (umask $mkdir_umask &&
34598af18c5Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34698af18c5Smrg	    then
34798af18c5Smrg	      if test -z "$dir_arg" || {
34898af18c5Smrg		   # Check for POSIX incompatibilities with -m.
34998af18c5Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35098af18c5Smrg		   # other-writeable bit of parent directory when it shouldn't.
35198af18c5Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35298af18c5Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35398af18c5Smrg		   case $ls_ld_tmpdir in
35498af18c5Smrg		     d????-?r-*) different_mode=700;;
35598af18c5Smrg		     d????-?--*) different_mode=755;;
35698af18c5Smrg		     *) false;;
35798af18c5Smrg		   esac &&
35898af18c5Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35998af18c5Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36098af18c5Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36198af18c5Smrg		   }
36298af18c5Smrg		 }
36398af18c5Smrg	      then posix_mkdir=:
36498af18c5Smrg	      fi
36598af18c5Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36698af18c5Smrg	    else
36798af18c5Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36898af18c5Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36998af18c5Smrg	    fi
37098af18c5Smrg	    trap '' 0;;
37198af18c5Smrg	esac;;
37298af18c5Smrg    esac
3736f03b1f6Smrg
37498af18c5Smrg    if
37598af18c5Smrg      $posix_mkdir && (
37698af18c5Smrg	umask $mkdir_umask &&
37798af18c5Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37898af18c5Smrg      )
37998af18c5Smrg    then :
38098af18c5Smrg    else
3816f03b1f6Smrg
38298af18c5Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38398af18c5Smrg      # or it failed possibly due to a race condition.  Create the
38498af18c5Smrg      # directory the slow way, step by step, checking for races as we go.
3856f03b1f6Smrg
38698af18c5Smrg      case $dstdir in
38798af18c5Smrg	/*) prefix='/';;
38898af18c5Smrg	-*) prefix='./';;
38998af18c5Smrg	*)  prefix='';;
39098af18c5Smrg      esac
3916f03b1f6Smrg
39298af18c5Smrg      eval "$initialize_posix_glob"
3936f03b1f6Smrg
39498af18c5Smrg      oIFS=$IFS
39598af18c5Smrg      IFS=/
39698af18c5Smrg      $posix_glob set -f
39798af18c5Smrg      set fnord $dstdir
3986f03b1f6Smrg      shift
39998af18c5Smrg      $posix_glob set +f
40098af18c5Smrg      IFS=$oIFS
40198af18c5Smrg
40298af18c5Smrg      prefixes=
40398af18c5Smrg
40498af18c5Smrg      for d
40598af18c5Smrg      do
40698af18c5Smrg	test -z "$d" && continue
40798af18c5Smrg
40898af18c5Smrg	prefix=$prefix$d
40998af18c5Smrg	if test -d "$prefix"; then
41098af18c5Smrg	  prefixes=
41198af18c5Smrg	else
41298af18c5Smrg	  if $posix_mkdir; then
41398af18c5Smrg	    (umask=$mkdir_umask &&
41498af18c5Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41598af18c5Smrg	    # Don't fail if two instances are running concurrently.
41698af18c5Smrg	    test -d "$prefix" || exit 1
41798af18c5Smrg	  else
41898af18c5Smrg	    case $prefix in
41998af18c5Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42098af18c5Smrg	      *) qprefix=$prefix;;
42198af18c5Smrg	    esac
42298af18c5Smrg	    prefixes="$prefixes '$qprefix'"
42398af18c5Smrg	  fi
42498af18c5Smrg	fi
42598af18c5Smrg	prefix=$prefix/
42698af18c5Smrg      done
42798af18c5Smrg
42898af18c5Smrg      if test -n "$prefixes"; then
42998af18c5Smrg	# Don't fail if two instances are running concurrently.
43098af18c5Smrg	(umask $mkdir_umask &&
43198af18c5Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43298af18c5Smrg	  test -d "$dstdir" || exit 1
43398af18c5Smrg	obsolete_mkdir_used=true
4346f03b1f6Smrg      fi
43598af18c5Smrg    fi
4366f03b1f6Smrg  fi
4376f03b1f6Smrg
4386f03b1f6Smrg  if test -n "$dir_arg"; then
43998af18c5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44098af18c5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44198af18c5Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44298af18c5Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4436f03b1f6Smrg  else
4446f03b1f6Smrg
4456f03b1f6Smrg    # Make a couple of temp file names in the proper directory.
4466f03b1f6Smrg    dsttmp=$dstdir/_inst.$$_
4476f03b1f6Smrg    rmtmp=$dstdir/_rm.$$_
4486f03b1f6Smrg
4496f03b1f6Smrg    # Trap to clean up those temp files at exit.
4506f03b1f6Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4516f03b1f6Smrg
4526f03b1f6Smrg    # Copy the file name to the temp name.
45398af18c5Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4546f03b1f6Smrg
4556f03b1f6Smrg    # and set any options; do chmod last to preserve setuid bits.
4566f03b1f6Smrg    #
4576f03b1f6Smrg    # If any of these fail, we abort the whole thing.  If we want to
4586f03b1f6Smrg    # ignore errors from any of these, just make sure not to ignore
4596f03b1f6Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4606f03b1f6Smrg    #
46198af18c5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46298af18c5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46398af18c5Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46498af18c5Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46598af18c5Smrg
46698af18c5Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46798af18c5Smrg    if $copy_on_change &&
46898af18c5Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46998af18c5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47098af18c5Smrg
47198af18c5Smrg       eval "$initialize_posix_glob" &&
47298af18c5Smrg       $posix_glob set -f &&
47398af18c5Smrg       set X $old && old=:$2:$4:$5:$6 &&
47498af18c5Smrg       set X $new && new=:$2:$4:$5:$6 &&
47598af18c5Smrg       $posix_glob set +f &&
47698af18c5Smrg
47798af18c5Smrg       test "$old" = "$new" &&
47898af18c5Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47998af18c5Smrg    then
48098af18c5Smrg      rm -f "$dsttmp"
48198af18c5Smrg    else
48298af18c5Smrg      # Rename the file to the real destination.
48398af18c5Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48498af18c5Smrg
48598af18c5Smrg      # The rename failed, perhaps because mv can't rename something else
48698af18c5Smrg      # to itself, or perhaps because mv is so ancient that it does not
48798af18c5Smrg      # support -f.
48898af18c5Smrg      {
48998af18c5Smrg	# Now remove or move aside any old file at destination location.
49098af18c5Smrg	# We try this two ways since rm can't unlink itself on some
49198af18c5Smrg	# systems and the destination file might be busy for other
49298af18c5Smrg	# reasons.  In this case, the final cleanup might fail but the new
49398af18c5Smrg	# file should still install successfully.
49498af18c5Smrg	{
49598af18c5Smrg	  test ! -f "$dst" ||
49698af18c5Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49798af18c5Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49898af18c5Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49998af18c5Smrg	  } ||
50098af18c5Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50198af18c5Smrg	    (exit 1); exit 1
50298af18c5Smrg	  }
50398af18c5Smrg	} &&
50498af18c5Smrg
50598af18c5Smrg	# Now rename the file to the real destination.
50698af18c5Smrg	$doit $mvcmd "$dsttmp" "$dst"
50798af18c5Smrg      }
50898af18c5Smrg    fi || exit 1
50998af18c5Smrg
51098af18c5Smrg    trap '' 0
51198af18c5Smrg  fi
5126f03b1f6Smrgdone
5136f03b1f6Smrg
5146f03b1f6Smrg# Local variables:
5156f03b1f6Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
5166f03b1f6Smrg# time-stamp-start: "scriptversion="
5176f03b1f6Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51898af18c5Smrg# time-stamp-time-zone: "UTC"
51998af18c5Smrg# time-stamp-end: "; # UTC"
5206f03b1f6Smrg# End:
521