install-sh revision 48e69166
1fd0c672fSmrg#!/bin/sh
2fd0c672fSmrg# install - install a program, script, or datafile
3fd0c672fSmrg
448e69166Smrgscriptversion=2009-04-28.21; # UTC
5fd0c672fSmrg
6fd0c672fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7fd0c672fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8fd0c672fSmrg# following copyright and license.
9fd0c672fSmrg#
10fd0c672fSmrg# Copyright (C) 1994 X Consortium
11fd0c672fSmrg#
12fd0c672fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13fd0c672fSmrg# of this software and associated documentation files (the "Software"), to
14fd0c672fSmrg# deal in the Software without restriction, including without limitation the
15fd0c672fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16fd0c672fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17fd0c672fSmrg# furnished to do so, subject to the following conditions:
18fd0c672fSmrg#
19fd0c672fSmrg# The above copyright notice and this permission notice shall be included in
20fd0c672fSmrg# all copies or substantial portions of the Software.
21fd0c672fSmrg#
22fd0c672fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23fd0c672fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24fd0c672fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25fd0c672fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26fd0c672fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27fd0c672fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28fd0c672fSmrg#
29fd0c672fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30fd0c672fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31fd0c672fSmrg# ings in this Software without prior written authorization from the X Consor-
32fd0c672fSmrg# tium.
33fd0c672fSmrg#
34fd0c672fSmrg#
35fd0c672fSmrg# FSF changes to this file are in the public domain.
36fd0c672fSmrg#
37fd0c672fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38fd0c672fSmrg# `make' implicit rules from creating a file called install from it
39fd0c672fSmrg# when there is no Makefile.
40fd0c672fSmrg#
41fd0c672fSmrg# This script is compatible with the BSD install script, but was written
4248e69166Smrg# from scratch.
4348e69166Smrg
4448e69166Smrgnl='
4548e69166Smrg'
4648e69166SmrgIFS=" ""	$nl"
47fd0c672fSmrg
48fd0c672fSmrg# set DOITPROG to echo to test this script
49fd0c672fSmrg
50fd0c672fSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
5148e69166Smrgdoit=${DOITPROG-}
5248e69166Smrgif test -z "$doit"; then
5348e69166Smrg  doit_exec=exec
5448e69166Smrgelse
5548e69166Smrg  doit_exec=$doit
5648e69166Smrgfi
57fd0c672fSmrg
5848e69166Smrg# Put in absolute file names if you don't have them in your path;
5948e69166Smrg# or use environment vars.
6048e69166Smrg
6148e69166Smrgchgrpprog=${CHGRPPROG-chgrp}
6248e69166Smrgchmodprog=${CHMODPROG-chmod}
6348e69166Smrgchownprog=${CHOWNPROG-chown}
6448e69166Smrgcmpprog=${CMPPROG-cmp}
6548e69166Smrgcpprog=${CPPROG-cp}
6648e69166Smrgmkdirprog=${MKDIRPROG-mkdir}
6748e69166Smrgmvprog=${MVPROG-mv}
6848e69166Smrgrmprog=${RMPROG-rm}
6948e69166Smrgstripprog=${STRIPPROG-strip}
7048e69166Smrg
7148e69166Smrgposix_glob='?'
7248e69166Smrginitialize_posix_glob='
7348e69166Smrg  test "$posix_glob" != "?" || {
7448e69166Smrg    if (set -f) 2>/dev/null; then
7548e69166Smrg      posix_glob=
7648e69166Smrg    else
7748e69166Smrg      posix_glob=:
7848e69166Smrg    fi
7948e69166Smrg  }
8048e69166Smrg'
81fd0c672fSmrg
8248e69166Smrgposix_mkdir=
8348e69166Smrg
8448e69166Smrg# Desired mode of installed file.
8548e69166Smrgmode=0755
86fd0c672fSmrg
87fd0c672fSmrgchgrpcmd=
8848e69166Smrgchmodcmd=$chmodprog
8948e69166Smrgchowncmd=
9048e69166Smrgmvcmd=$mvprog
91fd0c672fSmrgrmcmd="$rmprog -f"
9248e69166Smrgstripcmd=
9348e69166Smrg
94fd0c672fSmrgsrc=
95fd0c672fSmrgdst=
96fd0c672fSmrgdir_arg=
9748e69166Smrgdst_arg=
9848e69166Smrg
9948e69166Smrgcopy_on_change=false
100fd0c672fSmrgno_target_directory=
101fd0c672fSmrg
10248e69166Smrgusage="\
10348e69166SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104fd0c672fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105fd0c672fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106fd0c672fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107fd0c672fSmrg
108fd0c672fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109fd0c672fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110fd0c672fSmrgIn the 4th, create DIRECTORIES.
111fd0c672fSmrg
112fd0c672fSmrgOptions:
11348e69166Smrg     --help     display this help and exit.
11448e69166Smrg     --version  display version info and exit.
11548e69166Smrg
11648e69166Smrg  -c            (ignored)
11748e69166Smrg  -C            install only if different (preserve the last data modification time)
11848e69166Smrg  -d            create directories instead of installing files.
11948e69166Smrg  -g GROUP      $chgrpprog installed files to GROUP.
12048e69166Smrg  -m MODE       $chmodprog installed files to MODE.
12148e69166Smrg  -o USER       $chownprog installed files to USER.
12248e69166Smrg  -s            $stripprog installed files.
12348e69166Smrg  -t DIRECTORY  install into DIRECTORY.
12448e69166Smrg  -T            report an error if DSTFILE is a directory.
125fd0c672fSmrg
126fd0c672fSmrgEnvironment variables override the default commands:
12748e69166Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
12848e69166Smrg  RMPROG STRIPPROG
129fd0c672fSmrg"
130fd0c672fSmrg
13148e69166Smrgwhile test $# -ne 0; do
132fd0c672fSmrg  case $1 in
13348e69166Smrg    -c) ;;
13448e69166Smrg
13548e69166Smrg    -C) copy_on_change=true;;
136fd0c672fSmrg
13748e69166Smrg    -d) dir_arg=true;;
138fd0c672fSmrg
139fd0c672fSmrg    -g) chgrpcmd="$chgrpprog $2"
14048e69166Smrg	shift;;
141fd0c672fSmrg
142fd0c672fSmrg    --help) echo "$usage"; exit $?;;
143fd0c672fSmrg
14448e69166Smrg    -m) mode=$2
14548e69166Smrg	case $mode in
14648e69166Smrg	  *' '* | *'	'* | *'
14748e69166Smrg'*	  | *'*'* | *'?'* | *'['*)
14848e69166Smrg	    echo "$0: invalid mode: $mode" >&2
14948e69166Smrg	    exit 1;;
15048e69166Smrg	esac
15148e69166Smrg	shift;;
152fd0c672fSmrg
153fd0c672fSmrg    -o) chowncmd="$chownprog $2"
15448e69166Smrg	shift;;
155fd0c672fSmrg
15648e69166Smrg    -s) stripcmd=$stripprog;;
157fd0c672fSmrg
15848e69166Smrg    -t) dst_arg=$2
15948e69166Smrg	shift;;
160fd0c672fSmrg
16148e69166Smrg    -T) no_target_directory=true;;
162fd0c672fSmrg
163fd0c672fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164fd0c672fSmrg
16548e69166Smrg    --)	shift
166fd0c672fSmrg	break;;
16748e69166Smrg
16848e69166Smrg    -*)	echo "$0: invalid option: $1" >&2
16948e69166Smrg	exit 1;;
17048e69166Smrg
17148e69166Smrg    *)  break;;
172fd0c672fSmrg  esac
17348e69166Smrg  shift
174fd0c672fSmrgdone
175fd0c672fSmrg
17648e69166Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17748e69166Smrg  # When -d is used, all remaining arguments are directories to create.
17848e69166Smrg  # When -t is used, the destination is already specified.
17948e69166Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18048e69166Smrg  for arg
18148e69166Smrg  do
18248e69166Smrg    if test -n "$dst_arg"; then
18348e69166Smrg      # $@ is not empty: it contains at least $arg.
18448e69166Smrg      set fnord "$@" "$dst_arg"
18548e69166Smrg      shift # fnord
18648e69166Smrg    fi
18748e69166Smrg    shift # arg
18848e69166Smrg    dst_arg=$arg
18948e69166Smrg  done
19048e69166Smrgfi
19148e69166Smrg
19248e69166Smrgif test $# -eq 0; then
193fd0c672fSmrg  if test -z "$dir_arg"; then
194fd0c672fSmrg    echo "$0: no input file specified." >&2
195fd0c672fSmrg    exit 1
196fd0c672fSmrg  fi
197fd0c672fSmrg  # It's OK to call `install-sh -d' without argument.
198fd0c672fSmrg  # This can happen when creating conditional directories.
199fd0c672fSmrg  exit 0
200fd0c672fSmrgfi
201fd0c672fSmrg
20248e69166Smrgif test -z "$dir_arg"; then
20348e69166Smrg  trap '(exit $?); exit' 1 2 13 15
20448e69166Smrg
20548e69166Smrg  # Set umask so as not to create temps with too-generous modes.
20648e69166Smrg  # However, 'strip' requires both read and write access to temps.
20748e69166Smrg  case $mode in
20848e69166Smrg    # Optimize common cases.
20948e69166Smrg    *644) cp_umask=133;;
21048e69166Smrg    *755) cp_umask=22;;
21148e69166Smrg
21248e69166Smrg    *[0-7])
21348e69166Smrg      if test -z "$stripcmd"; then
21448e69166Smrg	u_plus_rw=
21548e69166Smrg      else
21648e69166Smrg	u_plus_rw='% 200'
21748e69166Smrg      fi
21848e69166Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
21948e69166Smrg    *)
22048e69166Smrg      if test -z "$stripcmd"; then
22148e69166Smrg	u_plus_rw=
22248e69166Smrg      else
22348e69166Smrg	u_plus_rw=,u+rw
22448e69166Smrg      fi
22548e69166Smrg      cp_umask=$mode$u_plus_rw;;
22648e69166Smrg  esac
22748e69166Smrgfi
22848e69166Smrg
229fd0c672fSmrgfor src
230fd0c672fSmrgdo
231fd0c672fSmrg  # Protect names starting with `-'.
232fd0c672fSmrg  case $src in
23348e69166Smrg    -*) src=./$src;;
234fd0c672fSmrg  esac
235fd0c672fSmrg
236fd0c672fSmrg  if test -n "$dir_arg"; then
237fd0c672fSmrg    dst=$src
23848e69166Smrg    dstdir=$dst
23948e69166Smrg    test -d "$dstdir"
24048e69166Smrg    dstdir_status=$?
241fd0c672fSmrg  else
24248e69166Smrg
243fd0c672fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244fd0c672fSmrg    # might cause directories to be created, which would be especially bad
245fd0c672fSmrg    # if $src (and thus $dsttmp) contains '*'.
246fd0c672fSmrg    if test ! -f "$src" && test ! -d "$src"; then
247fd0c672fSmrg      echo "$0: $src does not exist." >&2
248fd0c672fSmrg      exit 1
249fd0c672fSmrg    fi
250fd0c672fSmrg
25148e69166Smrg    if test -z "$dst_arg"; then
252fd0c672fSmrg      echo "$0: no destination specified." >&2
253fd0c672fSmrg      exit 1
254fd0c672fSmrg    fi
255fd0c672fSmrg
25648e69166Smrg    dst=$dst_arg
257fd0c672fSmrg    # Protect names starting with `-'.
258fd0c672fSmrg    case $dst in
25948e69166Smrg      -*) dst=./$dst;;
260fd0c672fSmrg    esac
261fd0c672fSmrg
262fd0c672fSmrg    # If destination is a directory, append the input filename; won't work
263fd0c672fSmrg    # if double slashes aren't ignored.
264fd0c672fSmrg    if test -d "$dst"; then
265fd0c672fSmrg      if test -n "$no_target_directory"; then
26648e69166Smrg	echo "$0: $dst_arg: Is a directory" >&2
267fd0c672fSmrg	exit 1
268fd0c672fSmrg      fi
26948e69166Smrg      dstdir=$dst
27048e69166Smrg      dst=$dstdir/`basename "$src"`
27148e69166Smrg      dstdir_status=0
27248e69166Smrg    else
27348e69166Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
27448e69166Smrg      dstdir=`
27548e69166Smrg	(dirname "$dst") 2>/dev/null ||
27648e69166Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
27748e69166Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
27848e69166Smrg	     X"$dst" : 'X\(//\)$' \| \
27948e69166Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
28048e69166Smrg	echo X"$dst" |
28148e69166Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
28248e69166Smrg		   s//\1/
28348e69166Smrg		   q
28448e69166Smrg		 }
28548e69166Smrg		 /^X\(\/\/\)[^/].*/{
28648e69166Smrg		   s//\1/
28748e69166Smrg		   q
28848e69166Smrg		 }
28948e69166Smrg		 /^X\(\/\/\)$/{
29048e69166Smrg		   s//\1/
29148e69166Smrg		   q
29248e69166Smrg		 }
29348e69166Smrg		 /^X\(\/\).*/{
29448e69166Smrg		   s//\1/
29548e69166Smrg		   q
29648e69166Smrg		 }
29748e69166Smrg		 s/.*/./; q'
29848e69166Smrg      `
29948e69166Smrg
30048e69166Smrg      test -d "$dstdir"
30148e69166Smrg      dstdir_status=$?
302fd0c672fSmrg    fi
303fd0c672fSmrg  fi
304fd0c672fSmrg
30548e69166Smrg  obsolete_mkdir_used=false
30648e69166Smrg
30748e69166Smrg  if test $dstdir_status != 0; then
30848e69166Smrg    case $posix_mkdir in
30948e69166Smrg      '')
31048e69166Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
31148e69166Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
31248e69166Smrg	umask=`umask`
31348e69166Smrg	case $stripcmd.$umask in
31448e69166Smrg	  # Optimize common cases.
31548e69166Smrg	  *[2367][2367]) mkdir_umask=$umask;;
31648e69166Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
31748e69166Smrg
31848e69166Smrg	  *[0-7])
31948e69166Smrg	    mkdir_umask=`expr $umask + 22 \
32048e69166Smrg	      - $umask % 100 % 40 + $umask % 20 \
32148e69166Smrg	      - $umask % 10 % 4 + $umask % 2
32248e69166Smrg	    `;;
32348e69166Smrg	  *) mkdir_umask=$umask,go-w;;
32448e69166Smrg	esac
32548e69166Smrg
32648e69166Smrg	# With -d, create the new directory with the user-specified mode.
32748e69166Smrg	# Otherwise, rely on $mkdir_umask.
32848e69166Smrg	if test -n "$dir_arg"; then
32948e69166Smrg	  mkdir_mode=-m$mode
33048e69166Smrg	else
33148e69166Smrg	  mkdir_mode=
33248e69166Smrg	fi
33348e69166Smrg
33448e69166Smrg	posix_mkdir=false
33548e69166Smrg	case $umask in
33648e69166Smrg	  *[123567][0-7][0-7])
33748e69166Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
33848e69166Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
33948e69166Smrg	    ;;
34048e69166Smrg	  *)
34148e69166Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
34248e69166Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
34348e69166Smrg
34448e69166Smrg	    if (umask $mkdir_umask &&
34548e69166Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
34648e69166Smrg	    then
34748e69166Smrg	      if test -z "$dir_arg" || {
34848e69166Smrg		   # Check for POSIX incompatibilities with -m.
34948e69166Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
35048e69166Smrg		   # other-writeable bit of parent directory when it shouldn't.
35148e69166Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
35248e69166Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
35348e69166Smrg		   case $ls_ld_tmpdir in
35448e69166Smrg		     d????-?r-*) different_mode=700;;
35548e69166Smrg		     d????-?--*) different_mode=755;;
35648e69166Smrg		     *) false;;
35748e69166Smrg		   esac &&
35848e69166Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
35948e69166Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
36048e69166Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
36148e69166Smrg		   }
36248e69166Smrg		 }
36348e69166Smrg	      then posix_mkdir=:
36448e69166Smrg	      fi
36548e69166Smrg	      rmdir "$tmpdir/d" "$tmpdir"
36648e69166Smrg	    else
36748e69166Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
36848e69166Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
36948e69166Smrg	    fi
37048e69166Smrg	    trap '' 0;;
37148e69166Smrg	esac;;
37248e69166Smrg    esac
373fd0c672fSmrg
37448e69166Smrg    if
37548e69166Smrg      $posix_mkdir && (
37648e69166Smrg	umask $mkdir_umask &&
37748e69166Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37848e69166Smrg      )
37948e69166Smrg    then :
38048e69166Smrg    else
381fd0c672fSmrg
38248e69166Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
38348e69166Smrg      # or it failed possibly due to a race condition.  Create the
38448e69166Smrg      # directory the slow way, step by step, checking for races as we go.
385fd0c672fSmrg
38648e69166Smrg      case $dstdir in
38748e69166Smrg	/*) prefix='/';;
38848e69166Smrg	-*) prefix='./';;
38948e69166Smrg	*)  prefix='';;
39048e69166Smrg      esac
391fd0c672fSmrg
39248e69166Smrg      eval "$initialize_posix_glob"
393fd0c672fSmrg
39448e69166Smrg      oIFS=$IFS
39548e69166Smrg      IFS=/
39648e69166Smrg      $posix_glob set -f
39748e69166Smrg      set fnord $dstdir
398fd0c672fSmrg      shift
39948e69166Smrg      $posix_glob set +f
40048e69166Smrg      IFS=$oIFS
40148e69166Smrg
40248e69166Smrg      prefixes=
40348e69166Smrg
40448e69166Smrg      for d
40548e69166Smrg      do
40648e69166Smrg	test -z "$d" && continue
40748e69166Smrg
40848e69166Smrg	prefix=$prefix$d
40948e69166Smrg	if test -d "$prefix"; then
41048e69166Smrg	  prefixes=
41148e69166Smrg	else
41248e69166Smrg	  if $posix_mkdir; then
41348e69166Smrg	    (umask=$mkdir_umask &&
41448e69166Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
41548e69166Smrg	    # Don't fail if two instances are running concurrently.
41648e69166Smrg	    test -d "$prefix" || exit 1
41748e69166Smrg	  else
41848e69166Smrg	    case $prefix in
41948e69166Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
42048e69166Smrg	      *) qprefix=$prefix;;
42148e69166Smrg	    esac
42248e69166Smrg	    prefixes="$prefixes '$qprefix'"
42348e69166Smrg	  fi
42448e69166Smrg	fi
42548e69166Smrg	prefix=$prefix/
42648e69166Smrg      done
42748e69166Smrg
42848e69166Smrg      if test -n "$prefixes"; then
42948e69166Smrg	# Don't fail if two instances are running concurrently.
43048e69166Smrg	(umask $mkdir_umask &&
43148e69166Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
43248e69166Smrg	  test -d "$dstdir" || exit 1
43348e69166Smrg	obsolete_mkdir_used=true
434fd0c672fSmrg      fi
43548e69166Smrg    fi
436fd0c672fSmrg  fi
437fd0c672fSmrg
438fd0c672fSmrg  if test -n "$dir_arg"; then
43948e69166Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
44048e69166Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
44148e69166Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
44248e69166Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443fd0c672fSmrg  else
444fd0c672fSmrg
445fd0c672fSmrg    # Make a couple of temp file names in the proper directory.
446fd0c672fSmrg    dsttmp=$dstdir/_inst.$$_
447fd0c672fSmrg    rmtmp=$dstdir/_rm.$$_
448fd0c672fSmrg
449fd0c672fSmrg    # Trap to clean up those temp files at exit.
450fd0c672fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451fd0c672fSmrg
452fd0c672fSmrg    # Copy the file name to the temp name.
45348e69166Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454fd0c672fSmrg
455fd0c672fSmrg    # and set any options; do chmod last to preserve setuid bits.
456fd0c672fSmrg    #
457fd0c672fSmrg    # If any of these fail, we abort the whole thing.  If we want to
458fd0c672fSmrg    # ignore errors from any of these, just make sure not to ignore
459fd0c672fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460fd0c672fSmrg    #
46148e69166Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
46248e69166Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
46348e69166Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
46448e69166Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
46548e69166Smrg
46648e69166Smrg    # If -C, don't bother to copy if it wouldn't change the file.
46748e69166Smrg    if $copy_on_change &&
46848e69166Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
46948e69166Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
47048e69166Smrg
47148e69166Smrg       eval "$initialize_posix_glob" &&
47248e69166Smrg       $posix_glob set -f &&
47348e69166Smrg       set X $old && old=:$2:$4:$5:$6 &&
47448e69166Smrg       set X $new && new=:$2:$4:$5:$6 &&
47548e69166Smrg       $posix_glob set +f &&
47648e69166Smrg
47748e69166Smrg       test "$old" = "$new" &&
47848e69166Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
47948e69166Smrg    then
48048e69166Smrg      rm -f "$dsttmp"
48148e69166Smrg    else
48248e69166Smrg      # Rename the file to the real destination.
48348e69166Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
48448e69166Smrg
48548e69166Smrg      # The rename failed, perhaps because mv can't rename something else
48648e69166Smrg      # to itself, or perhaps because mv is so ancient that it does not
48748e69166Smrg      # support -f.
48848e69166Smrg      {
48948e69166Smrg	# Now remove or move aside any old file at destination location.
49048e69166Smrg	# We try this two ways since rm can't unlink itself on some
49148e69166Smrg	# systems and the destination file might be busy for other
49248e69166Smrg	# reasons.  In this case, the final cleanup might fail but the new
49348e69166Smrg	# file should still install successfully.
49448e69166Smrg	{
49548e69166Smrg	  test ! -f "$dst" ||
49648e69166Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
49748e69166Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
49848e69166Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
49948e69166Smrg	  } ||
50048e69166Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
50148e69166Smrg	    (exit 1); exit 1
50248e69166Smrg	  }
50348e69166Smrg	} &&
50448e69166Smrg
50548e69166Smrg	# Now rename the file to the real destination.
50648e69166Smrg	$doit $mvcmd "$dsttmp" "$dst"
50748e69166Smrg      }
50848e69166Smrg    fi || exit 1
50948e69166Smrg
51048e69166Smrg    trap '' 0
51148e69166Smrg  fi
512fd0c672fSmrgdone
513fd0c672fSmrg
514fd0c672fSmrg# Local variables:
515fd0c672fSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516fd0c672fSmrg# time-stamp-start: "scriptversion="
517fd0c672fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
51848e69166Smrg# time-stamp-time-zone: "UTC"
51948e69166Smrg# time-stamp-end: "; # UTC"
520fd0c672fSmrg# End:
521