install-sh revision 0d590c07
1c76ae52dSmrg#!/bin/sh
2c76ae52dSmrg# install - install a program, script, or datafile
3c76ae52dSmrg
40d590c07Smrgscriptversion=2009-04-28.21; # UTC
5c76ae52dSmrg
6c76ae52dSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c76ae52dSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8c76ae52dSmrg# following copyright and license.
9c76ae52dSmrg#
10c76ae52dSmrg# Copyright (C) 1994 X Consortium
11c76ae52dSmrg#
12c76ae52dSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c76ae52dSmrg# of this software and associated documentation files (the "Software"), to
14c76ae52dSmrg# deal in the Software without restriction, including without limitation the
15c76ae52dSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c76ae52dSmrg# sell copies of the Software, and to permit persons to whom the Software is
17c76ae52dSmrg# furnished to do so, subject to the following conditions:
18c76ae52dSmrg#
19c76ae52dSmrg# The above copyright notice and this permission notice shall be included in
20c76ae52dSmrg# all copies or substantial portions of the Software.
21c76ae52dSmrg#
22c76ae52dSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c76ae52dSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c76ae52dSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c76ae52dSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c76ae52dSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c76ae52dSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c76ae52dSmrg#
29c76ae52dSmrg# Except as contained in this notice, the name of the X Consortium shall not
30c76ae52dSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c76ae52dSmrg# ings in this Software without prior written authorization from the X Consor-
32c76ae52dSmrg# tium.
33c76ae52dSmrg#
34c76ae52dSmrg#
35c76ae52dSmrg# FSF changes to this file are in the public domain.
36c76ae52dSmrg#
37c76ae52dSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38c76ae52dSmrg# `make' implicit rules from creating a file called install from it
39c76ae52dSmrg# when there is no Makefile.
40c76ae52dSmrg#
41c76ae52dSmrg# This script is compatible with the BSD install script, but was written
420d590c07Smrg# from scratch.
430d590c07Smrg
440d590c07Smrgnl='
450d590c07Smrg'
460d590c07SmrgIFS=" ""	$nl"
47c76ae52dSmrg
48c76ae52dSmrg# set DOITPROG to echo to test this script
49c76ae52dSmrg
50c76ae52dSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
510d590c07Smrgdoit=${DOITPROG-}
520d590c07Smrgif test -z "$doit"; then
530d590c07Smrg  doit_exec=exec
540d590c07Smrgelse
550d590c07Smrg  doit_exec=$doit
560d590c07Smrgfi
57c76ae52dSmrg
580d590c07Smrg# Put in absolute file names if you don't have them in your path;
590d590c07Smrg# or use environment vars.
600d590c07Smrg
610d590c07Smrgchgrpprog=${CHGRPPROG-chgrp}
620d590c07Smrgchmodprog=${CHMODPROG-chmod}
630d590c07Smrgchownprog=${CHOWNPROG-chown}
640d590c07Smrgcmpprog=${CMPPROG-cmp}
650d590c07Smrgcpprog=${CPPROG-cp}
660d590c07Smrgmkdirprog=${MKDIRPROG-mkdir}
670d590c07Smrgmvprog=${MVPROG-mv}
680d590c07Smrgrmprog=${RMPROG-rm}
690d590c07Smrgstripprog=${STRIPPROG-strip}
700d590c07Smrg
710d590c07Smrgposix_glob='?'
720d590c07Smrginitialize_posix_glob='
730d590c07Smrg  test "$posix_glob" != "?" || {
740d590c07Smrg    if (set -f) 2>/dev/null; then
750d590c07Smrg      posix_glob=
760d590c07Smrg    else
770d590c07Smrg      posix_glob=:
780d590c07Smrg    fi
790d590c07Smrg  }
800d590c07Smrg'
81c76ae52dSmrg
820d590c07Smrgposix_mkdir=
830d590c07Smrg
840d590c07Smrg# Desired mode of installed file.
850d590c07Smrgmode=0755
86c76ae52dSmrg
87c76ae52dSmrgchgrpcmd=
880d590c07Smrgchmodcmd=$chmodprog
890d590c07Smrgchowncmd=
900d590c07Smrgmvcmd=$mvprog
91c76ae52dSmrgrmcmd="$rmprog -f"
920d590c07Smrgstripcmd=
930d590c07Smrg
94c76ae52dSmrgsrc=
95c76ae52dSmrgdst=
96c76ae52dSmrgdir_arg=
970d590c07Smrgdst_arg=
980d590c07Smrg
990d590c07Smrgcopy_on_change=false
100c76ae52dSmrgno_target_directory=
101c76ae52dSmrg
1020d590c07Smrgusage="\
1030d590c07SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104c76ae52dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105c76ae52dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106c76ae52dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107c76ae52dSmrg
108c76ae52dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109c76ae52dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110c76ae52dSmrgIn the 4th, create DIRECTORIES.
111c76ae52dSmrg
112c76ae52dSmrgOptions:
1130d590c07Smrg     --help     display this help and exit.
1140d590c07Smrg     --version  display version info and exit.
1150d590c07Smrg
1160d590c07Smrg  -c            (ignored)
1170d590c07Smrg  -C            install only if different (preserve the last data modification time)
1180d590c07Smrg  -d            create directories instead of installing files.
1190d590c07Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1200d590c07Smrg  -m MODE       $chmodprog installed files to MODE.
1210d590c07Smrg  -o USER       $chownprog installed files to USER.
1220d590c07Smrg  -s            $stripprog installed files.
1230d590c07Smrg  -t DIRECTORY  install into DIRECTORY.
1240d590c07Smrg  -T            report an error if DSTFILE is a directory.
125c76ae52dSmrg
126c76ae52dSmrgEnvironment variables override the default commands:
1270d590c07Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1280d590c07Smrg  RMPROG STRIPPROG
129c76ae52dSmrg"
130c76ae52dSmrg
1310d590c07Smrgwhile test $# -ne 0; do
132c76ae52dSmrg  case $1 in
1330d590c07Smrg    -c) ;;
1340d590c07Smrg
1350d590c07Smrg    -C) copy_on_change=true;;
136c76ae52dSmrg
1370d590c07Smrg    -d) dir_arg=true;;
138c76ae52dSmrg
139c76ae52dSmrg    -g) chgrpcmd="$chgrpprog $2"
1400d590c07Smrg	shift;;
141c76ae52dSmrg
142c76ae52dSmrg    --help) echo "$usage"; exit $?;;
143c76ae52dSmrg
1440d590c07Smrg    -m) mode=$2
1450d590c07Smrg	case $mode in
1460d590c07Smrg	  *' '* | *'	'* | *'
1470d590c07Smrg'*	  | *'*'* | *'?'* | *'['*)
1480d590c07Smrg	    echo "$0: invalid mode: $mode" >&2
1490d590c07Smrg	    exit 1;;
1500d590c07Smrg	esac
1510d590c07Smrg	shift;;
152c76ae52dSmrg
153c76ae52dSmrg    -o) chowncmd="$chownprog $2"
1540d590c07Smrg	shift;;
155c76ae52dSmrg
1560d590c07Smrg    -s) stripcmd=$stripprog;;
157c76ae52dSmrg
1580d590c07Smrg    -t) dst_arg=$2
1590d590c07Smrg	shift;;
160c76ae52dSmrg
1610d590c07Smrg    -T) no_target_directory=true;;
162c76ae52dSmrg
163c76ae52dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164c76ae52dSmrg
1650d590c07Smrg    --)	shift
166c76ae52dSmrg	break;;
1670d590c07Smrg
1680d590c07Smrg    -*)	echo "$0: invalid option: $1" >&2
1690d590c07Smrg	exit 1;;
1700d590c07Smrg
1710d590c07Smrg    *)  break;;
172c76ae52dSmrg  esac
1730d590c07Smrg  shift
174c76ae52dSmrgdone
175c76ae52dSmrg
1760d590c07Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1770d590c07Smrg  # When -d is used, all remaining arguments are directories to create.
1780d590c07Smrg  # When -t is used, the destination is already specified.
1790d590c07Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1800d590c07Smrg  for arg
1810d590c07Smrg  do
1820d590c07Smrg    if test -n "$dst_arg"; then
1830d590c07Smrg      # $@ is not empty: it contains at least $arg.
1840d590c07Smrg      set fnord "$@" "$dst_arg"
1850d590c07Smrg      shift # fnord
1860d590c07Smrg    fi
1870d590c07Smrg    shift # arg
1880d590c07Smrg    dst_arg=$arg
1890d590c07Smrg  done
1900d590c07Smrgfi
1910d590c07Smrg
1920d590c07Smrgif test $# -eq 0; then
193c76ae52dSmrg  if test -z "$dir_arg"; then
194c76ae52dSmrg    echo "$0: no input file specified." >&2
195c76ae52dSmrg    exit 1
196c76ae52dSmrg  fi
197c76ae52dSmrg  # It's OK to call `install-sh -d' without argument.
198c76ae52dSmrg  # This can happen when creating conditional directories.
199c76ae52dSmrg  exit 0
200c76ae52dSmrgfi
201c76ae52dSmrg
2020d590c07Smrgif test -z "$dir_arg"; then
2030d590c07Smrg  trap '(exit $?); exit' 1 2 13 15
2040d590c07Smrg
2050d590c07Smrg  # Set umask so as not to create temps with too-generous modes.
2060d590c07Smrg  # However, 'strip' requires both read and write access to temps.
2070d590c07Smrg  case $mode in
2080d590c07Smrg    # Optimize common cases.
2090d590c07Smrg    *644) cp_umask=133;;
2100d590c07Smrg    *755) cp_umask=22;;
2110d590c07Smrg
2120d590c07Smrg    *[0-7])
2130d590c07Smrg      if test -z "$stripcmd"; then
2140d590c07Smrg	u_plus_rw=
2150d590c07Smrg      else
2160d590c07Smrg	u_plus_rw='% 200'
2170d590c07Smrg      fi
2180d590c07Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2190d590c07Smrg    *)
2200d590c07Smrg      if test -z "$stripcmd"; then
2210d590c07Smrg	u_plus_rw=
2220d590c07Smrg      else
2230d590c07Smrg	u_plus_rw=,u+rw
2240d590c07Smrg      fi
2250d590c07Smrg      cp_umask=$mode$u_plus_rw;;
2260d590c07Smrg  esac
2270d590c07Smrgfi
2280d590c07Smrg
229c76ae52dSmrgfor src
230c76ae52dSmrgdo
231c76ae52dSmrg  # Protect names starting with `-'.
232c76ae52dSmrg  case $src in
2330d590c07Smrg    -*) src=./$src;;
234c76ae52dSmrg  esac
235c76ae52dSmrg
236c76ae52dSmrg  if test -n "$dir_arg"; then
237c76ae52dSmrg    dst=$src
2380d590c07Smrg    dstdir=$dst
2390d590c07Smrg    test -d "$dstdir"
2400d590c07Smrg    dstdir_status=$?
241c76ae52dSmrg  else
2420d590c07Smrg
243c76ae52dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244c76ae52dSmrg    # might cause directories to be created, which would be especially bad
245c76ae52dSmrg    # if $src (and thus $dsttmp) contains '*'.
246c76ae52dSmrg    if test ! -f "$src" && test ! -d "$src"; then
247c76ae52dSmrg      echo "$0: $src does not exist." >&2
248c76ae52dSmrg      exit 1
249c76ae52dSmrg    fi
250c76ae52dSmrg
2510d590c07Smrg    if test -z "$dst_arg"; then
252c76ae52dSmrg      echo "$0: no destination specified." >&2
253c76ae52dSmrg      exit 1
254c76ae52dSmrg    fi
255c76ae52dSmrg
2560d590c07Smrg    dst=$dst_arg
257c76ae52dSmrg    # Protect names starting with `-'.
258c76ae52dSmrg    case $dst in
2590d590c07Smrg      -*) dst=./$dst;;
260c76ae52dSmrg    esac
261c76ae52dSmrg
262c76ae52dSmrg    # If destination is a directory, append the input filename; won't work
263c76ae52dSmrg    # if double slashes aren't ignored.
264c76ae52dSmrg    if test -d "$dst"; then
265c76ae52dSmrg      if test -n "$no_target_directory"; then
2660d590c07Smrg	echo "$0: $dst_arg: Is a directory" >&2
267c76ae52dSmrg	exit 1
268c76ae52dSmrg      fi
2690d590c07Smrg      dstdir=$dst
2700d590c07Smrg      dst=$dstdir/`basename "$src"`
2710d590c07Smrg      dstdir_status=0
2720d590c07Smrg    else
2730d590c07Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2740d590c07Smrg      dstdir=`
2750d590c07Smrg	(dirname "$dst") 2>/dev/null ||
2760d590c07Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2770d590c07Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2780d590c07Smrg	     X"$dst" : 'X\(//\)$' \| \
2790d590c07Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2800d590c07Smrg	echo X"$dst" |
2810d590c07Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2820d590c07Smrg		   s//\1/
2830d590c07Smrg		   q
2840d590c07Smrg		 }
2850d590c07Smrg		 /^X\(\/\/\)[^/].*/{
2860d590c07Smrg		   s//\1/
2870d590c07Smrg		   q
2880d590c07Smrg		 }
2890d590c07Smrg		 /^X\(\/\/\)$/{
2900d590c07Smrg		   s//\1/
2910d590c07Smrg		   q
2920d590c07Smrg		 }
2930d590c07Smrg		 /^X\(\/\).*/{
2940d590c07Smrg		   s//\1/
2950d590c07Smrg		   q
2960d590c07Smrg		 }
2970d590c07Smrg		 s/.*/./; q'
2980d590c07Smrg      `
2990d590c07Smrg
3000d590c07Smrg      test -d "$dstdir"
3010d590c07Smrg      dstdir_status=$?
302c76ae52dSmrg    fi
303c76ae52dSmrg  fi
304c76ae52dSmrg
3050d590c07Smrg  obsolete_mkdir_used=false
3060d590c07Smrg
3070d590c07Smrg  if test $dstdir_status != 0; then
3080d590c07Smrg    case $posix_mkdir in
3090d590c07Smrg      '')
3100d590c07Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3110d590c07Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3120d590c07Smrg	umask=`umask`
3130d590c07Smrg	case $stripcmd.$umask in
3140d590c07Smrg	  # Optimize common cases.
3150d590c07Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3160d590c07Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3170d590c07Smrg
3180d590c07Smrg	  *[0-7])
3190d590c07Smrg	    mkdir_umask=`expr $umask + 22 \
3200d590c07Smrg	      - $umask % 100 % 40 + $umask % 20 \
3210d590c07Smrg	      - $umask % 10 % 4 + $umask % 2
3220d590c07Smrg	    `;;
3230d590c07Smrg	  *) mkdir_umask=$umask,go-w;;
3240d590c07Smrg	esac
3250d590c07Smrg
3260d590c07Smrg	# With -d, create the new directory with the user-specified mode.
3270d590c07Smrg	# Otherwise, rely on $mkdir_umask.
3280d590c07Smrg	if test -n "$dir_arg"; then
3290d590c07Smrg	  mkdir_mode=-m$mode
3300d590c07Smrg	else
3310d590c07Smrg	  mkdir_mode=
3320d590c07Smrg	fi
3330d590c07Smrg
3340d590c07Smrg	posix_mkdir=false
3350d590c07Smrg	case $umask in
3360d590c07Smrg	  *[123567][0-7][0-7])
3370d590c07Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3380d590c07Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3390d590c07Smrg	    ;;
3400d590c07Smrg	  *)
3410d590c07Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3420d590c07Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3430d590c07Smrg
3440d590c07Smrg	    if (umask $mkdir_umask &&
3450d590c07Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3460d590c07Smrg	    then
3470d590c07Smrg	      if test -z "$dir_arg" || {
3480d590c07Smrg		   # Check for POSIX incompatibilities with -m.
3490d590c07Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3500d590c07Smrg		   # other-writeable bit of parent directory when it shouldn't.
3510d590c07Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3520d590c07Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3530d590c07Smrg		   case $ls_ld_tmpdir in
3540d590c07Smrg		     d????-?r-*) different_mode=700;;
3550d590c07Smrg		     d????-?--*) different_mode=755;;
3560d590c07Smrg		     *) false;;
3570d590c07Smrg		   esac &&
3580d590c07Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3590d590c07Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3600d590c07Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3610d590c07Smrg		   }
3620d590c07Smrg		 }
3630d590c07Smrg	      then posix_mkdir=:
3640d590c07Smrg	      fi
3650d590c07Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3660d590c07Smrg	    else
3670d590c07Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3680d590c07Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3690d590c07Smrg	    fi
3700d590c07Smrg	    trap '' 0;;
3710d590c07Smrg	esac;;
3720d590c07Smrg    esac
373c76ae52dSmrg
3740d590c07Smrg    if
3750d590c07Smrg      $posix_mkdir && (
3760d590c07Smrg	umask $mkdir_umask &&
3770d590c07Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3780d590c07Smrg      )
3790d590c07Smrg    then :
3800d590c07Smrg    else
381c76ae52dSmrg
3820d590c07Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3830d590c07Smrg      # or it failed possibly due to a race condition.  Create the
3840d590c07Smrg      # directory the slow way, step by step, checking for races as we go.
385c76ae52dSmrg
3860d590c07Smrg      case $dstdir in
3870d590c07Smrg	/*) prefix='/';;
3880d590c07Smrg	-*) prefix='./';;
3890d590c07Smrg	*)  prefix='';;
3900d590c07Smrg      esac
391c76ae52dSmrg
3920d590c07Smrg      eval "$initialize_posix_glob"
393c76ae52dSmrg
3940d590c07Smrg      oIFS=$IFS
3950d590c07Smrg      IFS=/
3960d590c07Smrg      $posix_glob set -f
3970d590c07Smrg      set fnord $dstdir
398c76ae52dSmrg      shift
3990d590c07Smrg      $posix_glob set +f
4000d590c07Smrg      IFS=$oIFS
4010d590c07Smrg
4020d590c07Smrg      prefixes=
4030d590c07Smrg
4040d590c07Smrg      for d
4050d590c07Smrg      do
4060d590c07Smrg	test -z "$d" && continue
4070d590c07Smrg
4080d590c07Smrg	prefix=$prefix$d
4090d590c07Smrg	if test -d "$prefix"; then
4100d590c07Smrg	  prefixes=
4110d590c07Smrg	else
4120d590c07Smrg	  if $posix_mkdir; then
4130d590c07Smrg	    (umask=$mkdir_umask &&
4140d590c07Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4150d590c07Smrg	    # Don't fail if two instances are running concurrently.
4160d590c07Smrg	    test -d "$prefix" || exit 1
4170d590c07Smrg	  else
4180d590c07Smrg	    case $prefix in
4190d590c07Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4200d590c07Smrg	      *) qprefix=$prefix;;
4210d590c07Smrg	    esac
4220d590c07Smrg	    prefixes="$prefixes '$qprefix'"
4230d590c07Smrg	  fi
4240d590c07Smrg	fi
4250d590c07Smrg	prefix=$prefix/
4260d590c07Smrg      done
4270d590c07Smrg
4280d590c07Smrg      if test -n "$prefixes"; then
4290d590c07Smrg	# Don't fail if two instances are running concurrently.
4300d590c07Smrg	(umask $mkdir_umask &&
4310d590c07Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4320d590c07Smrg	  test -d "$dstdir" || exit 1
4330d590c07Smrg	obsolete_mkdir_used=true
434c76ae52dSmrg      fi
4350d590c07Smrg    fi
436c76ae52dSmrg  fi
437c76ae52dSmrg
438c76ae52dSmrg  if test -n "$dir_arg"; then
4390d590c07Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4400d590c07Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4410d590c07Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4420d590c07Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443c76ae52dSmrg  else
444c76ae52dSmrg
445c76ae52dSmrg    # Make a couple of temp file names in the proper directory.
446c76ae52dSmrg    dsttmp=$dstdir/_inst.$$_
447c76ae52dSmrg    rmtmp=$dstdir/_rm.$$_
448c76ae52dSmrg
449c76ae52dSmrg    # Trap to clean up those temp files at exit.
450c76ae52dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451c76ae52dSmrg
452c76ae52dSmrg    # Copy the file name to the temp name.
4530d590c07Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454c76ae52dSmrg
455c76ae52dSmrg    # and set any options; do chmod last to preserve setuid bits.
456c76ae52dSmrg    #
457c76ae52dSmrg    # If any of these fail, we abort the whole thing.  If we want to
458c76ae52dSmrg    # ignore errors from any of these, just make sure not to ignore
459c76ae52dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460c76ae52dSmrg    #
4610d590c07Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4620d590c07Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4630d590c07Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4640d590c07Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4650d590c07Smrg
4660d590c07Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4670d590c07Smrg    if $copy_on_change &&
4680d590c07Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4690d590c07Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4700d590c07Smrg
4710d590c07Smrg       eval "$initialize_posix_glob" &&
4720d590c07Smrg       $posix_glob set -f &&
4730d590c07Smrg       set X $old && old=:$2:$4:$5:$6 &&
4740d590c07Smrg       set X $new && new=:$2:$4:$5:$6 &&
4750d590c07Smrg       $posix_glob set +f &&
4760d590c07Smrg
4770d590c07Smrg       test "$old" = "$new" &&
4780d590c07Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4790d590c07Smrg    then
4800d590c07Smrg      rm -f "$dsttmp"
4810d590c07Smrg    else
4820d590c07Smrg      # Rename the file to the real destination.
4830d590c07Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4840d590c07Smrg
4850d590c07Smrg      # The rename failed, perhaps because mv can't rename something else
4860d590c07Smrg      # to itself, or perhaps because mv is so ancient that it does not
4870d590c07Smrg      # support -f.
4880d590c07Smrg      {
4890d590c07Smrg	# Now remove or move aside any old file at destination location.
4900d590c07Smrg	# We try this two ways since rm can't unlink itself on some
4910d590c07Smrg	# systems and the destination file might be busy for other
4920d590c07Smrg	# reasons.  In this case, the final cleanup might fail but the new
4930d590c07Smrg	# file should still install successfully.
4940d590c07Smrg	{
4950d590c07Smrg	  test ! -f "$dst" ||
4960d590c07Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4970d590c07Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4980d590c07Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4990d590c07Smrg	  } ||
5000d590c07Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5010d590c07Smrg	    (exit 1); exit 1
5020d590c07Smrg	  }
5030d590c07Smrg	} &&
5040d590c07Smrg
5050d590c07Smrg	# Now rename the file to the real destination.
5060d590c07Smrg	$doit $mvcmd "$dsttmp" "$dst"
5070d590c07Smrg      }
5080d590c07Smrg    fi || exit 1
5090d590c07Smrg
5100d590c07Smrg    trap '' 0
5110d590c07Smrg  fi
512c76ae52dSmrgdone
513c76ae52dSmrg
514c76ae52dSmrg# Local variables:
515c76ae52dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516c76ae52dSmrg# time-stamp-start: "scriptversion="
517c76ae52dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5180d590c07Smrg# time-stamp-time-zone: "UTC"
5190d590c07Smrg# time-stamp-end: "; # UTC"
520c76ae52dSmrg# End:
521