install-sh revision 32034fa5
14ecf19b2Smrg#!/bin/sh
24ecf19b2Smrg# install - install a program, script, or datafile
332034fa5Smrg
432034fa5Smrgscriptversion=2009-04-28.21; # UTC
532034fa5Smrg
64ecf19b2Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
74ecf19b2Smrg# later released in X11R6 (xc/config/util/install.sh) with the
84ecf19b2Smrg# following copyright and license.
94ecf19b2Smrg#
104ecf19b2Smrg# Copyright (C) 1994 X Consortium
114ecf19b2Smrg#
124ecf19b2Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
134ecf19b2Smrg# of this software and associated documentation files (the "Software"), to
144ecf19b2Smrg# deal in the Software without restriction, including without limitation the
154ecf19b2Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164ecf19b2Smrg# sell copies of the Software, and to permit persons to whom the Software is
174ecf19b2Smrg# furnished to do so, subject to the following conditions:
184ecf19b2Smrg#
194ecf19b2Smrg# The above copyright notice and this permission notice shall be included in
204ecf19b2Smrg# all copies or substantial portions of the Software.
214ecf19b2Smrg#
224ecf19b2Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234ecf19b2Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244ecf19b2Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254ecf19b2Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264ecf19b2Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274ecf19b2Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284ecf19b2Smrg#
294ecf19b2Smrg# Except as contained in this notice, the name of the X Consortium shall not
304ecf19b2Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
314ecf19b2Smrg# ings in this Software without prior written authorization from the X Consor-
324ecf19b2Smrg# tium.
334ecf19b2Smrg#
344ecf19b2Smrg#
354ecf19b2Smrg# FSF changes to this file are in the public domain.
364ecf19b2Smrg#
374ecf19b2Smrg# Calling this script install-sh is preferred over install.sh, to prevent
384ecf19b2Smrg# `make' implicit rules from creating a file called install from it
394ecf19b2Smrg# when there is no Makefile.
404ecf19b2Smrg#
414ecf19b2Smrg# This script is compatible with the BSD install script, but was written
4232034fa5Smrg# from scratch.
434ecf19b2Smrg
4432034fa5Smrgnl='
4532034fa5Smrg'
4632034fa5SmrgIFS=" ""	$nl"
474ecf19b2Smrg
484ecf19b2Smrg# set DOITPROG to echo to test this script
494ecf19b2Smrg
504ecf19b2Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5132034fa5Smrgdoit=${DOITPROG-}
5232034fa5Smrgif test -z "$doit"; then
5332034fa5Smrg  doit_exec=exec
544ecf19b2Smrgelse
5532034fa5Smrg  doit_exec=$doit
564ecf19b2Smrgfi
574ecf19b2Smrg
5832034fa5Smrg# Put in absolute file names if you don't have them in your path;
5932034fa5Smrg# or use environment vars.
6032034fa5Smrg
6132034fa5Smrgchgrpprog=${CHGRPPROG-chgrp}
6232034fa5Smrgchmodprog=${CHMODPROG-chmod}
6332034fa5Smrgchownprog=${CHOWNPROG-chown}
6432034fa5Smrgcmpprog=${CMPPROG-cmp}
6532034fa5Smrgcpprog=${CPPROG-cp}
6632034fa5Smrgmkdirprog=${MKDIRPROG-mkdir}
6732034fa5Smrgmvprog=${MVPROG-mv}
6832034fa5Smrgrmprog=${RMPROG-rm}
6932034fa5Smrgstripprog=${STRIPPROG-strip}
7032034fa5Smrg
7132034fa5Smrgposix_glob='?'
7232034fa5Smrginitialize_posix_glob='
7332034fa5Smrg  test "$posix_glob" != "?" || {
7432034fa5Smrg    if (set -f) 2>/dev/null; then
7532034fa5Smrg      posix_glob=
7632034fa5Smrg    else
7732034fa5Smrg      posix_glob=:
7832034fa5Smrg    fi
7932034fa5Smrg  }
8032034fa5Smrg'
8132034fa5Smrg
8232034fa5Smrgposix_mkdir=
8332034fa5Smrg
8432034fa5Smrg# Desired mode of installed file.
8532034fa5Smrgmode=0755
8632034fa5Smrg
8732034fa5Smrgchgrpcmd=
8832034fa5Smrgchmodcmd=$chmodprog
8932034fa5Smrgchowncmd=
9032034fa5Smrgmvcmd=$mvprog
9132034fa5Smrgrmcmd="$rmprog -f"
9232034fa5Smrgstripcmd=
934ecf19b2Smrg
9432034fa5Smrgsrc=
9532034fa5Smrgdst=
9632034fa5Smrgdir_arg=
9732034fa5Smrgdst_arg=
984ecf19b2Smrg
9932034fa5Smrgcopy_on_change=false
10032034fa5Smrgno_target_directory=
1014ecf19b2Smrg
10232034fa5Smrgusage="\
10332034fa5SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
10432034fa5Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
10532034fa5Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
10632034fa5Smrg   or: $0 [OPTION]... -d DIRECTORIES...
1074ecf19b2Smrg
10832034fa5SmrgIn the 1st form, copy SRCFILE to DSTFILE.
10932034fa5SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
11032034fa5SmrgIn the 4th, create DIRECTORIES.
1114ecf19b2Smrg
11232034fa5SmrgOptions:
11332034fa5Smrg     --help     display this help and exit.
11432034fa5Smrg     --version  display version info and exit.
1154ecf19b2Smrg
11632034fa5Smrg  -c            (ignored)
11732034fa5Smrg  -C            install only if different (preserve the last data modification time)
11832034fa5Smrg  -d            create directories instead of installing files.
11932034fa5Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12032034fa5Smrg  -m MODE       $chmodprog installed files to MODE.
12132034fa5Smrg  -o USER       $chownprog installed files to USER.
12232034fa5Smrg  -s            $stripprog installed files.
12332034fa5Smrg  -t DIRECTORY  install into DIRECTORY.
12432034fa5Smrg  -T            report an error if DSTFILE is a directory.
1254ecf19b2Smrg
12632034fa5SmrgEnvironment variables override the default commands:
12732034fa5Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12832034fa5Smrg  RMPROG STRIPPROG
12932034fa5Smrg"
1304ecf19b2Smrg
13132034fa5Smrgwhile test $# -ne 0; do
13232034fa5Smrg  case $1 in
13332034fa5Smrg    -c) ;;
1344ecf19b2Smrg
13532034fa5Smrg    -C) copy_on_change=true;;
1364ecf19b2Smrg
13732034fa5Smrg    -d) dir_arg=true;;
1384ecf19b2Smrg
13932034fa5Smrg    -g) chgrpcmd="$chgrpprog $2"
14032034fa5Smrg	shift;;
1414ecf19b2Smrg
14232034fa5Smrg    --help) echo "$usage"; exit $?;;
1434ecf19b2Smrg
14432034fa5Smrg    -m) mode=$2
14532034fa5Smrg	case $mode in
14632034fa5Smrg	  *' '* | *'	'* | *'
14732034fa5Smrg'*	  | *'*'* | *'?'* | *'['*)
14832034fa5Smrg	    echo "$0: invalid mode: $mode" >&2
14932034fa5Smrg	    exit 1;;
15032034fa5Smrg	esac
15132034fa5Smrg	shift;;
1524ecf19b2Smrg
15332034fa5Smrg    -o) chowncmd="$chownprog $2"
15432034fa5Smrg	shift;;
1554ecf19b2Smrg
15632034fa5Smrg    -s) stripcmd=$stripprog;;
1574ecf19b2Smrg
15832034fa5Smrg    -t) dst_arg=$2
15932034fa5Smrg	shift;;
1604ecf19b2Smrg
16132034fa5Smrg    -T) no_target_directory=true;;
1624ecf19b2Smrg
16332034fa5Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1644ecf19b2Smrg
16532034fa5Smrg    --)	shift
16632034fa5Smrg	break;;
1674ecf19b2Smrg
16832034fa5Smrg    -*)	echo "$0: invalid option: $1" >&2
16932034fa5Smrg	exit 1;;
1704ecf19b2Smrg
17132034fa5Smrg    *)  break;;
17232034fa5Smrg  esac
17332034fa5Smrg  shift
17432034fa5Smrgdone
1754ecf19b2Smrg
17632034fa5Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17732034fa5Smrg  # When -d is used, all remaining arguments are directories to create.
17832034fa5Smrg  # When -t is used, the destination is already specified.
17932034fa5Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18032034fa5Smrg  for arg
18132034fa5Smrg  do
18232034fa5Smrg    if test -n "$dst_arg"; then
18332034fa5Smrg      # $@ is not empty: it contains at least $arg.
18432034fa5Smrg      set fnord "$@" "$dst_arg"
18532034fa5Smrg      shift # fnord
18632034fa5Smrg    fi
18732034fa5Smrg    shift # arg
18832034fa5Smrg    dst_arg=$arg
18932034fa5Smrg  done
19032034fa5Smrgfi
1914ecf19b2Smrg
19232034fa5Smrgif test $# -eq 0; then
19332034fa5Smrg  if test -z "$dir_arg"; then
19432034fa5Smrg    echo "$0: no input file specified." >&2
19532034fa5Smrg    exit 1
19632034fa5Smrg  fi
19732034fa5Smrg  # It's OK to call `install-sh -d' without argument.
19832034fa5Smrg  # This can happen when creating conditional directories.
19932034fa5Smrg  exit 0
20032034fa5Smrgfi
2014ecf19b2Smrg
20232034fa5Smrgif test -z "$dir_arg"; then
20332034fa5Smrg  trap '(exit $?); exit' 1 2 13 15
20432034fa5Smrg
20532034fa5Smrg  # Set umask so as not to create temps with too-generous modes.
20632034fa5Smrg  # However, 'strip' requires both read and write access to temps.
20732034fa5Smrg  case $mode in
20832034fa5Smrg    # Optimize common cases.
20932034fa5Smrg    *644) cp_umask=133;;
21032034fa5Smrg    *755) cp_umask=22;;
21132034fa5Smrg
21232034fa5Smrg    *[0-7])
21332034fa5Smrg      if test -z "$stripcmd"; then
21432034fa5Smrg	u_plus_rw=
21532034fa5Smrg      else
21632034fa5Smrg	u_plus_rw='% 200'
21732034fa5Smrg      fi
21832034fa5Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21932034fa5Smrg    *)
22032034fa5Smrg      if test -z "$stripcmd"; then
22132034fa5Smrg	u_plus_rw=
22232034fa5Smrg      else
22332034fa5Smrg	u_plus_rw=,u+rw
22432034fa5Smrg      fi
22532034fa5Smrg      cp_umask=$mode$u_plus_rw;;
22632034fa5Smrg  esac
22732034fa5Smrgfi
2284ecf19b2Smrg
22932034fa5Smrgfor src
23032034fa5Smrgdo
23132034fa5Smrg  # Protect names starting with `-'.
23232034fa5Smrg  case $src in
23332034fa5Smrg    -*) src=./$src;;
23432034fa5Smrg  esac
23532034fa5Smrg
23632034fa5Smrg  if test -n "$dir_arg"; then
23732034fa5Smrg    dst=$src
23832034fa5Smrg    dstdir=$dst
23932034fa5Smrg    test -d "$dstdir"
24032034fa5Smrg    dstdir_status=$?
24132034fa5Smrg  else
24232034fa5Smrg
24332034fa5Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
24432034fa5Smrg    # might cause directories to be created, which would be especially bad
24532034fa5Smrg    # if $src (and thus $dsttmp) contains '*'.
24632034fa5Smrg    if test ! -f "$src" && test ! -d "$src"; then
24732034fa5Smrg      echo "$0: $src does not exist." >&2
24832034fa5Smrg      exit 1
24932034fa5Smrg    fi
25032034fa5Smrg
25132034fa5Smrg    if test -z "$dst_arg"; then
25232034fa5Smrg      echo "$0: no destination specified." >&2
25332034fa5Smrg      exit 1
25432034fa5Smrg    fi
25532034fa5Smrg
25632034fa5Smrg    dst=$dst_arg
25732034fa5Smrg    # Protect names starting with `-'.
25832034fa5Smrg    case $dst in
25932034fa5Smrg      -*) dst=./$dst;;
26032034fa5Smrg    esac
2614ecf19b2Smrg
26232034fa5Smrg    # If destination is a directory, append the input filename; won't work
26332034fa5Smrg    # if double slashes aren't ignored.
26432034fa5Smrg    if test -d "$dst"; then
26532034fa5Smrg      if test -n "$no_target_directory"; then
26632034fa5Smrg	echo "$0: $dst_arg: Is a directory" >&2
26732034fa5Smrg	exit 1
26832034fa5Smrg      fi
26932034fa5Smrg      dstdir=$dst
27032034fa5Smrg      dst=$dstdir/`basename "$src"`
27132034fa5Smrg      dstdir_status=0
27232034fa5Smrg    else
27332034fa5Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27432034fa5Smrg      dstdir=`
27532034fa5Smrg	(dirname "$dst") 2>/dev/null ||
27632034fa5Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27732034fa5Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27832034fa5Smrg	     X"$dst" : 'X\(//\)$' \| \
27932034fa5Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28032034fa5Smrg	echo X"$dst" |
28132034fa5Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28232034fa5Smrg		   s//\1/
28332034fa5Smrg		   q
28432034fa5Smrg		 }
28532034fa5Smrg		 /^X\(\/\/\)[^/].*/{
28632034fa5Smrg		   s//\1/
28732034fa5Smrg		   q
28832034fa5Smrg		 }
28932034fa5Smrg		 /^X\(\/\/\)$/{
29032034fa5Smrg		   s//\1/
29132034fa5Smrg		   q
29232034fa5Smrg		 }
29332034fa5Smrg		 /^X\(\/\).*/{
29432034fa5Smrg		   s//\1/
29532034fa5Smrg		   q
29632034fa5Smrg		 }
29732034fa5Smrg		 s/.*/./; q'
29832034fa5Smrg      `
29932034fa5Smrg
30032034fa5Smrg      test -d "$dstdir"
30132034fa5Smrg      dstdir_status=$?
30232034fa5Smrg    fi
30332034fa5Smrg  fi
30432034fa5Smrg
30532034fa5Smrg  obsolete_mkdir_used=false
30632034fa5Smrg
30732034fa5Smrg  if test $dstdir_status != 0; then
30832034fa5Smrg    case $posix_mkdir in
30932034fa5Smrg      '')
31032034fa5Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31132034fa5Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31232034fa5Smrg	umask=`umask`
31332034fa5Smrg	case $stripcmd.$umask in
31432034fa5Smrg	  # Optimize common cases.
31532034fa5Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31632034fa5Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31732034fa5Smrg
31832034fa5Smrg	  *[0-7])
31932034fa5Smrg	    mkdir_umask=`expr $umask + 22 \
32032034fa5Smrg	      - $umask % 100 % 40 + $umask % 20 \
32132034fa5Smrg	      - $umask % 10 % 4 + $umask % 2
32232034fa5Smrg	    `;;
32332034fa5Smrg	  *) mkdir_umask=$umask,go-w;;
32432034fa5Smrg	esac
32532034fa5Smrg
32632034fa5Smrg	# With -d, create the new directory with the user-specified mode.
32732034fa5Smrg	# Otherwise, rely on $mkdir_umask.
32832034fa5Smrg	if test -n "$dir_arg"; then
32932034fa5Smrg	  mkdir_mode=-m$mode
3304ecf19b2Smrg	else
33132034fa5Smrg	  mkdir_mode=
3324ecf19b2Smrg	fi
3334ecf19b2Smrg
33432034fa5Smrg	posix_mkdir=false
33532034fa5Smrg	case $umask in
33632034fa5Smrg	  *[123567][0-7][0-7])
33732034fa5Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33832034fa5Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33932034fa5Smrg	    ;;
34032034fa5Smrg	  *)
34132034fa5Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34232034fa5Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34332034fa5Smrg
34432034fa5Smrg	    if (umask $mkdir_umask &&
34532034fa5Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34632034fa5Smrg	    then
34732034fa5Smrg	      if test -z "$dir_arg" || {
34832034fa5Smrg		   # Check for POSIX incompatibilities with -m.
34932034fa5Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35032034fa5Smrg		   # other-writeable bit of parent directory when it shouldn't.
35132034fa5Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35232034fa5Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35332034fa5Smrg		   case $ls_ld_tmpdir in
35432034fa5Smrg		     d????-?r-*) different_mode=700;;
35532034fa5Smrg		     d????-?--*) different_mode=755;;
35632034fa5Smrg		     *) false;;
35732034fa5Smrg		   esac &&
35832034fa5Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35932034fa5Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36032034fa5Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36132034fa5Smrg		   }
36232034fa5Smrg		 }
36332034fa5Smrg	      then posix_mkdir=:
36432034fa5Smrg	      fi
36532034fa5Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36632034fa5Smrg	    else
36732034fa5Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36832034fa5Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36932034fa5Smrg	    fi
37032034fa5Smrg	    trap '' 0;;
37132034fa5Smrg	esac;;
37232034fa5Smrg    esac
3734ecf19b2Smrg
37432034fa5Smrg    if
37532034fa5Smrg      $posix_mkdir && (
37632034fa5Smrg	umask $mkdir_umask &&
37732034fa5Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37832034fa5Smrg      )
37932034fa5Smrg    then :
38032034fa5Smrg    else
38132034fa5Smrg
38232034fa5Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38332034fa5Smrg      # or it failed possibly due to a race condition.  Create the
38432034fa5Smrg      # directory the slow way, step by step, checking for races as we go.
38532034fa5Smrg
38632034fa5Smrg      case $dstdir in
38732034fa5Smrg	/*) prefix='/';;
38832034fa5Smrg	-*) prefix='./';;
38932034fa5Smrg	*)  prefix='';;
39032034fa5Smrg      esac
39132034fa5Smrg
39232034fa5Smrg      eval "$initialize_posix_glob"
39332034fa5Smrg
39432034fa5Smrg      oIFS=$IFS
39532034fa5Smrg      IFS=/
39632034fa5Smrg      $posix_glob set -f
39732034fa5Smrg      set fnord $dstdir
39832034fa5Smrg      shift
39932034fa5Smrg      $posix_glob set +f
40032034fa5Smrg      IFS=$oIFS
40132034fa5Smrg
40232034fa5Smrg      prefixes=
40332034fa5Smrg
40432034fa5Smrg      for d
40532034fa5Smrg      do
40632034fa5Smrg	test -z "$d" && continue
40732034fa5Smrg
40832034fa5Smrg	prefix=$prefix$d
40932034fa5Smrg	if test -d "$prefix"; then
41032034fa5Smrg	  prefixes=
41132034fa5Smrg	else
41232034fa5Smrg	  if $posix_mkdir; then
41332034fa5Smrg	    (umask=$mkdir_umask &&
41432034fa5Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41532034fa5Smrg	    # Don't fail if two instances are running concurrently.
41632034fa5Smrg	    test -d "$prefix" || exit 1
41732034fa5Smrg	  else
41832034fa5Smrg	    case $prefix in
41932034fa5Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42032034fa5Smrg	      *) qprefix=$prefix;;
42132034fa5Smrg	    esac
42232034fa5Smrg	    prefixes="$prefixes '$qprefix'"
42332034fa5Smrg	  fi
42432034fa5Smrg	fi
42532034fa5Smrg	prefix=$prefix/
42632034fa5Smrg      done
42732034fa5Smrg
42832034fa5Smrg      if test -n "$prefixes"; then
42932034fa5Smrg	# Don't fail if two instances are running concurrently.
43032034fa5Smrg	(umask $mkdir_umask &&
43132034fa5Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43232034fa5Smrg	  test -d "$dstdir" || exit 1
43332034fa5Smrg	obsolete_mkdir_used=true
43432034fa5Smrg      fi
43532034fa5Smrg    fi
43632034fa5Smrg  fi
43732034fa5Smrg
43832034fa5Smrg  if test -n "$dir_arg"; then
43932034fa5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44032034fa5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44132034fa5Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44232034fa5Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
44332034fa5Smrg  else
44432034fa5Smrg
44532034fa5Smrg    # Make a couple of temp file names in the proper directory.
44632034fa5Smrg    dsttmp=$dstdir/_inst.$$_
44732034fa5Smrg    rmtmp=$dstdir/_rm.$$_
44832034fa5Smrg
44932034fa5Smrg    # Trap to clean up those temp files at exit.
45032034fa5Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
45132034fa5Smrg
45232034fa5Smrg    # Copy the file name to the temp name.
45332034fa5Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
45432034fa5Smrg
45532034fa5Smrg    # and set any options; do chmod last to preserve setuid bits.
45632034fa5Smrg    #
45732034fa5Smrg    # If any of these fail, we abort the whole thing.  If we want to
45832034fa5Smrg    # ignore errors from any of these, just make sure not to ignore
45932034fa5Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
46032034fa5Smrg    #
46132034fa5Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46232034fa5Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46332034fa5Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46432034fa5Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46532034fa5Smrg
46632034fa5Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46732034fa5Smrg    if $copy_on_change &&
46832034fa5Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46932034fa5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47032034fa5Smrg
47132034fa5Smrg       eval "$initialize_posix_glob" &&
47232034fa5Smrg       $posix_glob set -f &&
47332034fa5Smrg       set X $old && old=:$2:$4:$5:$6 &&
47432034fa5Smrg       set X $new && new=:$2:$4:$5:$6 &&
47532034fa5Smrg       $posix_glob set +f &&
47632034fa5Smrg
47732034fa5Smrg       test "$old" = "$new" &&
47832034fa5Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47932034fa5Smrg    then
48032034fa5Smrg      rm -f "$dsttmp"
48132034fa5Smrg    else
48232034fa5Smrg      # Rename the file to the real destination.
48332034fa5Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48432034fa5Smrg
48532034fa5Smrg      # The rename failed, perhaps because mv can't rename something else
48632034fa5Smrg      # to itself, or perhaps because mv is so ancient that it does not
48732034fa5Smrg      # support -f.
48832034fa5Smrg      {
48932034fa5Smrg	# Now remove or move aside any old file at destination location.
49032034fa5Smrg	# We try this two ways since rm can't unlink itself on some
49132034fa5Smrg	# systems and the destination file might be busy for other
49232034fa5Smrg	# reasons.  In this case, the final cleanup might fail but the new
49332034fa5Smrg	# file should still install successfully.
49432034fa5Smrg	{
49532034fa5Smrg	  test ! -f "$dst" ||
49632034fa5Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49732034fa5Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49832034fa5Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49932034fa5Smrg	  } ||
50032034fa5Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50132034fa5Smrg	    (exit 1); exit 1
50232034fa5Smrg	  }
50332034fa5Smrg	} &&
50432034fa5Smrg
50532034fa5Smrg	# Now rename the file to the real destination.
50632034fa5Smrg	$doit $mvcmd "$dsttmp" "$dst"
50732034fa5Smrg      }
50832034fa5Smrg    fi || exit 1
50932034fa5Smrg
51032034fa5Smrg    trap '' 0
51132034fa5Smrg  fi
51232034fa5Smrgdone
5134ecf19b2Smrg
51432034fa5Smrg# Local variables:
51532034fa5Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
51632034fa5Smrg# time-stamp-start: "scriptversion="
51732034fa5Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51832034fa5Smrg# time-stamp-time-zone: "UTC"
51932034fa5Smrg# time-stamp-end: "; # UTC"
52032034fa5Smrg# End:
521